Skip to content

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

Reset session after closing

Jeremiah Lowin975f151a9bfc
diff --git a/src/fastmcp/client/client.py b/src/fastmcp/client/client.py
index 1fee12ff7..03fb7fd5a 100644
--- a/src/fastmcp/client/client.py
+++ b/src/fastmcp/client/client.py
@@ -268,6 +268,8 @@ class Client(Generic[ClientTransportT]):
async def close(self):
await self.transport.close()
+ self._session = None
+ self._initialize_result = None
# --- MCP Client Methods ---
diff --git a/tests/client/test_stdio.py b/tests/client/test_stdio.py
index 55d430f7c..71bbefe4a 100644
--- a/tests/client/test_stdio.py
+++ b/tests/client/test_stdio.py
@@ -121,9 +121,7 @@ class TestKeepAlive:
client = Client(transport=PythonStdioTransport(script_path=stdio_script))
assert client.transport.keep_alive is True
- with pytest.raises(
- RuntimeError, match="Server session was closed unexpectedly"
- ):
- async with client:
- await client.close()
+ async with client:
+ await client.close()
+ with pytest.raises(RuntimeError, match="Client is not connected"):
await client.call_tool("pid")