File tree Expand file tree Collapse file tree 3 files changed +17
-6
lines changed
Expand file tree Collapse file tree 3 files changed +17
-6
lines changed Original file line number Diff line number Diff line change 11import logging
22from contextlib import asynccontextmanager
3- from typing import Any
3+ from typing import Any , Callable
44from urllib .parse import urljoin , urlparse
55
66import anyio
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):
3031 """
3132 Client transport for SSE.
@@ -45,7 +46,7 @@ async def sse_client(
4546 async with anyio .create_task_group () as tg :
4647 try :
4748 logger .info (f"Connecting to SSE endpoint: { remove_request_params (url )} " )
48- async with create_mcp_http_client (headers = headers ) as client :
49+ async with httpx_client_factory (headers = headers ) as client :
4950 async with aconnect_sse (
5051 client ,
5152 "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 ,
@@ -427,6 +427,7 @@ async def streamablehttp_client(
427427 timeout : timedelta = timedelta (seconds = 30 ),
428428 sse_read_timeout : timedelta = timedelta (seconds = 60 * 5 ),
429429 terminate_on_close : bool = True ,
430+ httpx_client_factory : McpHttpClientFactory = create_mcp_http_client ,
430431) -> AsyncGenerator [
431432 tuple [
432433 MemoryObjectReceiveStream [SessionMessage | Exception ],
@@ -460,7 +461,7 @@ async def streamablehttp_client(
460461 try :
461462 logger .info (f"Connecting to StreamableHTTP endpoint: { url } " )
462463
463- async with create_mcp_http_client (
464+ async with httpx_client_factory (
464465 headers = transport .request_headers ,
465466 timeout = httpx .Timeout (
466467 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+ ) -> httpx .AsyncClient :
16+ ...
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