
OpenAPI → MCP Server
A Python CLI tool that generates a basic Node.js/TypeScript MCP server from an OpenAPI v3 spec (JSON/YAML), mapping each operation to an MCP tool.
What is OpenAPI to MCP Server Generator?
OpenAPI to MCP Server Generator is a Python CLI tool that generates a basic Node.js/TypeScript MCP (Model Context Protocol) server from an OpenAPI v3 specification file (JSON/YAML), mapping each operation to an MCP tool.
How to use OpenAPI to MCP Server Generator?
To use the tool, navigate to the project directory and run the command poetry run openapi-to-mcp generate --openapi-json <path_or_url>
to generate the MCP server code based on the OpenAPI specification.
Key features of OpenAPI to MCP Server Generator?
- Parses OpenAPI v3 JSON or YAML from local files or URLs.
- Validates the OpenAPI specification structure.
- Maps OpenAPI operations (GET, POST, PUT, DELETE, PATCH) to MCP tools.
- Generates a runnable Node.js/TypeScript MCP server project with clear setup instructions.
- Integrated linting and formatting tools, along with unit and integration tests.
Use cases of OpenAPI to MCP Server Generator?
- Quickly generate an MCP server for existing OpenAPI specifications.
- Facilitate the development of applications that require interaction with APIs defined by OpenAPI.
- Streamline the process of creating and testing MCP servers for various APIs.
FAQ from OpenAPI to MCP Server Generator?
-
What is MCP?
MCP stands for Model Context Protocol, which is a protocol for managing and interacting with models in a structured way. -
Is the tool free to use?
Yes! The OpenAPI to MCP Server Generator is open-source and free to use. -
What are the prerequisites?
You need Python 3.12 or higher, Poetry for dependency management, and Node.js version 20 or higher for the generated server.
OpenAPI → MCP Server
This Python CLI tool generates a basic Node.js/TypeScript MCP (Model Context Protocol) server based on an OpenAPI v3 specification file (JSON or YAML), provided either as a local file path or a URL. Each operation defined in the OpenAPI specification is mapped to a corresponding MCP tool within the generated server.
The generated server acts as a proxy, receiving MCP tool calls and translating them into HTTP requests to call the actual API defined in the OpenAPI specification.
✨ Features
- Parses OpenAPI v3 JSON or YAML from local files or URLs.
- Validates the OpenAPI specification structure.
- Maps OpenAPI operations (GET, POST, PUT, DELETE, PATCH) to MCP tools.
- Generates MCP tool
inputSchema
(JSON Schema) based on OpenAPI parameters and request bodies (handles basic types, objects, arrays, enums, formats, simple local$ref
s, basic cycle detection). - Generates a runnable Node.js/TypeScript MCP server project:
- Uses
@modelcontextprotocol/sdk
. - Configurable transport (stdio, sse).
- Configurable port for SSE transport.
- Reads target API base URL and optional authentication header from a
.env
file. - Includes basic error mapping from HTTP status codes to MCP error codes.
- Includes
package.json
,tsconfig.json
(with strict settings), and example.env
file. - Provides clear setup and run instructions in a generated
README.md
.
- Uses
- Integrated linting (
ruff
) and formatting (black
). - Unit and integration tests (
pytest
). - JSON logging.
🚀 Installation / Setup
Prerequisites:
- Python: Version 3.12 or higher is required (assumed to be installed).
- Poetry: This tool uses Poetry for dependency management (assumed to be installed).
- MCP SDK: Version 1.5.0 (installed automatically via Poetry).
- Node.js: Version 20 or higher is required for the generated server (assumed to be installed).
Steps:
-
Navigate to Project Directory: Open your terminal in the directory containing this
openapi-to-mcp
code. -
Install Dependencies:
- For running the tool:
This installs only the core dependencies needed to run the generator.poetry install --no-dev
- For development (including running tests, linting, formatting):
This installs all dependencies, including development tools likepoetry install --with dev # or simply: poetry install
pytest
,black
, andruff
.
- For running the tool:
-
Activate Virtual Environment (Optional but Recommended): Poetry creates a virtual environment to manage dependencies. You can activate it to run commands directly:
poetry shell
Alternatively, you can prefix commands with
poetry run
.
📋 Usage
The tool provides two main commands: generate
and test-server
.
Run commands from within the project directory (or with the virtual environment activated):
# Using poetry run
poetry run openapi-to-mcp [COMMAND] [OPTIONS]
# Or, if inside 'poetry shell'
openapi-to-mcp [COMMAND] [OPTIONS]
generate
Command
🛠️ This command generates the MCP server code.
Options:
--openapi-json
,-o
(Required): Path or URL to the OpenAPI specification file (JSON or YAML).--output-dir
,-d
: Output directory for the generated MCP server files. (Default:./mcp-server
)--mcp-server-name
,-n
: Name for the generated MCP server package. (Default:openapi-mcp-server
)--mcp-server-version
,-v
: Version for the generated MCP server package. (Default:1.0.0
)--transport
,-t
: Transport mechanism for the generated server (stdio
orsse
). (Default:stdio
)--port
,-p
: Port number to use if--transport
issse
. (Default:8080
)--help
: Show help for thegenerate
command.
Example:
For STDIO transport, you can run the following command to generate a server for the Swagger Petstore API example:
poetry run openapi-to-mcp generate \
--openapi-json https://petstore3.swagger.io/api/v3/openapi.json \
--output-dir ./generated-petstore-mcp \
--mcp-server-name petstore-mcp \
--transport stdio
This command will:
- Fetch and validate
https://petstore3.swagger.io/api/v3/openapi.json
. - Map the API operations to MCP tools.
- Generate the Node.js/TypeScript MCP server code in the
./generated-petstore-mcp
directory. - Configure the generated server to use STDIO transport.
For SSE transport, you can run the following command to generate a server for the Swagger Petstore API example:
poetry run openapi-to-mcp generate \
--openapi-json https://petstore3.swagger.io/api/v3/openapi.json \
--output-dir ./generated-petstore-mcp \
--mcp-server-name petstore-mcp \
--transport sse \
--port 8080
This command will:
- Fetch and validate
https://petstore3.swagger.io/api/v3/openapi.json
. - Map the API operations to MCP tools.
- Generate the Node.js/TypeScript MCP server code in the
./generated-petstore-mcp
directory. - Configure the generated server to use SSE transport on port 8080.
Post-Generation Steps
After running the generate
command, follow these steps in the generated server directory (<output-dir>
specified during generation):
- Navigate to the output directory:
cd <output-dir>
- Create/edit the
.env
file (the generator creates an.env.example
file you can copy) and provide the required values:TARGET_API_BASE_URL
: The base URL of the target API the generated server will interact with. The generated server validates this value on startup and will not run with placeholder URLs.TARGET_API_AUTH_HEADER
: (Optional) A full authorization header string if required by the target API (e.g.,Authorization: Bearer your_token
orX-API-Key: your_key
).
- Install dependencies:
npm install
- Build the TypeScript code:
npm run build
- Start the server:
npm start
The generated server's own README.md
file also contains these setup instructions. You can then test the running server using the test-server command or the MCP Inspector.
test-server
Command
🧪 This command allows you to send basic JSON-RPC requests (ListTools
, CallTool
) to a running MCP server (generated by this tool or any other).
Options:
--transport {sse,stdio}
: (Required) Specify the transport the server is using.--host <hostname>
: Hostname for SSE (default:localhost
).--port <port_number>
: Port for SSE (default:8080
).--server-cmd "<command>"
: (Required for stdio) The command to start the server (e.g.,"node ./generated-server/build/index.js"
). Ensure the path is correct relative to where you run the command.--list-tools
: Send aListTools
request.--tool-name <tool_name>
: Specify a tool name for aCallTool
request.--tool-args '<json_string>'
: (Requires--tool-name
) Provide arguments for the tool as a JSON string (e.g.,'{"petId": 123}'
).--env-source <source>
: (Optional, for stdio only) Provide environment variables to the server process.<source>
can be:- A direct JSON string:
'{"VAR1":"value1", "VAR2":"value2"}'
- A path to a JSON file:
./my_env.json
- A path to a
.env
file:./generated-server/.env
- A direct JSON string:
--help
: Show help for thetest-server
command.
Examples:
-
List tools via SSE (server running on port 8080):
poetry run openapi-to-mcp test-server --transport sse --port 8080 --list-tools
-
Call a tool via SSE (server running on port 8080):
poetry run openapi-to-mcp test-server --transport sse --port 8080 \ --tool-name getPetById --tool-args '{"petId": 1}'
-
List tools via stdio (using .env file):
# Ensure TARGET_API_BASE_URL is set in ./generated-petstore-mcp/.env poetry run openapi-to-mcp test-server --transport stdio \ --server-cmd "node ./generated-petstore-mcp/build/index.js" --list-tools \ --env-source ./generated-petstore-mcp/.env
-
Call a tool via stdio (using JSON string for env):
poetry run openapi-to-mcp test-server --transport stdio \ --server-cmd "node ./generated-petstore-mcp/build/index.js" \ --tool-name addPet --tool-args '{"requestBody": {"name": "doggie", "photoUrls": []}}' \ --env-source '{"TARGET_API_BASE_URL": "https://petstore3.swagger.io/api/v3"}'
🔍 Testing with MCP Inspector
Besides the test-server
command, the MCP Inspector provides a graphical interface for interacting with any running MCP server.
- Follow the installation instructions on the MCP Inspector documentation page.
- Launch the Inspector.
- Configure a new connection:
- Select the appropriate transport (
stdio
orsse
). - For
stdio
, provide the full command to start your generated server (e.g.,node /path/to/your/generated-server/build/index.js
). - For
sse
, provide the URL (e.g.,http://localhost:8080
or the port you specified).
- Select the appropriate transport (
- Connect to the server.
- Use the Inspector UI to view available tools (
ListTools
) and sendCallTool
requests with arguments.
This offers a more interactive way to explore and test the generated server.
🤝 Contributing
Interested in contributing? We welcome contributions of all kinds to openapi-to-mcp
! Whether you’re fixing bugs, adding features, improving documentation, or sharing ideas, your input is valuable to help us improve the project.
Please refer to our CONTRIBUTING.md for detailed guidelines on how to get started.
💻 Development Workflow
Ensure you have installed dependencies using poetry install --with dev
(or poetry install
). Tasks are run using poethepoet
via poetry run poe <task_name>
or poe <task_name>
if inside poetry shell
.
-
Formatting: Apply code formatting using Ruff:
poetry run poe format
-
Linting: Check for code style issues and apply automatic fixes using Ruff:
poetry run poe lint
-
Testing: Run unit and integration tests using Pytest with coverage reporting:
poetry run poe test
Coverage reports (terminal and HTML) will be generated (check
htmlcov/
directory for HTML report). -
All Checks: Run formatting, linting, and tests sequentially:
poetry run poe check
-
Clean Temporary Files: Remove temporary files and directories created during the build process:
poetry run poe clean
📄 License
This project is licensed under the Apache License 2.0. See the LICENSE file for details.