File tree 3 files changed +16
-5
lines changed
3 files changed +16
-5
lines changed Original file line number Diff line number Diff line change 10
10
from httpx_sse import aconnect_sse
11
11
12
12
import 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
14
14
from mcp .shared .message import SessionMessage
15
15
16
16
logger = logging .getLogger (__name__ )
@@ -26,6 +26,7 @@ async def sse_client(
26
26
headers : dict [str , Any ] | None = None ,
27
27
timeout : float = 5 ,
28
28
sse_read_timeout : float = 60 * 5 ,
29
+ httpx_client_factory : McpHttpClientFactory = create_mcp_http_client ,
29
30
auth : httpx .Auth | None = None ,
30
31
):
31
32
"""
@@ -53,7 +54,7 @@ async def sse_client(
53
54
async with anyio .create_task_group () as tg :
54
55
try :
55
56
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 :
57
58
async with aconnect_sse (
58
59
client ,
59
60
"GET" ,
Original file line number Diff line number Diff line change 19
19
from anyio .streams .memory import MemoryObjectReceiveStream , MemoryObjectSendStream
20
20
from httpx_sse import EventSource , ServerSentEvent , aconnect_sse
21
21
22
- from mcp .shared ._httpx_utils import create_mcp_http_client
22
+ from mcp .shared ._httpx_utils import McpHttpClientFactory , create_mcp_http_client
23
23
from mcp .shared .message import ClientMessageMetadata , SessionMessage
24
24
from mcp .types import (
25
25
ErrorData ,
@@ -430,6 +430,7 @@ async def streamablehttp_client(
430
430
timeout : timedelta = timedelta (seconds = 30 ),
431
431
sse_read_timeout : timedelta = timedelta (seconds = 60 * 5 ),
432
432
terminate_on_close : bool = True ,
433
+ httpx_client_factory : McpHttpClientFactory = create_mcp_http_client ,
433
434
auth : httpx .Auth | None = None ,
434
435
) -> AsyncGenerator [
435
436
tuple [
@@ -464,7 +465,7 @@ async def streamablehttp_client(
464
465
try :
465
466
logger .info (f"Connecting to StreamableHTTP endpoint: { url } " )
466
467
467
- async with create_mcp_http_client (
468
+ async with httpx_client_factory (
468
469
headers = transport .request_headers ,
469
470
timeout = httpx .Timeout (
470
471
transport .timeout .seconds , read = transport .sse_read_timeout .seconds
Original file line number Diff line number Diff line change 1
1
"""Utilities for creating standardized httpx AsyncClient instances."""
2
2
3
- from typing import Any
3
+ from typing import Any , Protocol
4
4
5
5
import httpx
6
6
7
7
__all__ = ["create_mcp_http_client" ]
8
8
9
9
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
+
10
19
def create_mcp_http_client (
11
20
headers : dict [str , str ] | None = None ,
12
21
timeout : httpx .Timeout | None = None ,
You can’t perform that action at this time.
0 commit comments