-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy path04_mcp_weather.py
More file actions
91 lines (71 loc) · 3.3 KB
/
Copy path04_mcp_weather.py
File metadata and controls
91 lines (71 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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 <secret>
# Store credentials via CLI or Agentspan UI:
agentspan credentials set MCP_TEST_API_KEY <secret>
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 conductor.ai.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)