From 975f151a9bfc5e701219c991d4fe907c6a9bb770 Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Wed, 28 May 2025 22:02:03 -0400 Subject: [PATCH] Reset session after closing --- src/fastmcp/client/client.py | 2 ++ tests/client/test_stdio.py | 8 +++----- 2 files changed, 5 insertions(+), 5 deletions(-) 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")