一个纯JavaScript实现的Model Context Protocol (MCP)服务器,它将OpenAPI 3.x规范转换为MCP工具,使大型语言模型(LLMs)能够通过标准化的MCP协议与REST API进行交互。
npm install
node src/index.js validate -s UserQueueList.json
node src/index.js serve -s UserQueueList.json -b https://api.example.com -t your-bearer-token
serve - 启动MCP服务器node src/index.js serve [options]
选项:
-s, --spec <path> OpenAPI规范文件路径(必需)
-b, --base-url <url> API请求的基础URL
-t, --token <token> 认证的Bearer Token
--timeout <ms> 请求超时时间(毫秒,默认:30000)
--transport <type> 传输类型(stdio或http,默认:stdio)
--http-port <port> HTTP服务器端口(当使用http传输时,默认:3000)
--http-host <host> HTTP服务器主机(当使用http传输时,默认:localhost)
validate - 验证OpenAPI规范node src/index.js validate -s <path-to-spec>
info - 显示服务器信息node src/index.js info
您可以使用环境变量代替命令行参数:
export OPENAPI_BASE_URL=https://api.example.com
export OPENAPI_BEARER_TOKEN=your-bearer-token
node src/index.js serve -s UserQueueList.json
node src/index.js serve -s UserQueueList.json --transport=http --http-port=8020
.json扩展名){
"components": {
"securitySchemes": {
"bearer": {
"type": "http",
"scheme": "bearer"
}
}
},
"security": [
{
"bearer": []
}
]
}
服务器会自动将OpenAPI操作转换为MCP工具:
operationIdsummary(已清理){method}_{path}(已清理)header_前缀requestBody参数对于UserQueueList.json规范:
// 工具:ListUsers (GET /domains/~/users/list)
{
"name": "ListUsers",
"description": "列出域中的用户基本信息",
"inputSchema": {
"type": "object",
"properties": {},
"required": []
}
}
// 工具:ListCallqueues (GET /domains/~/callqueues/list)
{
"name": "ListCallqueues",
"description": "读取域中的呼叫队列基本信息",
"inputSchema": {
"type": "object",
"properties": {},
"required": []
}
}
添加到您的Claude Desktop配置(~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"openapi-server": {
"command": "node",
"args": [
"/path/to/your/project/src/index.js",
"serve",
"-s", "/path/to/UserQueueList.json",
"-b", "https://your-api-domain.com",
"-t", "your-bearer-token"
]
}
}
}
对于HTTP传输,配置服务器URL:
{
"mcpServers": {
"openapi-server": {
"url": "http://localhost:3000/message"
}
}
}
然后单独启动服务器:
node src/index.js serve -s UserQueueList.json --transport http --http-port 3000
服务器返回结构化的JSON响应:
成功响应:
{
"status": 200,
"statusText": "OK",
"data": {
// API响应数据
}
}
错误响应:
{
"error": true,
"status": 404,
"statusText": "未找到",
"message": "域未找到",
"data": {
"code": 404,
"message": "示例域不存在"
}
}
基于UserQueueList.json规范:
// 工具调用
{
"name": "ListUsers",
"arguments": {}
}
// 响应:包含基本信息的用户对象数组
// 工具调用
{
"name": "ListCallqueues",
"arguments": {}
}
// 响应:包含配置的呼叫队列对象数组
src/
├── index.js # CLI入口点
├── server.js # MCP服务器实现
├── openapi-processor.js # OpenAPI规范处理器
├── http-client.js # API请求的HTTP客户端
└── utils.js # 实用函数
服务器提供全面的错误处理:
MIT许可 - 查看LICENSE文件获取详细信息
为了详细的日志记录,您可以修改服务器以启用调试输出:
# 服务器将日志输出到stderr以兼容MCP
node src/index.js serve -s UserQueueList.json -b https://api.example.com -t token 2>debug.log
在使用之前始终验证您的OpenAPI规范:
node src/index.js validate -s UserQueueList.json
这将显示: