Skip to main content
Glama
api2mcp
by BaoPHP

api2mcp

把任意 OpenAPI 3.x 服务变成 AI 智能体能调用的 MCP Server。

你有一个 REST API、一份 Swagger 文档,想让 Cursor / Claude Code / WorkBuddy 里的智能体直接调用它——不用手写一个工具函数,指向那份 OpenAPI 定义就行。

零依赖 Node.js 18+,单文件运行。当前版本 v0.2.0。

v0.2 新增了"接真实系统"必需的能力:环境变量密钥注入(不落盘)、5xx/429 指数退避重试、结构化错误解析、OpenAPI 3.1 / Pydantic anyOf 完整支持、鉴权方案自动提示。


30 秒试用

# 列出从 OpenAPI 定义生成了哪些工具
node server.js --spec examples/petstore.openapi.json --tools

# 以 MCP Server 模式启动(默认只暴露只读 GET 工具)
node server.js --spec examples/petstore.openapi.json \
  --base-url https://petstore3.swagger.io/api/v3

第一个命令会真实输出 8 个工具:

findpetsbystatus  GET /pet/findByStatus  Finds Pets by status.
getpetbyid        GET /pet/{petId}       Find pet by ID.
getinventory      GET /store/inventory   Returns pet inventories by status.
...

Related MCP server: OMCP

接入自有系统

任何有 OpenAPI 定义的服务都能接。以一个 FastAPI 服务(voicebox,22 个操作)为例:

node server.js \
  --spec /path/to/your-service/openapi.json \
  --base-url http://127.0.0.1:8000 \
  --allow-write \
  --secret "Authorization=Bearer $TOKEN"

下面的工具名和验证结果来自一次真实接入(见 案例);把 --spec 换成你自己的 OpenAPI 定义即可。

生成后的工具名简洁可用:

list_profiles   GET  /profiles
create_profile  POST /profiles
generate_speech POST /generate
transcribe_audio POST /transcribe

如果你的项目是 FastAPI 自动生成的 operationId(如 list_profiles_profiles_get),工具会自动简化为 list_profiles。


安全设计

行为

默认

GET / HEAD / OPTIONS

暴露

POST / PUT / DELETE / PATCH

不暴露,需显式 --allow-write

HTTP 超时

15 秒,可 --timeout 调整

失败重试

5xx/429 指数退避,默认 0 次,可 --retry 开启

错误反馈

上游错误体结构化解析(detail/message 等)

鉴权头

推荐 --secret 从环境变量读取,明文不落盘

stdout

仅 JSON-RPC 消息,日志走 stderr

写操作默认关闭是刻意的:让智能体动手改数据之前,人要先明确授权一次。

密钥安全:用 --secret 而不是 --header

--header 适合非敏感头;密钥务必用 --secret,值从环境变量读取,不出现在命令行历史、不写进生成物:

# 方式一:完整值来自环境变量
export API_TOKEN="xxx"
node server.js --spec ./openapi.json --secret "Authorization=Bearer $API_TOKEN"

# 方式二:带前缀拼接
node server.js --spec ./openapi.json --secret "X-API-Key=$API_KEY"

API 定义了 securitySchemes 时,--tools 会自动列出该配哪种鉴权及对应命令。

失败重试

真实 API 会抖动。开启重试后,5xx / 429 / 408 及网络错误会自动指数退避重试,429 还会遵守 Retry-After 响应头:

node server.js --spec ./openapi.json --retry 3 --retry-delay 500

返回结果里的 attempts 字段显示实际请求次数,4xx 不会重试。


参数

参数

说明

--spec <path>

OpenAPI 3.x JSON 文件路径(必填)

--base-url <url>

API 根地址;spec 里有绝对 server url 时可省略

--name <name>

Server 名称,默认取 info.title

--header "K: V"

注入请求头,可重复

--allow-write

允许生成写操作工具

--timeout <ms>

HTTP 超时,默认 15000

--secret "K=$ENV"

从环境变量读取密钥注入请求头(不落盘)

--retry <n>

5xx/429 重试次数,默认 0

--retry-delay <ms>

重试初始退避,默认 500

--verbose

stderr 输出每次请求(不打印密钥)

--tools

列出将生成的工具后退出(验收用)

--emit <dir>

生成独立可运行的 server 到指定目录

$ref 与请求体

  • 自动解析 #/components/schemas/... 引用和 allOf 组合。

  • JSON 请求体的字段会展开成独立参数,并标注 required,智能体能直接填。

  • path 参数替换、query 拼接、body 序列化都按 OpenAPI 规范处理。


生成独立 Server

node server.js --spec ./openapi.json --base-url https://api.example.com --emit ./my-mcp
cd my-mcp && node server.js

生成目录包含 server.js、api2mcp.config.json、README.md。改 API 地址或鉴权头直接编辑配置文件即可。


接入编辑器

把生成的服务配置到客户端 MCP 配置:

{
  "mcpServers": {
    "my-api": {
      "command": "node",
      "args": ["/absolute/path/to/api2mcp/server.js",
               "--spec", "/absolute/path/to/openapi.json",
               "--base-url", "https://api.example.com"]
    }
  }
}

随后在对话里说"用 my-api 查一下……"即可。


验证

node test/smoke.js

覆盖:真实 MCP 协议握手、只读安全约束、path/query/body 参数映射、自定义请求头、HTTP 转发、Petstore 真实规范、--emit 自举、voicebox 22 个操作接入、5xx/429 重试、错误体解析、--secret 环境变量注入、3.1 nullable / Pydantic anyOf、鉴权提示、生成物密钥不落盘。HTTP 全部走本地 mock,不依赖外网。

当前基线:18/18 PASS。

另有 voicebox 接入案例专项验证:

node test/voicebox-case.js

6/6 PASS——对 --emit 生成的独立 server 走完整 MCP 调用。完整案例见 docs/VOICEBOX_CASE.md。


许可

MIT

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      C
      maintenance
      Converts any OpenAPI 3.x spec into a live MCP server, making every endpoint a validated tool that AI agents can call without writing glue code.
      12 npm
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Turns any OpenAPI 3.x spec into an MCP server that exposes one validated tool per API operation, so AI agents can discover and call a REST API over stdio or Streamable HTTP. It is read-only by default, with allow/deny lists, environment-based auth, timeouts and response truncation for safe agent access.
      MIT