Skip to main content

MCP RESTful Adapter

将 RESTful API(Swagger 2.0 / OpenAPI 3.0)自动转换为 MCP Server。每个 API 端点变成一个 MCP Tool。

快速开始

1. 在 Claude Desktop / Cursor / Claude Code 中使用

编辑配置文件(Claude Desktop: ~/Library/Application Support/Claude/claude_desktop_config.json):

从 PyPI 安装(推荐):

{
  "mcpServers": {
    "petstore": {
      "command": "uvx",
      "args": ["mcp-restful-adapter"],
      "env": {
        "API_SPEC_URL": "https://petstore3.swagger.io/api/v3/openapi.json",
        "API_BASE_URL": "https://petstore3.swagger.io/api/v3",
        "API_TAGS": "pet,store"
      }
    }
  }
}

本地开发(未发布时):

{
  "mcpServers": {
    "petstore": {
      "command": "uv",
      "args": [
        "run",
        "--project", "/path/to/mcp-restful-adapter",
        "mcp-restful-adapter"
      ],
      "env": {
        "API_SPEC_URL": "https://petstore3.swagger.io/api/v3/openapi.json",
        "API_BASE_URL": "https://petstore3.swagger.io/api/v3",
        "API_TAGS": "pet,store"
      }
    }
  }
}

将 /path/to/mcp-restful-adapter 替换为本项目的实际路径。

配置完成后,Claude 就能直接调用 Petstore 的 API 了:

  • findPetsByStatus(status="available") → 查询可用宠物
  • getPetById(petId=1) → 查询宠物详情
  • addPet(name="Buddy", ...) → 创建宠物

2. 在终端中使用

# PyPI 版本
API_SPEC_URL=https://petstore3.swagger.io/api/v3/openapi.json \
API_BASE_URL=https://petstore3.swagger.io/api/v3 \
API_TAGS=pet \
uvx mcp-restful-adapter

# 本地开发
API_SPEC_URL=https://petstore3.swagger.io/api/v3/openapi.json \
API_BASE_URL=https://petstore3.swagger.io/api/v3 \
API_TAGS=pet \
uv run mcp-restful-adapter

3. 带认证的 API

{
  "mcpServers": {
    "my-api": {
      "command": "uvx",
      "args": ["mcp-restful-adapter"],
      "env": {
        "API_SPEC_URL": "https://your-api.example.com/openapi.json",
        "API_BASE_URL": "https://your-api.example.com",
        "API_HEADERS": "{\"Authorization\": \"Bearer your-bearer-token\", \"X-Tenant\": \"acme\"}"
      }
    }
  }
}

环境变量

变量 说明 默认值
API_SPEC_URL OpenAPI/Swagger 文档地址 必填
API_BASE_URL 后端 API 地址 必填
API_TAGS Tag 白名单,逗号分隔,OR 逻辑(匹配任一 tag 即保留) 全部
API_METHODS HTTP 方法白名单,逗号分隔 全部
API_PATHS 路径正则白名单(re.search 匹配) 全部
API_PATHS_EXCLUDE 路径正则黑名单(白名单优先,见下方说明) 无
API_HEADERS 自定义请求头(JSON 格式,含认证等) 空
LOG_LEVEL 日志级别(INFO 显示启动信息,DEBUG 记录请求头和 body) WARNING

白名单 / 黑名单策略

API_PATHS(白名单)和 API_PATHS_EXCLUDE(黑名单)配合使用,白名单优先:

白名单命中 → 放行(不再检查黑名单)
白名单未命中 + 黑名单命中 → 排除
白名单未命中 + 黑名单未命中 → 排除
仅配置黑名单(无白名单) → 未命中黑名单的放行

推荐配置 — 只暴露只读查询接口:

{
  "API_METHODS": "GET",
  "API_PATHS": "(?i)(get|query|detail|page|select|list|search|info|find|fetch|tree|export|download|template|count|stat|check|\\{[a-zA-Z]+\\})",
  "API_PATHS_EXCLUDE": "(?i)(save|update|delete|remove|create|add|clear|sync|generate|import|handle|flush|send|complete|fix|reset|process|submit|approve|cancel|enable|disable|trigger|notify|push|pull|upload|revoke|grant|bind|unbind|transfer|lock|unlock|batch)"
}

过滤示例

# 只要 GET 请求
API_METHODS=GET

# 只要 pet 或 store 相关的端点
API_TAGS=pet,store

# 只要 /api/v1/ 开头的路径
API_PATHS=^/api/v1/

# 排除包含 delete/remove 的路径(黑名单)
API_PATHS_EXCLUDE=(?i)(delete|remove)

# 组合使用:pet tag 下的 GET 和 POST
API_TAGS=pet
API_METHODS=GET,POST

# 只读查询(白名单 + 黑名单 + GET)
API_METHODS=GET
API_PATHS=(?i)(get|query|detail|page|list|search)
API_PATHS_EXCLUDE=(?i)(save|update|delete|remove|create)

开发

uv sync                                    # 安装依赖
uv run mcp-restful-adapter                 # 运行
uv run pytest tests/ -v                    # 运行测试
uv run pytest tests/ --cov=mcp_restful_adapter  # 覆盖率

示例代码

文件 说明
examples/agent_usage.py Agent 中使用 — 连接 MCP Server、列出 tools、调用 tools
examples/claude_desktop_config.json Claude Desktop 配置模板
uv run python examples/agent_usage.py

Metadata

Release files for mcp-restful-adapter 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mcp-restful-adapter 0.5.0
File Size Uploaded Provenance Actions
mcp_restful_adapter-0.5.0.tar.gz 26.0 kB Without provenance Details for mcp_restful_adapter-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mcp-restful-adapter 0.5.0
File Interpreter ABI Platform Actions
mcp_restful_adapter-0.5.0-py3-none-any.whl Python 3 none any Details for mcp_restful_adapter-0.5.0-py3-none-any.whl

Total release size: 40.8 kB

Release files / mcp_restful_adapter-0.5.0.tar.gz

Download URL mcp_restful_adapter-0.5.0.tar.gz
Size 26.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1eedb354f98c4b8c3d59c0c20d6c2a0cd4fae12cc2c828660a34876f705c22ef
BLAKE2b-256 checksum
How to use checksums
6e2b07d278ce9ae8450b5e61da34c8df39f7b90b95d00b1b1c083f88f154178a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / mcp_restful_adapter-0.5.0-py3-none-any.whl

Download URL mcp_restful_adapter-0.5.0-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf342ac85dc0ce7153399de48b90bd7155dc00bf9f68cdfe7619702e0b6815f1
BLAKE2b-256 checksum
How to use checksums
c664f3c0d0324b5eb9fef22c068fa7ddf743c1c9a0169d1fce5723b2c20e3e6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Security for mcp-restful-adapter 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page