OpenAPI to MCP Generator

by LostInBrittany

21 256 downloads Not rated yet MIT

About

A Java program that generates a Quarkus MCP server form an Open API Swagger file

Details

License
MIT

Explore

clever features enable tokens

- Multiple HTTP Methods: Support for GET, POST, PUT, DELETE, and PATCH
- Content Type Handling: Proper handling of different content types
- Error Handling: Detailed error reporting with status codes and response bodies
- Authentication: Support for API keys, Bearer tokens, and Basic authentication
- Timeouts: Configurable connection, read, and write timeouts
- Multiple Servers: Support for selecting from multiple server URLs defined in the OpenAPI specification

To run the generated MCP server:

jbang GeneratedMcpServer.java

The generated MCP server supports multiple authentication methods through environment variables:

- API Key: Set API_KEY and API_KEY_HEADER environment variables
- Bearer Token: Set BEARER_TOKEN environment variable
- Basic Auth: Set API_USERNAME and API_PASSWORD environment variables

Example:

export API_KEY="your-api-key"
export API_KEY_HEADER="X-API-Key"
jbang GeneratedMcpServer.java
cd examples/open-meteo
jbang OpenMeteoMcpServer.java

``bash
cd examples/clever-cloud

npm install -g clever-tools

The jbang-wrapper.sh` script addresses environment issues when running from AI assistants like Claude Desktop on Mac, ensuring the correct PATH and environment variables are available.

This project provides a powerful tool for automatically converting OpenAPI/Swagger specifications into Model Context Protocol (MCP) servers, allowing LLMs to interact with any REST API through standardized tools.

Components

SwaggerToMcpGenerator.java

A comprehensive utility that converts any OpenAPI/Swagger specification into a fully functional MCP server:
- Parses OpenAPI specification files
- Converts API endpoints to MCP tools
- Handles path parameters, query parameters, and request bodies
- Supports multiple HTTP methods (GET, POST, PUT, DELETE, PATCH)
- Provides authentication support (API keys, Bearer tokens, Basic auth)
- Formats JSON responses for readability
- Generates robust error handling
- Includes parameter documentation with valid values and defaults

Generating an MCP Server from OpenAPI

To generate an MCP server from any OpenAPI specification:

jbang SwaggerToMcpGenerator.java path/to/swagger.json GeneratedMcpServer [options]

Parameters:
- path/to/swagger.json: Path to the OpenAPI/Swagger specification file
- GeneratedMcpServer: Name of the output Java file (without .java extension)

Options:
- --server-index <index>: Index of the server to use from the OpenAPI specification (0-based)
- --server-url <url>: URL of the server to use (overrides server-index)

This will create a new file GeneratedMcpServer.java that implements an MCP server with tools for each API endpoint defined in the swagger file. The generator will emit a warning if multiple servers are defined in the OpenAPI specification and none is explicitly selected.

Running the Generated MCP Server

To run the generated MCP server:

jbang GeneratedMcpServer.java

Server Selection

The generated MCP server includes constants for all servers defined in the OpenAPI specification, allowing you to choose which server to use at runtime. By default, the first server in the list is used, but you can select a specific server using environment variables:

```bash

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.