Repository navigation
[Server] Fix lost responses on concurrent requests of the same session (Streamable HTTP) - #535
Draft
guillaume-sainthillier wants to merge 5 commits into
Conversation
…n (Streamable HTTP) Fixes modelcontextprotocol#275 and modelcontextprotocol#467. A response to a POST went through the session's outgoing queue, which concurrent requests of the same session read and write back whole. One request could then answer with another's response, and the other with an empty 202. StreamableHttpTransport now implements InlineResponseTransportInterface: Protocol hands it the responses through send() and the transport answers the POST with them. The queue keeps server-initiated requests and notifications.
PHPStan 2.3.0 reports the runtime checks of fromArray() as redundant with its phpdoc shape. They guard input from the wire, so keep them.
chr-hertel
reviewed
Oct 6, 2026
Member
|
nevermind changelog conflicts. they will happen all the time, and i can easily solve them while merging. just put the PR to ready for review when you think you're done :) |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #275, fixes #467. Implements the approach discussed in #275 (comment), replacing the filtering of #508.
Symptom
On Streamable HTTP (handshake era, JSON responses), concurrent requests on one session lose responses. A POST answers
202with an empty body instead of its JSON-RPC response, or answers with another request's response, sometimes inside an array. The client waits until it times out (TypeScript SDK:MCP error -32001: Request timed out). Parallel tool calls from LLM agents (n8n / LangChain) and Claude Code's concurrenttools/listandresources/liston connect run into it.Reproduction
Any server with several workers and a shared session store. Initialize a session, then send N POSTs at once with the same
Mcp-Session-Id. Measured with https://github.com/chr-hertel/mcp-concurrency-test (file store, loopback):tools/callphp -S, 8 workerstools/callphp -S, 8 workerstools/list+resources/listIn production on FrankenPHP worker mode (mcp/sdk 0.8.1 through symfony/mcp-bundle), 1 call in 10 got a
202.Root cause
A response was not returned by the POST that carried its request. It went through the session:
Protocol::sendResponse()queued it in_mcp.outgoing_queue(Protocol.php#L476-L482, #L503-L508) in the session data loaded at the start of the request.doProcessInput()saved that whole session back (#L223).createJsonResponse()reloaded the session and took the whole queue (StreamableHttpTransport.php#L224-L234, Protocol.php#L516-L524).With two requests A and B on one session:
202.202.Filtering the queue by request id (#508) fixes the second case but not the first.
Fix
StreamableHttpTransportimplements a new marker interface,InlineResponseTransportInterface. For such a transport,Protocol::sendResponse()hands every response toTransportInterface::send(), with the session id in thesession_idcontext key, instead of queueing it. This is the path session-less errors already took.consumeOutgoingMessages()only saves when it took something. Saving an unchanged session on every POST could only overwrite what a concurrent request had saved in the meantime.Tests
StreamableHttpTransportTest::testConcurrentPostsOfOneSessionEachGetTheirOwnResponseruns twotools/callPOSTs through realServerandStreamableHttpTransportinstances that share one store. The fixtureInterleavingSessionStoreruns B from inside A's session save, so the interleaving is deterministic and needs no real concurrency. There are two cases:Each POST must answer
200with its own id and the session header. Both cases fail onmain, where A answers202.testStreamedBatchCarriesInlineResponses: a batch whose tool call suspends to send progress still streams thepingresponse.Unit, integration and inspector suites pass. php-cs-fixer is clean. PHPStan only reports the two errors in
ElicitationSchema.phpthat are already there and unrelated.Backward compatibility
InlineResponseTransportInterface.StdioTransport,InMemoryTransportand custom transports keep the queue. Stdio would work unchanged on the inline path too; making it the default for every transport could be a follow-up.StreamableHttpTransport: no signature changes.createJsonResponse()andcreateStreamedResponse()keep their protected signatures.Protocol::consumeOutgoingMessages()returns the same thing. It just skips a save that would not change anything.Not in this PR
flockforFileSessionStore, symfony/lock for the PSR-16 store). symfony/mcp-bundle would then need an option to wire it. Happy to follow up if that direction suits you.mcp.server.<name>.controllerwrappinghandle()in symfony/lock'sLockFactory::createLock('mcp-session-'.$sessionId). With this PR, that lock is no longer needed for responses.