You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Parse error code for a JSON body that is not a request #321
JSON-RPC 2.0 section 5.1 reserves -32700 for invalid JSON and gives -32600 to JSON that is not a valid Request object. I sent a JSON body that is not a request to the conformance test server in modelcontextprotocol/swift-sdk (Sources/MCPConformance/Server, commit a0ae212) in an initialized session and got -32700:
The body {"jsonrpc": "2.0", "id": 7}, which has no method, gets the same code, where -32600 is expected for both. Malformed JSON such as not valid json{ correctly gets -32700. Without the session header the first body gets -32600 ("Missing MCP-Session-Id header"), so a probe that skips the session sees a conformant server.
Can you check whether the -32700 for these two bodies is a bug, and fix it to -32600 if so?
I traced this through the current main implementation and reproduced the behavior in the HTTP transport classification path.
For a request with a valid session:
{"not_a_valid_request": true} is valid JSON, but it is not a valid JSON-RPC message.
{"jsonrpc":"2.0","id":7} is also valid JSON, but it is not a valid Request because it has no method.
not valid json{ is malformed JSON.
JSONRPCMessageKind.init(data:) currently returns nil for both JSON parsing failures and valid JSON that cannot be classified as a request, notification, or response. StatefulHTTPServerTransport.handlePost then maps every nil result to MCPError.parseError, which produces -32700.
That means the malformed JSON case correctly returns -32700, but the first two valid JSON cases should return -32600 Invalid Request. The HTTP status can remain 400; the issue is the JSON-RPC error code in the response body.
My proposed fix is to preserve the distinction between syntax parsing and message classification:
return -32700 only when the body is not valid JSON;
return -32600 when the body is valid JSON but does not represent a valid JSON-RPC message;
add regression tests for both stateful and stateless HTTP transports, including a malformed JSON control case.
This should be an internal transport change without affecting the public API. If this approach looks correct, I would be happy to open a PR to fix it.
Fixed on rel/1.0.0 (#372) in #401 and #404: Server and both HTTP server transports now answer valid JSON that is not a valid JSON-RPC message (such as {"not_a_valid_request": true}, scalars, or objects missing method/result/error) with -32600 (Invalid Request), reserving -32700 (Parse Error) strictly for malformed JSON per JSON-RPC 2.0 §5.1 (RequestLifecycleTests, HTTPServerTransportTests). Closing as resolved in 1.0.0.
Activity
bbdu3 commentedon Nov 6, 2026
I traced this through the current
mainimplementation and reproduced the behavior in the HTTP transport classification path.For a request with a valid session:
{"not_a_valid_request": true}is valid JSON, but it is not a valid JSON-RPC message.{"jsonrpc":"2.0","id":7}is also valid JSON, but it is not a valid Request because it has nomethod.not valid json{is malformed JSON.JSONRPCMessageKind.init(data:)currently returnsnilfor both JSON parsing failures and valid JSON that cannot be classified as a request, notification, or response.StatefulHTTPServerTransport.handlePostthen maps everynilresult toMCPError.parseError, which produces-32700.That means the malformed JSON case correctly returns
-32700, but the first two valid JSON cases should return-32600 Invalid Request. The HTTP status can remain400; the issue is the JSON-RPC error code in the response body.My proposed fix is to preserve the distinction between syntax parsing and message classification:
-32700only when the body is not valid JSON;-32600when the body is valid JSON but does not represent a valid JSON-RPC message;This should be an internal transport change without affecting the public API. If this approach looks correct, I would be happy to open a PR to fix it.
jeanmatthieu commentedon Dec 5, 2026
Fixed on
rel/1.0.0(#372) in #401 and #404:Serverand both HTTP server transports now answer valid JSON that is not a valid JSON-RPC message (such as{"not_a_valid_request": true}, scalars, or objects missingmethod/result/error) with-32600(Invalid Request), reserving-32700(Parse Error) strictly for malformed JSON per JSON-RPC 2.0 §5.1 (RequestLifecycleTests,HTTPServerTransportTests). Closing as resolved in 1.0.0.