diff --git a/src/mcp/server/streamable_http.py b/src/mcp/server/streamable_http.py index 416dd9e2b4..ca518bcb6a 100644 --- a/src/mcp/server/streamable_http.py +++ b/src/mcp/server/streamable_http.py @@ -775,16 +775,13 @@ async def _handle_get_request(self, request: Request, send: Send) -> None: # Create SSE stream sse_stream_writer, sse_stream_reader = anyio.create_memory_object_stream[SSEEvent](0) + self._request_streams[GET_STREAM_KEY] = anyio.create_memory_object_stream[EventMessage]( + REQUEST_STREAM_BUFFER_SIZE + ) + standalone_stream_reader = self._request_streams[GET_STREAM_KEY][1] async def standalone_sse_writer(): try: - # Create a standalone message stream for server-initiated messages - - self._request_streams[GET_STREAM_KEY] = anyio.create_memory_object_stream[EventMessage]( - REQUEST_STREAM_BUFFER_SIZE - ) - standalone_stream_reader = self._request_streams[GET_STREAM_KEY][1] - async with sse_stream_writer, standalone_stream_reader: # Process messages from the standalone stream async for event_message in standalone_stream_reader: @@ -803,7 +800,6 @@ async def standalone_sse_writer(): logger.exception("Error in standalone SSE writer") # pragma: no cover finally: logger.debug("Closing standalone SSE writer") - await self._clean_up_memory_streams(GET_STREAM_KEY) # Create and start EventSourceResponse response = EventSourceResponse( @@ -817,8 +813,8 @@ async def standalone_sse_writer(): await response(request.scope, request.receive, send) except Exception: # pragma: lax no cover logger.exception("Error in standalone SSE response") - await self._clean_up_memory_streams(GET_STREAM_KEY) finally: + await self._clean_up_memory_streams(GET_STREAM_KEY) await sse_stream_writer.aclose() await sse_stream_reader.aclose() diff --git a/tests/shared/test_streamable_http.py b/tests/shared/test_streamable_http.py index 655d9941dc..a992c4f60d 100644 --- a/tests/shared/test_streamable_http.py +++ b/tests/shared/test_streamable_http.py @@ -902,6 +902,46 @@ async def test_get_sse_stream(basic_app: Starlette) -> None: assert second_get.status_code == 409 +@pytest.mark.anyio +async def test_concurrent_get_sse_streams_reject_duplicate(basic_app: Starlette) -> None: + """Concurrent GET requests for the same session establish one SSE stream and reject the other with 409.""" + async with make_client(basic_app) as client: + init_response = await client.post( + "/mcp", + headers={ + "Accept": "application/json, text/event-stream", + "Content-Type": "application/json", + }, + json=INIT_REQUEST, + ) + assert init_response.status_code == 200 + + session_id = init_response.headers.get(MCP_SESSION_ID_HEADER) + assert session_id is not None + negotiated_version = extract_protocol_version_from_sse(init_response) + + get_headers = { + "Accept": "text/event-stream", + MCP_SESSION_ID_HEADER: session_id, + MCP_PROTOCOL_VERSION_HEADER: negotiated_version, + } + statuses: list[int] = [] + both_responded = anyio.Event() + + async def open_stream() -> None: + async with client.stream("GET", "/mcp", headers=get_headers) as response: + statuses.append(response.status_code) + if len(statuses) == 2: + both_responded.set() + await both_responded.wait() + + async with anyio.create_task_group() as tg: + tg.start_soon(open_stream) + tg.start_soon(open_stream) + + assert sorted(statuses) == [200, 409] + + @pytest.mark.anyio async def test_get_validation(basic_app: Starlette) -> None: """A GET without an Accept header covering text/event-stream is rejected with 406.""" @@ -2345,8 +2385,10 @@ async def asgi_receive() -> Message: tg.start_soon(transport.handle_request, scope, asgi_receive, asgi_send) await stream_registered.wait() standalone_send = transport._request_streams[GET_STREAM_KEY][0] # pyright: ignore[reportPrivateUsage] - # Zero-buffer rendezvous: once send() returns, the writer has dequeued the event - # and is blocked forwarding it past the closed gate — the between-dequeues window. + while not standalone_send.statistics().tasks_waiting_receive: + await anyio.sleep(0) + # Once the writer is parked in receive(), send() hands the event directly to it + # and yields while it forwards the event past the closed gate — the between-dequeues window. await standalone_send.send(EventMessage(notification)) await transport._clean_up_memory_streams(GET_STREAM_KEY) # pyright: ignore[reportPrivateUsage] # Unblock the response; the writer's next dequeue hits its closed stream.