Skip to main content
Glama
gujord

OpenAPI to Model Context Protocol (MCP)

by gujord

OpenAPI 到模型上下文协议 (MCP)

许可证:MIT 回购规模最后提交未解决的问题Python 版本

OpenAPI-MCP 代理将 OpenAPI 规范转换为 MCP 工具,使 AI 代理无需自定义包装器即可访问外部 API!

OpenAPI-MCP

弥合人工智能代理与外部 API 之间的差距

OpenAPI 到模型上下文协议 (MCP) 代理服务器通过将 OpenAPI 规范动态转换为标准化的MCP 工具资源提示,弥合了 AI 代理与外部 API 之间的差距。这消除了对自定义 API 包装器的需求,从而简化了集成。


如果您发现它有用,请在 GitHub 上给它一个⭐!


Related MCP server: OpenAPI to MCP

主要特点

  • **FastMCP 传输:**针对stdio进行了优化,可与流行的 LLM 协调器一起开箱即用。

  • **OpenAPI 集成:**解析并注册 OpenAPI 操作作为可调用工具。

  • **资源注册:**自动将 OpenAPI 组件模式转换为具有定义 URI 的资源对象。

  • **提示生成:**根据 API 操作生成上下文提示,以指导 LLM 使用 API。

  • **OAuth2 支持:**通过客户端凭证流处理机器身份验证。

  • **JSON-RPC 2.0 支持:**完全兼容的请求/响应结构。

  • **自动元数据:**从 OpenAPI 规范中获取工具名称、摘要和模式。

  • **清理工具名称:**确保与 MCP 名称约束兼容。

  • **灵活的参数解析:**支持查询字符串(以“?”为前导)和多种 JSON 变体(包括带点和数字值的键)。

  • **增强的参数处理:**自动将参数转换为正确的数据类型。

  • **扩展工具元数据:**包括详细的参数信息和响应模式。

快速入门

安装

git clone https://github.com/gujord/OpenAPI-MCP.git
cd OpenAPI-MCP
pip install -r requirements.txt

LLM Orchestrator 配置

对于Claude DesktopCursorWindsurf ,使用下面的代码片段并相应地调整路径:

{
  "mcpServers": {

    "petstore3": {
      "command": "full_path_to_openapi_mcp/venv/bin/python",
      "args": ["full_path_to_openapi_mcp/src/server.py"],
      "env": {
        "SERVER_NAME": "petstore3",
        "OPENAPI_URL": "https://petstore3.swagger.io/api/v3/openapi.json"
      },
      "transport": "stdio"
    }

  }
}

将此配置应用到以下文件:

  • 游标: ~/.cursor/mcp.json

  • 风帆冲浪: ~/.codeium/windsurf/mcp_config.json

  • Claude 桌面: ~/Library/Application Support/Claude/claude_desktop_config.json

full_path_to_openapi_mcp替换为您的实际安装路径。

环境配置

多变的

描述

必需的

默认

OPENAPI_URL

OpenAPI 规范的 URL

是的

-

SERVER_NAME

MCP 服务器名称

openapi_proxy_server

OAUTH_CLIENT_ID

OAuth 客户端 ID

-

OAUTH_CLIENT_SECRET

OAuth 客户端机密

-

OAUTH_TOKEN_URL

OAuth 令牌端点 URL

-

OAUTH_SCOPE

OAuth 范围

api

工作原理

  1. **解析 OpenAPI 规范:**如果需要,使用httpxPyYAML加载 OpenAPI 规范。

  2. **注册操作:**提取 API 操作并生成具有适当输入和响应模式的 MCP 兼容工具。

  3. **资源注册:**自动将 OpenAPI 组件模式转换为具有分配 URI 的资源对象(例如, /resource/{name} )。

  4. **提示生成:**根据 API 操作创建上下文提示,以帮助 LLM 了解 API 的使用情况。

  5. **身份验证:**通过客户端凭证流支持 OAuth2 身份验证。

  6. **参数处理:**将参数转换为所需的数据类型,并支持灵活的查询字符串和 JSON 格式。

  7. **JSON-RPC 2.0 合规性:**确保工具交互的标准通信协议。

sequenceDiagram
    participant LLM as LLM (Claude/GPT)
    participant MCP as OpenAPI-MCP Proxy
    participant API as External API

    Note over LLM, API: Communication Process

    LLM->>MCP: 1. Initialize (initialize)
    MCP-->>LLM: Metadata, tools, resources, and prompts

    LLM->>MCP: 2. Request tools (tools_list)
    MCP-->>LLM: Detailed list of tools, resources, and prompts

    LLM->>MCP: 3. Call tool (tools_call)

    alt With OAuth2
        MCP->>API: Request OAuth2 token
        API-->>MCP: Access Token
    end

    MCP->>API: 4. Execute API call with proper formatting
    API-->>MCP: 5. API response (JSON)

    alt Type Conversion
        MCP->>MCP: 6. Convert parameters to correct data types
    end

    MCP-->>LLM: 7. Formatted response from API

    alt Dry Run Mode
        LLM->>MCP: Call with dry_run=true
        MCP-->>LLM: Display request information without executing call
    end

资源与提示

除了工具之外,代理服务器现在还会自动注册:

  • **资源:**从 OpenAPI 组件模式派生而来的资源对象通过定义的 URI(例如/resource/{name} )进行注册,以进行结构化数据处理。

  • **提示:**根据 API 操作生成上下文提示,为 LLM 提供使用指导,增强他们对可用端点的理解。

此扩展元数据通过提供全面的 API 上下文来改善集成。

OpenAPI-MCP

贡献

  • 分叉此存储库。

  • 创建新分支。

  • 提交拉取请求并清晰描述您的更改。

执照

MIT 许可证

如果您发现它有用,请在 GitHub 上给它一个⭐!

Related MCP Connectors

Related MCP Servers

  • F
    license
    Not graded
    quality
    D
    maintenance
    A dynamic proxy that converts OpenAPI Specification (OAS) endpoints into Message Communication Protocol (MCP) tools, allowing AI agents to use existing REST APIs as if they were native MCP tools without manual implementation.
    16
    -
  • A
    license
    Not graded
    quality
    D
    maintenance
    A standalone proxy that transforms any OpenAPI or Swagger-described REST API into an MCP server by mapping API operations to executable MCP tools. It enables AI clients to interact with existing web services through automated HTTP requests based on their official documentation.
    19 npm
    16
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A universal MCP server that connects any REST API to AI assistants via OpenAPI or Postman specifications. It enables dynamic tool creation with GraphQL-style field selection and automatic schema inference for efficient data retrieval.
    31 npm
    6
    -
  • A
    license
    Not graded
    quality
    C
    maintenance
    Turns any OpenAPI specification into a fully working MCP server with a single command, enabling AI agents to call APIs without writing any glue code.
    9 npm
    MIT