Skip to content

Comments

fix: call onerror for silently swallowed transport errors#1576

Open
qing-ant wants to merge 1 commit intomodelcontextprotocol:mainfrom
qing-ant:fix/onerror-transport-errors
Open

fix: call onerror for silently swallowed transport errors#1576
qing-ant wants to merge 1 commit intomodelcontextprotocol:mainfrom
qing-ant:fix/onerror-transport-errors

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.

Changes

packages/server/src/server/streamableHttp.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)

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

Added 11 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

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

  • pnpm typecheck:all passes
  • pnpm --filter @modelcontextprotocol/server test — all tests pass (existing + 11 new)
  • pnpm build:all succeeds

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

changeset-bot bot commented Feb 23, 2026

⚠️ No Changeset found

Latest commit: 7e10f2c

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.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

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 23, 2026

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/server

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

@modelcontextprotocol/express

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 7e10f2c

@qing-ant qing-ant force-pushed the fix/onerror-transport-errors branch from 9d0b31f to 68401d6 Compare February 23, 2026 18:20
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.
All tests verified to fail without the fix and pass with it.
@qing-ant qing-ant force-pushed the fix/onerror-transport-errors branch from 68401d6 to 7e10f2c Compare February 23, 2026 19:25
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.

Some transport errors are silently swallowed due to missing onerror callback usage

1 participant