|
| 1 | +# mcp-openapi |
| 2 | + |
| 3 | +OpenAPI 3.x to MCP server bridge in TypeScript. |
| 4 | + |
| 5 | +This project takes an OpenAPI spec and exposes every operation as an MCP tool, while proxying calls to the original REST API. |
| 6 | + |
| 7 | +## Feature checklist |
| 8 | + |
| 9 | +- OpenAPI 3.0+ support |
| 10 | + - Loads YAML/JSON, dereferences `$ref`, compiles operations into MCP tools. |
| 11 | +- Proxy behavior |
| 12 | + - `tools/call` executes real HTTP requests against your API and returns structured results. |
| 13 | +- Authentication support (env-driven) |
| 14 | + - API key, Bearer, Basic, OAuth2/OpenID Connect token injection. |
| 15 | +- Zod validation |
| 16 | + - Generates Zod schemas from OpenAPI-derived JSON Schema for runtime input validation. |
| 17 | +- Typed server |
| 18 | + - Fully typed TypeScript codebase with strict compile checks. |
| 19 | +- Multiple transports |
| 20 | + - `stdio` |
| 21 | + - `streamable-http` (Hono) |
| 22 | + - deprecated `sse` (Hono + SDK SSE transport) |
| 23 | +- Project scaffold |
| 24 | + - Includes `tsconfig.json`, `package.json`, scripts, entrypoint, tests. |
| 25 | +- Built-in HTML test clients |
| 26 | + - `/test/streamable` and `/test/sse` for browser-based interaction testing. |
| 27 | + |
| 28 | +## Install |
| 29 | + |
| 30 | +```bash |
| 31 | +npm install |
| 32 | +``` |
| 33 | + |
| 34 | +## Run |
| 35 | + |
| 36 | +### 1) stdio transport (default) |
| 37 | + |
| 38 | +```bash |
| 39 | +npm run dev -- --spec ./openapi.yaml |
| 40 | +``` |
| 41 | + |
| 42 | +### 2) StreamableHTTP transport (Hono) |
| 43 | + |
| 44 | +```bash |
| 45 | +npm run dev -- --spec ./openapi.yaml --transport streamable-http --port 3000 |
| 46 | +``` |
| 47 | + |
| 48 | +Endpoints: |
| 49 | + |
| 50 | +- `http://localhost:3000/health` |
| 51 | +- `http://localhost:3000/mcp` |
| 52 | +- `http://localhost:3000/test/streamable` |
| 53 | + |
| 54 | +### 3) SSE transport (deprecated, for compatibility) |
| 55 | + |
| 56 | +```bash |
| 57 | +npm run dev -- --spec ./openapi.yaml --transport sse --port 3000 |
| 58 | +``` |
| 59 | + |
| 60 | +Endpoints: |
| 61 | + |
| 62 | +- `http://localhost:3000/health` |
| 63 | +- `http://localhost:3000/sse` |
| 64 | +- `http://localhost:3000/messages?sessionId=...` |
| 65 | +- `http://localhost:3000/test/sse` |
| 66 | + |
| 67 | +## Common options |
| 68 | + |
| 69 | +```bash |
| 70 | +--server-url <url> Override OpenAPI server URL |
| 71 | +--timeout-ms <ms> HTTP timeout per attempt (default: 20000) |
| 72 | +--retries <n> Retries on 408/429/5xx + transient network errors (default: 2) |
| 73 | +--retry-delay-ms <ms> Base retry delay (default: 500) |
| 74 | +--watch-spec Reload tools when spec changes |
| 75 | +``` |
| 76 | + |
| 77 | +## Authentication env vars |
| 78 | + |
| 79 | +- API key: `MCP_OPENAPI_API_KEY` |
| 80 | +- Bearer: `MCP_OPENAPI_BEARER_TOKEN` |
| 81 | +- Basic: `MCP_OPENAPI_BASIC_USERNAME`, `MCP_OPENAPI_BASIC_PASSWORD` |
| 82 | +- OAuth2/OIDC bearer token: `MCP_OPENAPI_OAUTH2_ACCESS_TOKEN` |
| 83 | +- Per-scheme override: `MCP_OPENAPI_<SCHEME_NAME>_TOKEN` |
| 84 | + |
| 85 | +## Validation model |
| 86 | + |
| 87 | +Input validation pipeline for `tools/call`: |
| 88 | + |
| 89 | +1. Zod validation (generated from OpenAPI-derived schema) |
| 90 | +2. JSON Schema validation (Ajv) |
| 91 | + |
| 92 | +Success responses can also be validated against OpenAPI response schemas, and tool output is validated against MCP `outputSchema`. |
| 93 | + |
| 94 | +## Generated tool shape |
| 95 | + |
| 96 | +Each OpenAPI operation becomes an MCP tool with: |
| 97 | + |
| 98 | +- `name` |
| 99 | +- `description` |
| 100 | +- `inputSchema` |
| 101 | +- `outputSchema` (when available) |
| 102 | +- tool annotations (`readOnlyHint`, `idempotentHint`, etc.) |
| 103 | + |
| 104 | +Input argument groups: |
| 105 | + |
| 106 | +```json |
| 107 | +{ |
| 108 | + "path": {}, |
| 109 | + "query": {}, |
| 110 | + "header": {}, |
| 111 | + "cookie": {}, |
| 112 | + "body": {}, |
| 113 | + "pagination": { |
| 114 | + "enabled": false, |
| 115 | + "mode": "autoCursor", |
| 116 | + "maxPages": 5, |
| 117 | + "cursorParam": "cursor", |
| 118 | + "nextCursorPath": "next_cursor", |
| 119 | + "pageParam": "page", |
| 120 | + "startPage": 1 |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## Request/response features |
| 126 | + |
| 127 | +- Query/path/header/cookie style-aware serialization |
| 128 | +- Request body content types: |
| 129 | + - `application/json` |
| 130 | + - `application/x-www-form-urlencoded` |
| 131 | + - `multipart/form-data` |
| 132 | + - `application/octet-stream` |
| 133 | +- Pagination helpers: |
| 134 | + - cursor mode |
| 135 | + - incrementing page mode |
| 136 | +- Retry with `Retry-After` support |
| 137 | +- Progress notifications via MCP `_meta.progressToken` |
| 138 | +- Cancellation support via MCP cancellation |
| 139 | + |
| 140 | +## Build/test/smoke |
| 141 | + |
| 142 | +```bash |
| 143 | +npm run check |
| 144 | +npm run build |
| 145 | +npm test |
| 146 | +npm run smoke |
| 147 | +``` |
| 148 | + |
| 149 | +`npm run smoke` starts a full MCP client/server+HTTP flow and verifies that an OpenAPI spec is transformed into callable MCP tools. |
| 150 | + |
| 151 | +## Browser test clients |
| 152 | + |
| 153 | +After launching a web transport: |
| 154 | + |
| 155 | +- StreamableHTTP client: `http://localhost:<port>/test/streamable` |
| 156 | +- SSE client: `http://localhost:<port>/test/sse` |
| 157 | + |
| 158 | +These pages let you initialize, list tools, and call tools directly in-browser. |
0 commit comments