# Copyright (c) 2025 Agentspan # Licensed under the MIT License. See LICENSE file in the project root for details. """MCP Weather — using Conductor's MCP system tasks for live weather. Demonstrates the `mcp_tool()` function which uses Conductor's built-in LIST_MCP_TOOLS and CALL_MCP_TOOL system tasks. The MCP test server provides deterministic weather data, and the Conductor server handles all MCP protocol communication — **no worker process needed**. Flow: ListMcpTools → LLM (picks tool) → CallMcpTool → Final LLM MCP Test Server Setup (mcp-testkit): pip install mcp-testkit # Start without auth: mcp-testkit --transport http # Or start with auth (requires storing the secret as a credential): mcp-testkit --transport http --auth # Store credentials via CLI or Agentspan UI: agentspan credentials set MCP_TEST_API_KEY Requirements: - Conductor server with LLM support - mcp-testkit running on http://localhost:3001 (see setup above) - AGENTSPAN_SERVER_URL=http://localhost:6767/api as environment variable - AGENTSPAN_LLM_MODEL=openai/gpt-4o-mini as environment variable Docker gotcha: When the AgentSpan server runs in Docker (e.g. `agentspan server start`), the *server* makes the MCP calls — not your local process. The server resolves `localhost` to its own container loopback, not your host machine. Fix: use `host.docker.internal` so the container can reach your host: weather = mcp_tool(server_url="http://host.docker.internal:3001/mcp", ...) DNS rebinding protection: mcp-testkit rejects unknown Host headers with HTTP 421. If you hit this, patch the validation in the venv that the mcp-testkit process uses: sed -i '' \ 's/return Response("Invalid Host header", status_code=421)/return None/' \ $(python -c "import mcp.server.transport_security as m; print(m.__file__)") """ from agentspan.agents import Agent, AgentRuntime, mcp_tool from settings import settings # Create MCP tool — Conductor discovers tools from mcp-testkit at runtime # ${MCP_TEST_API_KEY} is resolved server-side from the credential store. weather = mcp_tool( server_url="http://localhost:3001/mcp", name="weather_mcp", description="Weather and air quality tools via MCP, use it to get current and historical weather information for " "a city", headers={"Authorization": "Bearer ${MCP_TEST_API_KEY}"}, credentials=["MCP_TEST_API_KEY"], ) agent = Agent( name="weather_mcp_agent", model=settings.llm_model, max_tokens=10240, tools=[weather], instructions=( "You are a weather assistant. Use the available MCP tools " "to answer questions about weather conditions around the world." "when asked get the current temperature in F" "use the tools provided" ), ) if __name__ == "__main__": with AgentRuntime() as runtime: result = runtime.run(agent, "What's the weather like in San Francisco (CA) right now?") result.print_result() # Production pattern: # 1. Deploy once during CI/CD: # runtime.deploy(agent) # CLI alternative: # agentspan deploy --package examples.04_mcp_weather # # 2. In a separate long-lived worker process: # runtime.serve(agent)