The vulnerability analysis identified a critical flaw in the MCP Python SDK's server-side session management. The root cause of the Denial of Service is an unhandled anyio.ClosedResourceError exception within the mcp.shared.session.Session._receive_loop function. This function is responsible for processing incoming messages over a streamable HTTP connection.
When a client establishes a connection and then disconnects abruptly (e.g., by crashing or closing the connection without proper termination), the underlying anyio stream raises a ClosedResourceError. The vulnerable version of the _receive_loop function did not have an exception handler for this specific error. As a result, the exception would go uncaught, leading to the termination of the server's execution task and effectively crashing the server process.
The provided patch directly addresses this issue by wrapping the message-reading loop (async for message in self._read_stream:) inside a try...except block. The newly added except anyio.ClosedResourceError: block catches the specific exception, logs a debug message, and allows the server to continue running without crashing. This ensures that a misbehaving or crashing client cannot bring down the entire server, thus mitigating the Denial of Service vulnerability.