|
| 1 | +"""Example demonstrating custom httpx_client_factory for MCPServerStreamableHttp. |
| 2 | +
|
| 3 | +This example shows how to configure custom HTTP client behavior for MCP StreamableHTTP |
| 4 | +connections, including SSL certificates, proxy settings, and custom timeouts. |
| 5 | +""" |
| 6 | + |
| 7 | +import asyncio |
| 8 | +import os |
| 9 | +import shutil |
| 10 | +import subprocess |
| 11 | +import time |
| 12 | +from typing import Any |
| 13 | + |
| 14 | +import httpx |
| 15 | + |
| 16 | +from agents import Agent, Runner, gen_trace_id, trace |
| 17 | +from agents.mcp import MCPServer, MCPServerStreamableHttp |
| 18 | +from agents.model_settings import ModelSettings |
| 19 | + |
| 20 | + |
| 21 | +def create_custom_http_client( |
| 22 | + headers: dict[str, str] | None = None, |
| 23 | + timeout: httpx.Timeout | None = None, |
| 24 | + auth: httpx.Auth | None = None, |
| 25 | +) -> httpx.AsyncClient: |
| 26 | + """Create a custom HTTP client with specific configurations. |
| 27 | +
|
| 28 | + This function demonstrates how to configure: |
| 29 | + - Custom SSL verification settings |
| 30 | + - Custom timeouts |
| 31 | + - Custom headers |
| 32 | + - Proxy settings (commented out) |
| 33 | + """ |
| 34 | + if headers is None: |
| 35 | + headers = { |
| 36 | + "X-Custom-Client": "agents-mcp-example", |
| 37 | + "User-Agent": "OpenAI-Agents-MCP/1.0", |
| 38 | + } |
| 39 | + if timeout is None: |
| 40 | + timeout = httpx.Timeout(60.0, read=120.0) |
| 41 | + if auth is None: |
| 42 | + auth = None |
| 43 | + return httpx.AsyncClient( |
| 44 | + # Disable SSL verification for testing (not recommended for production) |
| 45 | + verify=False, |
| 46 | + # Set custom timeout |
| 47 | + timeout=httpx.Timeout(60.0, read=120.0), |
| 48 | + # Add custom headers that will be sent with every request |
| 49 | + headers=headers, |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +async def run_with_custom_client(mcp_server: MCPServer): |
| 54 | + """Run the agent with a custom HTTP client configuration.""" |
| 55 | + agent = Agent( |
| 56 | + name="Assistant", |
| 57 | + instructions="Use the tools to answer the questions.", |
| 58 | + mcp_servers=[mcp_server], |
| 59 | + model_settings=ModelSettings(tool_choice="required"), |
| 60 | + ) |
| 61 | + |
| 62 | + # Use the `add` tool to add two numbers |
| 63 | + message = "Add these numbers: 7 and 22." |
| 64 | + print(f"Running: {message}") |
| 65 | + result = await Runner.run(starting_agent=agent, input=message) |
| 66 | + print(result.final_output) |
| 67 | + |
| 68 | + |
| 69 | +async def main(): |
| 70 | + """Main function demonstrating different HTTP client configurations.""" |
| 71 | + |
| 72 | + print("=== Example: Custom HTTP Client with SSL disabled and custom headers ===") |
| 73 | + async with MCPServerStreamableHttp( |
| 74 | + name="Streamable HTTP with Custom Client", |
| 75 | + params={ |
| 76 | + "url": "http://localhost:8000/mcp", |
| 77 | + "httpx_client_factory": create_custom_http_client, |
| 78 | + }, |
| 79 | + ) as server: |
| 80 | + trace_id = gen_trace_id() |
| 81 | + with trace(workflow_name="Custom HTTP Client Example", trace_id=trace_id): |
| 82 | + print(f"View trace: https://platform.openai.com/logs/trace?trace_id={trace_id}\n") |
| 83 | + await run_with_custom_client(server) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + # Let's make sure the user has uv installed |
| 88 | + if not shutil.which("uv"): |
| 89 | + raise RuntimeError( |
| 90 | + "uv is not installed. Please install it: https://docs.astral.sh/uv/getting-started/installation/" |
| 91 | + ) |
| 92 | + |
| 93 | + # We'll run the Streamable HTTP server in a subprocess. Usually this would be a remote server, but for this |
| 94 | + # demo, we'll run it locally at http://localhost:8000/mcp |
| 95 | + process: subprocess.Popen[Any] | None = None |
| 96 | + try: |
| 97 | + this_dir = os.path.dirname(os.path.abspath(__file__)) |
| 98 | + server_file = os.path.join(this_dir, "server.py") |
| 99 | + |
| 100 | + print("Starting Streamable HTTP server at http://localhost:8000/mcp ...") |
| 101 | + |
| 102 | + # Run `uv run server.py` to start the Streamable HTTP server |
| 103 | + process = subprocess.Popen(["uv", "run", server_file]) |
| 104 | + # Give it 3 seconds to start |
| 105 | + time.sleep(3) |
| 106 | + |
| 107 | + print("Streamable HTTP server started. Running example...\n\n") |
| 108 | + except Exception as e: |
| 109 | + print(f"Error starting Streamable HTTP server: {e}") |
| 110 | + exit(1) |
| 111 | + |
| 112 | + try: |
| 113 | + asyncio.run(main()) |
| 114 | + finally: |
| 115 | + if process: |
| 116 | + process.terminate() |
0 commit comments