Skip to content

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

Fix kept-alive stdio transports ignoring client timeouts (#6156)

HootRock0a125c1c0b5a
diff --git a/docs/clients/transports.mdx b/docs/clients/transports.mdx
index 1c026c927..32967b3a8 100644
--- a/docs/clients/transports.mdx
+++ b/docs/clients/transports.mdx
@@ -97,6 +97,13 @@ async def efficient_multiple_operations():
await client.call_tool("process_data", {"file": "data.csv"})
```
+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.
+
For complete isolation between connections, disable session persistence:
```python
diff --git a/fastmcp_slim/fastmcp/client/transports/stdio.py b/fastmcp_slim/fastmcp/client/transports/stdio.py
index ef9e669c8..ce1483819 100644
--- a/fastmcp_slim/fastmcp/client/transports/stdio.py
+++ b/fastmcp_slim/fastmcp/client/transports/stdio.py
@@ -68,6 +68,7 @@ class StdioTransport(ClientTransport):
self._session: ClientSession | None = None
self._session_options: TransportOptions | None = None
+ self._session_read_timeout: float | None = None
self._active_sessions = 0
self._connect_lock = anyio.Lock()
self._connect_task: asyncio.Task | None = None
@@ -109,7 +110,13 @@ 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.
- if self._connect_task is not None and self._session_options != options:
+ # 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:
raise RuntimeError(
"This stdio transport has a live session built for different "
@@ -136,6 +143,7 @@ class StdioTransport(ClientTransport):
# comparing against an unset value would read it as a mismatch and
# tear down the connection being established.
self._session_options = options
+ self._session_read_timeout = session_kwargs.get("read_timeout_seconds")
# start the connection task
self._connect_task = asyncio.create_task(
@@ -192,6 +200,7 @@ class StdioTransport(ClientTransport):
self._connect_task = None
self._session = None
self._session_options = None
+ self._session_read_timeout = None
self._stop_event = anyio.Event()
self._ready_event = anyio.Event()
diff --git a/tests/client/test_stdio_session_kwargs.py b/tests/client/test_stdio_session_kwargs.py
new file mode 100644
index 000000000..7fa555393
--- /dev/null
+++ b/tests/client/test_stdio_session_kwargs.py
@@ -0,0 +1,187 @@
+import asyncio
+from pathlib import Path
+
+import pytest
+from mcp.shared.exceptions import MCPError
+
+from fastmcp import Client
+from fastmcp.client.transports import PythonStdioTransport
+from fastmcp.server import create_proxy
+from fastmcp.server.providers.proxy import ProxyClient
+
+pytestmark = pytest.mark.subprocess_heavy
+
+
+def sleepy_server(tmp_path: Path) -> Path:
+ script = tmp_path / "sleepy_server.py"
+ script.write_text(
+ "import asyncio\n"
+ "from fastmcp import FastMCP\n"
+ "server = FastMCP('sleepy')\n"
+ "@server.tool\n"
+ "async def sleep(seconds: float) -> str:\n"
+ " await asyncio.sleep(seconds)\n"
+ " return 'done'\n"
+ "if __name__ == '__main__':\n"
+ " server.run()\n",
+ encoding="utf-8",
+ )
+ return script
+
+
+ "first_timeout, second_timeout",
+ [(1, 3), (3, 1), (None, 1), (1, None), (0, 1), (1, 0)],
+)
+async def test_second_client_uses_its_own_timeout(
+ sleepy_server: Path, first_timeout: float | None, second_timeout: float | None
+) -> None:
+ transport = PythonStdioTransport(sleepy_server)
+ try:
+ async with Client(transport, timeout=first_timeout) as first:
+ await first.list_tools()
+ first_session = first.session
+ async with Client(transport, timeout=second_timeout) as second:
+ if second_timeout == 1:
+ with pytest.raises(MCPError, match="timed out"):
+ await second.call_tool("sleep", {"seconds": 2})
+ else:
+ result = await second.call_tool("sleep", {"seconds": 2})
+ assert result.data == "done"
+ assert second.session is not first_session
+ finally:
+ await transport.close()
+
+
[email protected]("second_timeout", [1, None])
+async def test_different_client_settings_do_not_replace_an_active_session(
+ sleepy_server: Path,
+ second_timeout: float | None,
+) -> 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
+ result = await first.call_tool("sleep", {"seconds": 0})
+ assert result.data == "done"
+ finally:
+ await transport.close()
+
+
+async def test_same_client_reuses_session_and_per_call_timeout_overrides_default(
+ sleepy_server: Path,
+) -> None:
+ transport = PythonStdioTransport(sleepy_server)
+ client = Client(transport, timeout=1)
+ try:
+ async with client:
+ session = client.session
+ async with client:
+ assert client.session is session
+ result = await client.call_tool("sleep", {"seconds": 2}, timeout=3)
+ assert result.data == "done"
+ finally:
+ await transport.close()
+
+
+async def test_matching_timeouts_reuse_active_and_idle_connections() -> None:
+ script = Path(__file__).parent / "minimal_stdio_server.py"
+ transport = PythonStdioTransport(script)
+ try:
+ async with transport.connect_session(read_timeout_seconds=3) as first:
+ await first.initialize()
+ async with transport.connect_session(read_timeout_seconds=3) as second:
+ assert second is first
+ async with transport.connect_session(read_timeout_seconds=3) as third:
+ assert third is first
+ finally:
+ await transport.close()
+
+
+async def test_proxy_client_clones_keep_the_stdio_session(sleepy_server: Path) -> None:
+ transport = PythonStdioTransport(sleepy_server)
+ proxy = ProxyClient(transport)
+ try:
+ async with proxy.new() as first:
+ session = first.session
+ async with proxy.new() as second:
+ assert second.session is session
+ result = await second.call_tool("sleep", {"seconds": 0})
+ assert result.data == "done"
+ async with proxy.new() as third:
+ assert third.session is session
+ result = await third.call_tool("sleep", {"seconds": 0})
+ assert result.data == "done"
+ finally:
+ await transport.close()
+
+
+async def test_separate_clients_with_matching_timeouts_share_the_session(
+ sleepy_server: Path,
+) -> None:
+ transport = PythonStdioTransport(sleepy_server)
+ try:
+ async with Client(transport, timeout=3) as first:
+ async with Client(transport, timeout=3) as second:
+ assert second.session is first.session
+ result = await second.call_tool("sleep", {"seconds": 0})
+ assert result.data == "done"
+ finally:
+ await transport.close()
+
+
+async def test_proxy_requests_preserve_backend_state(tmp_path: Path) -> None:
+ script = tmp_path / "stateful_server.py"
+ script.write_text(
+ "import asyncio\n"
+ "import os\n"
+ "from fastmcp import FastMCP\n"
+ "server = FastMCP('stateful')\n"
+ "counter = 0\n"
+ "@server.tool\n"
+ "async def count() -> dict[str, int]:\n"
+ " global counter\n"
+ " counter += 1\n"
+ " captured = counter\n"
+ " await asyncio.sleep(0.05)\n"
+ " return {'pid': os.getpid(), 'count': captured}\n"
+ "if __name__ == '__main__':\n"
+ " server.run(show_banner=False)\n",
+ encoding="utf-8",
+ )
+ transport = PythonStdioTransport(script)
+ try:
+ async with Client(create_proxy(transport)) as client:
+ first = (await client.call_tool("count")).data
+ second = (await client.call_tool("count")).data
+ assert second["pid"] == first["pid"]
+ assert [first["count"], second["count"]] == [1, 2]
+ results = await asyncio.gather(
+ client.call_tool("count"), client.call_tool("count")
+ )
+ assert sorted(result.data["count"] for result in results) == [3, 4]
+ assert {result.data["pid"] for result in results} == {first["pid"]}
+ finally:
+ await transport.close()
+
+
+async def test_omitted_and_explicit_none_reuse_default_timeout() -> None:
+ transport = PythonStdioTransport(Path(__file__).parent / "minimal_stdio_server.py")
+ try:
+ async with transport.connect_session() as first:
+ await first.initialize()
+ async with transport.connect_session(read_timeout_seconds=None) as second:
+ assert second is first
+ finally:
+ await transport.close()