Skip to content

fix: distinguish invalid JSON-RPC messages from JSON parse errors in HTTP server transports - #332

Open
bbdu3 wants to merge 1 commit into
modelcontextprotocol:mainfrom
bbdu3:fix/http-jsonrpc-error-classification
Open

bbdu3 wants to merge 1 commit into
modelcontextprotocol:mainfrom
bbdu3:fix/http-jsonrpc-error-classification

Conversation

@bbdu3

@bbdu3 bbdu3 commented Nov 23, 2026

Copy link
Copy Markdown

Summary

Both HTTP server transports currently return JSON-RPC error code -32700 when a POST body cannot be classified as a request, notification, or response. This conflates malformed JSON with valid JSON that does not represent a JSON-RPC message.

Fixes #321.

For example, within an initialized stateful session:

Request body Before After
{"not_a_valid_request": true} -32700 -32600
{"jsonrpc":"2.0","id":7} -32700 -32600
not valid json{ -32700 -32700

JSON-RPC 2.0 §5.1 reserves -32700 for invalid JSON and defines -32600 for an invalid Request object. MCP’s Streamable HTTP specification requires each POST body to contain a single JSON-RPC request, notification, or response.

HTTP responses remain 400 Bad Request, with id: null and the existing session headers.

Changes

  • Add an internal throwing initializer to JSONRPCMessageKind that distinguishes JSON parsing failures from message classification failures:
    • Throw MCPError.parseError when the body is malformed JSON.
    • Throw MCPError.invalidRequest when the body is valid JSON but cannot be classified.
  • Use .fragmentsAllowed during JSON parsing so valid JSON scalars are rejected as invalid messages rather than reported as syntax errors.
  • Update both StatefulHTTPServerTransport and StatelessHTTPServerTransport to return the specific classification error.
  • Preserve the existing failable initializer for callers that only need an optional classification result.
  • Document the error distinction on the internal initializer.

Validation

Validated in an isolated WSL Ubuntu 22.04 environment using Swift 6.1.

  • Before the fix: the regression tests failed on the expected error-code assertions in both transports. Malformed JSON control cases passed.
  • Before and after the fix: reproduced the three issue examples against the actual HTTP conformance server after initializing a valid session. The two valid JSON bodies changed from -32700 to -32600; malformed JSON continued to return -32700.
  • swift build — passed.
  • swift test — all 497 tests passed, including the parameterized regression cases.
  • Server core conformance suite, using modelcontextprotocol/conformance@v0.1.15 — 40 passed, 0 failed.
  • git diff --check — passed.

macOS validation was not performed locally.

Signed-off-by: bbdu3 <ergouyang854@gmail.com>
@bbdu3
bbdu3 requested a review from a team as a code owner November 23, 2026 03:16
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.

Parse error code for a JSON body that is not a request

1 participant