Skip to content

fix: call onerror for silently swallowed transport errors#1580

Merged
felixweinberger merged 2 commits intomodelcontextprotocol:v1.xfrom
qing-ant:fix/onerror-transport-errors-v1x
Feb 24, 2026
Merged

fix: call onerror for silently swallowed transport errors#1580
felixweinberger merged 2 commits intomodelcontextprotocol:v1.xfrom
qing-ant:fix/onerror-transport-errors-v1x

Conversation

@qing-ant
Copy link

Summary

Fixes #1395 - Transport errors are silently swallowed due to missing onerror callback invocation.

Several handleRequest error paths in WebStandardStreamableHTTPServerTransport were returning error responses via createJsonErrorResponse() without calling the onerror callback, making transport errors invisible to consumers who set transport.onerror.

Rebased from #1576 onto v1.x with correct file paths.

Changes

src/server/webStandardStreamableHttp.ts

Added this.onerror?.(new Error(...)) calls at 16 locations:

  • handleGetRequest: Missing Accept header (406), concurrent SSE stream conflict (409)
  • replayEvents: Event store not configured (400), invalid event ID format (400), stream conflict (409)
  • handlePostRequest: Invalid Accept header (406), unsupported Content-Type (415), JSON parse error (400), invalid JSON-RPC message (400), re-initialization attempt (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 (was silently returning false)

test/server/streamableHttp.test.ts

Added 12 test cases verifying onerror is called for each error condition:

  • Invalid JSON
  • Invalid JSON-RPC message
  • Missing Accept header on POST
  • Unsupported Content-Type
  • Server not initialized
  • Invalid session ID
  • Re-initialization attempt
  • Missing Accept header on GET
  • Concurrent SSE streams
  • Unsupported protocol version
  • Unsupported HTTP method
  • Invalid event ID in replay

Pattern Used

Follows the existing pattern already present in the codebase (e.g., validateRequestHeaders):

this.onerror?.(new Error('Error message here'));
return this.createJsonErrorResponse(status, code, 'Error message here');

Testing

  • npm run typecheck passes
  • npm test — all tests pass (existing + 12 new)
  • npm run lint passes

@qing-ant qing-ant requested a review from a team as a code owner February 24, 2026 20:24
@changeset-bot
Copy link

changeset-bot bot commented Feb 24, 2026

⚠️ No Changeset found

Latest commit: 02bc01b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 24, 2026

Open in StackBlitz

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/sdk@1580

commit: 02bc01b

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.
@qing-ant qing-ant force-pushed the fix/onerror-transport-errors-v1x branch from d9dbb1b to e270e15 Compare February 24, 2026 20:35
Copy link
Contributor

@felixweinberger felixweinberger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@felixweinberger felixweinberger merged commit 09a85a8 into modelcontextprotocol:v1.x Feb 24, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants