mcp-openapi

mcp-openapi

Turn any OpenAPI/Swagger spec into MCP tools. Zero config, zero code. Supports Swagger 2.0, OpenAPI 3.x, Bearer/API-key/OAuth2 auth, flat parameter schemas for better LLM accuracy, and smart response truncation.

Category
访问服务器

README

mcp-openapi

Turn any OpenAPI/Swagger spec into MCP tools — so Claude and other AI assistants can call your REST APIs.

npm version License: MIT npm downloads

Point mcp-openapi at any OpenAPI 3.x or Swagger 2.0 spec URL and it generates Model Context Protocol (MCP) tools automatically. No code generation, no config files, no boilerplate. Your AI assistant gets callable tools for every API endpoint in seconds.


Quick Start

1. Run it (no install required):

npx mcp-openapi --spec https://petstore3.swagger.io/api/v3/openapi.json

2. Add it to Claude Desktop (claude_desktop_config.json):

{
  "mcpServers": {
    "petstore": {
      "command": "npx",
      "args": [
        "mcp-openapi",
        "--spec", "https://petstore3.swagger.io/api/v3/openapi.json"
      ]
    }
  }
}

3. Ask Claude to use it:

"List all available pets in the store"

Claude sees MCP tools like find_pets_by_status, get_pet_by_id, add_pet and calls them directly.


Why mcp-openapi?

Most MCP-to-API bridges require you to write tool definitions by hand or generate code from a spec. mcp-openapi skips all of that.

Feature mcp-openapi Hand-written MCP servers Generic HTTP tools
Zero config setup Yes No Partial
OpenAPI 3.x + Swagger 2.0 Yes N/A N/A
Flat parameter schemas (LLM-optimized) Yes Manual No
Smart tool naming from operationId Yes Manual No
Auth (API key, Bearer, OAuth2) Built-in DIY DIY
Retry with exponential backoff Built-in DIY DIY
Response truncation for LLM context Built-in DIY No

Flat parameter schemas are the key differentiator. Instead of passing nested JSON objects (which LLMs frequently get wrong), mcp-openapi flattens path, query, header, and body parameters into a single flat object. This dramatically improves tool-calling accuracy.


How It Works

OpenAPI/Swagger Spec          mcp-openapi               AI Assistant
     (URL or file)                                      (Claude, etc.)
          |                         |                         |
          |   1. Parse & validate   |                         |
          |------------------------>|                         |
          |                         |                         |
          |   2. Generate MCP tools |                         |
          |   (one per endpoint)    |                         |
          |------------------------>|                         |
          |                         |                         |
          |                         |   3. Register tools     |
          |                         |   via stdio transport   |
          |                         |------------------------>|
          |                         |                         |
          |                         |   4. AI calls a tool    |
          |                         |<------------------------|
          |                         |                         |
          |   5. Build & execute    |                         |
          |   HTTP request          |                         |
          |<------------------------|                         |
          |                         |                         |
          |   6. Return truncated   |                         |
          |   response to AI        |                         |
          |------------------------>|------------------------>|

Each API endpoint becomes one MCP tool:

  • Tool name is derived from operationId (converted to snake_case) or from method + path
  • Parameters are flattened into a single input schema (path, query, header, and body params merged)
  • Responses are truncated to ~50KB to stay within LLM context limits
  • Errors (429, 5xx) trigger automatic retries with exponential backoff (up to 3 retries)

Claude Desktop Integration

Add any API to Claude Desktop by editing your config file:

Location:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json

Public API (no auth)

{
  "mcpServers": {
    "petstore": {
      "command": "npx",
      "args": [
        "mcp-openapi",
        "--spec", "https://petstore3.swagger.io/api/v3/openapi.json"
      ]
    }
  }
}

API with Bearer Token

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": [
        "mcp-openapi",
        "--spec", "https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.json",
        "--auth-type", "bearer",
        "--auth-token", "$GITHUB_TOKEN",
        "--prefix", "github",
        "--include", "listReposForAuthenticatedUser,getRepo,listIssues,createIssue"
      ],
      "env": {
        "GITHUB_TOKEN": "ghp_your_token_here"
      }
    }
  }
}

API with API Key

{
  "mcpServers": {
    "weather": {
      "command": "npx",
      "args": [
        "mcp-openapi",
        "--spec", "https://api.weather.example.com/openapi.json",
        "--auth-type", "api-key",
        "--auth-name", "X-API-Key",
        "--auth-value", "$WEATHER_API_KEY",
        "--auth-in", "header"
      ],
      "env": {
        "WEATHER_API_KEY": "your_key_here"
      }
    }
  }
}

CLI Reference

npx mcp-openapi --spec <url-or-path> [options]

General Options

Option Short Default Description
--spec <url|path> -s required OpenAPI spec URL or local file path
--config <path> -c JSON config file path
--base-url <url> from spec Override the API base URL
--prefix <name> Prefix for all tool names (e.g. github -> github_list_repos)
--include <patterns> all Comma-separated operationIds to include
--exclude <patterns> none Comma-separated operationIds to exclude
--timeout <ms> 30000 HTTP request timeout in milliseconds
--max-retries <n> 3 Max retries on 429/5xx responses
--header <name:value> -H Custom header (repeatable)
--transport <type> stdio Transport type: stdio or sse
--port <n> 3000 Port for SSE transport
--help -h Show help
--version -v Show version
--license-key <key> Pro license key (or $MCP_OPENAPI_LICENSE_KEY env)

Auth Options

Bearer token:

Option Description
--auth-type bearer Use Bearer token authentication
--auth-token <token> The token value (supports $ENV_VAR syntax)

API key:

Option Description
--auth-type api-key Use API key authentication
--auth-name <name> Header or query parameter name
--auth-value <value> The API key value (supports $ENV_VAR syntax)
--auth-in <header|query> Where to send the key (default: header)

OAuth2 client credentials:

Option Description
--auth-type oauth2 Use OAuth2 client credentials flow
--auth-client-id <id> OAuth2 client ID
--auth-client-secret <secret> OAuth2 client secret
--auth-token-url <url> Token endpoint URL
--auth-scopes <scopes> Comma-separated scopes

CLI Examples

# Basic usage with a remote spec
npx mcp-openapi --spec https://petstore3.swagger.io/api/v3/openapi.json

# Local YAML spec with Bearer auth
npx mcp-openapi --spec ./api.yaml --auth-type bearer --auth-token '$API_KEY'

# Filter to specific endpoints with a prefix
npx mcp-openapi --spec ./api.json --prefix myapi --include 'listUsers,getUser'

# Override base URL (useful for local dev)
npx mcp-openapi --spec https://api.example.com/openapi.json --base-url http://localhost:3000

# Add custom headers
npx mcp-openapi --spec ./api.json -H 'X-Custom: value' -H 'X-Another: value2'

# Use a JSON config file
npx mcp-openapi --config ./mcp-config.json

Config File Format

Instead of CLI flags, you can use a JSON config file:

{
  "spec": "https://api.example.com/openapi.json",
  "prefix": "myapi",
  "include": ["listUsers", "getUser", "createUser"],
  "auth": {
    "type": "bearer",
    "token": "$API_TOKEN"
  },
  "timeout": 15000,
  "maxRetries": 2,
  "headers": {
    "X-Custom-Header": "value"
  }
}

CLI arguments take precedence over config file values.


Supported Specs

Format Versions File types
OpenAPI 3.0.x, 3.1.x .json, .yaml, .yml
Swagger 2.0 .json, .yaml, .yml

Specs can be loaded from:

  • Remote URLs (https://...)
  • Local file paths (./api.yaml, /absolute/path/spec.json)

Pro Features (v0.2.0+)

mcp-openapi includes optional Pro features for teams and power users, gated by a license key.

Custom Response Transforms

Shape API responses with JMESPath expressions before they reach the LLM — reducing token usage and improving accuracy:

{
  "spec": "https://api.github.com/openapi.json",
  "licenseKey": "$MCP_OPENAPI_LICENSE_KEY",
  "transforms": {
    "list_repos": "data[].{name: name, stars: stargazers_count, url: html_url}",
    "list_*": "data[].{id: id, name: name}"
  }
}

Smart Response Handling

Instead of hard-truncating large responses at 50KB, Pro enables intelligent truncation:

  • Array slicing: Large arrays show first N items + metadata ("showing 10 of 847 items")
  • Depth pruning: Deep nested objects are summarized beyond a configurable depth
  • Structure preservation: You always see the shape of the data, never a mid-JSON cut
{
  "spec": "./api.json",
  "licenseKey": "$MCP_OPENAPI_LICENSE_KEY",
  "response": {
    "maxLength": 50000,
    "arraySliceSize": 10,
    "maxDepth": 4
  }
}

Coming Soon

  • Multi-API Composition — Load multiple OpenAPI specs into one MCP session
  • Usage Analytics — Track tool calls, latency, and error rates

Interested in Pro? Star the repo and open an issue to get early access.


Programmatic Usage

You can also use mcp-openapi as a library in your own MCP server:

import { createServer } from 'mcp-openapi';

const { server, tools, spec } = await createServer({
  spec: 'https://petstore3.swagger.io/api/v3/openapi.json',
  prefix: 'petstore',
  auth: {
    type: 'bearer',
    token: process.env.API_TOKEN,
  },
});

console.log(`Loaded ${tools.length} tools from ${spec.info.title}`);

Requirements

  • Node.js 18 or later
  • An OpenAPI 3.x or Swagger 2.0 spec (URL or local file)

Contributing

Contributions are welcome. Here is how to get started:

git clone https://github.com/Docat0209/mcp-openapi.git
cd mcp-openapi
pnpm install
pnpm test
pnpm build

Before submitting a PR:

  1. Add tests for new features
  2. Run pnpm lint and fix any issues
  3. Follow Conventional Commits for commit messages

Related

License

MIT


Keywords

mcp, model-context-protocol, openapi, swagger, claude, ai, llm, api, tools, rest-api, ai-tools, mcp-server

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选