Openapi Template
MCP OpenApi Template
Ask AI about Openapi Template
Powered by Claude Β· Grounded in docs
I know everything about Openapi Template. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Reviews
Documentation
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) andsse(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
- User obtains JWT from Auth Gateway (login)
- User calls
set_auth_token(jwt)in the MCP - MCP fetches API credentials from Auth Gateway
- Credentials are cached for the session
- 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.
