Skip to content

fix(server): invoke onerror callback for all error responses#1725

Open
owendevereaux wants to merge 2 commits intomodelcontextprotocol:mainfrom
owendevereaux:fix/transport-onerror-callbacks
Open

fix(server): invoke onerror callback for all error responses#1725
owendevereaux wants to merge 2 commits intomodelcontextprotocol:mainfrom
owendevereaux:fix/transport-onerror-callbacks

Conversation

@owendevereaux
Copy link

Summary

Fixes #1395

Several error responses in WebStandardStreamableHTTPServerTransport were returned without invoking the onerror callback, making it impossible to debug or log these errors.

Problem

As reported in #1395, errors in nested try/catch blocks within handlePostRequest, handleGetRequest, and validation methods were being silently swallowed. The onerror callback was only invoked in the outermost catch block, so errors like:

  • Accept header validation failures (406)
  • Content-Type validation failures (415)
  • JSON parse errors (400)
  • Session validation errors (400, 404)

...were never reported via the callback, preventing any form of debugging.

Solution

Added this.onerror?.(new Error(message)) calls before all createJsonErrorResponse returns, matching the existing pattern already used in validateRequestHeaders().

For parse errors, the original exception is preserved via { cause: e }:

} catch (e) {
    const error = 'Parse error: Invalid JSON';
    this.onerror?.(new Error(error, { cause: e }));
    return this.createJsonErrorResponse(400, -32_700, error);
}

Error Types Now Reported

  • Accept header validation (406)
  • Content-Type validation (415)
  • JSON parse errors (400)
  • JSON-RPC message parse errors (400)
  • Session validation errors (400, 404)
  • Protocol version errors (400)
  • Initialization errors (400)
  • SSE stream conflicts (409)
  • Event store errors (400, 409, 500)

Testing

Build passes. The changes are straightforward additions of onerror calls before existing error returns.

Previously, several error responses in StreamableHTTPServerTransport
were returned without invoking the onerror callback, making it
impossible to debug or log these errors.

This change ensures all error responses call this.onerror() before
returning, matching the existing pattern in validateRequestHeaders().

Error types now properly reported:
- Accept header validation (406)
- Content-Type validation (415)
- JSON parse errors (400)
- JSON-RPC message parse errors (400)
- Session validation errors (400, 404)
- Protocol version errors (400)
- Initialization errors (400)
- SSE stream conflicts (409)
- Event store errors (400, 409, 500)

Fixes modelcontextprotocol#1395
@owendevereaux owendevereaux requested a review from a team as a code owner March 20, 2026 16:28
@changeset-bot
Copy link

changeset-bot bot commented Mar 20, 2026

🦋 Changeset detected

Latest commit: 305a1d1

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

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