Skip to content

Commit 3a1a8b7

Browse files
author
kai-agent-free
committed
fix: address review feedback - handleUnsupportedRequest centralization and error cause preservation
- Convert handleUnsupportedRequest() to use createJsonErrorResponse() for consistency - Add optional 'cause' parameter to createJsonErrorResponse() to preserve error fidelity - Pass caught errors as 'cause' in catch blocks to maintain original error information - Add test for onerror callback on unsupported HTTP methods
1 parent 15514e5 commit 3a1a8b7

2 files changed

Lines changed: 15 additions & 20 deletions

File tree

packages/server/src/server/streamableHttp.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
284284
status: number,
285285
code: number,
286286
message: string,
287-
options?: { headers?: Record<string, string>; data?: string }
287+
options?: { headers?: Record<string, string>; data?: string; cause?: Error }
288288
): Response {
289289
const error: { code: number; message: string; data?: string } = { code, message };
290-
this.onerror?.(new Error(message));
290+
this.onerror?.(options?.cause ?? new Error(message));
291291
if (options?.data !== undefined) {
292292
error.data = options.data;
293293
}
@@ -584,23 +584,9 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
584584
* Handles unsupported requests (`PUT`, `PATCH`, etc.)
585585
*/
586586
private handleUnsupportedRequest(): Response {
587-
return Response.json(
588-
{
589-
jsonrpc: '2.0',
590-
error: {
591-
code: -32_000,
592-
message: 'Method not allowed.'
593-
},
594-
id: null
595-
},
596-
{
597-
status: 405,
598-
headers: {
599-
Allow: 'GET, POST, DELETE',
600-
'Content-Type': 'application/json'
601-
}
602-
}
603-
);
587+
return this.createJsonErrorResponse(405, -32_000, 'Method not allowed.', {
588+
headers: { Allow: 'GET, POST, DELETE' }
589+
});
604590
}
605591

606592
/**
@@ -805,7 +791,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
805791
return new Response(readable, { status: 200, headers });
806792
} catch (error) {
807793
// return JSON-RPC formatted error
808-
return this.createJsonErrorResponse(400, -32_700, 'Parse error', { data: String(error) });
794+
return this.createJsonErrorResponse(400, -32_700, 'Parse error', { data: String(error), cause: error instanceof Error ? error : new Error(String(error)) });
809795
}
810796
}
811797

packages/server/test/server/streamableHttp.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,5 +844,14 @@ describe('Zod v4', () => {
844844
expect(errors.length).toBe(1);
845845
expect(errors[0]!.message).toMatch(/Not Acceptable/);
846846
});
847+
848+
it('should call onerror for unsupported HTTP methods', async () => {
849+
const request = new Request('http://localhost/mcp', { method: 'PUT' });
850+
const response = await transport.handleRequest(request);
851+
852+
expect(response.status).toBe(405);
853+
expect(errors.length).toBe(1);
854+
expect(errors[0]!.message).toMatch(/Method not allowed/);
855+
});
847856
});
848857
});

0 commit comments

Comments
 (0)