一个 Model Context Protocol (MCP) 服务器,用于加载和提供多个 OpenAPI 规范,以实现由 LLM 支持的 IDE 集成。该服务器充当您的 OpenAPI 规范与 Cursor 等 LLM 支持的开发工具之间的桥梁。
这个开源的 MCP 服务器由 ReAPI 赞助,ReAPI 是一个下一代 API 平台,简化了 API 设计和测试。虽然此服务器为开发提供了本地 OpenAPI 集成,但 ReAPI 提供了两个强大的模块:
在 reapi.com 免费试用 ReAPI,体验未来的 API 开发。
要将 MCP OpenAPI 服务器与 Cursor IDE 集成,您有两种配置位置选择:
在您的项目目录中创建一个 .cursor/mcp.json
文件。我们推荐此选项,因为它允许您为不同的项目维护不同的规范集
{
"mcpServers": {
"@reapi/mcp-openapi": {
"command": "npx",
"args": ["-y", "@reapi/mcp-openapi@latest", "--dir", "./specs"],
"env": {}
}
}
}

提示:使用相对路径(如
./specs
)可以使配置更便携,并且更容易在团队成员之间共享。注意:我们建议使用
@latest
标签,因为我们经常更新服务器,添加新功能和改进。重要:项目特定配置有助于管理 LLM 上下文限制。当所有规范都放在一个文件夹中时,合并后的元数据可能会超出 LLM 的上下文窗口,导致错误。按项目组织规范可以保持上下文大小可控。
在您的主目录中创建或编辑 ~/.cursor/mcp.json
,使服务器在所有项目中可用:
{
"mcpServers": {
"@reapi/mcp-openapi": {
"command": "npx",
"args": ["-y", "@reapi/mcp-openapi@latest", "--dir", "/path/to/your/specs"],
"env": {}
}
}
}

添加配置后:
注意: 默认情况下,Cursor 需要对每个 MCP 工具执行进行确认。如果您希望允许自动执行而无需确认,可以在 Cursor 设置中启用 Yolo 模式。
服务器现在已经可以使用了。当您向目录中添加新的 OpenAPI 规范时,可以通过以下步骤刷新目录:
"请刷新 API 目录" "重新加载 OpenAPI 规范"
将您的 OpenAPI 3.x 规范放置在目标目录中:
.json
、.yaml
或 .yml
扩展名规范 ID 配置:
x-spec-id
:openapi: 3.0.0
info:
title: My API
version: 1.0.0
x-spec-id: my-custom-api-id # 自定义规范 ID

重要: 当处理具有以下情况的多个规范时,设置自定义
x-spec-id
至关重要:
- 类似或相同的端点路径
- 相同的模式名称
- 重叠的操作 ID
规范 ID 有助于区分这些相似的资源,并防止命名冲突。例如:
# user-service.yaml info: x-spec-id: user-service paths: /users: get: ... # admin-service.yaml info: x-spec-id: admin-service paths: /users: get: ...
现在您可以将这些端点具体引用为
user-service/users
和admin-service/users
refresh-api-catalog
get-api-catalog
search-api-operations
query
(字符串):搜索查询specId
(可选字符串):要搜索的具体 API 规范 IDsearch-api-schemas
query
(字符串):搜索查询specId
(可选字符串):要搜索的具体 API 规范 IDload-api-operation-by-operationId
specId
(字符串):API 规范 IDoperationId
(字符串):要加载的操作 IDload-api-operation-by-path-and-method
specId
(字符串):API 规范 IDpath
(字符串):API 端点路径method
(字符串):HTTP 方法load-api-schema-by-schemaName
specId
(字符串):API 规范 IDschemaName
(字符串):要加载的模式名称语义搜索
远程规范同步
代码模板
社区贡献
以下是一些您可以在 Cursor IDE 中使用的示例提示,以便与您的 API 进行交互:
探索可用的 API
"Show me all available APIs in the catalog with their operations" "List all API specifications and their endpoints"
API 操作详情
"Show me the details of the create pet API endpoint" "What are the required parameters for creating a new pet?" "Explain the response schema for the pet creation endpoint"
模式和模拟数据
"Generate mock data for the Pet schema" "Create a valid request payload for the create pet endpoint" "Show me examples of valid pet objects based on the schema"
代码生成
"Generate an Axios client for the create pet API" "Create a TypeScript interface for the Pet schema" "Write a React hook that calls the create pet endpoint"
API 集成辅助
"Help me implement error handling for the pet API endpoints" "Generate unit tests for the pet API client" "Create a service class that encapsulates all pet-related API calls"
文档和使用
"Show me example usage of the pet API with curl" "Generate JSDoc comments for the pet API client methods" "Create a README section explaining the pet API integration"
验证和类型
"Generate Zod validation schema for the Pet model" "Create TypeScript types for all pet-related API responses" "Help me implement request payload validation for the pet endpoints"
API 搜索和发现
"Find all endpoints related to pet management" "Show me all APIs that accept file uploads" "List all endpoints that return paginated responses"
这些提示展示了如何利用 MCP 服务器的功能进行 API 开发。请根据您的具体需求自由调整或组合它们以完成更复杂的任务。
欢迎贡献!请随时提交 Pull Request。