本示例演示如何通过 MCPToolset 接入本地 MCP Server,并验证 Agent 对 MCP 工具的自动发现、调用与结果消费链路。
- 协议化工具接入:通过 MCP 协议连接工具服务,而非在 Agent 内硬编码工具
- 多 transport 支持:内置
stdio、sse、streamable-http三种接入模式 - 示例服务可独立运行:
mcp_server.py提供天气查询与计算工具 - 端到端可观测:日志展示“用户问题 -> MCP 工具调用 -> 工具返回 -> 生成回答”
本例是单 Agent + MCP Toolset 结构:
mcp_assistant (LlmAgent)
├── model: OpenAIModel
└── tools:
└── StdioMCPToolset (default)
└── mcp_server.py
├── get_weather(location)
└── calculate(operation, a, b)
关键文件:
- examples/mcp_tools/run_agent.py:示例入口,执行多轮问题
- examples/mcp_tools/mcp_server.py:MCP 服务端工具定义
- examples/mcp_tools/agent/agent.py:创建
mcp_assistant - examples/mcp_tools/agent/tools.py:三种 MCP 连接参数封装
- 在
agent/agent.py里默认使用StdioMCPToolset() - Agent 执行时由 MCP Toolset 拉取工具列表并转发调用
mcp_server.py通过FastMCP暴露get_weather和calculate- 返回值被框架包装为工具响应并传回 LLM
run_agent.py中逐轮打印function_call和function_response- 便于确认 MCP 请求确实被触发且结果被正确消费
- Python 3.12
git clone https://github.com/trpc-group/trpc-agent-python.git
cd trpc-agent-python
python3 -m venv .venv
source .venv/bin/activate
pip3 install -e .在 examples/mcp_tools/.env 中配置(或通过 export):
TRPC_AGENT_API_KEYTRPC_AGENT_BASE_URLTRPC_AGENT_MODEL_NAME
cd examples/mcp_tools
python3 run_agent.py🆔 Session ID: 04b4022f...
📝 User: What's the weather like in Beijing?
🔧 [Invoke Tool: get_weather({'location': 'Beijing'})]
📊 [Tool Result: {'result': 'Sunny, 15°C, humidity 45%'}]
The weather in Beijing is sunny with a temperature of 15°C and humidity at 45%.
----------------------------------------
🆔 Session ID: 901952a7...
📝 User: Calculate 15 multiplied by 3.5
🔧 [Invoke Tool: calculate({'operation': 'multiply', 'a': 15, 'b': 3.5})]
📊 [Tool Result: {'result': '52.5'}]
The result of 15 multiplied by 3.5 is 52.5.
----------------------------------------
🆔 Session ID: 78e9f717...
📝 User: How is the weather in Shanghai?
🔧 [Invoke Tool: get_weather({'location': 'Shanghai'})]
📊 [Tool Result: {'result': 'Cloudy, 18°C, humidity 65%'}]
The weather in Shanghai is currently cloudy with a temperature of 18°C and 65% humidity.
----------------------------------------
🆔 Session ID: e2e59952...
📝 User: What is 100 divided by 4?
🔧 [Invoke Tool: calculate({'operation': 'divide', 'a': 100, 'b': 4})]
📊 [Tool Result: {'result': '25.0'}]
100 divided by 4 is 25.0.
----------------------------------------
结论:符合本示例测试要求。
- 工具路由正确:天气问题命中
get_weather,计算问题命中calculate - 参数传递正确:调用参数与用户问题一致(如
multiply 15 * 3.5、divide 100 / 4) - 结果消费正确:模型回答与工具返回值一致,形成自然语言输出
- 多轮稳定性正常:连续 4 轮均完成“调用 -> 返回 -> 回答”闭环
- 当前默认模式
- 可直接运行
python3 run_agent.py StdioMCPToolset通过子进程与mcp_server.py通信
Step 1:在 mcp_server.py 启用:
# app.run(transport="stdio")
app.run(transport="sse")
# app.run(transport="streamable-http")Step 2:在 agent/agent.py 使用 SseMCPToolset。
Step 3:先启动 mcp_server.py,再运行 run_agent.py。
Step 1:在 mcp_server.py 启用:
# app.run(transport="stdio")
# app.run(transport="sse")
app.run(transport="streamable-http")Step 2:在 agent/agent.py 使用 StreamableHttpMCPToolset。
Step 3:先启动 mcp_server.py,再运行 run_agent.py。
若出现 Attempted to exit a cancel scope that isn't the current tasks's current cancel scope:
- 确保示例结束前调用
await runner.close() - 如仍出现,可在程序入口调用:
from trpc_agent_sdk.tools import patch_mcp_cancel_scope_exit_issue
patch_mcp_cancel_scope_exit_issue()