OpenAPI MCP服务器

从OpenAPI规范文件生成模型上下文协议(MCP)服务器,使AI模型能够通过MCP工具与定义的API端点交互。

  • by yutashx
  • TypeScript
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 yutashx/openapi-mcp-server

使用说明

OpenAPI MCP服务器

此项目从OpenAPI(v3)规范文件生成模型上下文协议(MCP)服务器。它允许像Claude这样的人工智能模型通过MCP工具与定义的API端点进行交互。 所有源代码均由生成 克莱恩 和gemini-2.5-pro使用 此需求定义

日文版文档

需求

安装

  1. 克隆或下载此项目。
  2. 导航到项目目录:
    cd openapi-mcp-server
    
  3. 安装依赖项:
    bun install
    

建筑

将TypeScript代码编译为可执行的JavaScript,以Bun运行时为目标(这是必要的,因为依赖关系像 node-fetch 使用Node.js API):

bun build ./src/index.ts --outdir ./dist --target=bun

这将在中创建输出文件 dist 目录,为Bun环境正确捆绑。

手动运行(用于测试/调试)

您可以直接从终端运行编译后的服务器。这对于测试解析和基本功能很有用,但除非配置,否则它不会连接到MCP客户端。

bun run ./dist/index.js <path-to-openapi-spec> [optional-base-url]

论据:

  • <path-to-openapi-spec>:(必填)OpenAPI规范文件(JSON或YAML)的绝对或相对路径。
  • [optional-base-url]:(可选)用于覆盖的基本URL servers[0].url 在OpenAPI规范中定义。如果省略并且在规范中找不到服务器URL,服务器将退出并出错。

例子:

# Using the included sample spec
bun run ./dist/index.js ./openapi.yaml

# Using a different spec and overriding the base URL
bun run ./dist/index.js /path/to/my/api.json https://my-api.example.com/v2

服务器将把日志打印到stderr,并在stdin/stdout上监听MCP通信。按 Ctrl+C 阻止它。

配置为MCP服务器(例如,用于克劳德桌面/VS代码扩展)

要使此服务器可用于像Claude VS Code扩展这样的MCP客户端,请将其配置添加到相应的设置文件中。

示例 cline_mcp_settings.json (VS代码):

位置可以变化。, ~/Library/Application Support/Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json 是macOS。

{
  "mcpServers": {
    "my-custom-api": { // Choose a unique name for your server
      "command": "bun",
      "args": [
        "run",
        // Use the *absolute path* to the compiled index.js
        "/path/to/your/openapi-mcp-server/dist/index.js",
        // Use the *absolute path* to your OpenAPI spec
        "/path/to/your/openapi-mcp-server/openapi.yaml"
        // Add the base URL override here if needed
        // "https://override-base-url.com"
      ],
      "env": {
        // Add any necessary environment variables here (e.g., API keys)
        // "API_KEY": "your-secret-key"
      },
      "disabled": false, // Set to true to temporarily disable
      "autoApprove": [] // List tool names to auto-approve if desired
    }
    // ... other servers
  }
}

重要提示:

  • 替换 /path/to/your/openapi-mcp-server/ 使用系统上此项目目录的实际绝对路径。
  • 确保路径 dist/index.js OpenAPI规范文件是正确的绝对路径。
  • MCP客户端(VS代码扩展等)将根据此配置自动启动和管理服务器进程。通过MCP使用时,您不需要手动运行它。

配置后,客户端将连接到服务器,您可以使用生成的工具(如 listPets, showPetById 通过要求AI执行相应的操作,从示例规范中获取。