openapi-mcp-codegen 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +255 -0
- package/dist/cli.js +1151 -0
- package/dist/generator/index.d.ts +3 -0
- package/dist/generator/index.d.ts.map +1 -0
- package/dist/generator/templates.d.ts +3 -0
- package/dist/generator/templates.d.ts.map +1 -0
- package/dist/index.cjs +1133 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1104 -0
- package/dist/index.js.map +1 -0
- package/dist/mapper/index.d.ts +3 -0
- package/dist/mapper/index.d.ts.map +1 -0
- package/dist/parser/index.d.ts +3 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/refs.d.ts +12 -0
- package/dist/parser/refs.d.ts.map +1 -0
- package/dist/types.d.ts +145 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 devla
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# openapi-mcp-codegen
|
|
2
|
+
|
|
3
|
+
Generate [MCP](https://modelcontextprotocol.io) servers from OpenAPI 3.x specifications. One command, working server.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npx openapi-mcp-codegen generate -i petstore.yaml -o ./mcp-server
|
|
7
|
+
cd mcp-server && npm install && npm start
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
The generated server works with Claude Desktop, Claude Code, Cursor, VS Code Copilot, ChatGPT, Gemini CLI, and any other MCP-compatible client.
|
|
11
|
+
|
|
12
|
+
## What it does
|
|
13
|
+
|
|
14
|
+
Takes an OpenAPI 3.x spec (YAML or JSON) and produces a TypeScript MCP server where each API operation becomes an MCP tool with:
|
|
15
|
+
|
|
16
|
+
- Zod-validated input schema derived from parameters and request body
|
|
17
|
+
- Proper path interpolation and query string building
|
|
18
|
+
- Auth handling (API key, Bearer, OAuth2 client credentials, OAuth2 authorization code + PKCE)
|
|
19
|
+
- Automatic pagination (cursor-based wrapper for offset/page/cursor APIs)
|
|
20
|
+
- Streaming response support (SSE, NDJSON)
|
|
21
|
+
- `$ref` resolution (inlined at build time for maximum client compatibility)
|
|
22
|
+
- Streamable HTTP transport (in addition to stdio)
|
|
23
|
+
|
|
24
|
+
## Quick start
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# From a local file
|
|
28
|
+
npx openapi-mcp-codegen generate -i api-spec.yaml -o ./my-server
|
|
29
|
+
|
|
30
|
+
# From a URL
|
|
31
|
+
npx openapi-mcp-codegen generate -i https://petstore3.swagger.io/api/v3/openapi.json -o ./petstore
|
|
32
|
+
|
|
33
|
+
# Preview tools without generating (dry run)
|
|
34
|
+
npx openapi-mcp-codegen generate -i spec.yaml --dry-run
|
|
35
|
+
|
|
36
|
+
# Generate with Streamable HTTP transport
|
|
37
|
+
npx openapi-mcp-codegen generate -i spec.yaml -o ./server --transport streamable-http
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Use with Claude Desktop
|
|
41
|
+
|
|
42
|
+
Add the generated server to your Claude Desktop config (`claude_desktop_config.json`):
|
|
43
|
+
|
|
44
|
+
```json
|
|
45
|
+
{
|
|
46
|
+
"mcpServers": {
|
|
47
|
+
"my-api": {
|
|
48
|
+
"command": "node",
|
|
49
|
+
"args": ["path/to/mcp-server/dist/server.js"],
|
|
50
|
+
"env": {
|
|
51
|
+
"API_BASE_URL": "https://api.example.com",
|
|
52
|
+
"API_KEY": "your-key"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Use with Cursor
|
|
60
|
+
|
|
61
|
+
Add to `.cursor/mcp.json` in your project:
|
|
62
|
+
|
|
63
|
+
```json
|
|
64
|
+
{
|
|
65
|
+
"mcpServers": {
|
|
66
|
+
"my-api": {
|
|
67
|
+
"command": "node",
|
|
68
|
+
"args": ["path/to/mcp-server/dist/server.js"],
|
|
69
|
+
"env": {
|
|
70
|
+
"API_BASE_URL": "https://api.example.com",
|
|
71
|
+
"API_KEY": "your-key"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## CLI options
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
openapi-mcp-codegen generate [options]
|
|
82
|
+
|
|
83
|
+
Options:
|
|
84
|
+
-i, --input <path> Path or URL to OpenAPI spec (required)
|
|
85
|
+
-o, --output <dir> Output directory (default: ./mcp-server)
|
|
86
|
+
-n, --name <name> Server name (default: spec title)
|
|
87
|
+
--transport <type> stdio | streamable-http (default: stdio)
|
|
88
|
+
--auth <type> none | api-key | bearer | oauth2 | oauth2-auth-code
|
|
89
|
+
(default: auto-detect from spec)
|
|
90
|
+
--include-tags <tags> Only include operations with these tags (comma-separated)
|
|
91
|
+
--exclude-tags <tags> Exclude operations with these tags (comma-separated)
|
|
92
|
+
--dry-run Print tool definitions as JSON without generating
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Auth
|
|
96
|
+
|
|
97
|
+
Auth is auto-detected from the spec's `securitySchemes` when `--auth` is not set. If the spec has an `authorizationCode` flow, it auto-selects `oauth2-auth-code`.
|
|
98
|
+
|
|
99
|
+
| Mode | Env vars | Description |
|
|
100
|
+
|------|----------|-------------|
|
|
101
|
+
| `none` | - | No authentication |
|
|
102
|
+
| `api-key` | `API_KEY` | API key sent in the header defined in spec (e.g. `X-API-Key`) |
|
|
103
|
+
| `bearer` | `API_TOKEN` | Bearer token in `Authorization` header |
|
|
104
|
+
| `oauth2` | `OAUTH_CLIENT_ID`, `OAUTH_CLIENT_SECRET`, `OAUTH_TOKEN_URL` | Client credentials grant with token caching |
|
|
105
|
+
| `oauth2-auth-code` | `OAUTH_CLIENT_ID`, `OAUTH_CLIENT_SECRET`, `OAUTH_AUTH_URL`, `OAUTH_TOKEN_URL`, `OAUTH_SCOPES` | Authorization code + PKCE with local callback server, automatic token refresh |
|
|
106
|
+
|
|
107
|
+
### OAuth2 Authorization Code + PKCE
|
|
108
|
+
|
|
109
|
+
When using `oauth2-auth-code`, the generated server:
|
|
110
|
+
|
|
111
|
+
1. Starts a temporary local HTTP server on port 8976 (configurable via `OAUTH_REDIRECT_PORT`)
|
|
112
|
+
2. Prints an authorization URL to stderr
|
|
113
|
+
3. Waits for the OAuth callback with the authorization code
|
|
114
|
+
4. Exchanges the code for tokens using PKCE (S256 challenge)
|
|
115
|
+
5. Automatically refreshes tokens when they expire
|
|
116
|
+
6. Falls back to re-authorization if refresh fails
|
|
117
|
+
|
|
118
|
+
This is the recommended mode for APIs that require user authentication (Google, GitHub, Slack, etc.).
|
|
119
|
+
|
|
120
|
+
## Pagination
|
|
121
|
+
|
|
122
|
+
Pagination is auto-detected from parameter names in GET endpoints:
|
|
123
|
+
|
|
124
|
+
| Pattern | Detection | Example params |
|
|
125
|
+
|---------|-----------|----------------|
|
|
126
|
+
| Offset-based | `limit` + `offset`/`skip` | `?limit=20&offset=40` |
|
|
127
|
+
| Page-based | `per_page`/`page_size` + `page` | `?per_page=20&page=3` |
|
|
128
|
+
| Cursor-based | `limit` + `cursor`/`after`/`starting_after` | `?limit=20&after=abc123` |
|
|
129
|
+
|
|
130
|
+
When pagination is detected, the generated tool:
|
|
131
|
+
- Replaces raw offset/page params with an opaque `cursor` parameter
|
|
132
|
+
- Adds a `limit` parameter
|
|
133
|
+
- Returns `{ items, has_more, next_cursor, total }` in the response
|
|
134
|
+
- Encodes/decodes cursor state as base64url
|
|
135
|
+
|
|
136
|
+
You can also explicitly configure pagination with vendor extensions:
|
|
137
|
+
|
|
138
|
+
```yaml
|
|
139
|
+
paths:
|
|
140
|
+
/messages:
|
|
141
|
+
get:
|
|
142
|
+
x-mcp-pagination:
|
|
143
|
+
type: offset
|
|
144
|
+
limitParam: count
|
|
145
|
+
offsetParam: skip
|
|
146
|
+
itemsPath: data
|
|
147
|
+
totalPath: total_count
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Streaming
|
|
151
|
+
|
|
152
|
+
Endpoints that return `text/event-stream`, `application/x-ndjson`, or `application/stream+json` are automatically detected and handled with a streaming reader that collects all chunks before returning.
|
|
153
|
+
|
|
154
|
+
## Vendor extensions
|
|
155
|
+
|
|
156
|
+
Control how operations are mapped to MCP tools:
|
|
157
|
+
|
|
158
|
+
```yaml
|
|
159
|
+
paths:
|
|
160
|
+
/internal/health:
|
|
161
|
+
get:
|
|
162
|
+
x-mcp-exclude: true # Skip this operation entirely
|
|
163
|
+
|
|
164
|
+
/users:
|
|
165
|
+
get:
|
|
166
|
+
x-mcp-name: search_users # Override the tool name
|
|
167
|
+
x-mcp-description: | # Override the tool description
|
|
168
|
+
Search users by name or email.
|
|
169
|
+
Returns up to 100 results.
|
|
170
|
+
|
|
171
|
+
/messages:
|
|
172
|
+
get:
|
|
173
|
+
x-mcp-pagination: # Explicit pagination config
|
|
174
|
+
type: offset
|
|
175
|
+
limitParam: count
|
|
176
|
+
offsetParam: skip
|
|
177
|
+
itemsPath: data
|
|
178
|
+
totalPath: total_count
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Generated output
|
|
182
|
+
|
|
183
|
+
```
|
|
184
|
+
mcp-server/
|
|
185
|
+
src/
|
|
186
|
+
server.ts # MCP server with all tool definitions
|
|
187
|
+
package.json # Dependencies: @modelcontextprotocol/sdk, zod
|
|
188
|
+
tsconfig.json # TypeScript config
|
|
189
|
+
.env.example # Environment variables template
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Build and run:
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
cd mcp-server
|
|
196
|
+
npm install
|
|
197
|
+
npm run build
|
|
198
|
+
npm start
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Or with environment variables:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
API_BASE_URL=https://api.example.com API_KEY=xxx npm start
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## How it works
|
|
208
|
+
|
|
209
|
+
1. **Parser** loads the spec (file or URL), validates OpenAPI 3.x, resolves all `$ref` inline. Inline resolution is required because some MCP clients (Claude Code, Amazon Bedrock) don't handle `$ref` in tool schemas.
|
|
210
|
+
|
|
211
|
+
2. **Mapper** converts each operation to an MCP tool definition. Parameters (path, query) and request body properties are flattened into a single input schema. Auth requirements are extracted from security schemes. Pagination patterns are auto-detected from parameter names and response schemas. Streaming content types are flagged.
|
|
212
|
+
|
|
213
|
+
3. **Generator** renders a TypeScript file with MCP SDK imports, Zod schemas, auth helpers (including full OAuth2 PKCE flow), pagination cursor encode/decode, and transport setup (stdio or Streamable HTTP). Outputs a complete project with `package.json` and `tsconfig.json`.
|
|
214
|
+
|
|
215
|
+
## Programmatic API
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
import { parseSpec, mapToMcpTools, generate } from "openapi-mcp-codegen";
|
|
219
|
+
|
|
220
|
+
const spec = await parseSpec("./api-spec.yaml");
|
|
221
|
+
const tools = mapToMcpTools(spec, {
|
|
222
|
+
includeTags: ["public"],
|
|
223
|
+
excludeOperations: ["deprecated_endpoint"],
|
|
224
|
+
detectPagination: true, // default: true
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
await generate({
|
|
228
|
+
tools,
|
|
229
|
+
spec,
|
|
230
|
+
outputDir: "./output",
|
|
231
|
+
serverName: "My API",
|
|
232
|
+
transport: "stdio",
|
|
233
|
+
auth: "oauth2-auth-code",
|
|
234
|
+
});
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Roadmap
|
|
238
|
+
|
|
239
|
+
- [x] OAuth2 Authorization Code + PKCE
|
|
240
|
+
- [x] Cursor-based pagination wrapper (offset, page, cursor)
|
|
241
|
+
- [x] Streaming response handling (SSE, NDJSON)
|
|
242
|
+
- [x] Streamable HTTP transport
|
|
243
|
+
- [ ] Workflow grouping (merge related endpoints into composite tools)
|
|
244
|
+
- [ ] Multi-client schema adaptation (Claude vs Bedrock vs GPT)
|
|
245
|
+
- [ ] MCP 2026-07-28 spec support (stateless)
|
|
246
|
+
- [ ] Pre-built presets for popular APIs (Stripe, GitHub, Notion)
|
|
247
|
+
- [ ] npm publish
|
|
248
|
+
|
|
249
|
+
## Contributing
|
|
250
|
+
|
|
251
|
+
PRs welcome. Please open an issue first to discuss non-trivial changes.
|
|
252
|
+
|
|
253
|
+
## License
|
|
254
|
+
|
|
255
|
+
MIT
|