Repository navigation
Conversation
The POST handler answered a body that is not valid JSON with Invalid Request (-32600). JSON-RPC 2.0 section 5.1 reserves -32700 for invalid JSON and -32600 for JSON that is not a valid request object. Parse the body as JSON first and answer -32700 when that fails, then deserialize the JSON-RPC message and keep -32600 for envelope failures such as an explicit null request id. Fixes modelcontextprotocol#1918
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1918
The Streamable HTTP POST handler caught every
JsonExceptionfrom reading the body in one place and answeredInvalidRequest(-32600). That catch covered two different failures: a body that is not valid JSON, and valid JSON that theJsonRpcMessageconverter rejects (for example"id": null). JSON-RPC 2.0 §5.1 assigns -32700 (Parse error) to the first and -32600 to the second.The handler now reads the body as a
JsonElementfirst and answersParseError(-32700) if that fails, then deserializes theJsonRpcMessagefrom the element and keepsInvalidRequest(-32600) for envelope failures. HTTP status stays 400 and the error id stays null.SseHandler, which sharesReadJsonRpcMessageAsync, is unchanged. This matches the stdio transport, which already answersParseErrorsince #1629.Tests:
PostMalformedJson_Returns400_InvalidRequest_WithNullIdasserted -32600 for malformed JSON; it is nowPostMalformedJson_Returns400_ParseError_WithNullId, a theory over{ this is not valid jsonand the issue's truncated{"jsonrpc":. Both cases fail on main (expected -32700, actual -32600) and pass with this change.PostRequestWithExplicitNullId_Returns400_InvalidRequest_WithNullIdstill passes, pinning -32600 for valid JSON that is not a valid request. Builds on net8.0/net9.0/net10.0 with 0 warnings; tests run on net10.0.Reproduced against
samples/AspNetCoreMcpServerby POSTing{"jsonrpc":: main answers -32600, this branch answers -32700 (recording below).Notes: overlaps the
catchblock edited in #1844, so whichever lands second needs a small rebase. Valid requests now parse once into aJsonElementbefore deserializing; happy to measure if that's a concern. Opened as a draft so the approach can be confirmed on the issue first.AI disclosure: this change was written with Claude Code (investigation, code and tests). I reviewed the diff and ran the build and tests locally.
Evidence: real MCP clients against the SDK's own
samples/AspNetCoreMcpServer. Left pane: the sample server, built from main (before) or this branch (after), with its request log. Right: the official MCP Inspector connects over Streamable HTTP and callsechonormally, then a small "buggy client" page POSTs (1) a normalinitialize, (2) a body cut off mid-write ({"jsonrpc":), and (3) valid JSON with"id": null, showing the error code it gets back and what that code tells a client developer.Before (main): the cut-off body gets -32600 Invalid Request.
video-narrow.mp4
After (this branch): the cut-off body gets -32700 Parse error; the null-id request still gets -32600.
video-narrow.mp4