11"""Utilities for creating standardized httpx AsyncClient instances."""
22
3+ from __future__ import annotations
4+
35from typing import Any
46
57import httpx
68
9+ __all__ = ["create_mcp_http_client" ]
10+
711
812def create_mcp_http_client (
913 * ,
1014 headers : dict [str , Any ] | None = None ,
11- timeout : httpx .Timeout | float | None = None ,
15+ timeout : httpx .Timeout | None = None ,
1216 ** kwargs : Any ,
1317) -> httpx .AsyncClient :
1418 """Create a standardized httpx AsyncClient with MCP defaults.
1519
1620 This function provides common defaults used throughout the MCP codebase:
1721 - follow_redirects=True (always enabled)
1822 - Default timeout of 30 seconds if not specified
19- - Header will be merged
23+ - Headers will be merged with any existing headers in kwargs
2024
2125 Args:
2226 headers: Optional headers to include with all requests.
23- timeout: Request timeout in seconds (float) or httpx.Timeout object.
27+ timeout: Request timeout as httpx.Timeout object.
2428 Defaults to 30 seconds if not specified.
2529 **kwargs: Additional keyword arguments to pass to AsyncClient.
2630
2731 Returns:
2832 Configured httpx.AsyncClient instance with MCP defaults.
2933
34+ Note:
35+ The returned AsyncClient must be used as a context manager to ensure
36+ proper cleanup of connections.
37+
3038 Examples:
3139 # Basic usage with MCP defaults
3240 async with create_mcp_http_client() as client:
@@ -50,16 +58,16 @@ def create_mcp_http_client(
5058 # Handle timeout
5159 if timeout is None :
5260 defaults ["timeout" ] = httpx .Timeout (30.0 )
53- elif isinstance (timeout , int | float ):
54- defaults ["timeout" ] = httpx .Timeout (timeout )
5561 else :
5662 defaults ["timeout" ] = timeout
5763
58- # Handle headers
64+ # Handle headers with proper merging
5965 if headers is not None :
60- kwargs ["headers" ] = headers
66+ existing_headers = kwargs .get ("headers" , {})
67+ merged_headers = {** existing_headers , ** headers }
68+ kwargs ["headers" ] = merged_headers
6169
62- # Merge defaults with provided kwargs
63- kwargs = {** defaults , ** kwargs }
70+ # Merge kwargs with defaults (defaults take precedence)
71+ kwargs = {** kwargs , ** defaults }
6472
6573 return httpx .AsyncClient (** kwargs )
0 commit comments