10
10
11
11
12
12
def create_mcp_http_client (
13
- * ,
14
13
headers : dict [str , Any ] | None = None ,
15
14
timeout : httpx .Timeout | None = None ,
16
- ** kwargs : Any ,
17
15
) -> httpx .AsyncClient :
18
16
"""Create a standardized httpx AsyncClient with MCP defaults.
19
17
20
18
This function provides common defaults used throughout the MCP codebase:
21
19
- follow_redirects=True (always enabled)
22
20
- Default timeout of 30 seconds if not specified
23
- - Headers will be merged with any existing headers in kwargs
24
21
25
22
Args:
26
23
headers: Optional headers to include with all requests.
27
24
timeout: Request timeout as httpx.Timeout object.
28
25
Defaults to 30 seconds if not specified.
29
- **kwargs: Additional keyword arguments to pass to AsyncClient.
30
26
31
27
Returns:
32
28
Configured httpx.AsyncClient instance with MCP defaults.
@@ -42,32 +38,27 @@ def create_mcp_http_client(
42
38
43
39
# With custom headers
44
40
headers = {"Authorization": "Bearer token"}
45
- async with create_mcp_http_client(headers=headers ) as client:
41
+ async with create_mcp_http_client(headers) as client:
46
42
response = await client.get("/endpoint")
47
43
48
- # With custom timeout
44
+ # With both custom headers and timeout
49
45
timeout = httpx.Timeout(60.0, read=300.0)
50
- async with create_mcp_http_client(timeout= timeout) as client:
46
+ async with create_mcp_http_client(headers, timeout) as client:
51
47
response = await client.get("/long-request")
52
48
"""
53
49
# Set MCP defaults
54
- defaults : dict [str , Any ] = {
50
+ kwargs : dict [str , Any ] = {
55
51
"follow_redirects" : True ,
56
52
}
57
53
58
54
# Handle timeout
59
55
if timeout is None :
60
- defaults ["timeout" ] = httpx .Timeout (30.0 )
56
+ kwargs ["timeout" ] = httpx .Timeout (30.0 )
61
57
else :
62
- defaults ["timeout" ] = timeout
58
+ kwargs ["timeout" ] = timeout
63
59
64
- # Handle headers with proper merging
60
+ # Handle headers
65
61
if headers is not None :
66
- existing_headers = kwargs .get ("headers" , {})
67
- merged_headers = {** existing_headers , ** headers }
68
- kwargs ["headers" ] = merged_headers
62
+ kwargs ["headers" ] = headers
69
63
70
- # Merge kwargs with defaults (defaults take precedence)
71
- kwargs = {** kwargs , ** defaults }
72
-
73
- return httpx .AsyncClient (** kwargs )
64
+ return httpx .AsyncClient (** kwargs )
0 commit comments