1010
1111
1212def create_mcp_http_client (
13- * ,
1413 headers : dict [str , Any ] | None = None ,
1514 timeout : httpx .Timeout | None = None ,
16- ** kwargs : Any ,
1715) -> httpx .AsyncClient :
1816 """Create a standardized httpx AsyncClient with MCP defaults.
1917
2018 This function provides common defaults used throughout the MCP codebase:
2119 - follow_redirects=True (always enabled)
2220 - Default timeout of 30 seconds if not specified
23- - Headers will be merged with any existing headers in kwargs
2421
2522 Args:
2623 headers: Optional headers to include with all requests.
2724 timeout: Request timeout as httpx.Timeout object.
2825 Defaults to 30 seconds if not specified.
29- **kwargs: Additional keyword arguments to pass to AsyncClient.
3026
3127 Returns:
3228 Configured httpx.AsyncClient instance with MCP defaults.
@@ -42,32 +38,27 @@ def create_mcp_http_client(
4238
4339 # With custom headers
4440 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:
4642 response = await client.get("/endpoint")
4743
48- # With custom timeout
44+ # With both custom headers and timeout
4945 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:
5147 response = await client.get("/long-request")
5248 """
5349 # Set MCP defaults
54- defaults : dict [str , Any ] = {
50+ kwargs : dict [str , Any ] = {
5551 "follow_redirects" : True ,
5652 }
5753
5854 # Handle timeout
5955 if timeout is None :
60- defaults ["timeout" ] = httpx .Timeout (30.0 )
56+ kwargs ["timeout" ] = httpx .Timeout (30.0 )
6157 else :
62- defaults ["timeout" ] = timeout
58+ kwargs ["timeout" ] = timeout
6359
64- # Handle headers with proper merging
60+ # Handle headers
6561 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
6963
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