streamable HTTP: integer and string request ids with the same text share one response stream; a string id can displace the GET stream
Summary
StreamableHTTPServerTransport keys its per-request response streams by str(request_id). JSON-RPC and MCP allow a request id to be an integer or a string, and an integer 77 and a string "77" are different ids. After str() they map to the same key, so two valid, distinct in-flight requests on one session share one response stream.
The same string namespace holds the standalone GET stream's sentinel key, GET_STREAM_KEY = "_GET_stream". A request whose id is the string "_GET_stream" replaces the GET stream's registration.
This is distinct from #3060 / #2655 / #3137, which cover a client reusing the same id. Here the ids are different under the spec and still collide.
Where
src/mcp/server/streamable_http.py on v1.30.0 and v2.3.0 (latest at time of writing):
- v2.3.0:
request_id = str(message.id)(~:642),target_request_id = str(message.id)(~:1066),target_request_id = str(related_request_id)(~:1083), andGET_STREAM_KEY = "_GET_stream"(:62); - v1.30.0: the same sites at ~:599, ~:1063 and ~:1076, and :63.
Reproduction
Use the stateful streamable HTTP server, unmodified SDK, a real TCP client, and one protocol session.
- Send two concurrent requests with ids
77(integer) and"77"(string), each to a slow tool. - Cancel only the integer
77(notifications/cancelledwithrequestId: 77). - Observed: the HTTP stream of the string
"77"request receives integer77's cancellation response. The string request's own handler was not cancelled, but its response is lost or misdelivered. - Sentinel variant: with a GET stream open, send a request with id
"_GET_stream". Its POST registration replaces the GET stream's. The next server notification is lost, and a second GET is accepted (200) instead of being refused (409).
Matrix (JSON and SSE response modes × normal completion and cancellation × 7 id patterns):
- All 12 collision cells misroute: integer/string in both orders, plus the zero case
0and"0". - All 16 controls deliver correctly: integer-only, string-only, distinct mixed ids, and strings that cannot collide.
Expected
Responses route to the request with the exact same typed id. A string id never displaces the GET stream.
Proposed fix
Derive the internal stream key with one stateless, injective request-id-to-key helper, used at all three conversion sites, and keep a separate namespace for the GET sentinel. Wire ids stay unchanged.
Public close_sse_stream(request_id) should keep accepting the raw request id. A private helper should close by derived key. Replacing str() with json.dumps() alone looks like a fix, but it breaks raw-id close_sse_stream for string ids.
Thorough report. The additions worth making are two consequences you did not list, one second collision channel, and a detail on the fix.
Consequences beyond the lost response:
- The event store inherits the collision. In the message router the derived key is what goes to event_store.store_event(...) before delivery, so a replay after reconnect attributes the event to the colliding stream key. A client reconnecting with Last-Event-ID replays into the wrong request's stream, and that misattribution outlives the connection that caused it.
- In a gateway that multiplexes callers onto one session, request ids come from the callers. Two callers can independently choose 77 and "77", so this is not only self-inflicted: one caller's response can be delivered to another caller's stream, and a caller-chosen id of "_GET_stream" can displace server notifications. That moves the GET sentinel half from cosmetic to an integrity issue.
The second collision channel is at the related-id site. You cited a third conversion site as target_request_id = str(related_request_id), and in v1.30.0 that is str(session_message.metadata.related_request_id) (line 1076). That value comes from server-side metadata, not from the client's id space, so a server that stamps related_request_id as integer 77 collides with a client request whose id is the string "77". Same defect, different origin, and it means the fix has to cover all three sites rather than only the two that route a response.
On the fix, one addition to your note about json.dumps. You are right that swapping str() for json.dumps() alone breaks close_sse_stream for raw string ids, because the public method receives the wire id and would have to re-derive. A helper plus a separate close path is the right shape, and the sentinel does not need to share the string namespace at all:
_GET_STREAM = object() # identity key, unreachable from any client value
def _stream_key(request_id): return (type(request_id).__name__, request_id)
A tuple key keeps the value itself for equality, so 77 and "77" differ by the first element while the wire id stays inspectable in debug output. An object() sentinel makes the second half unrepresentable rather than merely unlikely, which is the difference between a fix and a mitigation.
If you add one cell to the matrix, make it the related-id channel: a response whose related_request_id is integer 77 arriving while a request with string id "77" is in flight. It needs no new harness and it tests the site your proposed helper would have to cover anyway.
Hi I'd like to work on this issue . Are you already working on a pr for the fix or would you be open to someone else working on it?
Hi I'd like to work on this issue . Are you already working on a pr for the fix or would you be open to someone else working on it?
I'm not. Fine by me!
Two verified data points for whoever picks this up. @riszhkejksnd, these are offered for your PR, not to take it over.
1. Fixing the stream keys doesn't fix the cancellation symptom. On main d7845c0, after the three str() sites are fixed, cancelling the earlier of 7/"7" over HTTP still hits the other request. The cause is the dispatcher, which folds ids into one _in_flight slot on every transport (stdio too). That's filed separately as #3807 because it needs its own decision. #3432 is related only in that both go through coerce_request_id.
2. A transport change that passes the full suite. One helper, used at the three sites:
def _stream_key(request_id: RequestId) -> str:
return str(request_id) if isinstance(request_id, int) else f"s:{request_id}"
- Integer keys are unchanged, so stored event-stream ids for integer requests don't move.
- No string id can equal
GET_STREAM_KEY. - The public
close_sse_stream(request_id)derives the key from the id it receives, and internal callers close by key. That keeps the raw-id contract described in the issue. - One tradeoff: events stored for string-id requests before an upgrade won't replay afterwards, because their keys change.
The regression test sends two concurrent POSTs with ids 7 and "7" and answers them in reverse order. Each POST must receive exactly its own typed response. It fails on main (one POST never completes) in both JSON and SSE modes.
Patch: https://github.com/ashirimi1019/mcp-faultline/blob/main/docs/upstream/patches/0002-Key-Streamable-HTTP-response-streams-by-the-typed-re.patch
Validated on Ubuntu from source, together with the dispatcher change (https://github.com/ashirimi1019/mcp-faultline/actions/runs/41533686584):
- upstream
scripts/testpasses with 100% coverage and strict-no-cover; - typed-id scenarios go from 11/24 to 24/24 over both SSE and JSON;
- 150 randomised scenarios pass.
#3192 would replace these sites. It keeps coerce_request_id for in-flight correlation, so the dispatcher issue would remain.
AI disclosure: found by an automated harness; this comment, the patch and its tests were drafted with AI assistance (Claude Code). Results come from the linked CI run.