Skip to content

Return ParseError (-32700) for malformed JSON on Streamable HTTP POST - #2046

Open
jhrabchak wants to merge 1 commit into
modelcontextprotocol:mainfrom
jhrabchak:fix/issue-1918
Open

jhrabchak wants to merge 1 commit into
modelcontextprotocol:mainfrom
jhrabchak:fix/issue-1918

Conversation

@jhrabchak

Copy link
Copy Markdown

Fixes #1918

Previously, StreamableHttpHandler.HandlePostRequestAsync caught all JsonExceptions from ReadJsonRpcMessageAsync and returned McpErrorCode.InvalidRequest (-32600), whether the failure was invalid JSON syntax or a valid JSON document that failed JSON-RPC envelope validation.

Changes

  • Parse the POST body into a JsonElement first (reusing the existing source-generated JsonTypeInfo<JsonElement> in McpJsonUtilities.ProtocolOptions), returning HTTP 400 with McpErrorCode.ParseError (-32700) and id: null when JSON parsing fails while preserving the parser error detail from Surface JSON parse detail in streamable-HTTP 400 responses #1844.
  • Deserialize JsonRpcMessage from the parsed JsonElement in a second step so invalid JSON-RPC envelopes (such as null, [], {}, wrong jsonrpc version, or explicit id: null) continue returning McpErrorCode.InvalidRequest (-32600).
  • Update the malformed and truncated JSON conformance tests to assert -32700, and add tests confirming invalid JSON-RPC envelopes still return -32600.

Fixes modelcontextprotocol#1918

In StreamableHttpHandler.HandlePostRequestAsync, parse the POST body
into a JsonElement first using the existing source-generated
JsonTypeInfo<JsonElement> contract, and deserialize JsonRpcMessage from
that element in a second step.

- Invalid JSON syntax (truncated payloads, malformed tokens, exceeded
  max depth) now returns HTTP 400 with McpErrorCode.ParseError (-32700)
  and a null id per JSON-RPC 2.0 section 5.1, while keeping the parser
  diagnostic message from modelcontextprotocol#1844.
- Valid JSON that is not a valid JSON-RPC 2.0 message (e.g., null, [],
  {}, invalid jsonrpc version, or explicit id: null) continues to return
  HTTP 400 with McpErrorCode.InvalidRequest (-32600).

This branch has not been deployed

No deployments
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.

Streamable HTTP returns -32600 instead of -32700 for malformed JSON

1 participant