|
| 1 | +# OpenAPI to MCP |
| 2 | + |
| 3 | +A command-line tool that generates Model Context Protocol (MCP) server code from OpenAPI specifications. This tool helps you quickly create an MCP server that acts as a bridge between LLMs (Large Language Models) and your API. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Automatic Tool Generation**: Converts each API endpoint in your OpenAPI spec into an MCP tool |
| 8 | +- **Multiple Transport Options**: Supports stdio, WebSocket, and HTTP transport methods |
| 9 | +- **Complete Project Setup**: Generates all necessary files to run an MCP server |
| 10 | +- **TypeScript Support**: Includes TypeScript definitions and configuration |
| 11 | +- **Easy Configuration**: Simple environment-based configuration for the generated server |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +# Clone this repository |
| 17 | +git clone https://github.com/yourusername/openapi-to-mcp.git |
| 18 | + |
| 19 | +# Navigate to the project directory |
| 20 | +cd openapi-to-mcp |
| 21 | + |
| 22 | +# Install dependencies |
| 23 | +npm install |
| 24 | + |
| 25 | +# Make the script executable |
| 26 | +chmod +x index.js |
| 27 | + |
| 28 | +# Optionally, install globally |
| 29 | +npm install -g . |
| 30 | +``` |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +Generate an MCP server from an OpenAPI specification: |
| 35 | + |
| 36 | +```bash |
| 37 | +./index.js --openapi path/to/openapi.json --output ./my-mcp-server |
| 38 | +``` |
| 39 | + |
| 40 | +Or if installed globally: |
| 41 | + |
| 42 | +```bash |
| 43 | +openapi-to-mcp --openapi path/to/openapi.json --output ./my-mcp-server |
| 44 | +``` |
| 45 | + |
| 46 | +### Command Line Options |
| 47 | + |
| 48 | +| Option | Alias | Description | Default | |
| 49 | +|--------|-------|-------------|---------| |
| 50 | +| `--openapi` | `-o` | Path or URL to OpenAPI specification | (required) | |
| 51 | +| `--output` | `-d` | Output directory for generated files | `./mcp-server` | |
| 52 | +| `--name` | `-n` | Name for the MCP server | `openapi-mcp-server` | |
| 53 | +| `--version` | `-v` | Version for the MCP server | `1.0.0` | |
| 54 | +| `--transport` | `-t` | Transport mechanism (stdio, websocket, http) | `stdio` | |
| 55 | +| `--port` | `-p` | Port for websocket or HTTP server | `3000` | |
| 56 | +| `--help` | `-h` | Show help information | | |
| 57 | + |
| 58 | +### Examples |
| 59 | + |
| 60 | +Generate from a local OpenAPI file: |
| 61 | + |
| 62 | +```bash |
| 63 | +./index.js --openapi ./specs/petstore.json --output ./petstore-mcp |
| 64 | +``` |
| 65 | + |
| 66 | +Generate from a remote OpenAPI URL: |
| 67 | + |
| 68 | +```bash |
| 69 | +./index.js --openapi https://petstore3.swagger.io/api/v3/openapi.json --output ./petstore-mcp |
| 70 | +``` |
| 71 | + |
| 72 | +Specify a WebSocket transport: |
| 73 | + |
| 74 | +```bash |
| 75 | +./index.js --openapi ./specs/petstore.json --transport websocket --port 8080 |
| 76 | +``` |
| 77 | + |
| 78 | +## Generated Files |
| 79 | + |
| 80 | +The tool generates the following files in the output directory: |
| 81 | + |
| 82 | +- `server.js` - The main MCP server implementation |
| 83 | +- `package.json` - Dependencies and scripts |
| 84 | +- `README.md` - Documentation for the generated server |
| 85 | +- `.env.example` - Template for environment variables |
| 86 | +- `types.d.ts` - TypeScript type definitions for the API |
| 87 | +- `tsconfig.json` - TypeScript configuration |
| 88 | + |
| 89 | +## Using the Generated Server |
| 90 | + |
| 91 | +After generating your MCP server: |
| 92 | + |
| 93 | +1. Navigate to the generated directory: |
| 94 | + ```bash |
| 95 | + cd my-mcp-server |
| 96 | + ``` |
| 97 | + |
| 98 | +2. Install dependencies: |
| 99 | + ```bash |
| 100 | + npm install |
| 101 | + ``` |
| 102 | + |
| 103 | +3. Create an environment file: |
| 104 | + ```bash |
| 105 | + cp .env.example .env |
| 106 | + ``` |
| 107 | + |
| 108 | +4. Edit `.env` to set your API base URL and any required headers: |
| 109 | + ``` |
| 110 | + API_BASE_URL=https://api.example.com |
| 111 | + API_HEADERS=Authorization:Bearer your-token-here |
| 112 | + ``` |
| 113 | + |
| 114 | +5. Start the server: |
| 115 | + ```bash |
| 116 | + npm start |
| 117 | + ``` |
| 118 | + |
| 119 | +## Requirements |
| 120 | + |
| 121 | +- Node.js 16.x or higher |
| 122 | +- npm 7.x or higher |
| 123 | + |
| 124 | +## License |
| 125 | + |
| 126 | +MIT |
0 commit comments