You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
StreamableHTTPServerTransport: TOCTOU race between GET 409 conflict check and standalone SSE slot registration allows concurrent GET streams #3891
In src/mcp/server/streamable_http.py (StreamableHTTPServerTransport._handle_get_request), the check rejecting a second standalone GET SSE stream (if GET_STREAM_KEY in self._request_streams: -> 409 Conflict) and the registration of self._request_streams[GET_STREAM_KEY] are not atomic:
The conflict check runs synchronously in _handle_get_request() (~line 768).
The slot is only inserted inside the async standalone_sse_writer() task (~line 783), which sse-starlette's EventSourceResponse schedules via task_group.start_soon(...) after the response begins.
By contrast, both POST paths register self._request_streams[request_id] synchronously before yielding control.
When two GET /mcp (Accept: text/event-stream) requests arrive concurrently for the same session:
Both pass if GET_STREAM_KEY in self._request_streams: before either standalone_sse_writer() runs.
Both return HTTP 200 instead of [200, 409].
The second writer overwrites self._request_streams[GET_STREAM_KEY], leaving the first stream orphaned (receiving no server-initiated messages), and when the first connection later closes its finally cleanup closes the second connection's active stream.
Minimal Reproduction
In tests/shared/test_streamable_http.py, dispatch two concurrent GET /mcp streams for the same initialized session via anyio.create_task_group():
Register self._request_streams[GET_STREAM_KEY] synchronously in _handle_get_request() immediately after the GET_STREAM_KEY in self._request_streams conflict check (matching the POST paths), reuse standalone_stream_reader inside standalone_sse_writer(), and ensure await self._clean_up_memory_streams(GET_STREAM_KEY) runs in the finally: block to cover cancellation/errors before standalone_sse_writer() starts.
Verified locally: concurrent GET test fails [200, 200] before the fix and passes [200, 409] after, with full test_streamable_http.py + ruff/pyright clean. Happy to submit a PR if helpful.
AI disclosure: AI assistance was used to help investigate and draft this issue; the reproduction and observed output were verified locally.
Activity