Skip to content

Commit 5771222

Browse files
author
patrick.rump
committed
Allow kwargs down in AsyncClient when instantiating sse or streamable_http clients.
1 parent 6353dd1 commit 5771222

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

src/mcp/client/sse.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ async def sse_client(
2626
headers: dict[str, Any] | None = None,
2727
timeout: float = 5,
2828
sse_read_timeout: float = 60 * 5,
29+
**client_kwargs: Any,
2930
):
3031
"""
3132
Client transport for SSE.
3233
3334
`sse_read_timeout` determines how long (in seconds) the client will wait for a new
3435
event before disconnecting. All other HTTP operations are controlled by `timeout`.
36+
37+
`**client_kwargs` : dict, optional - Additional http client configurations used to configure the AsyncClient.
38+
3539
"""
3640
read_stream: MemoryObjectReceiveStream[SessionMessage | Exception]
3741
read_stream_writer: MemoryObjectSendStream[SessionMessage | Exception]
@@ -45,7 +49,9 @@ async def sse_client(
4549
async with anyio.create_task_group() as tg:
4650
try:
4751
logger.info(f"Connecting to SSE endpoint: {remove_request_params(url)}")
48-
async with create_mcp_http_client(headers=headers) as client:
52+
async with create_mcp_http_client(
53+
headers=headers, client_kwargs=client_kwargs
54+
) as client:
4955
async with aconnect_sse(
5056
client,
5157
"GET",

src/mcp/client/streamable_http.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
**client_kwargs: Any,
430431
) -> AsyncGenerator[
431432
tuple[
432433
MemoryObjectReceiveStream[SessionMessage | Exception],
@@ -441,6 +442,8 @@ async def streamablehttp_client(
441442
`sse_read_timeout` determines how long (in seconds) the client will wait for a new
442443
event before disconnecting. All other HTTP operations are controlled by `timeout`.
443444
445+
`**client_kwargs` : dict, optional - Additional http client configurations used to configure the AsyncClient.
446+
444447
Yields:
445448
Tuple containing:
446449
- read_stream: Stream for reading messages from the server
@@ -465,6 +468,7 @@ async def streamablehttp_client(
465468
timeout=httpx.Timeout(
466469
transport.timeout.seconds, read=transport.sse_read_timeout.seconds
467470
),
471+
client_kwargs=client_kwargs,
468472
) as client:
469473
# Define callbacks that need access to tg
470474
def start_get_stream() -> None:

src/mcp/shared/_httpx_utils.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
def create_mcp_http_client(
1111
headers: dict[str, str] | None = None,
1212
timeout: httpx.Timeout | None = None,
13+
client_kwargs: dict[str, Any] | None = None,
1314
) -> httpx.AsyncClient:
1415
"""Create a standardized httpx AsyncClient with MCP defaults.
1516
@@ -21,6 +22,7 @@ def create_mcp_http_client(
2122
headers: Optional headers to include with all requests.
2223
timeout: Request timeout as httpx.Timeout object.
2324
Defaults to 30 seconds if not specified.
25+
client_kwargs : dict[str, Any], optional. Used to configure the AsyncClient.
2426
2527
Returns:
2628
Configured httpx.AsyncClient instance with MCP defaults.
@@ -45,18 +47,18 @@ def create_mcp_http_client(
4547
response = await client.get("/long-request")
4648
"""
4749
# Set MCP defaults
48-
kwargs: dict[str, Any] = {
49-
"follow_redirects": True,
50-
}
50+
if not client_kwargs:
51+
client_kwargs = {}
52+
client_kwargs["follow_redirects"] = True
5153

5254
# Handle timeout
5355
if timeout is None:
54-
kwargs["timeout"] = httpx.Timeout(30.0)
56+
client_kwargs["timeout"] = httpx.Timeout(30.0)
5557
else:
56-
kwargs["timeout"] = timeout
58+
client_kwargs["timeout"] = timeout
5759

5860
# Handle headers
5961
if headers is not None:
60-
kwargs["headers"] = headers
62+
client_kwargs["headers"] = headers
6163

62-
return httpx.AsyncClient(**kwargs)
64+
return httpx.AsyncClient(**client_kwargs)

tests/shared/test_httpx_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ def test_custom_parameters():
2222

2323
assert client.headers["Authorization"] == "Bearer token"
2424
assert client.timeout.connect == 60.0
25+
26+
27+
def test_client_kwargs_parameters():
28+
"""Test if additional kwargs are set correctly."""
29+
client_kwargs = {"max_redirects": 999}
30+
31+
client = create_mcp_http_client(client_kwargs=client_kwargs)
32+
assert client.max_redirects == 999

0 commit comments

Comments
 (0)