Skip to content

fix: call onerror callback for all transport error responses#1636

Open
dpdanpittman wants to merge 1 commit intomodelcontextprotocol:mainfrom
dpdanpittman:fix/transport-onerror-callbacks
Open

fix: call onerror callback for all transport error responses#1636
dpdanpittman wants to merge 1 commit intomodelcontextprotocol:mainfrom
dpdanpittman:fix/transport-onerror-callbacks

Conversation

@dpdanpittman
Copy link

Several error paths in WebStandardStreamableHTTPServerTransport returned error responses via createJsonErrorResponse() without calling the onerror callback, making these errors unobservable for debugging and logging purposes.

Fixes #1395

Motivation and Context

As described in #1395, nested try/catch blocks in handlePostRequest and other methods return error responses without calling this.onerror?.(). The outer catch block at the end of handlePostRequest does call it, but inner early-return paths bypass it entirely. This makes it impossible to log or observe errors like invalid JSON, bad headers, session validation failures, etc.

The validateRequestHeaders method already correctly calls this.onerror?.() before returning error responses — this PR makes the rest of the codebase consistent with that pattern.

Affected locations:

  • handlePostRequest: Accept header (406), Content-Type (415), invalid JSON (400), invalid JSON-RPC message (400), duplicate initialization (400), batch initialization (400)
  • handleGetRequest: Accept header (406), duplicate SSE stream (409)
  • validateSession: server not initialized (400), missing session ID (400), invalid session ID (404)
  • validateProtocolVersion: unsupported version (400)
  • handleUnsupportedRequest: method not allowed (405)
  • replayEvents: event store not configured (400), invalid event ID (400), stream conflict (409)

How Has This Been Tested?

Added 13 new tests in packages/server/test/server/streamableHttp.test.ts under a new HTTPServerTransport - onerror callback describe block. Each test sets an onerror spy on the transport and verifies it is called with the expected error message for each error condition. All 50 server tests pass. Full test suite passes (383/384 — single failure is a pre-existing Cloudflare Workers timeout unrelated to this change). Lint passes.

Breaking Changes

None. The onerror callback is optional (this.onerror?.()) and this only adds calls where none existed before. Existing code that doesn't set onerror is unaffected.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Several error paths in WebStandardStreamableHTTPServerTransport returned
error responses via createJsonErrorResponse() without calling the
onerror callback, making these errors unobservable for debugging and
logging purposes.

Fixes modelcontextprotocol#1395
@dpdanpittman dpdanpittman requested a review from a team as a code owner March 5, 2026 23:04
@changeset-bot
Copy link

changeset-bot bot commented Mar 5, 2026

⚠️ No Changeset found

Latest commit: ef0769e

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 Mar 5, 2026

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/server

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

@modelcontextprotocol/express

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 5a6ca4d

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