StreamableHTTPServerTransport: TOCTOU race between GET 409 conflict check and standalone SSE slot registration allows concurrent GET streams
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 64/100
Piste de recherche
Start in src/mcp/server/streamable_http.py: compare the synchronous registration in the POST paths with _handle_get_request (~line 768) and standalone_sse_writer (~line 783), where the GET slot is added after the response starts. Reproduce with the concurrent anyio.create_task_group() GET test in tests/shared/test_streamable_http.py, expecting [200, 409]. Done means the GET slot is registered synchronously after the conflict check, cleanup runs in finally, and the streamable HTTP tests plus ruff/pyright pass.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Describe the bug
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), whichsse-starlette'sEventSourceResponseschedules viatask_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 eitherstandalone_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 itsfinallycleanup 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():
statuses: dict[str, int] = {}
async def open_stream(key: str) -> None:
async with client.stream("GET", "/mcp", headers=get_headers) as response:
statuses[key] = response.status_code
async with anyio.create_task_group() as tg:
tg.start_soon(open_stream, "first")
tg.start_soon(open_stream, "second")
assert sorted(statuses.values()) == [200, 409]
Actual result on main@77001c96:
AssertionError: assert [200, 200] == [200, 409]
Suggested Fix
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.
Environment
modelcontextprotocol/python-sdk:main@77001c96(v2)- OS: macOS (Darwin arm64)
- Python: 3.12+
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.
- Langage dominant
- Python
- Étoiles
- 25k
- Forks
- 4.2k
- Merge moyen
- 10 min
- PR mergées (30 j)
- 1
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de modelcontextprotocol/python-sdk
-
v2
Difficulté 1/5 Moins d'une heure Accessibilité débutants 86/100
modelcontextprotocol/python-sdk#3978 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Client serves cached tools after its context exits with an exceptionPeut-être pris @jonxsong l’a pris il y a 1 jour. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
modelcontextprotocol/python-sdk#3968 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
modelcontextprotocol/python-sdk#3957 ·
Les mainteneurs répondent en général sous 1 jour
-
v2
Difficulté 1/5 Moins d'une heure Accessibilité débutants 91/100
modelcontextprotocol/python-sdk#3934 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
bug v1
Difficulté 2/5 1-3 heures Accessibilité débutants 79/100
modelcontextprotocol/python-sdk#3929 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de modelcontextprotocol/python-sdk
Issues similaires
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwisePeut-être pris @abdulwaarith0 l’a pris aujourd’hui. Ouverteneeds-triage
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
MakazhanAlpamys/Soup#3690 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
bug data-types native-format
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
ClickHouse/clickhouse-connect#1320 ·
Les mainteneurs répondent en général sous 1 jour
-
Public benchmark import end-to-end test fails when the test stack's worker runs the parity jobOuverteaccepted
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
future-agi/future-agi#6392 ·
Les mainteneurs répondent en général sous 1 jour