MCP OpenAPI Template

MCP OpenAPI Template

A template for building MCP servers from OpenAPI specifications, enabling automatic conversion of any REST API into LLM-accessible tools with built-in authentication and validation.

Category
访问服务器

README

MCP OpenAPI Template

A template for building MCP (Model Context Protocol) servers from OpenAPI specifications.

This template provides a production-ready foundation for creating MCP servers that expose any REST API with an OpenAPI specification to LLM-powered tools.

Features

  • Dynamic Tool Generation: Automatically creates MCP tools from OpenAPI specs
  • Auth Gateway Integration: Centralized authentication via Auth Gateway
  • Dual Transport: Supports both stdio (local) and sse (remote) transports
  • Configurable Validation: Customizable ID patterns and input validation
  • Tool Policies: Risk-based policies for blocking/logging operations
  • Docker Ready: Production-ready Dockerfile and docker-compose

Quick Start

1. Clone/Use Template

# Using GitHub template feature (recommended)
# Click "Use this template" on GitHub

# Or clone directly
git clone https://github.com/jesusperezdeveloper/mcp_openapi_template my-api-mcp
cd my-api-mcp

2. Initialize Your Service

python -m scripts.init_service \
  --name "github" \
  --display-name "GitHub" \
  --base-url "https://api.github.com" \
  --openapi-url "https://raw.githubusercontent.com/github/rest-api-description/main/descriptions/api.github.com/api.github.com.json"

3. Configure Auth Gateway

Edit .env with your Auth Gateway credentials:

AUTH_GATEWAY_URL=https://your-auth-gateway.com
AUTH_GATEWAY_API_KEY=your-api-key

4. Download OpenAPI Spec

python -m scripts.fetch_openapi

5. Run the Server

# Local mode (stdio)
PYTHONPATH=vendor python -m src.server

# Remote mode (SSE)
MCP_TRANSPORT=sse PYTHONPATH=vendor python -m src.server

Project Structure

mcp_openapi_template/
├── src/
│   ├── server.py           # Main MCP server
│   ├── config.py           # Configuration loader
│   ├── openapi_tools.py    # Dynamic tool generator
│   ├── auth_gateway.py     # Auth Gateway integration
│   ├── validation.py       # Input validation
│   └── tool_policies.py    # Risk policies
├── config/
│   ├── service.yaml        # Service configuration
│   ├── mcp.local.json      # Local MCP config
│   └── mcp.remote.json     # Remote MCP config
├── scripts/
│   ├── fetch_openapi.py    # Download OpenAPI spec
│   └── init_service.py     # Initialize new service
├── examples/
│   └── trello/             # Complete Trello example
├── openapi/                # OpenAPI specs (downloaded)
├── vendor/mcp/             # Vendorized MCP SDK
├── Dockerfile
└── docker-compose.yml

Configuration

service.yaml

The main configuration file (config/service.yaml) defines:

service:
  name: "myservice"
  display_name: "My Service"

api:
  base_url: "https://api.example.com"
  openapi_spec_url: "https://api.example.com/openapi.json"
  tool_prefix: "myservice"

auth:
  gateway_endpoint: "/credentials/myservice"
  credentials_format:
    - name: "api_key"
      query_param: "key"

validation:
  id_pattern: "^[a-zA-Z0-9]+$"

policies:
  blocked_patterns:
    - "delete_organization"

Environment Variables

Variable Required Description
AUTH_GATEWAY_URL Yes Auth Gateway URL
AUTH_GATEWAY_API_KEY Yes API Key for Auth Gateway
API_BASE_URL No Override API base URL
MCP_TRANSPORT No stdio (default) or sse
MCP_PORT No Port for SSE mode (default: 8000)

Authentication Flow

  1. User obtains JWT from Auth Gateway (login)
  2. User calls set_auth_token(jwt) in the MCP
  3. MCP fetches API credentials from Auth Gateway
  4. Credentials are cached for the session
  5. User can now use all available tools

Adding Custom Helper Tools

See examples/trello/helpers.py for a complete example of adding user-friendly wrapper tools.

# In src/helpers.py
def register_helper_tools(mcp, auth_params, client_factory, require_auth):
    @mcp.tool(description="My custom tool")
    async def my_tool(param: str) -> dict:
        require_auth()
        # Implementation
        pass

Docker Deployment

# Build with OpenAPI spec
docker build \
  --build-arg OPENAPI_SPEC_URL="https://api.example.com/openapi.json" \
  -t my-mcp .

# Run
docker run -p 8000:8000 --env-file .env my-mcp

Examples

See the examples/ directory for complete configurations:

  • Trello: Full configuration with helper tools, validation, and policies

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选