Skip to main content

Generate MCP servers from OpenAPI specifications

Project description

OpenAPI to MCP Server Generator

A Python tool that automatically converts OpenAPI specifications into fully functional Model Context Protocol (MCP) servers. Generates Docker-ready implementations with support for SSE/IO communication protocols, authentication, and comprehensive error handling.

Key features:

  • ๐Ÿ”„ OpenAPI to MCP tools/resources conversion
  • ๐Ÿณ Docker-ready with multi-stage builds
  • ๐Ÿ” Multiple authentication methods
  • โšก Async operations & rate limiting
  • ๐Ÿ“ก SSE/IO communication protocols
  • ๐Ÿ“ฆ Modular code structure with package support

Modular Code Structure

The generator has been refactored into a proper Python package while maintaining backward compatibility:

  1. Original Entry Point: The generator.py script still works exactly as before
  2. Modular Organization: Code is now split into focused modules
  3. Package Installation: Can be installed as a proper Python package
  4. Same Templates: Uses the exact same templates in the /templates directory
  5. Docker Support: Preserves all Docker functionality

You can use the tool in whatever way you prefer:

  1. Run generator.py directly (original approach)
  2. Install as a package and use mcp-generator command
  3. Use the module programmatically in your own Python code

For more details on the modular code structure, see MODULAR_REFACTORING.md.

Features

  • Convert OpenAPI specifications to MCP servers
  • Docker-ready implementation with multi-stage builds
  • Support for multiple authentication methods
  • Choice of SSE or IO communication protocols
  • Comprehensive error handling and logging
  • Built-in rate limiting and security features
  • Async operations for optimal performance
  • Extensive test suite with coverage reporting

Prerequisites

  • Python 3.10+
  • Docker (for running the generated server)
  • pip or uv (Python package manager)

Installation

From Source

# Clone the repository
git clone https://github.com/abutbul/openapi-mcp-generator.git
cd openapi-mcp-generator

# Install as a package (development mode)
pip install -e .

# Or using uv
uv venv
source .venv/bin/activate
uv pip install -e .

Using pip (once published)

pip install openapi-mcp-generator

Usage

# Using the original script (still works the same way)
python generator.py openapi.yaml --output-dir ./output --api-url https://api.example.com

# Using the new modular CLI tool after installation
mcp-generator openapi.yaml --output-dir ./output --api-url https://api.example.com

# Using the python module directly
python -m openapi_mcp_generator.cli openapi.yaml --output-dir ./output

Command Line Options

  • openapi_file: Path to the OpenAPI YAML file (required)
  • --output-dir: Output directory for the generated project (default: '.')
  • --api-url: Base URL for the API
  • --auth-type: Authentication type (bearer, token, basic)
  • --api-token: API token for authentication
  • --api-username: Username for basic authentication
  • --api-password: Password for basic authentication

Running the Generated Server

After generating the server, you can build and run it using Docker:

cd output/openapi-mcp-*
./docker.sh build
./docker.sh start --transport=sse --port=8000

Docker Script Options

The generated docker.sh script supports the following commands:

  • build: Build the Docker image
  • start: Start the container
    • --port=PORT: Set the port (default: 8000)
    • --transport=TYPE: Set transport type: 'sse' or 'io' (default: sse)
    • --log-level=LEVEL: Set logging level (default: info)
  • stop: Stop the container
  • clean: Remove the container and image
  • logs: View container logs

Project Structure

The modular generator has the following structure:

openapi-mcp-generator/
โ”œโ”€โ”€ generator.py              # Original entry point (maintained for backward compatibility)
โ”œโ”€โ”€ mcp_generator.py          # New entry point (uses the modular structure)
โ”œโ”€โ”€ openapi_mcp_generator/    # Main package (new modular structure)
โ”‚   โ”œโ”€โ”€ __init__.py           # Package initialization
โ”‚   โ”œโ”€โ”€ cli.py                # Command-line interface
โ”‚   โ”œโ”€โ”€ generator.py          # Main generator module
โ”‚   โ”œโ”€โ”€ generators.py         # Code generators for tools/resources
โ”‚   โ”œโ”€โ”€ http.py               # HTTP client utilities
โ”‚   โ”œโ”€โ”€ parser.py             # OpenAPI parser
โ”‚   โ””โ”€โ”€ project.py            # Project builder
โ”œโ”€โ”€ templates/                # Original templates directory (used by the modular code)
โ”‚   โ”œโ”€โ”€ config/
โ”‚   โ”œโ”€โ”€ docker/
โ”‚   โ”œโ”€โ”€ server/
โ”‚   โ”œโ”€โ”€ pyproject.toml
โ”‚   โ””โ”€โ”€ requirements.txt
โ”œโ”€โ”€ samples/                  # Sample implementations
โ”œโ”€โ”€ tests/                    # Test cases
โ”œโ”€โ”€ LICENSE                   # License file
โ”œโ”€โ”€ README.md                 # This file
โ”œโ”€โ”€ pyproject.toml            # Project metadata
โ””โ”€โ”€ setup.py                  # Package setup

The modular structure preserves all the existing functionality while making the code more maintainable:

  1. The original entry point (generator.py) can still be used as before
  2. The existing templates in /templates are used by the new modular code
  3. All Docker-related functionality is preserved exactly as it was
  4. The project can now be installed as a proper Python package

Sample Implementations

Check out our sample implementations to see the generator in action:

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Run the test suite
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Usage Examples

1. Using the library without installing (direct from source)

To generate an MCP server from the Elasticsearch 6.1 spec folder:

# Run the original script directly
python generator.py samples/elasticsearch_6.1/api

# Or use the modular entry point
python mcp_generator.py samples/elasticsearch_6.1/api

2. Using the library after installing with pip

First, install the package (from the project root):

pip install .

Then use the CLI tool to convert the Trilium ETAPI spec:

mcp-generator samples/TriliumNext/etapi.openapi.yaml

Or use it programmatically in your own Python code:

from openapi_mcp_generator import generator

generator.generate('samples/TriliumNext/etapi.openapi.yaml')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openapi_mcp_generator-0.1.1.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openapi_mcp_generator-0.1.1-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file openapi_mcp_generator-0.1.1.tar.gz.

File metadata

  • Download URL: openapi_mcp_generator-0.1.1.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for openapi_mcp_generator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8842dd3a8379e3d8b1de3a09c065531a704751b47434a6d22047f1890a69abb2
MD5 0b01d5b55b76c68e9d7402e73b549ea4
BLAKE2b-256 8dbb01164fdcbf667e3513a87c4045850550055ce3500bc88fcd912e40fca281

See more details on using hashes here.

File details

Details for the file openapi_mcp_generator-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for openapi_mcp_generator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f483a513098991e77593b2e925f242903501f41539d3181b72e1736249407889
MD5 fcc5220802d940a1f0a20a1fadad8545
BLAKE2b-256 d9f62aa32e59fb31ad4580bd222c430947f54ab381def1bce0d54303ffcb45b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page