增加测试案例
This commit is contained in:
81
README.md
81
README.md
@@ -31,6 +31,11 @@
|
||||
|
||||
---
|
||||
|
||||
## 待完成
|
||||
|
||||
- [ ] 阿里云百炼 Key 的测试兼容
|
||||
- [ ] FASTMCP 兼容适配(MCPHUB适配),server目录下的已经测试案例适配
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 1) 安装
|
||||
@@ -109,6 +114,82 @@ python examples/qwen_langgraph_stream.py
|
||||
python examples/qwen_langgraph_custom_model.py
|
||||
```
|
||||
|
||||
## 使用 MCP(FastMCP,HTTP streamable)注入工具到 LangGraph
|
||||
|
||||
通过官方库 langchain-mcp-adapters,可以把任意 MCP 服务器暴露的工具注入到 LangGraph 的 ReAct 代理里使用。我们提供了开箱示例与一份更工程化的封装建议。
|
||||
|
||||
### 安装依赖
|
||||
|
||||
```bash
|
||||
uv pip install -e '.[mcp-adapters]' # 本仓库 extras,等价安装 langchain-mcp-adapters
|
||||
# 最少还需要 fastapi/uvicorn(若用我们内置的演示服务)
|
||||
uv pip install fastapi uvicorn
|
||||
```
|
||||
|
||||
> 若你只需要客户端能力(连接已有 MCP 服务器),安装 langchain-mcp-adapters 即可;若要本地起一个演示工具服务,再装 fastapi/uvicorn。
|
||||
|
||||
### 启动测试mcp服务器
|
||||
|
||||
```bash
|
||||
python examples/mcp_adapters/server/weather_server.py
|
||||
```
|
||||
|
||||
该服务注册了一个异步工具:
|
||||
|
||||
get_weather(location: str) -> str:返回“某地晴”的示例文案
|
||||
|
||||
想换你自己的工具?改这个文件里用 @mcp.tool() 注册即可。
|
||||
|
||||
### 执行测试案例
|
||||
|
||||
```bash
|
||||
# 强烈建议避免本地回环被代理: NO_PROXY=localhost,127.0.0.1
|
||||
export NO_PROXY=localhost,127.0.0.1,127.0.0.1:8000
|
||||
|
||||
# 指向你刚刚起的 MCP 服务地址(注意尾斜杠)
|
||||
export WEATHER_MCP_URL='http://127.0.0.1:8000/mcp'
|
||||
export WEATHER_TRANSPORT='streamable_http'
|
||||
|
||||
# Qwen(OpenAI兼容)后端必需
|
||||
export QWEN_BASE_URL='https://your-gateway-or-llamabox-host/v1' # 或完整 /v1/chat/completions
|
||||
export QWEN_MODEL='qwen3-coder-flash-1M'
|
||||
# 若你的后端需要鉴权,额外配置:
|
||||
# export QWEN_API_KEY='...' # 或 OPENAI_API_KEY / DASHSCOPE_API_KEY
|
||||
# export QWEN_AUTH_HEADER='Authorization' # 默认即可
|
||||
# export QWEN_AUTH_SCHEME='Bearer' # 裸 key 时置空: ''
|
||||
|
||||
# 调试(可选)
|
||||
export QWEN_DEBUG=1
|
||||
export QWEN_DEBUG_BODY=1
|
||||
export QWEN_DEBUG_RESP=1
|
||||
python examples/mcp_adapters/inject_to_langgraph.py
|
||||
```
|
||||
|
||||
预期输出:
|
||||
|
||||
首行打印 Discovered tools: get_weather
|
||||
|
||||
模型首轮会“列出工具并选择一个调用”,随后 LangGraph ToolNode 会执行 MCP 工具,并给出中文总结
|
||||
|
||||
为什么 URL 需要尾斜杠?
|
||||
我们演示服务注册的是 POST /mcp/,不少 HTTP 路由器对 /mcp 与 /mcp/ 区分严格,建议总是使用尾斜杠或在服务端做 301/307 兼容。
|
||||
|
||||
### 封装使用
|
||||
|
||||
新建 `langgraph_qwen/mcp.py`, `langgraph_qwen/__main__.py`
|
||||
|
||||
```bash
|
||||
# 方式一:用环境变量
|
||||
export WEATHER_MCP_URL='http://127.0.0.1:8000/mcp/'
|
||||
qwen-mcp-agent --prompt '在北京查天气并总结'
|
||||
|
||||
# 方式二:传入多服务器 JSON
|
||||
qwen-mcp-agent --servers-json '{
|
||||
"weather":{"url":"http://127.0.0.1:8000/mcp/","transport":"streamable_http"},
|
||||
"calc":{"url":"http://127.0.0.1:8011/mcp/","transport":"streamable_http"}
|
||||
}'
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 自定义 ChatModel:`ChatQwenOpenAICompat`
|
||||
|
||||
Reference in New Issue
Block a user