🌐 OPENAPI到MCP代理
一个动态MCP代理服务器,可以根据多个不同的本地OpenAPI规范自动创建工具。
🚀 概述
此项目创建了一个代理MCP服务器,该服务器动态生成可调用的 MCP工具 基于 OpenAPI规范。它扫描不同规格的产品 提供商 在 规格 目录,并通过FastMCP服务器将其作为工具提供。
✨ 特性
- 🔄 根据OpenAPI规范自动生成工具
- 🔌 支持多个不同的API提供商
- 🔐 API身份验证的自定义标头和身份验证支持
- 🛣️ 处理不同的参数位置(查询、路径、标头)
- 🖥️ 灵活的服务器部署(STDIO或SSE)
📋 需求
- Python 3.8+
- 中列出的依赖关系
requirements.txt - 已验证的OpenAPI规范(建议:与https://editor.swagger.io)
🔧 设置
- 克隆存储库:
git clone
cd openapi-2-mcp-proxy- 创建虚拟环境:
python -m venv .venv
# On Linux/MacOs:
source .venv/bin/activate
# On Windows:
.venv\Scripts\activate- 安装依赖项:
pip install -r requirements.txt- 设置每个所需的API提供程序配置。
地点在 specifications/{ProviderName}/ 目录:
- OpenAPI规范文件(specification.json, specification.yaml 或 specification.yml) - A. config.json 提交 base_url 可选 headers (您应该在此处设置身份验证)
📁 项目结构
openapi-2-mcp-proxy/
├── main.py # ▶️ Main entry point
├── server.py # ⚙️ Server setup and configuration
├── utils.py # 📙 Utility functions and classes
├── specifications/ # 📂 API specifications directory
│ ├── Sensefinity/
│ │ ├── specification.yml
│ │ └── config.json
│ ├── ExampleProvider/
│ │ ├── specification.yaml
│ │ └── config.json
│ ├── AnotherProvider/
│ │ ├── specification.json
│ │ └── config.json
│ ├── .../
└── README.md # ⬅️ This file🚀 用法
使用SSE运行服务器(默认):
python main.py使用STDIO运行服务器:
python main.py --stdio自定义主机和端口:
python main.py --host 127.0.0.1 --port 9000📝 添加新的API提供程序
- 在中创建新目录
specifications/使用您的提供商名称
- 添加OpenAPI规范文件(
specification.json,specification.yaml或specification.yml)
- 创建一个
config.json具有以下结构的文件:
{
"base_url": "https://api.example.com",
"headers": {
"Authorization": "Bearer YOUR_TOKEN", (or a different authentication method)
"Content-Type": "application/json"
}
}❗ 相关OpenAPI规范字段❗
此工具使用时,以下OpenAPI规范字段至关重要:
OpenAPI Specification
...
├── paths
│ └── /endpoint/{path_param} # ❗ Each path becomes a tool
│ ├── [method] # ❗ HTTP method (get, post, put, patch, delete, head, options)
│ │ ├── operationId # ❗ Used as as the name for the tool
│ │ ├── description # ❗ Used as as the description for the tool
│ │ ├── parameters
│ │ │ ├── name # ❗ Argument name
│ │ │ │ ├── in # ❗ Parameter location (query, path, header)
│ │ │ │ ├── required # ❗ Whether parameter is mandatory (true, false)
│ │ │ │ └── description # ❗ Used in the tool description
│ │ │ └── ...
│ │ └── ...
│ ├── [method]
│ └── ...
└── ...已使用字段
- 路径:
- 操作 (路径+方法):创建MCP工具的起点 - 操作ID:用于为每个工具生成唯一的函数名称 - 描述 : ⚠️ 描述作为指导方针传递给AI代理。必须明确包括工具的功能以及何时/如何使用它 - 参数:定义工具需要的参数 - 名字:要使用的参数名称 - 在……里面:确定如何将参数发送到API(query, path, header) - 必需的:确定是否必须向工具提供参数 - 描述: ⚠️ 在工具描述中使用。必须明确指出论点是什么
当前未使用
- “信息”和“服务器”字段
- 基本网址或身份验证设置(改用config.json)
- 响应模式
- 此处未提及的其余字段
