MCPcopy
hub / github.com/modelcontextprotocol/python-sdk / create_mcp_http_client

Function create_mcp_http_client

src/mcp/shared/_httpx_utils.py:23–95  ·  view source on GitHub ↗

Create a standardized httpx AsyncClient with MCP defaults. Always enables follow_redirects and applies an SSE-friendly default timeout. Args: headers: Optional headers to include with all requests. timeout: Request timeout as httpx.Timeout object. Defaults to 30s for

(
    headers: dict[str, str] | None = None,
    timeout: httpx.Timeout | None = None,
    auth: httpx.Auth | None = None,
)

Source from the content-addressed store, hash-verified

21
22
23def create_mcp_http_client(
24 headers: dict[str, str] | None = None,
25 timeout: httpx.Timeout | None = None,
26 auth: httpx.Auth | None = None,
27) -> httpx.AsyncClient:
28 """Create a standardized httpx AsyncClient with MCP defaults.
29
30 Always enables follow_redirects and applies an SSE-friendly default timeout.
31
32 Args:
33 headers: Optional headers to include with all requests.
34 timeout: Request timeout as httpx.Timeout object. Defaults to 30s for
35 connect/write/pool and 300s for read (for long-lived SSE streams).
36 auth: Optional authentication handler.
37
38 Returns:
39 Configured httpx.AsyncClient instance with MCP defaults.
40
41 Note:
42 The returned AsyncClient must be used as a context manager to ensure
43 proper cleanup of connections.
44
45 Example:
46 Basic usage with MCP defaults:
47
48 ```python
49 async with create_mcp_http_client() as client:
50 response = await client.get("https://api.example.com")
51 ```
52
53 With custom headers:
54
55 ```python
56 headers = {"Authorization": "Bearer token"}
57 async with create_mcp_http_client(headers) as client:
58 response = await client.get("/endpoint")
59 ```
60
61 With both custom headers and timeout:
62
63 ```python
64 timeout = httpx.Timeout(60.0, read=300.0)
65 async with create_mcp_http_client(headers, timeout) as client:
66 response = await client.get("/long-request")
67 ```
68
69 With authentication:
70
71 ```python
72 from httpx import BasicAuth
73 auth = BasicAuth(username="user", password="pass")
74 async with create_mcp_http_client(headers, timeout, auth) as client:
75 response = await client.get("/protected-endpoint")
76 ```
77 """
78 # Set MCP defaults
79 kwargs: dict[str, Any] = {"follow_redirects": True}
80

Callers 5

_establish_sessionMethod · 0.90
streamable_http_clientFunction · 0.90
test_default_settingsFunction · 0.90
test_custom_parametersFunction · 0.90
fetch_websiteFunction · 0.90

Calls

no outgoing calls

Tested by 2

test_default_settingsFunction · 0.72
test_custom_parametersFunction · 0.72