File tree Expand file tree Collapse file tree 3 files changed +16
-5
lines changed Expand file tree Collapse file tree 3 files changed +16
-5
lines changed Original file line number Diff line number Diff line change 1010from httpx_sse import aconnect_sse
1111
1212import mcp .types as types
13- from mcp .shared ._httpx_utils import create_mcp_http_client
13+ from mcp .shared ._httpx_utils import McpHttpClientFactory , create_mcp_http_client
1414from mcp .shared .message import SessionMessage
1515
1616logger = logging .getLogger (__name__ )
@@ -26,6 +26,7 @@ async def sse_client(
2626 headers : dict [str , Any ] | None = None ,
2727 timeout : float = 5 ,
2828 sse_read_timeout : float = 60 * 5 ,
29+ httpx_client_factory : McpHttpClientFactory = create_mcp_http_client ,
2930 auth : httpx .Auth | None = None ,
3031):
3132 """
@@ -53,7 +54,7 @@ async def sse_client(
5354 async with anyio .create_task_group () as tg :
5455 try :
5556 logger .info (f"Connecting to SSE endpoint: { remove_request_params (url )} " )
56- async with create_mcp_http_client (headers = headers , auth = auth ) as client :
57+ async with httpx_client_factory (headers = headers , auth = auth ) as client :
5758 async with aconnect_sse (
5859 client ,
5960 "GET" ,
Original file line number Diff line number Diff line change 1919from anyio .streams .memory import MemoryObjectReceiveStream , MemoryObjectSendStream
2020from httpx_sse import EventSource , ServerSentEvent , aconnect_sse
2121
22- from mcp .shared ._httpx_utils import create_mcp_http_client
22+ from mcp .shared ._httpx_utils import McpHttpClientFactory , create_mcp_http_client
2323from mcp .shared .message import ClientMessageMetadata , SessionMessage
2424from mcp .types import (
2525 ErrorData ,
@@ -430,6 +430,7 @@ async def streamablehttp_client(
430430 timeout : timedelta = timedelta (seconds = 30 ),
431431 sse_read_timeout : timedelta = timedelta (seconds = 60 * 5 ),
432432 terminate_on_close : bool = True ,
433+ httpx_client_factory : McpHttpClientFactory = create_mcp_http_client ,
433434 auth : httpx .Auth | None = None ,
434435) -> AsyncGenerator [
435436 tuple [
@@ -464,7 +465,7 @@ async def streamablehttp_client(
464465 try :
465466 logger .info (f"Connecting to StreamableHTTP endpoint: { url } " )
466467
467- async with create_mcp_http_client (
468+ async with httpx_client_factory (
468469 headers = transport .request_headers ,
469470 timeout = httpx .Timeout (
470471 transport .timeout .seconds , read = transport .sse_read_timeout .seconds
Original file line number Diff line number Diff line change 11"""Utilities for creating standardized httpx AsyncClient instances."""
22
3- from typing import Any
3+ from typing import Any , Protocol
44
55import httpx
66
77__all__ = ["create_mcp_http_client" ]
88
99
10+ class McpHttpClientFactory (Protocol ):
11+ def __call__ (
12+ self ,
13+ headers : dict [str , str ] | None = None ,
14+ timeout : httpx .Timeout | None = None ,
15+ auth : httpx .Auth | None = None ,
16+ ) -> httpx .AsyncClient : ...
17+
18+
1019def create_mcp_http_client (
1120 headers : dict [str , str ] | None = None ,
1221 timeout : httpx .Timeout | None = None ,
You can’t perform that action at this time.
0 commit comments