examples/mcp/streamablehttp_custom_client_example/README.md
This repository example targets MCP Python SDK v2 and httpx2, and is intended to run with the repository's locked development environment. The Agents SDK client itself supports MCP v1 with httpx and MCP v2 with httpx2.
This example demonstrates how to use the new httpx_client_factory parameter in MCPServerStreamableHttp to configure custom HTTP client behavior for MCP StreamableHTTP connections.
Make sure you have uv installed: https://docs.astral.sh/uv/getting-started/installation/
Run the example:
cd examples/mcp/streamablehttp_custom_client_example
uv run main.py
import httpx2
from agents.mcp import MCPServerStreamableHttp
def create_custom_http_client() -> httpx2.AsyncClient:
return httpx2.AsyncClient(
verify=False, # Disable SSL verification for testing
timeout=httpx2.Timeout(60.0, read=120.0),
headers={"X-Custom-Client": "my-app"},
)
async with MCPServerStreamableHttp(
name="Custom Client Server",
params={
"url": "http://localhost:<port>/mcp",
"httpx_client_factory": create_custom_http_client,
},
) as server:
# Use the server...
This example will auto-pick a free localhost port unless you set STREAMABLE_HTTP_PORT; use STREAMABLE_HTTP_HOST to change the bind address.