SSE: ConnectionClosed exception after session disconnect
Initial Checks
- I confirm that I'm using the latest version of MCP Python SDK
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Description
In our environment, the SSE connections sometimes get dropped:
DEBUG:sse_starlette.sse:Got event: http.disconnect. Stop streaming.
DEBUG:root:Client session disconnected 081769ba-c683-471b-af22-1333409ab669
When this happens, on the next request for that session, we get a ClosedResourceError exception:
INFO: 100.100.0.154:49316 - "POST /messages/?session_id=081769bac683471baf221333409ab669 HTTP/1.1" 202 Accepted
ERROR:__main__:SSE post message error:
Traceback (most recent call last):
File "/app/mcp_renewables/server.py", line 714, in handle_post_message_with_error_handling
return await sse.handle_post_message(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.12/site-packages/mcp/server/sse.py", line 249, in handle_post_message
await writer. Send(session_message)
File "/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 242, in send
self.send_nowait(item)
File "/app/.venv/lib/python3.12/site-packages/anyio/streams/memory.py", line 211, in send_nowait
raise ClosedResourceError
anyio.ClosedResourceError
I believe the reason is that when the session disconnects, its file handles are not properly removed as explained in #703. I have forked the repo and applied the fix and it seems to solve the problem.
Example Code: the fix I applied
diff --git a/src/mcp/server/sse.py b/src/mcp/server/sse.py index b7ff332..29acecb 100644 --- a/src/mcp/server/sse.py +++ b/src/mcp/server/sse.py @@ -179,7 +179,7 @@ class SseServerTransport: async with anyio.create_task_group() as tg: - async def response_wrapper(scope: Scope, receive: Receive, send: Send): + async def response_wrapper(scope: Scope, receive: Receive, send: Send, transport: SseServerTransport): """ The EventSourceResponse returning signals a client close / disconnect. In this case we close our side of the streams to signal the client that @@ -190,10 +190,13 @@ class SseServerTransport: ) await read_stream_writer.aclose() await write_stream_reader.aclose() + await read_stream.aclose() + await write_stream.aclose() + transport._read_stream_writers.pop(session_id) logging.debug(f"Client session disconnected {session_id}") logger.debug("Starting SSE response task") - tg.start_soon(response_wrapper, scope, receive, send) + tg.start_soon(response_wrapper, scope, receive, send, self) logger.debug("Yielding read and write streams") yield (read_stream, write_stream)
Python & MCP Python SDK
References
Related PRs: