Skip to content

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

Document that a custom httpx_client_factory owns its timeout (#5923)

Asem ahmedc9f64de499fa
diff --git a/docs/clients/transports.mdx b/docs/clients/transports.mdx
index 8122397e7..f30d0bfc6 100644
--- a/docs/clients/transports.mdx
+++ b/docs/clients/transports.mdx
@@ -194,6 +194,30 @@ transport = StreamableHttpTransport(
client = Client(transport)
```
+### Custom HTTP Client
+
+For anything beyond SSL settings β€” proxies, mTLS, event hooks β€” pass an `httpx_client_factory`. The factory receives `headers`, `auth`, and `follow_redirects`, plus `timeout` when the client has one configured (for example `Client(transport, timeout=60)`). Accept `**kwargs` so the factory stays forward-compatible.
+
+When no client timeout is configured, the factory **owns the timeout**: none is passed, and an `httpx2.AsyncClient` built without one uses httpx's 5 second default, which is too short for many MCP tool calls. Set a default with `setdefault`, so an explicit client timeout still wins:
+
+```python
+import httpx2
+from fastmcp import Client
+from fastmcp.client.transports import StreamableHttpTransport
+
+def httpx_client_factory(**kwargs):
+ kwargs.setdefault("timeout", httpx2.Timeout(30.0, read=300.0))
+ return httpx2.AsyncClient(verify=False, **kwargs)
+
+transport = StreamableHttpTransport(
+ url="https://api.example.com/mcp",
+ httpx_client_factory=httpx_client_factory,
+)
+client = Client(transport)
+```
+
+These 30s connect / 300s read values match the defaults FastMCP applies when no factory is used.
+
### SSE Transport
Server-Sent Events transport is maintained for backward compatibility. Use Streamable HTTP for new deployments unless you have specific infrastructure requirements.
diff --git a/fastmcp_slim/fastmcp/client/transports/http.py b/fastmcp_slim/fastmcp/client/transports/http.py
index e4e008529..10a698339 100644
--- a/fastmcp_slim/fastmcp/client/transports/http.py
+++ b/fastmcp_slim/fastmcp/client/transports/http.py
@@ -51,7 +51,12 @@ class StreamableHttpTransport(ClientTransport):
httpx_client_factory: Optional factory for creating httpx2.AsyncClient.
If provided, must accept keyword arguments: headers, auth,
follow_redirects, and optionally timeout. Using **kwargs is
- recommended to ensure forward compatibility.
+ recommended to ensure forward compatibility. `timeout` is passed
+ only when the client has one (e.g. `Client(transport, timeout=30)`);
+ otherwise the factory owns the timeout. An `httpx2.AsyncClient`
+ built without one uses httpx's 5 second default, so set a default
+ in the factory, e.g.
+ `kwargs.setdefault("timeout", httpx2.Timeout(30.0, read=300.0))`.
verify: SSL certificate verification. Accepts False to disable
verification, a path to a CA bundle, or an ssl.SSLContext
for full control. None (default) uses httpx defaults (verification
diff --git a/tests/client/test_streamable_http.py b/tests/client/test_streamable_http.py
index 93ac4526d..8a675a6bb 100644
--- a/tests/client/test_streamable_http.py
+++ b/tests/client/test_streamable_http.py
@@ -2,10 +2,13 @@ import asyncio
import json
import sys
from contextlib import suppress
+from typing import Any, cast
from unittest.mock import AsyncMock, call
+import httpx2
import pytest
from mcp import MCPError
+from mcp.shared._httpx_utils import McpHttpClientFactory
from mcp_types import TextResourceContents
from fastmcp import Context
@@ -276,3 +279,34 @@ class TestTimeout:
async with streamable_http_server.client(timeout=2) as client:
with pytest.raises(MCPError):
await client.call_tool("sleep", {"seconds": 0.2}, timeout=0.1)
+
+
[email protected]("mode", ["auto", "legacy"])
[email protected]("client_timeout", [None, 0.25])
+async def test_factory_owned_timeout_is_preserved(
+ streamable_http_server: ASGIServer, mode: str, client_timeout: float | None
+):
+ """A factory's own timeout stands when the client sets none, and an
+ explicit client timeout is still forwarded to the factory."""
+ created: list[httpx2.AsyncClient] = []
+
+ def factory(**kwargs: Any) -> httpx2.AsyncClient:
+ kwargs.setdefault("timeout", httpx2.Timeout(0.15))
+ http_client = streamable_http_server.http_client(**kwargs)
+ created.append(http_client)
+ return http_client
+
+ transport = StreamableHttpTransport(
+ streamable_http_server.url,
+ httpx_client_factory=cast(McpHttpClientFactory, factory),
+ )
+ async with Client(transport, timeout=client_timeout, mode=mode):
+ pass
+
+ expected = (
+ httpx2.Timeout(0.15)
+ if client_timeout is None
+ else httpx2.Timeout(30.0, read=client_timeout)
+ )
+ assert created
+ assert all(client.timeout == expected for client in created)