TS

openapi-mcp-generator

by harsha-iiiv/openapi-mcp-generator

0 views

A tool that converts OpenAPI specifications to MCP server

nodejstypescriptCode Generation

OpenAPI to MCP Generator (openapi-mcp-generator)

npm version License: MIT GitHub repository

Generate Model Context Protocol (MCP) servers from OpenAPI specifications.

This CLI tool automates the generation of MCP-compatible servers that proxy requests to existing REST APIsโ€”enabling AI agents and other MCP clients to seamlessly interact with your APIs using your choice of transport methods.


โœจ Features

  • ๐Ÿ”ง OpenAPI 3.0 Support: Converts any OpenAPI 3.0+ spec into an MCP-compatible server.
  • ๐Ÿ” Proxy Behavior: Proxies calls to your original REST API while validating request structure and security.
  • ๐Ÿ” Authentication Support: API keys, Bearer tokens, Basic auth, and OAuth2 supported via environment variables.
  • ๐Ÿงช Zod Validation: Automatically generates Zod schemas from OpenAPI definitions for runtime input validation.
  • โš™๏ธ Typed Server: Fully typed, maintainable TypeScript code output.
  • ๐Ÿ”Œ Multiple Transports: Communicate over stdio, SSE via Hono, or StreamableHTTP.
  • ๐Ÿงฐ Project Scaffold: Generates a complete Node.js project with tsconfig.json, package.json, and entry point.
  • ๐Ÿงช Built-in HTML Test Clients: Test API interactions visually in your browser (for web-based transports).

๐Ÿš€ Installation

npm install -g openapi-mcp-generator

You can also use yarn global add openapi-mcp-generator or pnpm add -g openapi-mcp-generator


๐Ÿ›  Usage

# Generate an MCP server (stdio)
openapi-mcp-generator --input path/to/openapi.json --output path/to/output/dir

# Generate an MCP web server with SSE
openapi-mcp-generator --input path/to/openapi.json --output path/to/output/dir --transport=web --port=3000

# Generate an MCP StreamableHTTP server
openapi-mcp-generator --input path/to/openapi.json --output path/to/output/dir --transport=streamable-http --port=3000

CLI Options

OptionAliasDescriptionDefault
--input-iPath or URL to OpenAPI specification (YAML or JSON)Required
--output-oDirectory to output the generated MCP projectRequired
--server-name-nName of the MCP server (package.json:name)OpenAPI title or mcp-api-server
--server-version-vVersion of the MCP server (package.json:version)OpenAPI version or 1.0.0
--base-url-bBase URL for API requests. Required if OpenAPI servers missing or ambiguous.Auto-detected if possible
--transport-tTransport mode: "stdio" (default), "web", or "streamable-http""stdio"
--port-pPort for web-based transports3000
--forceOverwrite existing files in the output directory without confirmationfalse

๐Ÿ“ฆ Programmatic API

You can also use this package programmatically in your Node.js applications:

import { getToolsFromOpenApi } from 'openapi-mcp-generator';

// Extract MCP tool definitions from an OpenAPI spec
const tools = await getToolsFromOpenApi('./petstore.json');

// With options
const filteredTools = await getToolsFromOpenApi('https://example.com/api-spec.json', {
  baseUrl: 'https://api.example.com',
  dereference: true,
  excludeOperationIds: ['deletePet'],
  filterFn: (tool) => tool.method.toLowerCase() === 'get',
});

For full documentation of the programmatic API, see PROGRAMMATIC_API.md.


๐Ÿงฑ Project Structure

The generated project includes:

<output_directory>/
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ tsconfig.json
โ”œโ”€โ”€ .env.example
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ index.ts
โ”‚   โ””โ”€โ”€ [transport-specific-files]
โ””โ”€โ”€ public/          # For web-based transports
    โ””โ”€โ”€ index.html   # Test client

Core dependencies:

  • @modelcontextprotocol/sdk - MCP protocol implementation
  • axios - HTTP client for API requests
  • zod - Runtime validation
  • json-schema-to-zod - Convert JSON Schema to Zod
  • Transport-specific deps (Hono, uuid, etc.)

๐Ÿ“ก Transport Modes

Stdio (Default)

Communicates with MCP clients via standard input/output. Ideal for local development or integration with LLM tools.

Web Server with SSE

Launches a fully functional HTTP server with:

  • Server-Sent Events (SSE) for bidirectional messaging
  • REST endpoint for client โ†’ server communication
  • In-browser test client UI
  • Multi-connection support
  • Built with lightweight Hono framework

StreamableHTTP

Implements the MCP StreamableHTTP transport which offers:

  • Stateful JSON-RPC over HTTP POST requests
  • Session management using HTTP headers
  • Proper HTTP response status codes
  • Built-in error handling
  • Compatibility with MCP StreamableHTTPClientTransport
  • In-browser test client UI
  • Built with lightweight Hono framework

Transport Comparison

Featurestdioweb (SSE)streamable-http
ProtocolJSON-RPC over stdioJSON-RPC over SSEJSON-RPC over HTTP
ConnectionPersistentPersistentRequest/response
BidirectionalYesYesYes (stateful)
Multiple clientsNoYesYes
Browser compatibleNoYesYes
Firewall friendlyNoYesYes
Load balancingNoLimitedYes
Status codesNoLimitedFull HTTP codes
HeadersNoLimitedFull HTTP headers
Test clientNoYesYes

๐Ÿ” Environment Variables for Authentication

Configure auth credentials in your environment:

Auth TypeVariable Format
API KeyAPI_KEY_<SCHEME_NAME>
BearerBEARER_TOKEN_<SCHEME_NAME>
Basic AuthBASIC_USERNAME_<SCHEME_NAME>, BASIC_PASSWORD_<SCHEME_NAME>
OAuth2OAUTH_CLIENT_ID_<SCHEME_NAME>, OAUTH_CLIENT_SECRET_<SCHEME_NAME>, OAUTH_SCOPES_<SCHEME_NAME>

โ–ถ๏ธ Running the Generated Server

cd path/to/output/dir
npm install

# Run in stdio mode
npm start

# Run in web server mode
npm run start:web

# Run in StreamableHTTP mode
npm run start:http

Testing Web-Based Servers

For web and StreamableHTTP transports, a browser-based test client is automatically generated:

  1. Start the server using the appropriate command
  2. Open your browser to http://localhost:<port>
  3. Use the test client to interact with your MCP server

โš ๏ธ Requirements

  • Node.js v20 or later

Star History

Star History Chart

๐Ÿค Contributing

Contributions are welcome!

  1. Fork the repo
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Run npm run format.write to format your code
  4. Commit your changes: git commit -m "Add amazing feature"
  5. Push and open a PR

๐Ÿ“Œ Repository: github.com/harsha-iiiv/openapi-mcp-generator


๐Ÿ“„ License

MIT License โ€” see LICENSE for full details.

Install

{
  "mcpServers": {
    "openapi-mcp-generator": {
      "command": "openapi-mcp-generator",
      "args": [
        "--input",
        "path/to/openapi.json",
        "--output",
        "path/to/output/dir"
      ]
    }
  }
}
For more configuration details, refer to the content on the left

Related

Related projects feature coming soon

Will recommend related projects based on sub-categories