Invalid JSON-RPC envelope errors are not correlated with the original request id
Initial Checks
- [x] I confirm that I'm using the latest version of MCP Python SDK
- [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Description
After a normal initialization flow, several id-bearing JSON-RPC messages that are syntactically valid JSON but invalid JSON-RPC request envelopes are not correlated back to the original request id.
This is not meant to require implementations to recover an id after every low-level parse/deserialization failure. The narrower concern is that these payloads are parsed far enough to produce request-specific validation errors, while the original top-level id is still present in the payload. Preserving that id where feasible would make the error response easier for clients to correlate.
The tested inputs were:
| Case | Request | Validation detail |
|---|---|---|
wrong jsonrpc version |
{"jsonrpc":"1.0","id":3,"method":"ping","params":{}} |
JSONRPCRequest.jsonrpc: value should be "2.0" |
missing jsonrpc field |
{"id":4,"method":"ping","params":{}} |
JSONRPCRequest.jsonrpc: field required |
method as number |
{"jsonrpc":"2.0","id":8,"method":12345,"params":{}} |
JSONRPCRequest.method: input should be a valid string |
For all three envelope-invalid inputs, I would expect -32600 Invalid Request or -32602 error response that can be correlated to the original request where possible.
Observed behavior was consistent by transport:
- stdio: The server emits a
notifications/messagelog notification withlevel:"error"anddata:"Internal Server Error", but no JSON-RPC error response is sent for the original requestid. A follow-uppingsucceeds. - Streamable HTTP: The server returns HTTP 400 with a JSON-RPC error response using
id:"server-error"andcode:-32602, rather than the original requestid. A follow-uppingsucceeds over SSE.
Example Code
"""
With a Python SDK MCP server running over stdio or Streamable HTTP,
complete a normal initialization flow, then send these requests in isolation.
"""
requests = [
# Wrong JSON-RPC version: valid JSON, invalid JSON-RPC envelope.
{"jsonrpc": "1.0", "id": 3, "method": "ping", "params": {}},
# Missing jsonrpc field: valid JSON, invalid JSON-RPC envelope.
{"id": 4, "method": "ping", "params": {}},
# method is not a string: valid JSON, invalid JSON-RPC envelope.
{"jsonrpc": "2.0", "id": 8, "method": 12345, "params": {}},
]
Python & MCP Python SDK
* Python: `3.12.3`
* MCP Python SDK stable release: `v1.27.2` (`62137874ff26dd74d2fea80ff528a7fd9ca7a5e7`)
* Transports: stdio and Streamable HTTP
Great reproduction — the asymmetry between transport behaviors is the tell that this is a gap in the error-response dispatch, not a transport-specific bug.
The id: "server-error" in Streamable HTTP responses is particularly problematic because it breaks any client-side request/response correlation. If a client sends requests with ids 1, 2, 3 and #2 triggers this, the client sees a response with a non-matching id and has no way to know which request failed without falling back to timeout-based inference.
The root cause looks like the validation error path creates a new error response object without preserving the original request id from the invalid envelope. Even though the envelope is malformed, the id field is still parseable JSON — it just doesn't meet the JSON-RPC 2.0 structural requirements. Pydantic validators can be configured with model_validate to capture the raw input before validation fails, making the original id available for error response construction.
Pattern we use: when an incoming message fails envelope validation, we extract id from the raw parsed JSON (regardless of whether the rest of the envelope is valid), construct the error response with that id, and THEN validate the full structure. This way the client always gets a response correlated to its request, even for malformed envelopes.
Something like:
raw = json.loads(message)
raw_id = raw.get("id")
try:
request = JSONRPCRequest.model_validate(raw)
except ValidationError:
if raw_id is not None:
# Send correlated error
send_error(id=raw_id, code=-32600, message="Invalid Request")
else:
# Notification with no id — silently drop per spec
pass
The method as number case is interesting — Pydantic strict mode would catch that at validation time with the original id available for the error response.
Confirmed against current main (57394b0): all three cases still behave as reported — streamable HTTP answers HTTP 400 with id: null (and -32602), while stdio emits no JSON-RPC error response at all.
I have a fix working on a branch. Approach: transports hand undecodable frames to the session as a typed parse-failure exception carrying the raw payload; the receiving side recovers the top-level id when it is a scalar string/int and answers INVALID_REQUEST (-32600) with that id echoed. Applied uniformly on stdio, the 2025-era streamable HTTP entry, and the 2026-07-28 single-exchange entry, so the error correlation contract is identical everywhere.
One deliberate behavior change to flag for review: the legacy HTTP path's malformed/batched-body code moves -32602 → -32600, matching both the JSON-RPC 2.0 meaning of an invalid Request object and the modern entry's existing choice.
Regression tests cover all three inputs from this issue on every affected transport; full suite, 100% coverage, ruff, and pyright are green locally. Happy to open a PR if the issue gets assigned to me.
(AI-assisted analysis and drafting, prepared with a coding agent and reviewed by me.)