Skip to content

Commit 9478c6e

Browse files
Support for proxy in SSE and Streamable client.
1 parent eaf7cf4 commit 9478c6e

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/mcp/client/sse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from contextlib import asynccontextmanager
3-
from typing import Any
3+
from typing import Any, Optional
44
from urllib.parse import urljoin, urlparse
55

66
import anyio
@@ -28,6 +28,7 @@ async def sse_client(
2828
sse_read_timeout: float = 60 * 5,
2929
httpx_client_factory: McpHttpClientFactory = create_mcp_http_client,
3030
auth: httpx.Auth | None = None,
31+
proxy: Optional[str] = None
3132
):
3233
"""
3334
Client transport for SSE.
@@ -55,7 +56,7 @@ async def sse_client(
5556
try:
5657
logger.debug(f"Connecting to SSE endpoint: {remove_request_params(url)}")
5758
async with httpx_client_factory(
58-
headers=headers, auth=auth, timeout=httpx.Timeout(timeout, read=sse_read_timeout)
59+
headers=headers, auth=auth, timeout=httpx.Timeout(timeout, read=sse_read_timeout), proxy=proxy
5960
) as client:
6061
async with aconnect_sse(
6162
client,

src/mcp/client/streamable_http.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from contextlib import asynccontextmanager
1212
from dataclasses import dataclass
1313
from datetime import timedelta
14+
from typing import Optional
1415

1516
import anyio
1617
import httpx
@@ -448,6 +449,7 @@ async def streamablehttp_client(
448449
terminate_on_close: bool = True,
449450
httpx_client_factory: McpHttpClientFactory = create_mcp_http_client,
450451
auth: httpx.Auth | None = None,
452+
proxy: Optional[str] = None
451453
) -> AsyncGenerator[
452454
tuple[
453455
MemoryObjectReceiveStream[SessionMessage | Exception],
@@ -481,6 +483,7 @@ async def streamablehttp_client(
481483
headers=transport.request_headers,
482484
timeout=httpx.Timeout(transport.timeout, read=transport.sse_read_timeout),
483485
auth=transport.auth,
486+
proxy=proxy
484487
) as client:
485488
# Define callbacks that need access to tg
486489
def start_get_stream() -> None:

src/mcp/shared/_httpx_utils.py

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

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

55
import httpx
66

@@ -13,13 +13,15 @@ def __call__(
1313
headers: dict[str, str] | None = None,
1414
timeout: httpx.Timeout | None = None,
1515
auth: httpx.Auth | None = None,
16+
proxy: Optional[str] | None = None
1617
) -> httpx.AsyncClient: ...
1718

1819

1920
def create_mcp_http_client(
2021
headers: dict[str, str] | None = None,
2122
timeout: httpx.Timeout | None = None,
2223
auth: httpx.Auth | None = None,
24+
proxy: Optional[str] | None = None
2325
) -> httpx.AsyncClient:
2426
"""Create a standardized httpx AsyncClient with MCP defaults.
2527
@@ -80,4 +82,8 @@ def create_mcp_http_client(
8082
if auth is not None:
8183
kwargs["auth"] = auth
8284

85+
# Configure proxy if applied
86+
if proxy is not None:
87+
kwargs["proxy"] = proxy
88+
8389
return httpx.AsyncClient(**kwargs)

0 commit comments

Comments
 (0)