Title: Concurrent requests sharing a JSON-RPC id on one session receive each other's responses · Issue #3137 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Concurrent requests sharing a JSON-RPC id on one session receive each other's responses · Issue #3137 · modelcontextprotocol/python-sdk
X Title: Concurrent requests sharing a JSON-RPC id on one session receive each other's responses · Issue #3137 · modelcontextprotocol/python-sdk
Description: Concurrent requests sharing a JSON-RPC id on one session receive each other's responses Repo: modelcontextprotocol/python-sdk Versions: reproduced on mcp 1.28.1 (latest) with fastmcp 3.4.4, and on mcp 1.26.0 with fastmcp 3.2.0. Python 3....
Open Graph Description: Concurrent requests sharing a JSON-RPC id on one session receive each other's responses Repo: modelcontextprotocol/python-sdk Versions: reproduced on mcp 1.28.1 (latest) with fastmcp 3.4.4, and on ...
X Description: Concurrent requests sharing a JSON-RPC id on one session receive each other's responses Repo: modelcontextprotocol/python-sdk Versions: reproduced on mcp 1.28.1 (latest) with fastmcp 3.4.4, and...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3137
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Concurrent requests sharing a JSON-RPC id on one session receive each other's responses","articleBody":"# Concurrent requests sharing a JSON-RPC id on one session receive each other's responses\n\n**Repo:** `modelcontextprotocol/python-sdk`\n**Versions:** reproduced on `mcp` 1.28.1 (latest) with `fastmcp` 3.4.4, and on `mcp` 1.26.0 with `fastmcp` 3.2.0. Python 3.14, macOS + Linux.\n**Transport:** streamable HTTP, stateful (default)\n\n## Summary\n\nIn stateful streamable HTTP, the server registers each in-flight request's response stream in a per-session dict keyed by the **bare JSON-RPC request id**. In `_handle_post_request` (`src/mcp/server/streamable_http.py`), both the JSON-response and SSE branches do this:\n\n```python\nrequest_id = str(message.id)\n...\nself._request_streams[request_id] = anyio.create_memory_object_stream[EventMessage](\n REQUEST_STREAM_BUFFER_SIZE\n)\nrequest_stream_reader = self._request_streams[request_id][1]\n```\n\nThe assignment is unconditional. If a second request arrives on the same session with an id that is already in flight, it silently **overwrites** the first request's stream entry. When the first request's handler completes, the response is routed to whatever stream currently sits at that key:\n\n```python\nif request_stream_id in self._request_streams:\n await self._request_streams[request_stream_id][0].send(EventMessage(message, event_id))\n```\n\nThe result is that **one caller receives another caller's response payload**, and the rightful caller's POST hangs until it times out.\n\nJSON-RPC 2.0 requires ids to be unique only within a client's own request stream. Any component that multiplexes multiple logical clients over one MCP session — a gateway, a proxy, a connector with a shared session pool — will produce colliding ids as normal operation, because each client leg numbers its own requests independently from 1.\n\n## Reproduction\n\n`server.py`:\n\n```python\nimport time\nfrom fastmcp import FastMCP\n\nmcp = FastMCP(\"crossing-repro\")\n\n@mcp.tool()\ndef alice() -> str:\n \"\"\"Finishes first (1s).\"\"\"\n time.sleep(1)\n return \"SECRET-BELONGING-TO-ALICE\"\n\n@mcp.tool()\ndef bob() -> str:\n \"\"\"Finishes later (3s).\"\"\"\n time.sleep(3)\n return \"SECRET-BELONGING-TO-BOB\"\n\nif __name__ == \"__main__\":\n mcp.run(transport=\"http\", host=\"127.0.0.1\", port=8177, show_banner=False)\n```\n\n`client.py` — one session, two concurrent `tools/call` POSTs sharing `id=2`. Leg A calls `alice` at t=0; leg B calls `bob` at t=0.3s:\n\n```python\nimport json, threading, httpx\n\nBASE = \"http://127.0.0.1:8177/mcp\"\nHEADERS = {\"Content-Type\": \"application/json\",\n \"Accept\": \"application/json, text/event-stream\"}\nCOLLIDING_ID = 2\n\ndef payloads(resp):\n msgs = [json.loads(l[6:]) for l in resp.text.splitlines() if l.startswith(\"data: \")]\n out = []\n for msg in msgs:\n try:\n out.append(msg[\"result\"][\"content\"][0][\"text\"])\n except (KeyError, IndexError, TypeError):\n out.append(json.dumps(msg))\n return out\n\nwith httpx.Client(timeout=12) as c:\n r = c.post(BASE, headers=HEADERS, json={\n \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"initialize\",\n \"params\": {\"protocolVersion\": \"2025-06-18\", \"capabilities\": {},\n \"clientInfo\": {\"name\": \"repro\", \"version\": \"0\"}}})\n h = dict(HEADERS, **{\"mcp-session-id\": r.headers[\"mcp-session-id\"]})\n c.post(BASE, headers=h, json={\"jsonrpc\": \"2.0\",\n \"method\": \"notifications/initialized\"})\n\n results = {}\n def leg(name, tool, delay):\n import time as t\n t.sleep(delay)\n try:\n rr = c.post(BASE, headers=h, json={\n \"jsonrpc\": \"2.0\", \"id\": COLLIDING_ID, \"method\": \"tools/call\",\n \"params\": {\"name\": tool, \"arguments\": {}}})\n results[name] = (tool, rr.status_code, payloads(rr))\n except httpx.ReadTimeout:\n results[name] = (tool, \"TIMEOUT\", [])\n\n ts = [threading.Thread(target=leg, args=(\"leg A\", \"alice\", 0.0)),\n threading.Thread(target=leg, args=(\"leg B\", \"bob\", 0.3))]\n for t_ in ts: t_.start()\n for t_ in ts: t_.join()\n for name in (\"leg A\", \"leg B\"):\n tool, status, body = results[name]\n print(f\"{name} called {tool!r:8} -> HTTP {status}; \"\n f\"received: {body[0] if body else '(nothing — request hung)'}\")\n```\n\n## Actual output\n\n```\nleg A called 'alice' -> HTTP TIMEOUT; received: (nothing — request hung)\nleg B called 'bob' -> HTTP 200; received: SECRET-BELONGING-TO-ALICE\n```\n\nLeg B asked for `bob` and was handed `alice`'s return value. Leg A never received anything. Identical output on `mcp` 1.28.1 / `fastmcp` 3.4.4 and on 1.26.0 / 3.2.0.\n\n## Expected\n\nEach POST receives the response to the request it sent. If the server cannot support a duplicate in-flight id on one session, it should reject the second request explicitly (e.g. an `-32600` error naming the collision) rather than silently rerouting a response to the wrong caller.\n\n## Why this matters\n\nBeyond the correctness bug, on a multi-tenant or gateway-fronted server the misrouted payload is **another caller's data**, delivered to a caller that never requested it and cannot tell it is wrong. There is no error, no warning, and nothing in the response that identifies the mismatch — the payload is a well-formed JSON-RPC result carrying the colliding id. We hit this in production: a tool call returned a completely unrelated tool's receipt from a different logical session, and the only reason it was caught is that the payload shape was obviously wrong for the call that was made. A same-shaped response would have passed silently.\n\n## Workaround (verified)\n\nRunning the server in stateless mode gives each request its own transport, so the shared per-session registry never comes into play. Same repro, `FASTMCP_STATELESS_HTTP=true`:\n\n```\nleg A called 'alice' -> HTTP 200; received: SECRET-BELONGING-TO-ALICE [OK]\nleg B called 'bob' -> HTTP 200; received: SECRET-BELONGING-TO-BOB [OK]\n```\n\nThis is only available to servers that don't need stateful features (server-initiated messages, resumability, subscriptions).\n\n## Related\n\n#1764 touches the same `_request_streams` registration but is a different failure mode (zero-buffer deadlock, since fixed by `REQUEST_STREAM_BUFFER_SIZE`). The id-collision misrouting described here is unaffected by that fix and reproduces on current `main`'s logic.\n\n## Suggested fix\n\nEither:\n\n1. **Key the registry by something unique per request** rather than the raw JSON-RPC id — e.g. `(stream/transport identity, request id)`, or an internally-minted token. This preserves current behavior for well-behaved single-client sessions and fixes multiplexed ones.\n2. **Reject the collision explicitly** — if `request_id` is already present in `_request_streams`, return a JSON-RPC error for the second request instead of overwriting. Less useful for gateways, but it converts silent data misrouting into a loud, diagnosable failure.\n\nOption 1 is preferable: id collisions across independent client legs are legal JSON-RPC, so a gateway shouldn't have to rewrite ids to stay correct.\n\nI'm happy to open a PR for either approach if you'd like to indicate a preference.\n","author":{"url":"https://github.com/touchedtone","@type":"Person","name":"touchedtone"},"datePublished":"2026-07-19T23:10:20Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3137"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:49053abf-f6ec-7117-6b8e-8baae5968d75 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CA5C:29AEE8:A1E9D:DD44A:6B00B9A2 |
| html-safe-nonce | 9d41d6f4c649f521ef4927841d9869e3c8b8d4335464b9c5b2d57a6d91685de0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQTVDOjI5QUVFODpBMUU5RDpERDQ0QTo2QjAwQjlBMiIsInZpc2l0b3JfaWQiOiIxOTg3MjgzOTQyMzM5ODE5OTM4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 094e4a5f5e01dd7f75689cec3b04d00b505abc18dbd7be88094844a90162fce0 |
| hovercard-subject-tag | issue:4924751237 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3137/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cc606213e80fced1474f1619c93ce4fc8221038365e904e3d33635900b2308d8/modelcontextprotocol/python-sdk/issues/3137 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cc606213e80fced1474f1619c93ce4fc8221038365e904e3d33635900b2308d8/modelcontextprotocol/python-sdk/issues/3137 |
| og:image:alt | Concurrent requests sharing a JSON-RPC id on one session receive each other's responses Repo: modelcontextprotocol/python-sdk Versions: reproduced on mcp 1.28.1 (latest) with fastmcp 3.4.4, and on ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | touchedtone |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1d07c25977adfc5ff6acb0a962a9bc168c07afbda6fef765f9761c350c2ae4a2 |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5429cbfc6aca8c7b9a086a3c8f47400346f38cc9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width