Skip to content

Commit 10cf0f7

Browse files
authored
Support custom httpx client creation (#752)
1 parent 7c8ad51 commit 10cf0f7

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/mcp/client/sse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from httpx_sse import aconnect_sse
1111

1212
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
1414
from mcp.shared.message import SessionMessage
1515

1616
logger = 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",

src/mcp/client/streamable_http.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
2020
from 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
2323
from mcp.shared.message import ClientMessageMetadata, SessionMessage
2424
from 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

src/mcp/shared/_httpx_utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
"""Utilities for creating standardized httpx AsyncClient instances."""
22

3-
from typing import Any
3+
from typing import Any, Protocol
44

55
import 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+
1019
def create_mcp_http_client(
1120
headers: dict[str, str] | None = None,
1221
timeout: httpx.Timeout | None = None,

0 commit comments

Comments
 (0)