OpenAPI-MCP代理将OpenAPI规范转换为MCP工具,使AI代理能够无需自定义包装器即可访问外部API!

OpenAPI到模型上下文协议(MCP)代理服务器通过动态翻译OpenAPI规范为标准化的MCP工具、资源和提示,桥接了AI代理与外部API之间的鸿沟。这简化了集成,消除了自定义API包装器的需求。
使用官方MCP模式和最佳实践构建的FastMCP框架,该服务器提供:
✅ 官方FastMCP集成 - 使用最新的FastMCP框架以获得最佳性能
✅ 正确的MCP传输 - 支持stdio、SSE和可流式传输的HTTP传输
✅ 模块化架构 - 清晰的责任分离和依赖注入
✅ 生产就绪 - 健全的错误处理、全面的日志记录和类型安全
如果你觉得它有用,请在GitHub上给它一个⭐!
stdio进行了优化,开箱即用支持流行的LLM编排器。git clone https://github.com/gujord/OpenAPI-MCP.git
cd OpenAPI-MCP
python3.12 -m venv venv
source venv/bin/activate # 在Windows上:venv\Scripts\activate
pip install -r requirements.txt
选项1:快速测试(挪威天气API)
# 激活虚拟环境
source venv/bin/activate
# 运行天气API服务器
OPENAPI_URL="https://api.met.no/weatherapi/locationforecast/2.0/swagger" \
SERVER_NAME="weather" \
python src/fastmcp_server.py
选项2:HTTP传输(推荐用于Claude桌面)
# 启动带有HTTP传输的天气API
source venv/bin/activate && \
OPENAPI_URL="https://api.met.no/weatherapi/locationforecast/2.0/swagger" \
SERVER_NAME="weather" \
MCP_HTTP_ENABLED="true" \
MCP_HTTP_PORT="8001" \
python src/fastmcp_server.py
1. 复制提供的配置:
cp claude_desktop_config.json ~/Library/Application\ Support/Claude/claude_desktop_config.json
2. 启动天气服务器:
source venv/bin/activate && \
OPENAPI_URL="https://api.met.no/weatherapi/locationforecast/2.0/swagger" \
SERVER_NAME="weather" \
MCP_HTTP_ENABLED="true" \
MCP_HTTP_PORT="8001" \
python src/fastmcp_server.py
3. 在Claude桌面中测试:
weather_get__compact工具!同时运行多个OpenAPI服务:
# 终端1:天气API
source venv/bin/activate && \
OPENAPI_URL="https://api.met.no/weatherapi/locationforecast/2.0/swagger" \
SERVER_NAME="weather" \
MCP_HTTP_ENABLED="true" \
MCP_HTTP_PORT="8001" \
python src/fastmcp_server.py
# 终端2:宠物商店API
source venv/bin/activate && \
OPENAPI_URL="https://petstore3.swagger.io/api/v3/openapi.json" \
SERVER_NAME="petstore" \
MCP_HTTP_ENABLED="true" \
MCP_HTTP_PORT="8002" \
python src/fastmcp_server.py
使用Docker快速启动:
# 启动所有服务(天气+宠物商店)
./docker-start.sh
# 或手动
docker-compose up --build -d
这将自动运行:
HTTP传输(推荐):
使用提供的配置文件:
cp claude_desktop_config.json ~/Library/Application\ Support/Claude/claude_desktop_config.json
或手动创建:
{
"mcpServers": {
"weather": {
"command": "npx",
"args": ["mcp-remote", "http://127.0.0.1:8001/sse"]
},
"petstore": {
"command": "npx",
"args": ["mcp-remote", "http://127.0.0.1:8002/sse"]
}
}
}
Stdio传输(替代方案):
{
"mcpServers": {
"weather": {
"command": "/full/path/to/OpenAPI-MCP/venv/bin/python",
"args": ["/full/path/to/OpenAPI-MCP/src/fastmcp_server.py"],
"env": {
"SERVER_NAME": "weather",
"OPENAPI_URL": "https://api.met.no/weatherapi/locationforecast/2.0/swagger"
},
"transport": "stdio"
}
}
}
注意:替换
/full/path/to/OpenAPI-MCP为你实际的安装路径。
{
"mcpServers": {
"local_api": {
"command": "/full/path/to/OpenAPI-MCP/venv/bin/python",
"args": ["/full/path/to/OpenAPI-MCP/src/fastmcp_server.py"],
"env": {
"SERVER_NAME": "local_api",
"OPENAPI_URL": "./specs/my-api.yaml",
"MCP_AUTH_HEADERS": "{\"X-API-Key\": \"your-key-here\"}"
},
"transport": "stdio"
}
}
}
{
"mcpServers": {
"secure_api": {
"command": "full_path_to_openapi_mcp/venv/bin/python",
"args": ["full_path_to_openapi_mcp/src/server.py"],
"env": {
"SERVER_NAME": "secure_api",
"OPENAPI_URL": "https://api.example.com/openapi.json",
"API_USERNAME": "your_username",
"API_PASSWORD": "your_password"
},
"transport": "stdio"
}
}
}
{
"mcpServers": {
"oauth_api": {
"command": "full_path_to_openapi_mcp/venv/bin/python",
"args": ["full_path_to_openapi_mcp/src/server.py"],
"env": {
"SERVER_NAME": "oauth_api",
"OPENAPI_URL": "https://api.example.com/openapi.json",
"OAUTH_CLIENT_ID": "your_client_id",
"OAUTH_CLIENT_SECRET": "your_client_secret",
"OAUTH_TOKEN_URL": "https://api.example.com/oauth/token"
},
"transport": "stdio"
}
}
}
配置多个OpenAPI服务以同时运行:
{
"mcpServers": {
"weather": {
"command": "npx",
"args": [
"mcp-remote",
"http://127.0.0.1:8001/sse"
]
},
"petstore": {
"command": "npx",
"args": [
"mcp-remote",
"http://127.0.0.1:8002/sse"
]
}
}
}
此配置允许Claude同时访问天气数据和宠物商店API工具,具有明确的工具命名如weather_get__compact和petstore_addPet。
对于单个API服务:
标准SSE配置:
{
"mcpServers": {
"openapi_service": {
"command": "npx",
"args": [
"mcp-remote",
"http://127.0.0.1:8001/sse"
]
}
}
}
可流式传输的HTTP配置:
{
"mcpServers": {
"openapi_service": {
"command": "npx",
"args": [
"mcp-remote",
"http://127.0.0.1:8001/mcp"
]
}
}
}
带调试(用于开发):
{
"mcpServers": {
"openapi_service": {
"command": "npx",
"args": [
"mcp-remote",
"http://127.0.0.1:8001/sse",
"--debug"
]
}
}
}
带自定义传输策略:
{
"mcpServers": {
"openapi_service": {
"command": "npx",
"args": [
"mcp-remote",
"http://127.0.0.1:8001/mcp",
"--transport",
"streamable-http"
]
}
}
}
{
"mcpServers": {
"streaming_api": {
"command": "full_path_to_openapi_mcp/venv/bin/python",
"args": ["full_path_to_openapi_mcp/src/server.py"],
"env": {
"SERVER_NAME": "streaming_api",
"OPENAPI_URL": "https://api.example.com/openapi.json",
"SSE_ENABLED": "true",
"SSE_HOST": "127.0.0.1",
"SSE_PORT": "8001"
},
"transport": "stdio"
}
}
}
将此配置应用于以下文件:
~/.cursor/mcp.json~/.codeium/windsurf/mcp_config.json~/Library/Application Support/Claude/claude_desktop_config.json替换
full_path_to_openapi_mcp为你实际的安装路径。
复制提供的示例配置:
cp claude_desktop_config.json ~/Library/Application\ Support/Claude/claude_desktop_config.json
启动两个服务:
# 终端1
source venv/bin/activate && \
OPENAPI_URL="https://api.met.no/weatherapi/locationforecast/2.0/swagger" \
SERVER_NAME="weather" \
MCP_HTTP_ENABLED="true" \
MCP_HTTP_PORT="8001" \
python src/fastmcp_server.py
# 终端2
source venv/bin/activate && \
OPENAPI_URL="https://petstore3.swagger.io/api/v3/openapi.json" \
SERVER_NAME="petstore" \
MCP_HTTP_ENABLED="true" \
MCP_HTTP_PORT="8002" \
python src/fastmcp_server.py
结果:Claude可以访问带有前缀工具名的天气和宠物商店API。
| 变量 | 描述 | 是否必需 | 默认值 |
|---|---|---|---|
OPENAPI_URL | OpenAPI规范的URL或本地文件路径 | 是 | - |
SERVER_NAME | MCP服务器名称 | 否 | openapi_proxy_server |
| 变量 | 描述 | 是否必需 | 默认值 |
|---|---|---|---|
OAUTH_CLIENT_ID | OAuth客户端ID | 否 | - |
OAUTH_CLIENT_SECRET | OAuth客户端密钥 | 否 | - |
OAUTH_TOKEN_URL | OAuth令牌端点URL | 否 | - |
OAUTH_SCOPE | OAuth范围 | 否 | api |
| 变量 | 描述 | 是否必需 | 默认值 |
|---|---|---|---|
API_USERNAME | 认证使用的API用户名 | 否 | - |
API_PASSWORD | 认证使用的API密码 | 否 | - |
API_LOGIN_ENDPOINT | 登录端点URL | 否 | 自动检测 |
| 变量 | 描述 | 是否必需 | 默认值 |
|---|---|---|---|
MCP_AUTH_HEADERS | 自定义认证头(JSON或键值对格式) | 否 | - |
| 变量 | 描述 | 是否必需 | 默认值 |
|---|---|---|---|
MCP_HTTP_ENABLED | 启用MCP HTTP传输 | 否 | false |
MCP_HTTP_HOST | MCP HTTP服务器主机 | 否 | 127.0.0.1 |
MCP_HTTP_PORT | MCP HTTP服务器端口 | 否 | 8000 |
MCP_CORS_ORIGINS | CORS源(逗号分隔) | 否 | * |
MCP_MESSAGE_SIZE_LIMIT | 消息大小限制 | 否 | 4mb |
MCP_BATCH_TIMEOUT | 批处理超时(秒) | 否 | 30 |
MCP_SESSION_TIMEOUT | 会话超时(秒) | 否 | 3600 |
| 变量 | 描述 | 是否必需 | 默认值 |
|---|---|---|---|
SSE_ENABLED | 启用SSE流支持 | 否 | false |
SSE_HOST | SSE服务器主机 | 否 | 127.0.0.1 |
SSE_PORT | SSE服务器端口 | 否 | 8000 |
现在可以从本地文件系统加载OpenAPI规范,而不需要远程URL:
source venv/bin/activate
OPENAPI_URL="./specs/my-api.json" \
SERVER_NAME="local_api" \
python src/fastmcp_server.py
source venv/bin/activate
OPENAPI_URL="../shared/api.yaml" \
SERVER_NAME="local_api" \
python src/fastmcp_server.py
source venv/bin/activate
OPENAPI_URL="/Users/myuser/projects/api-spec.json" \
SERVER_NAME="local_api" \
python src/fastmcp_server.py