Skip to content

fix(transport): call onerror for silently swallowed parse errors#1626

Open
nielskaspers wants to merge 2 commits intomodelcontextprotocol:mainfrom
nielskaspers:fix/1395-transport-onerror-silent-swallow
Open

fix(transport): call onerror for silently swallowed parse errors#1626
nielskaspers wants to merge 2 commits intomodelcontextprotocol:mainfrom
nielskaspers:fix/1395-transport-onerror-silent-swallow

Conversation

@nielskaspers
Copy link
Contributor

Summary

Fixes #1395

Several nested try/catch blocks inside StreamableHTTPServerTransport.handlePostRequest were returning 400 error responses without calling the onerror callback. This made those errors completely invisible to server authors who rely on transport.onerror for logging and observability.

Affected paths (before this fix):

  • Invalid JSON body (req.json() throws) → returned 400, onerror never called
  • Invalid JSON-RPC message (JSONRPCMessageSchema.parse throws) → returned 400, onerror never called

The outer catch at the end of handlePostRequest already correctly calls this.onerror?.(error as Error), so this change makes the nested catches consistent with that pattern.

Changes

  • packages/server/src/server/streamableHttp.ts: capture the error in two nested catch blocks and call this.onerror?.(parseError as Error) before returning the JSON error response

Test plan

  • Attach an onerror handler to a StreamableHTTPServerTransport and send a request with a malformed JSON body — the handler should now fire
  • Send a request with a valid JSON body that is not a valid JSON-RPC message — the handler should now fire

Several nested try/catch blocks in StreamableHTTPServerTransport.handlePostRequest
returned error responses without calling the onerror callback, making it
impossible to observe or log those errors.

Add onerror calls before returning 400 responses for invalid JSON and
invalid JSON-RPC message parse failures, consistent with the outer catch
block that already calls onerror.

Fixes modelcontextprotocol#1395
@nielskaspers nielskaspers requested a review from a team as a code owner March 4, 2026 22:14
@changeset-bot
Copy link

changeset-bot bot commented Mar 4, 2026

⚠️ No Changeset found

Latest commit: d7e63d2

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

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/server

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

@modelcontextprotocol/express

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 2e0bcae

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

2 participants