# 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.
- **`$ref` Resolution**: 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
```bash
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.
```python
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:
1. Fetch the OpenAPI spec from `petstore.swagger.io`.
2. Expose only the endpoints under the `/pet` path.
3. Look for a header named `api_key` and 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.
```python
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:
```bash
pip install -r requirements.txt
pytest
```
Raw data
{
"_id": null,
"home_page": "https://github.com/itssri5/openapi-to-mcp",
"name": "openapi-to-mcp",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8",
"maintainer_email": null,
"keywords": "openapi, mcp, fastapi, api, converter, generator",
"author": "Sriram Krishna",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/97/04/8de73cf242e63d6e57f390df10ab3c37b6643ee62e31590734b27e474090/openapi_to_mcp-0.1.1.tar.gz",
"platform": null,
"description": "# OpenAPI to MCP Converter (Python)\n\nA Python library to automatically generate MCP (Model-View-Controller Protocol) servers from OpenAPI specifications.\n\nThis 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.\n\n## Features\n\n- **Dynamic Conversion**: Generate MCP servers from any public OpenAPI v3 specification URL.\n- **Path Filtering**: Selectively expose a subset of API endpoints.\n- **Dynamic Upstream URL**: Forward requests to a different base URL than the one in the OpenAPI spec.\n- **Authentication Forwarding**: Pass authentication headers to the upstream API.\n- **`$ref` Resolution**: Automatically resolves JSON references (`$ref`) in the OpenAPI spec for complete schema definitions.\n- **Security**: Built-in SSRF protection with a configurable domain whitelist.\n- **Framework Integration**: Plug-and-play integration with FastAPI.\n\n## Installation\n\n```bash\npip install openapi-to-mcp\n```\n\n## Quick Start - FastAPI Integration\n\nHere's how to add an MCP server to your existing FastAPI application. This example sets a static OpenAPI spec URL and enables security.\n\n```python\nfrom fastapi import FastAPI\nfrom openapi_to_mcp.fastapi import add_mcp_route\n\napp = FastAPI()\n\n# Add the MCP route\n# This will expose an MCP server at /mcp\nadd_mcp_route(\n app,\n openapi_url=\"https://petstore.swagger.io/v2/swagger.json\",\n allowed_domains=[\"petstore.swagger.io\"] # SSRF Protection\n)\n\n@app.get(\"/\")\ndef read_root():\n return {\"Hello\": \"World\"}\n```\n\n## Advanced Configuration\n\nYou can configure the MCP server statically when you initialize it or dynamically using query parameters.\n\n### Static Configuration\n\nPass a configuration object to `add_mcp_route`:\n\n- `route_prefix` (optional, default: `/mcp`): The base path for the MCP server.\n- `openapi_url` (optional): A default OpenAPI specification URL.\n- `allowed_domains` (optional, default: `[]`): A list of allowed domains for the OpenAPI spec URL and the upstream server to prevent SSRF attacks.\n\n### Dynamic Configuration (Query Parameters)\n\nThe 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.\n\n- `s`: The URL of the OpenAPI specification (e.g., `?s=https://example.com/api/swagger.json`).\n- `u`: The upstream server URL to which requests will be forwarded. Overrides the server URL in the OpenAPI spec.\n- `f`: A comma-separated list of path prefixes to filter. Only paths starting with these prefixes will be exposed (e.g., `?f=/users,/products`).\n- `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.\n\n**Example URL:**\n`http://localhost:8000/mcp?s=https://petstore.swagger.io/v2/swagger.json&f=/pet&h=api_key`\n\nThis URL configures the MCP server to:\n\n1. Fetch the OpenAPI spec from `petstore.swagger.io`.\n2. Expose only the endpoints under the `/pet` path.\n3. Look for a header named `api_key` and forward it in requests to the Petstore API.\n\n## Security: SSRF Protection\n\nTo 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.\n\nIf `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.\n\n```python\nadd_mcp_route(\n app,\n allowed_domains=[\"api.example.com\", \"petstore.swagger.io\"]\n)\n```\n\n## How it Works\n\nThe 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.\n\n## Running Tests\n\nThe library uses pytest for testing. To run the tests:\n\n```bash\npip install -r requirements.txt\npytest\n```\n",
"bugtrack_url": null,
"license": "MPL-2.0",
"summary": "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.",
"version": "0.1.1",
"project_urls": {
"Homepage": "https://github.com/itssri5/openapi-to-mcp",
"Repository": "https://github.com/itssri5/openapi-to-mcp"
},
"split_keywords": [
"openapi",
" mcp",
" fastapi",
" api",
" converter",
" generator"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d94c64fe4fd9c65d1d5aed14f16decbfd4415b9211b9c57509eaa469f3a44002",
"md5": "d8a9cabf0ec4a23157561653e396b70c",
"sha256": "ff44dc2e1c5436adc8ce2abf3740995be8967c8372872782daf6403780800da9"
},
"downloads": -1,
"filename": "openapi_to_mcp-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d8a9cabf0ec4a23157561653e396b70c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 13223,
"upload_time": "2025-07-26T22:24:06",
"upload_time_iso_8601": "2025-07-26T22:24:06.090989Z",
"url": "https://files.pythonhosted.org/packages/d9/4c/64fe4fd9c65d1d5aed14f16decbfd4415b9211b9c57509eaa469f3a44002/openapi_to_mcp-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "97048de73cf242e63d6e57f390df10ab3c37b6643ee62e31590734b27e474090",
"md5": "0b077e50b6c3f779ce2d43bb64b8af57",
"sha256": "860f7631dca1cfbf785ba712b49b925873ebe7a1fa19cb5f04445ca8ddd4517a"
},
"downloads": -1,
"filename": "openapi_to_mcp-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "0b077e50b6c3f779ce2d43bb64b8af57",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 11099,
"upload_time": "2025-07-26T22:24:07",
"upload_time_iso_8601": "2025-07-26T22:24:07.195410Z",
"url": "https://files.pythonhosted.org/packages/97/04/8de73cf242e63d6e57f390df10ab3c37b6643ee62e31590734b27e474090/openapi_to_mcp-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-26 22:24:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "itssri5",
"github_project": "openapi-to-mcp",
"github_not_found": true,
"lcname": "openapi-to-mcp"
}