使用说明
openapi到mcp
将任何OpenAPI/Swagger规范转换为MCP服务器,立即将您的REST API公开为Claude的工具。
uvx openapi-to-mcp serve ./spec.json --base-url https://api.example.com -a bearer --bearer-token $TOKEN
安装
无需安装 --只需使用 uvx:
uvx openapi-to-mcp --help
或全局安装:
uv tool install openapi-to-mcp
快速开始
# See what tools will be generated
uvx openapi-to-mcp list-endpoints ./openapi.json
# Run MCP server
uvx openapi-to-mcp serve ./openapi.json --base-url https://api.example.com
认证
支持所有OpenAPI/Swagger安全方案:
# HTTP Basic
uvx openapi-to-mcp serve spec.json -b https://api.example.com \
-a basic -u user -p pass
# Bearer Token
uvx openapi-to-mcp serve spec.json -b https://api.example.com \
-a bearer --bearer-token $TOKEN
# API Key (header, query, or cookie)
uvx openapi-to-mcp serve spec.json -b https://api.example.com \
-a api-key-header --api-key $KEY --api-key-name X-API-Key
# OAuth2 Password (cookie-based login)
uvx openapi-to-mcp serve spec.json -b https://api.example.com \
-a oauth2-password -u user -p pass --login-url /auth/login
# OAuth2 Password (token-based)
uvx openapi-to-mcp serve spec.json -b https://api.example.com \
-a oauth2-password -u user -p pass --token-url /oauth/token
# OAuth2 Client Credentials
uvx openapi-to-mcp serve spec.json -b https://api.example.com \
-a oauth2-client --client-id $ID --client-secret $SECRET --token-url /oauth/token
添加到克劳德代码
增添 ~/.claude.json 或 .mcp.json:
{
"mcpServers": {
"my-api": {
"command": "uvx",
"args": [
"openapi-to-mcp",
"serve",
"/path/to/spec.json",
"--base-url", "https://api.example.com",
"--auth-type", "bearer",
"--bearer-token", "your-token"
]
}
}
}
或者生成一个配置:
uvx openapi-to-mcp generate-config ./spec.json \
--base-url https://api.example.com \
--server-name my-api \
-a bearer --bearer-token $TOKEN
筛选端点
大型API可以公开数百个工具。使用标签过滤器:
# Only include specific tags
uvx openapi-to-mcp serve spec.json -b https://api.example.com \
--include-tag Users --include-tag Orders
# Exclude tags
uvx openapi-to-mcp serve spec.json -b https://api.example.com \
--exclude-tag Admin --exclude-tag Internal
命令
| 命令 | 描述 |
|---|---|
serve |
为OpenAPI规范运行MCP服务器 |
list-endpoints |
预览端点和生成的工具名称 |
inspect |
以JSON格式显示完整的工具定义 |
generate-config |
生成克劳德代码MCP配置 |
工具命名
端点转换为干净的工具名称:
| 方法 | 路径 | 工具名称 |
|---|---|---|
| GET | /用户 | list_users |
| POST | /用户 | create_user |
| GET | /users/{id} | get_user |
| PATCH | /users/{id} | update_user |
| 删除 | /users/{id} | delete_user |
运作原理
- 解析 --读取OpenAPI 3.x规范(JSON或YAML、文件或URL)
- 生成 --将每个端点转换为具有正确输入模式的MCP工具
- 验证 --处理登录流,存储令牌/Cookie
- 服务 --运行stdio MCP服务器,在工具调用时执行HTTP调用
许可证
麻省理工学院
