[BUG]: translate --expose-streamable-http mixes up responses between concurrent clients (id collision on shared stdio child)
🐞 Bug Summary
mcpgateway.translate --stdio ... --expose-streamable-http shares one stdio child process between all HTTP clients, but POST /mcp correlates the child's responses using the client-supplied JSON-RPC id only. When two clients have a request in flight with the same id (which is normal: each MCP client numbers its own requests, typically from 0 or 1), both handlers wait for "the first message on the broadcast bus whose id matches". The first response that arrives satisfies both, so one client receives the response to another client's request (for example the result of a different tool).
A second, smaller race in the same handler: the handler subscribes to the pub/sub bus after writing the request to stdio, so a fast response can be published before the subscription exists. The client then gets 202 accepted (no response yet) instead of the result.
🧩 Affected Component
- [x] Federation or Transports (
mcpgateway/translate.py,mcp_posthandler)
🔁 Steps to Reproduce
What we observed is the symptom through the gateway (below); the minimal scenario here is derived from the code.
- Wrap any stdio MCP server that can answer out of order (an async server that runs tool calls concurrently), for example:
python -m mcpgateway.translate --stdio "<some-mcp-server>" --expose-streamable-http --port 9100 - From two independent clients (or two concurrent sessions through the gateway), send
tools/callrequests for two different tools at the same time, both with"id": 1. - At least one client can receive the other tool's result. On our setup, 3 concurrent calls through the gateway to an async stdio server were enough to trigger it, while servers that answer strictly in order did not show it.
Relevant code (main @ 077071bbb435, identical in v1.0.10), mcp_post:
await stdio.send(body.decode().rstrip() + "\n") # 1. request written to the shared child
if isinstance(obj, dict) and "id" in obj:
queue = pubsub.subscribe() # 2. subscription happens AFTER the send
...
if isinstance(candidate, dict) and candidate.get("id") == obj.get("id"):
return ORJSONResponse(candidate) # 3. first message with the same id wins
_PubSub.publish fans every stdout line out to all subscribers, so every waiting handler sees every response.
🤔 Expected Behavior
Each HTTP request receives the response to its own request, regardless of how many clients share the stdio child.
A usual fix for a multiplexing bridge:
- rewrite the request
idto a bridge-unique value before writing to stdio (e.g. a counter or UUID), keep a mapunique_id -> (original_id, waiter), and restore the originalidin the response returned to the client; - register the waiter before writing to stdio.
📓 Logs / Error Output
Seen from a strict MCP client (Claude Code) calling tools through the gateway: the client rejects the payload with
Invalid structured content returned by tool <tool_A> whose content actually belongs to <tool_B>. The same tool called alone, or the same set of calls issued sequentially, always succeeds. Servers that process requests strictly in order rarely trigger it; an async server (concurrent tool execution) triggers it with 3 parallel calls.
🧠 Environment Info
| Key | Value |
|---|---|
| Version or commit | mcp-contextforge-gateway 1.0.10 (code unchanged on main @ 077071bbb435) |
| Runtime | Python 3.12.14, python -m mcpgateway.translate |
| Platform / OS | Linux, Docker |
| Container | Docker (one translate bridge per stdio server, federated behind a ContextForge gateway) |
🧩 Additional Context (optional)
Workaround in use: never send more than 2 concurrent calls to the same wrapped server. Servers exposing native Streamable HTTP are not affected, since no shared stdio child is involved.