π The fast, Pythonic way to build MCP servers and clients.
fix: preserve exception propagation through transport cleanup (#2591)
Jeremiah Lowin95e58e87aa10
diff --git a/src/fastmcp/client/client.py b/src/fastmcp/client/client.pyindex 4651edadb..c498039e9 100644--- a/src/fastmcp/client/client.py+++ b/src/fastmcp/client/client.py@@ -16,7 +16,7 @@ import httpximport mcp.typesimport pydantic_corefrom exceptiongroup import catch-from mcp import ClientSession+from mcp import ClientSession, McpErrorfrom mcp.types import (CancelTaskRequest,CancelTaskRequestParams,@@ -514,7 +514,8 @@ class Client(Generic[ClientTransportT]):raise RuntimeError("Session task completed without exception but connection failed")- if isinstance(exception, httpx.HTTPStatusError):+ # Preserve specific exception types that clients may want to handle+ if isinstance(exception, httpx.HTTPStatusError | McpError):raise exceptionraise RuntimeError(f"Client failed to connect: {exception}"diff --git a/src/fastmcp/client/transports.py b/src/fastmcp/client/transports.pyindex a6336d123..eff3e2ba9 100644--- a/src/fastmcp/client/transports.py+++ b/src/fastmcp/client/transports.py@@ -866,7 +866,11 @@ class FastMCPTransport(ClientTransport):client_read, client_write = client_streamsserver_read, server_write = server_streams- # Create a cancel scope for the server task+ # Capture exceptions to re-raise after task group cleanup.+ # anyio task groups can suppress exceptions when cancel_scope.cancel()+ # is called during cleanup, so we capture and re-raise manually.+ exception_to_raise: BaseException | None = None+async with (anyio.create_task_group() as tg,_enter_server_lifespan(server=self.server),@@ -892,9 +896,15 @@ class FastMCPTransport(ClientTransport):**session_kwargs,) as client_session:yield client_session+ except BaseException as e:+ exception_to_raise = efinally:tg.cancel_scope.cancel()+ # Re-raise after task group has exited cleanly+ if exception_to_raise is not None:+ raise exception_to_raise+def __repr__(self) -> str:return f"<FastMCPTransport(server='{self.server.name}')>"