OpenAPI的MCP服务器
OpenAPI规范中的MCP服务器实现,支持SSE(服务器发送事件)和流式响应。
先决条件
- Python 3.x
- pip(Python包管理器)
- 无烟煤API密钥(适用于MCP客户)
- Google API密钥(适用于ADK代理)
安装
- 创建并激活虚拟环境:
python3 -m venv venv
source venv/bin/activate- 安装依赖项:
pip install -r requirements.txt服务器安装程序
服务器可以在两种模式下运行:
SSE模式
python server/mcp_server_sse.py --spec server/httpbin_openapi.yaml --name httpbin --base-url https://httpbin.org流媒体模式
python server/mcp_server_stream.py --spec server/httpbin_openapi.yaml --name httpbin --base-url https://httpbin.org客户端设置
MCP客户端
- 创建一个
.env文件,并添加您的Anthropic API密钥:
ANTHROPIC_API_KEY=your_api_key_here- 运行您选择的客户端:
- 对于 SSE 客户端:
python client/sse_client.py- 对于流媒体客户端:
python client/stream_client.pyMCP或流媒体客户端的示例用法:
MCP Client Started!
Type your queries or 'quit' to exit.
Query: Get Headers
I'll help you get the headers using the api_call function.
[Calling tool api_call with args {'kwargs': '{"action": "get_headers"}'}]
Here are the headers from the response:
{ "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "python-httpx/0.28.1", "X-Amzn-Trace-Id": "Root=1-683fecc4-674af7e9323b027c4b9bc740" }
### ADK代理商
#### 先决条件
1. 安装ADK:
pip install google-adk
#### 设置步骤
1. **重要**:流模式解决方法
- 下载最新 `mcp_session_manager.py` 从 [谷歌ADK存储库](https://github.com/google/adk-python/blob/main/src/google/adk/tools/mcp_tool/mcp_session_manager.py)
- 替换ADK安装中的现有文件:# Typical path (adjust Python version as needed) cp mcp_session_manager.py venv/lib/python3.13/site-packages/google/adk/tools/mcp_tool/
1. 导航到Clients目录:
cd client
3. 配置环境:
- 添加以下内容 `.env` 文件在两个 `httpb_sse_agent` 和 `httpbin_stream_agent` 目录:
GOOGLE_GENAI_USE_VERTEXAI="False" GOOGLE_API_KEY="your_google_api_key_here"
1. 启动ADK web界面:
adk web
## 特性
- 基于OpenAPI规范的端点生成
- 支持SSE和流媒体响应
- 多种客户端实现(MCP和ADK)
- 通过环境变量轻松配置
## 故障排除
### ADK流模式问题
如果您在流模式下遇到ADK代理问题:
1. 验证您是否已更换 `mcp_session_manager.py` 文件
1. 检查您的Google API密钥是否在 `.env` 文件
1. 确保您使用的是最新版本的ADK
## 许可证
此项目根据Apache 2.0许可证获得许可。