π The fast, Pythonic way to build MCP servers and clients.
Add client factory
Jeremiah Lowinf779624a7451
diff --git a/src/fastmcp/client/transports.py b/src/fastmcp/client/transports.pyindex 134dffa24..42b524485 100644--- a/src/fastmcp/client/transports.py+++ b/src/fastmcp/client/transports.py@@ -6,9 +6,17 @@ import osimport shutilimport sysimport warnings-from collections.abc import AsyncIterator+from collections.abc import AsyncIterator, Callablefrom pathlib import Path-from typing import TYPE_CHECKING, Any, Literal, TypedDict, TypeVar, cast, overload+from typing import (+ TYPE_CHECKING,+ Any,+ Literal,+ TypedDict,+ TypeVar,+ cast,+ overload,+)import httpxfrom mcp import ClientSession, StdioServerParameters@@ -154,6 +162,7 @@ class SSETransport(ClientTransport):headers: dict[str, str] | None = None,auth: httpx.Auth | Literal["oauth"] | str | None = None,sse_read_timeout: datetime.timedelta | float | int | None = None,+ httpx_client_factory: Callable[[], httpx.AsyncClient] | None = None,):if isinstance(url, AnyUrl):url = str(url)@@ -162,6 +171,7 @@ class SSETransport(ClientTransport):self.url = urlself.headers = headers or {}self._set_auth(auth)+ self.httpx_client_factory = httpx_client_factoryif isinstance(sse_read_timeout, int | float):sse_read_timeout = datetime.timedelta(seconds=sse_read_timeout)@@ -196,6 +206,9 @@ class SSETransport(ClientTransport):)client_kwargs["timeout"] = read_timeout_seconds.total_seconds()+ if self.httpx_client_factory is not None:+ client_kwargs["httpx_client_factory"] = self.httpx_client_factory+async with sse_client(self.url, auth=self.auth, **client_kwargs) as transport:read_stream, write_stream = transportasync with ClientSession(@@ -216,6 +229,7 @@ class StreamableHttpTransport(ClientTransport):headers: dict[str, str] | None = None,auth: httpx.Auth | Literal["oauth"] | str | None = None,sse_read_timeout: datetime.timedelta | float | int | None = None,+ httpx_client_factory: Callable[[], httpx.AsyncClient] | None = None,):if isinstance(url, AnyUrl):url = str(url)@@ -224,6 +238,7 @@ class StreamableHttpTransport(ClientTransport):self.url = urlself.headers = headers or {}self._set_auth(auth)+ self.httpx_client_factory = httpx_client_factoryif isinstance(sse_read_timeout, int | float):sse_read_timeout = datetime.timedelta(seconds=sse_read_timeout)@@ -255,8 +270,13 @@ class StreamableHttpTransport(ClientTransport):if session_kwargs.get("read_timeout_seconds", None) is not None:client_kwargs["timeout"] = session_kwargs.get("read_timeout_seconds")+ if self.httpx_client_factory is not None:+ client_kwargs["httpx_client_factory"] = self.httpx_client_factory+async with streamablehttp_client(- self.url, auth=self.auth, **client_kwargs+ self.url,+ auth=self.auth,+ **client_kwargs,) as transport:read_stream, write_stream, _ = transportasync with ClientSession(