OpenAPI转MCP工具

将OpenAPI规范转换为实时MCP服务器,为变更操作提供内置确认提示。

  • by Cl0v1s
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 Cl0v1s/openapi-mcpify

使用说明

openapi mcpify

将任何OpenAPI规范转换为实时MCP服务器,并内置用于变异操作的确认提示。

它做什么

openapi-mcpify 读取OpenAPI 3.x规范,为每条路由生成一个MCP工具,并通过stdio公开它们。当AI代理尝试呼叫 POST, PUT,或 DELETE 路由,它会触发一个MCP启发对话框,要求用户在实际发送HTTP请求之前进行确认。

OpenAPI spec  →  MCP tools  →  AI agent calls  →  (confirm if mutating)  →  HTTP request

使用(用户)

与bunx

bunx openapi-mcpify --spec ./openapi.json --url https://api.example.com

参数

参数 必填 说明
--spec yes OpenAPI 3.x规范的路径或URL(JSON或YAML)
--url yes 目标API的基本URL
--default-args no JSON字符串注入到每个工具调用中(对auth标头有用)
--disable-methods no 要排除的逗号分隔的HTTP方法(例如。 post,delete)

示例

# Basic usage
dist/index.js --spec ./petstore.json --url https://petstore.example.com

# With an auth header injected by default
dist/index.js \
  --spec ./petstore.json \
  --url https://petstore.example.com \
  --default-args '{"headers": {"Authorization": "Bearer mytoken"}}'

# Read-only mode: disable all mutating methods
dist/index.js \
  --spec ./petstore.json \
  --url https://petstore.example.com \
  --disable-methods post,put,delete,patch

克劳德桌面/MCP客户端配置

将此添加到MCP客户端配置中(例如。 claude_desktop_config.json):

{
  "mcpServers": {
    "petstore": {
      "command": "dist/index.js",
      "args": [
        "--spec", "/path/to/openapi.json",
        "--url", "https://api.example.com",
        "--default-args", "{\"headers\":{\"Authorization\":\"Bearer mytoken\"}}"
      ]
    }
  }
}

发展

先决条件

安装

bun install

构建

bun run build

编译后的输出在 dist/.

项目结构

src/
  index.ts   — entry point, wires CLI args → OpenAPI → MCP server
  cli.ts     — argument parser
  swager.ts  — OpenAPI spec loader & route extractor
  tool.ts    — OpenAPI route → MCP tool + HTTP call + elicitation logic

工具是如何生成的

规范中的每条路线都成为一个名为的MCP工具 {METHOD}_{path} (例如。 GET_pets__id_).其输入模式基于以下内容构建:

  • 路径参数 → 必填字段
  • 查询参数 → 根据规范,可选或必需
  • 请求体 (application/json) → body 领域
  • headers → 始终可选,与合并 --default-args 标头

激发(确认对话框)

对于 POST, PUT,以及 DELETE 路由,服务器调用 server.elicitInput() 在执行HTTP请求之前。客户端(例如Claude Desktop)显示一个确认表单,显示方法、路径和参数。只有当用户确认时,才会发送请求。


许可证

麻省理工学院