Streamable HTTP client hangs on a null-id JSON-RPC error in a 200 JSON response
Initial Checks
- [x] I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)
- [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Release line
2.x (current stable)
Description
On current main (2118f14f), when a server answers a request POST with a 200 application/json body holding a JSON-RPC error with "id": null, the streamable HTTP client never resolves the call. session.list_tools() (or any request) waits forever, or until a read timeout if one is set.
JSON-RPC 2.0 allows id: null on an error when the server could not determine the request id. Gateways and non-SDK servers can send one with status 200.
_handle_json_response in src/mcp/client/streamable_http.py forwards the parsed message unchanged. The dispatcher then drops it as a response to an unknown id (jsonrpc_dispatcher.py, "unknown/late request id").
The other two response paths in the same transport already handle this:
- SSE (
_handle_sse_event): replaces a response's id with the original request id. - Non-2xx JSON body (
_handle_post_request): rebuilds the error under the request's id. The comment there says: "The server may have setid: null(request rejected before its id was parsed); use this request's id so correlation works."
So the same error body resolves the call over SSE or with a 4xx status, but hangs with a 200.
Expected: the call raises MCPError with the server's error, the same as the SSE and non-2xx paths.
Proposed fix: in _handle_json_response, when the parsed message is a JSONRPCError, set its id to the POST's request id. A 2xx JSON body answers exactly that POST, so nothing else can be waiting on it. That is a 4-line change plus a regression test next to the existing non-2xx null-id test in tests/client/test_notification_response.py.
I'd like to fix this and have a branch ready: https://github.com/hyeonsang010716/python-sdk/tree/fix/streamable-http-json-null-id
I found and prepared this with AI assistance. I've reviewed the change, checked the repro below fails on main and passes with the fix, and can explain it.
Example Code
import json
import anyio
import httpx2
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from starlette.routing import Route
from mcp import ClientSession, MCPError
from mcp.client.streamable_http import streamable_http_client
async def handle_mcp(request: Request) -> Response:
data = json.loads(await request.body())
if data.get("method") == "initialize":
result = {"protocolVersion": "2025-06-18", "capabilities": {}, "serverInfo": {"name": "s", "version": "1"}}
return JSONResponse({"jsonrpc": "2.0", "id": data["id"], "result": result})
if "id" not in data:
return Response(status_code=202)
# e.g. a gateway that fails before it has parsed the request id
return JSONResponse({"jsonrpc": "2.0", "id": None, "error": {"code": -32603, "message": "upstream failed"}})
async def main() -> None:
app = Starlette(routes=[Route("/mcp", handle_mcp, methods=["POST"])])
async with httpx2.AsyncClient(transport=httpx2.ASGITransport(app=app)) as http:
async with streamable_http_client("http://localhost/mcp", http_client=http) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
try:
with anyio.fail_after(3):
await session.list_tools()
except MCPError as exc:
print("raised MCPError:", exc.error.message)
except TimeoutError:
print("list_tools() never returned")
anyio.run(main)
Output on main:
list_tools() never returned
Output with the fix:
raised MCPError: upstream failed
Python & MCP Python SDK
Python 3.14.7
mcp main @ 2118f14f (also present in v2.3.0)
The asymmetry you found is the fix: the other two response paths in the same transport already normalize the id, and only the 200-JSON path forwards it unchanged. That means the dispatcher is behaving correctly, and this is a gap in _handle_json_response, not a dispatcher problem.
_handle_sse_event rewrites the response id to the original request id, and _handle_post_request rebuilds the error under the request id for non-2xx. The 200-JSON path should do exactly the same thing: when the parsed body is a JSON-RPC error with id: null, substitute this request's id before handing it to the dispatcher. JSON-RPC 2.0 explicitly permits a null id on errors the server could not correlate, so a conformant client must accept it, and a gateway that emits 200 + {error, id: null} is a legitimate producer.
Two details worth getting right in the patch:
-
Only substitute for error responses with a null id. A response with a real id must pass through untouched, and a null-id success result is not something to guess at. Scope the rewrite to
"error" in msg and msg.get("id") is None. -
After substitution, resolve the pending request with the error rather than leaving it pending. Your repro expects
MCPError: upstream failed, not a timeout, and that only happens if the rewritten message reaches the dispatcher as a settled response for the open id. If the dispatcher has already dropped the entry, rewrite-then-deliver will not help, so confirm the pending entry is still live at that point.
The third option, cleaner than rewriting, is to key pending requests by id and allow a null-id error to settle the oldest in-flight request for that session when exactly one is outstanding. That is more invasive though, and since the codebase already has the rewrite pattern in two other paths, matching it here is the consistent choice.
The reason this matters beyond the SDK is that non-SDK servers and gateways are the common case, and they are exactly the ones that emit null-id errors on a 200. A client that hangs forever on that shape fails the interop test that matters most.
We hit this class of problem wiring third-party APIs behind one MCP server (corpusiq.io): an upstream that returns a well-formed error the transport does not normalize produces a silent hang, and the fix is always to normalize at the transport boundary so every response shape resolves the call.
Hi team,
I investigated this issue against current main (2118f14f).
Looking at _handle_json_response in src/mcp/client/streamable_http.py:
async def _handle_json_response(
self,
response: httpx2.Response,
read_stream_writer: StreamWriter,
*,
request_id: RequestId,
) -> None:
content = await response.aread()
message = jsonrpc_message_adapter.validate_json(content, by_name=False)
session_message = SessionMessage(message)
await read_stream_writer.send(session_message)
The function receives request_id, but does not apply it when the returned JSONRPCError (or response) has id: None. When this happens, _resolve_pending in jsonrpc_dispatcher.py cannot correlate the response, logs "dropping response for unknown/late request id None", and the calling task hangs awaiting the pending future.
The SSE path (_handle_sse_event, line 193) and the non-2xx branch (_handle_post_request, line 410) already correlate null-id errors with the original request ID.
I'm putting together a targeted PR that:
- Updates
_handle_json_responseto correlatemessage.id = request_idwhenmessage.id is None. - Adds a regression test in
tests/client/test_streamable_http.pyverifying that a 200 response with{"jsonrpc": "2.0", "id": null, "error": {...}}cleanly raisesMcpErrorrather than hanging.
Opening a PR shortly with the reproduction test and fix.
I hit the same hang on current main: a 200 application/json body with a JSON-RPC error whose id is null is forwarded unchanged from _handle_json_response, so the in-flight waiter never resolves. The non-2xx branch already remaps id: null to the outbound request id (~L408–411); the 200 JSON path does not.
Proposed fix: in _handle_json_response, when the parsed message is a JSONRPCError with a null id, rewrite id to the request_id argument before sending on read_stream_writer. Add a regression test where a Streamable HTTP stub returns that 200 body and assert list_tools() (or any request) fails fast with the remapped error instead of hanging.
If that direction looks right, I'd like to be assigned so I can open the PR under the external-contributor rule.
Thanks @Binary-Refinery — yes, a two-request case would be a useful complement to the single-request regression. It would show that a null-id error only settles the matching in-flight HTTP request, while a concurrent request still completes normally. I'll include that coverage in the PR once maintainers are happy for this to move forward.