Skip to content

fix: call onerror for nested catch blocks in handlePostRequest#1677

Open
kai-agent-free wants to merge 2 commits intomodelcontextprotocol:mainfrom
kai-agent-free:fix/transport-onerror-1395
Open

fix: call onerror for nested catch blocks in handlePostRequest#1677
kai-agent-free wants to merge 2 commits intomodelcontextprotocol:mainfrom
kai-agent-free:fix/transport-onerror-1395

Conversation

@kai-agent-free
Copy link

Summary

Fixes #1395

In handlePostRequest, two nested try/catch blocks were silently swallowing errors:

  1. JSON parsing (req.json()) — returned createJsonErrorResponse(400, -32_700, 'Parse error: Invalid JSON') without calling this.onerror
  2. JSON-RPC schema validation (JSONRPCMessageSchema.parse) — returned createJsonErrorResponse(400, -32_700, 'Parse error: Invalid JSON-RPC message') without calling this.onerror

The outer catch at the end of handlePostRequest correctly calls this.onerror?.(error as Error), but these nested catches did not, making it impossible for consumers to observe these error conditions.

Changes

  • Added this.onerror?.(error as Error) to both nested catch blocks, consistent with the pattern used in the outer catch and in other methods (e.g., replayEvents, validateRequestHeaders)
  • Added 2 tests verifying onerror is called for invalid JSON and invalid JSON-RPC messages

Testing

All existing tests pass plus 2 new tests (39 total in server package).

In handlePostRequest, two nested try/catch blocks (JSON parsing and
JSON-RPC schema validation) were creating error responses via
createJsonErrorResponse but not calling this.onerror, silently
swallowing the errors. The outer catch correctly called this.onerror.

Add this.onerror?.(error as Error) calls to both nested catches,
consistent with the pattern used elsewhere in the codebase.

Add tests verifying onerror is called for both error cases.

Fixes modelcontextprotocol#1395
@kai-agent-free kai-agent-free requested a review from a team as a code owner March 13, 2026 11:31
@changeset-bot
Copy link

changeset-bot bot commented Mar 13, 2026

🦋 Changeset detected

Latest commit: 49d2573

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@modelcontextprotocol/server Patch
@modelcontextprotocol/express Patch
@modelcontextprotocol/hono Patch
@modelcontextprotocol/node Patch

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 13, 2026

Open in StackBlitz

@modelcontextprotocol/client

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

@modelcontextprotocol/server

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

@modelcontextprotocol/express

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

@modelcontextprotocol/hono

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

@modelcontextprotocol/node

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

commit: 1debab0

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