A Python library to automatically convert any OpenAPI v2/v3 specification into a Model Context Protocol (MCP) server, with integrations for popular frameworks like FastAPI.
Project description
OpenAPI to MCP Converter (Python)
A Python library to automatically generate MCP (Model-View-Controller Protocol) servers from OpenAPI specifications.
This library allows you to expose any API with an OpenAPI v3 specification as an MCP-compliant server, making it easy to integrate with AI agents and other MCP-enabled tools.
Features
- Dynamic Conversion: Generate MCP servers from any public OpenAPI v3 specification URL.
- Path Filtering: Selectively expose a subset of API endpoints.
- Dynamic Upstream URL: Forward requests to a different base URL than the one in the OpenAPI spec.
- Authentication Forwarding: Pass authentication headers to the upstream API.
$refResolution: Automatically resolves JSON references ($ref) in the OpenAPI spec for complete schema definitions.- Security: Built-in SSRF protection with a configurable domain whitelist.
- Framework Integration: Plug-and-play integration with FastAPI.
Installation
pip install openapi-to-mcp
Quick Start - FastAPI Integration
Here's how to add an MCP server to your existing FastAPI application. This example sets a static OpenAPI spec URL and enables security.
from fastapi import FastAPI
from openapi_to_mcp.fastapi import add_mcp_route
app = FastAPI()
# Add the MCP route
# This will expose an MCP server at /mcp
add_mcp_route(
app,
openapi_url="https://petstore.swagger.io/v2/swagger.json",
allowed_domains=["petstore.swagger.io"] # SSRF Protection
)
@app.get("/")
def read_root():
return {"Hello": "World"}
Advanced Configuration
You can configure the MCP server statically when you initialize it or dynamically using query parameters.
Static Configuration
Pass a configuration object to add_mcp_route:
route_prefix(optional, default:/mcp): The base path for the MCP server.openapi_url(optional): A default OpenAPI specification URL.allowed_domains(optional, default:[]): A list of allowed domains for the OpenAPI spec URL and the upstream server to prevent SSRF attacks.
Dynamic Configuration (Query Parameters)
The MCP endpoint accepts query parameters to override the static configuration. This is useful for AI agents that need to configure the MCP gateway on the fly.
s: The URL of the OpenAPI specification (e.g.,?s=https://example.com/api/swagger.json).u: The upstream server URL to which requests will be forwarded. Overrides the server URL in the OpenAPI spec.f: A comma-separated list of path prefixes to filter. Only paths starting with these prefixes will be exposed (e.g.,?f=/users,/products).h: The name of an HTTP header to forward for authentication (e.g.,?h=Authorization). The MCP server will look for a header with this name in the incoming request and forward it to the upstream API.
Example URL:
http://localhost:8000/mcp?s=https://petstore.swagger.io/v2/swagger.json&f=/pet&h=api_key
This URL configures the MCP server to:
- Fetch the OpenAPI spec from
petstore.swagger.io. - Expose only the endpoints under the
/petpath. - Look for a header named
api_keyand forward it in requests to the Petstore API.
Security: SSRF Protection
To prevent Server-Side Request Forgery (SSRF) attacks, the library uses a domain whitelist. The add_mcp_route function takes an allowed_domains option, which is a list of strings.
If allowed_domains is configured, the library will only allow requests to OpenAPI spec URLs and upstream server URLs that match a domain in the list.
add_mcp_route(
app,
allowed_domains=["api.example.com", "petstore.swagger.io"]
)
How it Works
The library fetches the OpenAPI specification, parses it, resolves all $ref references, and dynamically creates the necessary endpoints to conform to the MCP standard. It handles the mapping of paths, parameters, and authentication methods.
Running Tests
The library uses pytest for testing. To run the tests:
pip install -r requirements.txt
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openapi_to_mcp-0.1.1.tar.gz.
File metadata
- Download URL: openapi_to_mcp-0.1.1.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.6 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
860f7631dca1cfbf785ba712b49b925873ebe7a1fa19cb5f04445ca8ddd4517a
|
|
| MD5 |
0b077e50b6c3f779ce2d43bb64b8af57
|
|
| BLAKE2b-256 |
97048de73cf242e63d6e57f390df10ab3c37b6643ee62e31590734b27e474090
|
File details
Details for the file openapi_to_mcp-0.1.1-py3-none-any.whl.
File metadata
- Download URL: openapi_to_mcp-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.6 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff44dc2e1c5436adc8ce2abf3740995be8967c8372872782daf6403780800da9
|
|
| MD5 |
d8a9cabf0ec4a23157561653e396b70c
|
|
| BLAKE2b-256 |
d94c64fe4fd9c65d1d5aed14f16decbfd4415b9211b9c57509eaa469f3a44002
|