Skip to content

Commit d9dbb1b

Browse files
committed
fix: call onerror for silently swallowed transport errors
Fixes modelcontextprotocol#1395 - Several handleRequest error paths in WebStandardStreamableHTTPServerTransport were returning error responses via createJsonErrorResponse() without calling the onerror callback, making transport errors invisible to consumers. Added this.onerror?.() calls at 16 locations: - handleGetRequest: Missing Accept header (406), concurrent SSE stream (409) - replayEvents: Event store not configured (400), invalid event ID (400), stream conflict (409) - handlePostRequest: Invalid Accept header (406), unsupported Content-Type (415), JSON parse error (400), invalid JSON-RPC (400), re-initialization (400), multiple init requests (400) - validateSession: Server not initialized (400), missing session ID (400), session ID mismatch (404) - validateProtocolVersion: Unsupported protocol version (400) - handleUnsupportedRequest: Method not allowed (405) - writeSSEEvent: SSE write failure Added 12 test cases verifying onerror is called for each error condition. Rebased from PR modelcontextprotocol#1576 (main) onto v1.x branch with path adjustments: - Source: src/server/webStandardStreamableHttp.ts (v1.x) vs packages/server/src/server/streamableHttp.ts (main) - Tests: Added to existing test/server/streamableHttp.test.ts
1 parent e79d14a commit d9dbb1b

2 files changed

Lines changed: 295 additions & 1 deletion

File tree

‎src/server/webStandardStreamableHttp.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
383383
// The client MUST include an Accept header, listing text/event-stream as a supported content type.
384384
const acceptHeader = req.headers.get('accept');
385385
if (!acceptHeader?.includes('text/event-stream')) {
386+
this.onerror?.(new Error('Not Acceptable: Client must accept text/event-stream'));
386387
return this.createJsonErrorResponse(406, -32000, 'Not Acceptable: Client must accept text/event-stream');
387388
}
388389

@@ -409,6 +410,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
409410
// Check if there's already an active standalone SSE stream for this session
410411
if (this._streamMapping.get(this._standaloneSseStreamId) !== undefined) {
411412
// Only one GET SSE stream is allowed per session
413+
this.onerror?.(new Error('Conflict: Only one SSE stream is allowed per session'));
412414
return this.createJsonErrorResponse(409, -32000, 'Conflict: Only one SSE stream is allowed per session');
413415
}
414416

@@ -460,6 +462,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
460462
*/
461463
private async replayEvents(lastEventId: string): Promise<Response> {
462464
if (!this._eventStore) {
465+
this.onerror?.(new Error('Event store not configured'));
463466
return this.createJsonErrorResponse(400, -32000, 'Event store not configured');
464467
}
465468

@@ -470,11 +473,13 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
470473
streamId = await this._eventStore.getStreamIdForEventId(lastEventId);
471474

472475
if (!streamId) {
476+
this.onerror?.(new Error('Invalid event ID format'));
473477
return this.createJsonErrorResponse(400, -32000, 'Invalid event ID format');
474478
}
475479

476480
// Check conflict with the SAME streamId we'll use for mapping
477481
if (this._streamMapping.get(streamId) !== undefined) {
482+
this.onerror?.(new Error('Conflict: Stream already has an active connection'));
478483
return this.createJsonErrorResponse(409, -32000, 'Conflict: Stream already has an active connection');
479484
}
480485
}
@@ -556,7 +561,8 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
556561
eventData += `data: ${JSON.stringify(message)}\n\n`;
557562
controller.enqueue(encoder.encode(eventData));
558563
return true;
559-
} catch {
564+
} catch (error) {
565+
this.onerror?.(error as Error);
560566
return false;
561567
}
562568
}
@@ -565,6 +571,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
565571
* Handles unsupported requests (PUT, PATCH, etc.)
566572
*/
567573
private handleUnsupportedRequest(): Response {
574+
this.onerror?.(new Error('Method not allowed.'));
568575
return new Response(
569576
JSON.stringify({
570577
jsonrpc: '2.0',
@@ -593,6 +600,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
593600
const acceptHeader = req.headers.get('accept');
594601
// The client MUST include an Accept header, listing both application/json and text/event-stream as supported content types.
595602
if (!acceptHeader?.includes('application/json') || !acceptHeader.includes('text/event-stream')) {
603+
this.onerror?.(new Error('Not Acceptable: Client must accept both application/json and text/event-stream'));
596604
return this.createJsonErrorResponse(
597605
406,
598606
-32000,
@@ -602,6 +610,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
602610

603611
const ct = req.headers.get('content-type');
604612
if (!ct || !ct.includes('application/json')) {
613+
this.onerror?.(new Error('Unsupported Media Type: Content-Type must be application/json'));
605614
return this.createJsonErrorResponse(415, -32000, 'Unsupported Media Type: Content-Type must be application/json');
606615
}
607616

@@ -618,6 +627,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
618627
try {
619628
rawMessage = await req.json();
620629
} catch {
630+
this.onerror?.(new Error('Parse error: Invalid JSON'));
621631
return this.createJsonErrorResponse(400, -32700, 'Parse error: Invalid JSON');
622632
}
623633
}
@@ -632,6 +642,7 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
632642
messages = [JSONRPCMessageSchema.parse(rawMessage)];
633643
}
634644
} catch {
645+
this.onerror?.(new Error('Parse error: Invalid JSON-RPC message'));
635646
return this.createJsonErrorResponse(400, -32700, 'Parse error: Invalid JSON-RPC message');
636647
}
637648

@@ -642,9 +653,11 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
642653
// If it's a server with session management and the session ID is already set we should reject the request
643654
// to avoid re-initialization.
644655
if (this._initialized && this.sessionId !== undefined) {
656+
this.onerror?.(new Error('Invalid Request: Server already initialized'));
645657
return this.createJsonErrorResponse(400, -32600, 'Invalid Request: Server already initialized');
646658
}
647659
if (messages.length > 1) {
660+
this.onerror?.(new Error('Invalid Request: Only one initialization request is allowed'));
648661
return this.createJsonErrorResponse(400, -32600, 'Invalid Request: Only one initialization request is allowed');
649662
}
650663
this.sessionId = this.sessionIdGenerator?.();
@@ -824,18 +837,21 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
824837
}
825838
if (!this._initialized) {
826839
// If the server has not been initialized yet, reject all requests
840+
this.onerror?.(new Error('Bad Request: Server not initialized'));
827841
return this.createJsonErrorResponse(400, -32000, 'Bad Request: Server not initialized');
828842
}
829843

830844
const sessionId = req.headers.get('mcp-session-id');
831845

832846
if (!sessionId) {
833847
// Non-initialization requests without a session ID should return 400 Bad Request
848+
this.onerror?.(new Error('Bad Request: Mcp-Session-Id header is required'));
834849
return this.createJsonErrorResponse(400, -32000, 'Bad Request: Mcp-Session-Id header is required');
835850
}
836851

837852
if (sessionId !== this.sessionId) {
838853
// Reject requests with invalid session ID with 404 Not Found
854+
this.onerror?.(new Error('Session not found'));
839855
return this.createJsonErrorResponse(404, -32001, 'Session not found');
840856
}
841857

@@ -859,6 +875,12 @@ export class WebStandardStreamableHTTPServerTransport implements Transport {
859875
const protocolVersion = req.headers.get('mcp-protocol-version');
860876

861877
if (protocolVersion !== null && !SUPPORTED_PROTOCOL_VERSIONS.includes(protocolVersion)) {
878+
this.onerror?.(
879+
new Error(
880+
`Bad Request: Unsupported protocol version: ${protocolVersion}` +
881+
` (supported versions: ${SUPPORTED_PROTOCOL_VERSIONS.join(', ')})`
882+
)
883+
);
862884
return this.createJsonErrorResponse(
863885
400,
864886
-32000,

0 commit comments

Comments
 (0)