Skip to content

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

Let clients with different timeouts share a kept-alive stdio session (#6223)

nate nowack868ae9f0eb5e
diff --git a/docs/clients/transports.mdx b/docs/clients/transports.mdx
index 32967b3a8..33772d525 100644
--- a/docs/clients/transports.mdx
+++ b/docs/clients/transports.mdx
@@ -99,10 +99,10 @@ async def efficient_multiple_operations():
A kept-alive session is reused when the transport options and request timeout
match. Connecting with a different `Client(timeout=...)` restarts an idle
-subprocess; if another connection still uses that session, the transport raises
-an error instead. Use separate transports when clients with different timeouts
-must stay connected concurrently. Per-call `timeout` overrides do not restart
-the session.
+subprocess. If another client is still connected, the new client shares its
+session and that session's timeout, and FastMCP logs a warning; use separate
+transports when concurrent clients need different defaults. Per-call `timeout`
+overrides do not restart the session.
For complete isolation between connections, disable session persistence:
diff --git a/fastmcp_slim/fastmcp/client/transports/stdio.py b/fastmcp_slim/fastmcp/client/transports/stdio.py
index ce1483819..2550ec47b 100644
--- a/fastmcp_slim/fastmcp/client/transports/stdio.py
+++ b/fastmcp_slim/fastmcp/client/transports/stdio.py
@@ -110,21 +110,32 @@ class StdioTransport(ClientTransport):
# to a client that wants different ones would silently give it the
# first client's behavior. Rebuild it when it's idle; refuse when
# another client is using it, since tearing it down would break them.
- # Compare the timeout value, not callback identity: proxy clients
- # rebind forwarding callbacks for each request on the same transport.
- if self._connect_task is not None and (
- self._session_options != options
- or self._session_read_timeout
- != session_kwargs.get("read_timeout_seconds")
- ):
- if self._active_sessions:
+ # A different request timeout alone does not refuse: the client
+ # shares the live session and its default timeout, as before the
+ # timeout was compared. Compare the timeout value, not callback
+ # identity: proxy clients rebind forwarding callbacks per request.
+ options_differ = self._session_options != options
+ timeout_differs = self._session_read_timeout != session_kwargs.get(
+ "read_timeout_seconds"
+ )
+ if self._connect_task is not None and (options_differ or timeout_differs):
+ if self._active_sessions and options_differ:
raise RuntimeError(
"This stdio transport has a live session built for different "
"connection options and another client is still using it. "
"Sharing one transport across clients that need different "
"sessions is not supported; give each client its own transport."
)
- await self.disconnect()
+ if not self._active_sessions:
+ await self.disconnect()
+ else:
+ logger.warning(
+ "Sharing a live stdio session whose request timeout is "
+ f"{self._session_read_timeout}s; this client's timeout "
+ f"({session_kwargs.get('read_timeout_seconds')}s) applies "
+ "only to calls that pass their own timeout. Give each "
+ "client its own transport to keep separate defaults."
+ )
# If the connect task completed or the session's streams are dead,
# the subprocess has exited. Tear down so we can start fresh.
diff --git a/tests/client/test_stdio_session_kwargs.py b/tests/client/test_stdio_session_kwargs.py
index 7fa555393..6966bb300 100644
--- a/tests/client/test_stdio_session_kwargs.py
+++ b/tests/client/test_stdio_session_kwargs.py
@@ -56,19 +56,25 @@ async def test_second_client_uses_its_own_timeout(
@pytest.mark.timeout(20)
[email protected]("second_timeout", [1, None])
-async def test_different_client_settings_do_not_replace_an_active_session(
[email protected]("first_timeout, second_timeout", [(3, 1), (None, 5)])
+async def test_nested_client_with_another_timeout_shares_the_active_session(
sleepy_server: Path,
- second_timeout: float | None,
+ first_timeout: float | None,
+ second_timeout: float,
+ caplog: pytest.LogCaptureFixture,
) -> None:
transport = PythonStdioTransport(sleepy_server)
try:
- async with Client(transport, timeout=3) as first:
- with pytest.raises(RuntimeError, match="still using it"):
- async with Client(transport, timeout=second_timeout):
- pass
+ async with Client(transport, timeout=first_timeout) as first:
+ async with Client(transport, timeout=second_timeout) as second:
+ assert second.session is first.session
+ result = await second.call_tool("sleep", {"seconds": 0})
+ assert result.data == "done"
+ with pytest.raises(MCPError, match="timed out"):
+ await second.call_tool("sleep", {"seconds": 2}, timeout=1)
result = await first.call_tool("sleep", {"seconds": 0})
assert result.data == "done"
+ assert "Sharing a live stdio session" in caplog.text
finally:
await transport.close()