Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/server/src/server/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,8 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
if (options?.parsedBody === undefined) {
try {
rawMessage = await req.json();
} catch {
} catch (error) {
this.onerror?.(error instanceof Error ? error : new Error(String(error)));
return this.createJsonErrorResponse(400, -32_700, 'Parse error: Invalid JSON');
}
} else {
Expand All @@ -649,7 +650,8 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
messages = Array.isArray(rawMessage)
? rawMessage.map(msg => JSONRPCMessageSchema.parse(msg))
: [JSONRPCMessageSchema.parse(rawMessage)];
} catch {
} catch (error) {
this.onerror?.(error instanceof Error ? error : new Error(String(error)));
return this.createJsonErrorResponse(400, -32_700, 'Parse error: Invalid JSON-RPC message');
}

Expand Down
38 changes: 38 additions & 0 deletions packages/server/test/server/streamableHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,44 @@ describe('Zod v4', () => {
expectErrorResponse(errorData, -32_700, /Parse error.*Invalid JSON/);
});

it('should call onerror for invalid JSON', async () => {
const errorSpy = vi.fn();
transport.onerror = errorSpy;

const request = new Request('http://localhost/mcp', {
method: 'POST',
headers: {
Accept: 'application/json, text/event-stream',
'Content-Type': 'application/json'
},
body: 'not valid json'
});

const response = await transport.handleRequest(request);

expect(response.status).toBe(400);
expect(errorSpy).toHaveBeenCalledTimes(1);
});

it('should call onerror for invalid JSON-RPC message', async () => {
const errorSpy = vi.fn();
transport.onerror = errorSpy;

const request = new Request('http://localhost/mcp', {
method: 'POST',
headers: {
Accept: 'application/json, text/event-stream',
'Content-Type': 'application/json'
},
body: JSON.stringify({ bad: 'shape' })
});

const response = await transport.handleRequest(request);

expect(response.status).toBe(400);
expect(errorSpy).toHaveBeenCalledTimes(1);
});

it('should accept notifications without session and return 202', async () => {
sessionId = await initializeServer();

Expand Down
Loading