Swagger MCP Server
Automatically converts Swagger 2.0 API specifications into MCP tools, enabling AI assistants to directly call REST APIs with full parameter support, type validation, and comprehensive HTTP method coverage
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Swagger MCP Serverget the list of users from the petstore API"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
Swagger 2.0 MCP 服务器
这是一个基于 Model Context Protocol (MCP) 的服务器,可以自动将 Swagger 2.0 API 规范转换为 MCP 工具,让 AI 助手能够直接调用 REST API。
功能特性
🔄 自动转换: 自动将 Swagger 2.0 规范转换为 MCP 工具
📝 完整参数支持: 支持路径参数、查询参数、请求头、表单数据和请求体
🛡️ 类型验证: 基于 Swagger 规范进行参数类型验证
🌐 HTTP 方法支持: 支持 GET、POST、PUT、DELETE 等所有 HTTP 方法
📊 响应处理: 智能处理 JSON 和文本响应
🚨 错误处理: 完善的错误处理和状态码报告
Related MCP server: Swagger to MCP
安装
方式一:通过 npm 安装(推荐)
# 全局安装
npm install -g swagger-mcp
# 或者使用 npx(无需安装)
npx swagger-mcp --url https://petstore.swagger.io/v2/swagger.json方式二:从源码安装
克隆项目:
git clone <repository-url>
cd swagger-mcp安装依赖:
npm install准备你的
swagger.json文件,放在项目根目录下。
使用方法
启动服务器
使用本地 Swagger 文件
npm start或者使用启动脚本:
./start.sh使用远程 Swagger URL
# 直接使用 Node.js
node src/index.js --url https://petstore.swagger.io/v2/swagger.json
# 或者使用启动脚本
./start.sh --url https://petstore.swagger.io/v2/swagger.json
# 也可以使用 --swagger 参数
./start.sh --swagger https://api.example.com/docs/swagger.json开发模式(支持热重载)
npm run devURL 参数支持
服务器支持以下方式指定 Swagger 文档:
本地文件(默认):从项目根目录的
swagger.json文件读取远程 URL:通过
--url或--swagger参数指定远程 Swagger 文档
远程 URL 功能特性
✅ 自动验证: 验证 URL 格式和响应内容
✅ 超时控制: 可配置的请求超时时间(默认 10 秒)
✅ 缓存支持: 可选择将远程文档缓存到本地
✅ 错误处理: 详细的错误信息和重试机制
✅ 安全控制: 可在配置中禁用远程 URL 功能
配置选项
在 config.json 中可以配置以下选项:
{
"swagger": {
"allowRemoteUrl": true, // 是否允许远程URL
"urlTimeout": 10000, // URL请求超时时间(ms)
"cacheRemoteSpec": true // 是否缓存远程文档
}
}Swagger 文件格式
确保你的 swagger.json 文件符合 Swagger 2.0 规范。示例结构:
{
"swagger": "2.0",
"info": {
"title": "My API",
"version": "1.0.0"
},
"host": "api.example.com",
"basePath": "/v1",
"schemes": ["https"],
"paths": {
"/users": {
"get": {
"summary": "获取用户列表",
"parameters": [
{
"name": "limit",
"in": "query",
"type": "integer",
"required": false
}
]
}
}
}
}工具命名规则
MCP 工具的命名遵循以下规则:
格式:
{HTTP方法}_{路径}路径中的
/替换为_路径参数的
{}被移除
示例:
GET /users→GET_usersPOST /users/{id}/posts→POST_users_id_postsPUT /articles/{articleId}→PUT_articles_articleId
参数处理
服务器支持以下类型的参数:
路径参数 (Path Parameters)
{
"name": "id",
"in": "path",
"required": true,
"type": "integer"
}查询参数 (Query Parameters)
{
"name": "limit",
"in": "query",
"required": false,
"type": "integer"
}请求头 (Header Parameters)
{
"name": "Authorization",
"in": "header",
"required": true,
"type": "string"
}表单数据 (Form Data)
{
"name": "file",
"in": "formData",
"required": true,
"type": "file"
}请求体 (Body Parameters)
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"type": "object",
"properties": {
"name": { "type": "string" },
"email": { "type": "string" }
}
}
}响应格式
服务器返回的响应包含:
HTTP 状态码
响应数据(JSON 或文本格式)
示例响应:
状态码: 200
响应数据: {
"id": 1,
"name": "John Doe",
"email": "john@example.com"
}错误处理
服务器会处理以下类型的错误:
Swagger 文件读取错误
无效的工具名称
API 调用失败
网络连接错误
所有错误都会以 MCP 错误格式返回,包含详细的错误信息。
示例
本地示例
项目包含一个示例 swagger.json 文件,使用 JSONPlaceholder API 进行演示。你可以直接运行服务器来测试以下功能:
获取所有文章:
GET_posts根据 ID 获取文章:
GET_posts_id创建新文章:
POST_posts更新文章:
PUT_posts_id删除文章:
DELETE_posts_id获取用户列表:
GET_users根据 ID 获取用户:
GET_users_id
远程 URL 示例
你可以使用以下公开的 Swagger API 进行测试:
1. Swagger Petstore API
./start.sh --url https://petstore.swagger.io/v2/swagger.json2. JSONPlaceholder API
./start.sh --url https://jsonplaceholder.typicode.com/swagger.json3. 自定义 API
# 使用你自己的 API 文档
./start.sh --url https://your-api.com/swagger.json
# 或者使用不同的端点路径
./start.sh --swagger https://your-api.com/api-docs在 Cursor 中使用
1. 配置 MCP 服务器
在 Cursor 的 MCP 配置文件中(通常位于 ~/.cursor/mcp.json)添加以下配置:
{
"mcpServers": {
"swagger-mcp": {
"command": "npx",
"args": [
"swagger-mcp",
"--url",
"https://petstore.swagger.io/v2/swagger.json"
]
}
}
}2. 多个 API 配置示例
{
"mcpServers": {
"petstore-api": {
"command": "npx",
"args": [
"swagger-mcp",
"--url",
"https://petstore.swagger.io/v2/swagger.json"
]
},
"my-custom-api": {
"command": "npx",
"args": [
"swagger-mcp",
"--swagger",
"https://your-api.com/swagger.json"
]
},
"local-swagger": {
"command": "npx",
"args": ["swagger-mcp"]
}
}
}3. 重启 Cursor
配置完成后,重启 Cursor 即可使用 Swagger API 工具。
使用流程示例
配置 MCP 服务器: 在 Cursor 中配置 MCP 服务器(如上所示)
重启 Cursor: 重启后 Cursor 会自动连接到 MCP 服务器
使用 API 工具: 在 Cursor 中直接调用 API,例如:
询问:"帮我查找状态为 available 的宠物"
询问:"创建一个新的宠物记录"
询问:"获取商店库存信息"
开发
项目结构
swagger-mcp/
├── src/
│ └── index.js # 主服务器文件
├── swagger.json # Swagger API 规范
├── package.json # 项目配置
└── README.md # 项目文档自定义配置
你可以通过修改 swagger.json 文件来配置不同的 API:
更新
host字段指向你的 API 服务器修改
basePath设置 API 基础路径添加或修改
paths定义你的 API 端点更新
definitions定义数据模型
发布到 npm
如果您想发布自己的版本:
修改包名: 在
package.json中修改name字段为您的包名登录 npm:
npm login发布包:
npm publish使用发布的包:
npx your-package-name --url https://your-api.com/swagger.json
许可证
MIT License
贡献
欢迎提交 Issue 和 Pull Request!
故障排除
常见问题
1. 远程 URL 无法访问
❌ 无法从远程URL获取Swagger文档: HTTP错误: 404 Not Found解决方案:
检查 URL 是否正确
确认 API 文档是否公开可访问
检查网络连接
2. URL 格式错误
❌ 无效的URL格式: invalid-url解决方案:
确保 URL 以
http://或https://开头检查 URL 拼写是否正确
3. 配置不允许远程 URL
❌ 配置不允许使用远程URL解决方案:
在
config.json中设置"allowRemoteUrl": true
4. 请求超时
❌ 远程URL请求超时 (10000ms)解决方案:
检查网络连接
在
config.json中增加urlTimeout值尝试使用其他网络环境
5. Swagger 文档格式错误
❌ 不是有效的Swagger 2.0文档解决方案:
确认文档是 Swagger 2.0 格式(不是 OpenAPI 3.0)
检查 JSON 格式是否正确
验证必需字段是否存在
调试技巧
启用详细日志: 在
config.json中设置"enableConsole": true检查缓存文件: 如果启用了缓存,检查生成的
swagger-cache-*.json文件测试 URL 可访问性:
curl -H "Accept: application/json" https://your-api.com/swagger.json验证 Swagger 文档: 使用在线工具如 Swagger Editor 验证文档格式
支持
如果你遇到任何问题,请:
检查上述故障排除指南
确认
swagger.json文件或远程 URL 格式是否正确确认 API 服务器是否可访问
查看控制台错误信息
提交 Issue 描述问题详情,包括:
使用的命令
错误信息
Swagger 文档 URL(如果使用远程 URL)
Available Tools
3 toolsapi_callC
调用示例API API的通用工具。支持所有HTTP方法和路径。
| Name | Required | Description | Default |
|---|---|---|---|
| method | Yes | HTTP方法 | |
| path | Yes | API路径,例如: /users/{id} 或 /posts | |
| pathParams | No | 路径参数,例如: {"id": "123"} | |
| queryParams | No | 查询参数,例如: {"limit": 10, "offset": 0} | |
| headers | No | 请求头,例如: {"Authorization": "Bearer token"} | |
| body | No | 请求体数据(用于POST/PUT等方法) |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool supports all HTTP methods and paths, implying it can perform various operations (e.g., GET, POST, DELETE), but does not disclose critical behavioral traits such as authentication requirements, rate limits, error handling, or whether it's safe or destructive. For a general-purpose API tool with no annotations, this is a significant gap in transparency.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is concise with two sentences that directly state the tool's general function and capabilities. It is front-loaded and wastes no words, though it could be slightly more informative without losing efficiency. The structure is clear but minimal, earning a high score for brevity and directness.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
Given the tool's complexity (6 parameters, no output schema, no annotations), the description is incomplete. It lacks details on behavioral aspects like authentication, error handling, or response formats, which are crucial for a general API tool. Without annotations or output schema, the description should provide more context to guide the agent effectively, but it falls short, leaving significant gaps in understanding.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
Schema description coverage is 100%, with detailed descriptions for all parameters in the input schema (e.g., 'HTTP方法' for method, 'API路径' for path). The description adds no additional meaning beyond the schema, as it only mentions supporting all HTTP methods and paths without explaining parameter interactions or usage. With high schema coverage, the baseline score of 3 is appropriate, as the schema adequately documents parameters.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description states the tool '调用示例API API的通用工具' (calls example API general tool) and '支持所有HTTP方法和路径' (supports all HTTP methods and paths), which provides a vague purpose. It mentions the general function but lacks specificity about what resources it operates on or how it differs from siblings like 'api_get_endpoint_info' and 'api_list_endpoints'. The description is not tautological but remains broad without clear differentiation.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
The description provides no guidance on when to use this tool versus alternatives. It does not mention any context, prerequisites, or exclusions, such as when to choose this over sibling tools like 'api_get_endpoint_info' for endpoint details or 'api_list_endpoints' for listing. Without explicit or implied usage instructions, the agent lacks direction on appropriate tool selection.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
api_get_endpoint_infoC
获取示例API API特定端点的详细信息
| Name | Required | Description | Default |
|---|---|---|---|
| method | Yes | HTTP方法 | |
| path | Yes | API路径 |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool retrieves detailed information, implying a read-only operation, but doesn't specify what '详细信息' (detailed information) includes—such as response formats, error handling, rate limits, or authentication needs. For a tool with zero annotation coverage, this leaves significant gaps in understanding its behavior.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, efficient sentence: '获取示例API API特定端点的详细信息'. It's front-loaded with the core action and resource, with zero wasted words. Every part of the sentence contributes directly to clarifying the tool's purpose, making it highly concise and well-structured.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
Given the complexity (a tool with 2 required parameters, no annotations, and no output schema), the description is incomplete. It doesn't explain what '详细信息' entails—such as the structure or type of information returned—leaving the agent uncertain about the tool's output. For a tool that retrieves endpoint details, more context on the expected result is needed to be fully helpful.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
The input schema has 100% description coverage, with parameters 'method' (HTTP method with enum values) and 'path' (API path) clearly documented. The description adds no additional meaning beyond what the schema provides—it doesn't explain parameter interactions, examples, or constraints. With high schema coverage, the baseline score of 3 is appropriate, as the description doesn't compensate but also doesn't detract.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states the tool's purpose: '获取示例API API特定端点的详细信息' (Get detailed information about a specific endpoint of the example API). It specifies the verb ('获取' - get) and resource ('API特定端点的详细信息' - detailed information about a specific API endpoint), making the intent unambiguous. However, it doesn't explicitly differentiate from sibling tools like 'api_list_endpoints' (which likely lists endpoints rather than getting details for one).
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools like 'api_call' (which might invoke an endpoint) or 'api_list_endpoints' (which might list endpoints), nor does it specify prerequisites, contexts, or exclusions for usage. The agent must infer usage from the tool name and description alone.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
api_list_endpointsC
列出示例API API的所有可用端点
| Name | Required | Description | Default |
|---|---|---|---|
| filter | No | 过滤端点的关键词(可选) | |
| method | No | 按HTTP方法过滤(可选) |
TDQS
Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?
No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool lists endpoints but doesn't describe behavioral traits like whether it's a read-only operation, if it requires authentication, rate limits, pagination, or the format of the returned data. For a tool with no annotations, this leaves significant gaps in understanding how it behaves.
Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.
Is the description appropriately sized, front-loaded, and free of redundancy?
The description is a single, efficient sentence in Chinese: '列出示例API API的所有可用端点'. It is front-loaded with the core action and resource, with no unnecessary words or redundancy. Every part of the sentence contributes directly to the tool's purpose, making it highly concise and well-structured.
Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.
Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?
Given the lack of annotations and output schema, the description is incomplete for effective tool use. It doesn't explain what the output looks like (e.g., list format, data structure), behavioral aspects like safety or performance, or how it relates to sibling tools. For a tool with no structured metadata, the description should provide more context to compensate, but it falls short.
Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.
Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?
The input schema has 100% description coverage, with clear documentation for both parameters ('filter' and 'method'), including an enum for 'method'. The description adds no additional meaning beyond what the schema provides, such as examples or usage tips. With high schema coverage, the baseline score of 3 is appropriate, as the schema adequately handles parameter semantics.
Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.
Does the description clearly state what the tool does and how it differs from similar tools?
The description clearly states the tool's purpose: '列出示例API API的所有可用端点' (List all available endpoints of the example API API). It specifies the verb '列出' (list) and resource '端点' (endpoints), making the action clear. However, it doesn't explicitly differentiate from sibling tools like 'api_get_endpoint_info', which might provide detailed information about a specific endpoint.
Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.
Does the description explain when to use this tool, when not to, or what alternatives exist?
The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools such as 'api_call' or 'api_get_endpoint_info', nor does it specify any prerequisites, exclusions, or contextual cues for usage. The agent must infer usage based on the tool name and description alone.
Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
3 tool updates
- First observed
api_call - First observed
api_get_endpoint_info - First observed
api_list_endpoints
TDQS
Scored across 3 tools
Each tool has a clearly distinct purpose with no overlap. 'api_list_endpoints' lists available endpoints, 'api_get_endpoint_info' provides details for a specific endpoint, and 'api_call' performs actual API calls. The descriptions clearly differentiate their functions, making misselection unlikely.
All tools follow a consistent 'api_verb_noun' naming pattern (api_list_endpoints, api_get_endpoint_info, api_call). The structure is uniform throughout, using snake_case and starting with 'api_' as a prefix, which enhances predictability and readability.
With only 3 tools, the set feels thin for a server named 'Swagger MCP Server', which implies broader API documentation or management capabilities. While the tools cover basic operations (list, get info, call), the scope suggests more tools might be expected for comprehensive API interaction, such as schema validation or endpoint testing.
The tools provide a logical workflow: list endpoints, get endpoint details, and make API calls, covering core operations for interacting with an API. However, there are minor gaps, such as no tools for managing API schemas, authentication, or error handling, which could limit advanced use cases but are workable for basic needs.
Maintenance
Related MCP Connectors
MCP server for AI access to Swagger by SmartBear.
Pay-per-use tool marketplace for AI agents. Search, price-check, and call APIs via MCP.
The OpenRouter for tools. One MCP connection gives any AI agent 254 hosted tools, pay per call.
Build, validate, deploy — HTTP APIs, cron jobs, webhooks and MCP tools — from your AI client.
Related MCP Servers
- -licenseNot gradedqualityNot gradedmaintenanceDynamically generates MCP tools from Swagger/OpenAPI specifications by extracting swagger.json files at runtime. Enables natural language interaction with any REST API that has Swagger documentation.-
- AlicenseNot gradedqualityNot gradedmaintenanceAutomatically converts Swagger/OpenAPI specifications into dynamic MCP tools, enabling interaction with any REST API through natural language by loading specs from local files or URLs.-
- AlicenseNot gradedqualityCmaintenanceConverts any OpenAPI/Swagger API specification into MCP tools that AI assistants can use to interact with the API.17 npm7MIT
- AlicenseNot gradedqualityDmaintenanceConverts any REST API into MCP-compatible tools instantly by providing an OpenAPI/Swagger spec, enabling seamless integration with AI agents.MIT