使用说明
openapi到mcp
从任何OpenAPI规范生成一个精简的MCP服务器。一个命令-给它一个规范URL或文件,为每个API端点获得一个完整的TypeScript MCP服务器和工具。
快速开始
npx @softwaresavants/openapi-to-mcp https://petstore3.swagger.io/api/v3/openapi.json
就是这样。您现在有一个可用的MCP服务器 ./swagger-petstore-openapi-3-0-mcp/.
cd swagger-petstore-openapi-3-0-mcp
npm install
cp .env.example .env # fill in your API key
npm run build
npm start
用法
# From a URL
openapi-to-mcp https://api.example.com/openapi.json
# From a local file (JSON or YAML)
openapi-to-mcp ./api-spec.yaml
# Custom output directory and name
openapi-to-mcp spec.json -o ./my-mcp-server --name my-product
# Only include specific endpoints
openapi-to-mcp spec.json --include "/pets/**" "/users/**"
# Exclude endpoints
openapi-to-mcp spec.json --exclude "/admin/**" "/internal/**"
# Overwrite existing output
openapi-to-mcp spec.json -o ./existing-dir --force
程序化API
在您自己的工具中用作库:
import { generateMcpServer } from "@softwaresavants/openapi-to-mcp";
// Parse only (no file output)
const { spec } = await generateMcpServer("https://api.example.com/openapi.json");
console.log(`Found ${spec.tools.length} tools`);
// Parse + generate
const { files } = await generateMcpServer("https://api.example.com/openapi.json", {
outputDir: "./my-mcp-server",
name: "my-product",
include: ["/pets/**"],
});
它产生了什么
基于 mcp起动器 模板:
my-product-mcp/
├── package.json
├── tsconfig.json
├── .env.example # Pre-filled with your API's base URL
├── .gitignore
├── README.md
└── src/
├── index.ts # Server entry — all tools registered
├── auth.ts # API key + OAuth token auth
├── types.ts # ApiConfig type
├── oauth-provider.ts # Proxy OAuth provider
├── transports/
│ ├── stdio.ts # Stdio transport (default)
│ ├── http.ts # HTTP + OAuth transport
│ └── sse.ts # SSE transport (dev/testing)
└── tools/
├── list_pets.ts # One file per API operation
├── get_pet.ts
└── ...
三种运输方式
生成的服务器支持所有三种开箱即用的传输模式:
npm start # Stdio — API key auth (default, for Claude Desktop)
npm run start:http # HTTP — OAuth 2.1 (production, multi-user)
npm run start:sse # SSE — API key auth (dev/testing, browser clients)
设计理念
此工具生成以下MCP服务器 精益设计:
- 令牌高效描述。 工具描述的长度限制为80个字符。工具描述中的每个字符都会在每个LLM请求上花费令牌——臃肿的描述会浪费金钱和上下文窗口。
- 每次操作一个工具。 每个API端点都成为一个具有干净Zod模式的MCP工具。没有神的工具,没有超级处理者。
- 开箱即用OAuth。 生成的HTTP传输使用
ProxyOAuthServerProvider来自MCP SDK——使用PKCE的真正OAuth 2.1,而不是占位符代码。 - 兼容克劳德桌面。 工具名称通过碰撞检测自动截断为64个字符。生成的服务器与Claude Desktop一起工作,无需手动修复。
- 满的
$ref取消引用。 用途@apidevtools/swagger-parser解决所有问题$ref引用,包括深度嵌套和跨文件引用。没有“缺失模式”的意外。 - 过滤器,不要膨胀。 使用
--include/--exclude只生成重要的工具。5-10个工具是理想的——更少的工具意味着LLM每次都会选择正确的工具。
例子
检查 examples/ 您可以浏览的完整生成服务器的目录:
| 示例 | 源代码规范 | 工具 | 描述 |
|---|---|---|---|
petstore-mcp |
Swagger宠物店 | 19 | 经典宠物店--CRUD宠物、订单、用户 |
notion-mcp |
API通知 | 13 | 块、数据库、页面、用户 |
每个示例都是一个完整的、经过类型检查的项目——浏览 src/tools/ 查看生成的工具代码是什么样子的。
选项
| 标志 | 描述 | 默认值 |
|---|---|---|
-o, --output <dir> |
输出目录 | ./<server-name>-mcp |
-n, --name <name> |
服务器名称 | 来自规范 info.title |
--include <patterns...> |
仅包含与这些globs匹配的路径 | 所有路径 |
--exclude <patterns...> |
排除与这些glob匹配的路径 | 无 |
--force |
覆盖现有输出目录 | 提示 |
-h, --help |
显示帮助 |
需要生产MCP服务器吗?
此工具可生成一个坚实的起点。适用于具有自定义身份验证、权限和持续维护的生产级MCP服务器-- 和我们聊聊.
软件野人 --AI原生产品工作室。我们构建了精简的MCP服务器,不会使上下文窗口膨胀。
许可证
麻省理工学院
