Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Add timeout to client cleanup to prevent hangs

Chris Guidry6faef9d96f50
diff --git a/src/fastmcp/client/client.py b/src/fastmcp/client/client.py
index 90bbd32d9..c48b91e5d 100644
--- a/src/fastmcp/client/client.py
+++ b/src/fastmcp/client/client.py
@@ -374,7 +374,11 @@ class Client(Generic[ClientTransportT]):
return await self._connect()
async def __aexit__(self, exc_type, exc_val, exc_tb):
- await self._disconnect()
+ # Use a timeout to prevent hanging during cleanup if the connection is in a bad
+ # state (e.g., rate-limited). The MCP SDK's transport may try to terminate the
+ # session which can hang if the server is unresponsive.
+ with anyio.move_on_after(5):
+ await self._disconnect()
async def _connect(self):
"""