Repository navigation
Description
Summary
When a client sends two JSON-RPC requests with the same id on the same Streamable HTTP session before the first response is delivered, StreamableHTTPServerTransport overwrites the per-id response stream slot without warning. The first request's caller hangs until its read times out, and the second response is delivered to whichever caller wins the race. The server logs nothing.
The MCP base protocol requires that "the request ID MUST NOT have been previously used by the requestor within the same session," so a duplicate id is a protocol violation by the client. But the server has no way to tell the client (or an operator) that the violation happened, which makes this very hard to diagnose downstream.
Where it happens
In src/mcp/server/streamable_http.py lines 534-536 at v1.27.1:
request_id = str(message.root.id)
self._request_streams[request_id] = anyio.create_memory_object_stream[EventMessage](0)
request_stream_reader = self._request_streams[request_id][1]The assignment is unconditional. If request_id is already a key in _request_streams, the prior (send, receive) pair is dropped on the floor. The earlier request's writer eventually tries to push a response into a stream nobody is reading, and the earlier request's caller is reading a stream nobody will ever write to.
The router side at lines 1004-1033 uses the same str(id) keying to dispatch responses, so once the slot has been overwritten, response routing is undefined.
Reproduction signature
We hit this through an in-house client that was sending id: 1 for every concurrent tool call on a shared session. Under concurrent load, calls timed out and at least one caller received the wrong tool's response payload.
Proposed Behavior
When a request arrives with an id that already has an in-flight stream registered, the server should reject it with JSON-RPC -32600 Invalid Request and leave the existing in-flight request untouched. This:
- Surfaces the spec violation to the client immediately instead of timing out.
- Preserves the in-flight request that arrived first.
- Gives operators a log line to diagnose against.
A weaker variant (log-only, no error response) would still be a meaningful improvement over silent data loss.
Activity
truffle-dev commentedon May 21, 2026
Confirmed at HEAD on
v1.x. The unconditional dict assignment is still present atsrc/mcp/server/streamable_http.py:536(line 529 onmain). Whenrequest_idalready keys an in-flight stream, the prior(send, receive)pair is dropped on the floor exactly as you described. The earlier handler'srequest_stream_readerthen reads a closed stream, and the response router at line 1006 routes the response to whichever stream happens to be live.The existing
GET_STREAM_KEY in self._request_streamscollision check at line 690 is the precedent for the fix shape:This matches the GET-stream conflict (409 +
_create_error_response) and preserves the in-flight request untouched.One nuance worth a maintainer call: the existing helper builds the JSONRPCError with
id=None. For most callers that's correct (parse errors beforeidis known), but here the client knows the id. Mirroring the existing helper keeps the diff one-call; threading the offending id through would require either a new helper signature or an inlineJSONRPCErrorconstruction. I'd lean on the helper for consistency but can go either way.Happy to send the PR (
[v1.x]target, with a regression test that seeds_request_streams[request_id]and POSTs the duplicate) if it's welcome. Targetingv1.xsince this is a non-breaking server-side bug fix per AGENTS.md.[v1.x] fix(streamable-http): reject duplicate JSON-RPC ids with 409
norika1207-lab commentedon Jun 2, 2026
[v1.x] fix(streamable-http): reject duplicate JSON-RPC ids with 409
waseem1302-x commentedon Nov 11, 2026
Hi, I'm Waseem Mushtaq, a final-year Computer Science undergraduate at Albukhary International University (AIU) in Malaysia on a full scholarship. I'm working on AI engineering, tool calling and agent infrastructure, and this issue is directly in that area.
I reproduced this on main (v2). With a slow tool (3s) and a fast tool (0.5s) sent 0.3s later with the same request id, the second request got its own response in about 0.5s, but the first request was still hanging 12s later, with no error and no log. With unique ids, both requests completed correctly. The same happens in SSE mode.
The cause looks like the unconditional overwrite of the per-id response stream in _handle_post_request, so the first request's response is routed to the second request's stream and the first handler is left waiting.
I saw the earlier PRs #2657, #3063 and #3064 were closed in the backlog sweep, not on the merits, so I wanted to check before doing any work here. @truffle-dev and @ghost, are you still planning to continue with this? And could a maintainer confirm whether the transport-level guard is still the wanted scope, returning 409 with JSON-RPC -32600 when a request id is already in flight, and whether I can be assigned this issue to work on it?
Thanks!
saifullah-ai-engineer commentedon Nov 17, 2026
Confirmed on current main (77001c9). I reproduced this with the in-memory transport harness: two concurrent JSON-mode POSTs with id "dup-1" on one session. Both requests are forwarded to the session, the first caller then hangs, and the duplicate receives the first request's response. So the overwrite is still live on v2, in both the JSON-response and SSE branches of
_handle_post_request.Why it matters for my use case: I build MCP clients that fan out concurrent tool calls, and a client that reuses ids (or a retry that replays one) silently loses responses with no log line. A clear rejection would make this diagnosable instead of a hang.
Approach I would take, reviving #2657 on v2: a single guard right after
request_id = str(message.id), before both branches and before the priming-event mint, so no per-request state is created for a duplicate. If the id is already in_request_streams, log a warning and return 409 with a JSON-RPC -32600 Invalid Request error, leaving the in-flight request untouched. Regression tests intests/server/test_streamable_http_router.pycovering the guard plus a concurrent duplicate that still lets the first request complete.If you are happy with that approach, please assign #2655 to me and I will open the PR with the fix and tests.
Disclosure: I worked on the reproduction with AI assistance and have reviewed and tested it myself.