Skip to content

Commit 1b5fcdc

Browse files
committed
introduce a function to create a stnadard AsyncClient with options
1 parent a027d75 commit 1b5fcdc

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

examples/servers/simple-auth/mcp_simple_auth/server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import Any
77

88
import click
9-
import httpx
109
from pydantic import AnyHttpUrl
1110
from pydantic_settings import BaseSettings, SettingsConfigDict
1211
from starlette.exceptions import HTTPException
@@ -25,6 +24,7 @@
2524
from mcp.server.auth.settings import AuthSettings, ClientRegistrationOptions
2625
from mcp.server.fastmcp.server import FastMCP
2726
from mcp.shared.auth import OAuthClientInformationFull, OAuthToken
27+
from mcp.shared.httpx_utils import create_mcp_http_client
2828

2929
logger = logging.getLogger(__name__)
3030

@@ -123,7 +123,7 @@ async def handle_github_callback(self, code: str, state: str) -> str:
123123
client_id = state_data["client_id"]
124124

125125
# Exchange code for token with GitHub
126-
async with httpx.AsyncClient() as client:
126+
async with create_mcp_http_client() as client:
127127
response = await client.post(
128128
self.settings.github_token_url,
129129
data={
@@ -325,7 +325,7 @@ async def get_user_profile() -> dict[str, Any]:
325325
"""
326326
github_token = get_github_token()
327327

328-
async with httpx.AsyncClient() as client:
328+
async with create_mcp_http_client() as client:
329329
response = await client.get(
330330
"https://api.github.com/user",
331331
headers={

examples/servers/simple-tool/mcp_simple_tool/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import anyio
22
import click
3-
import httpx
43
import mcp.types as types
54
from mcp.server.lowlevel import Server
5+
from mcp.shared.httpx_utils import create_mcp_http_client
66

77

88
async def fetch_website(
@@ -11,7 +11,7 @@ async def fetch_website(
1111
headers = {
1212
"User-Agent": "MCP Test Server (github.com/modelcontextprotocol/python-sdk)"
1313
}
14-
async with httpx.AsyncClient(follow_redirects=True, headers=headers) as client:
14+
async with create_mcp_http_client(headers=headers) as client:
1515
response = await client.get(url)
1616
response.raise_for_status()
1717
return [types.TextContent(type="text", text=response.text)]

src/mcp/client/sse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +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
1314
from mcp.shared.message import SessionMessage
1415

1516
logger = logging.getLogger(__name__)
@@ -44,7 +45,7 @@ async def sse_client(
4445
async with anyio.create_task_group() as tg:
4546
try:
4647
logger.info(f"Connecting to SSE endpoint: {remove_request_params(url)}")
47-
async with httpx.AsyncClient(headers=headers) as client:
48+
async with create_mcp_http_client(headers=headers) as client:
4849
async with aconnect_sse(
4950
client,
5051
"GET",

src/mcp/client/streamable_http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
1919
from httpx_sse import EventSource, ServerSentEvent, aconnect_sse
2020

21+
from mcp.shared.httpx_utils import create_mcp_http_client
2122
from mcp.shared.message import ClientMessageMetadata, SessionMessage
2223
from mcp.types import (
2324
ErrorData,
@@ -446,12 +447,11 @@ async def streamablehttp_client(
446447
try:
447448
logger.info(f"Connecting to StreamableHTTP endpoint: {url}")
448449

449-
async with httpx.AsyncClient(
450+
async with create_mcp_http_client(
450451
headers=transport.request_headers,
451452
timeout=httpx.Timeout(
452453
transport.timeout.seconds, read=transport.sse_read_timeout.seconds
453454
),
454-
follow_redirects=True,
455455
) as client:
456456
# Define callbacks that need access to tg
457457
def start_get_stream() -> None:

src/mcp/shared/httpx_utils.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Utilities for creating standardized httpx AsyncClient instances."""
2+
3+
from typing import Any
4+
5+
import httpx
6+
7+
8+
def create_mcp_http_client(
9+
*,
10+
headers: dict[str, Any] | None = None,
11+
timeout: httpx.Timeout | float | None = None,
12+
**kwargs: Any,
13+
) -> httpx.AsyncClient:
14+
"""Create a standardized httpx AsyncClient with MCP defaults.
15+
16+
This function provides common defaults used throughout the MCP codebase:
17+
- follow_redirects=True (always enabled)
18+
- Default timeout of 30 seconds if not specified
19+
- Header will be merged
20+
21+
Args:
22+
headers: Optional headers to include with all requests.
23+
timeout: Request timeout in seconds (float) or httpx.Timeout object.
24+
Defaults to 30 seconds if not specified.
25+
**kwargs: Additional keyword arguments to pass to AsyncClient.
26+
27+
Returns:
28+
Configured httpx.AsyncClient instance with MCP defaults.
29+
30+
Examples:
31+
# Basic usage with MCP defaults
32+
async with create_mcp_http_client() as client:
33+
response = await client.get("https://api.example.com")
34+
35+
# With custom headers
36+
headers = {"Authorization": "Bearer token"}
37+
async with create_mcp_http_client(headers=headers) as client:
38+
response = await client.get("/endpoint")
39+
40+
# With custom timeout
41+
timeout = httpx.Timeout(60.0, read=300.0)
42+
async with create_mcp_http_client(timeout=timeout) as client:
43+
response = await client.get("/long-request")
44+
"""
45+
# Set MCP defaults
46+
defaults: dict[str, Any] = {
47+
"follow_redirects": True,
48+
}
49+
50+
# Handle timeout
51+
if timeout is None:
52+
defaults["timeout"] = httpx.Timeout(30.0)
53+
elif isinstance(timeout, int | float):
54+
defaults["timeout"] = httpx.Timeout(timeout)
55+
else:
56+
defaults["timeout"] = timeout
57+
58+
# Handle headers
59+
if headers is not None:
60+
kwargs["headers"] = headers
61+
62+
# Merge defaults with provided kwargs
63+
kwargs = {**defaults, **kwargs}
64+
65+
return httpx.AsyncClient(**kwargs)

0 commit comments

Comments
 (0)