@ampeco/public-api-mcp 0.2.2 → 0.3.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/README.md +235 -4
- package/dist/build/optimizer.d.ts.map +1 -1
- package/dist/build/optimizer.js +82 -21
- package/dist/build/optimizer.js.map +1 -1
- package/dist/build/parser.d.ts.map +1 -1
- package/dist/build/parser.js +74 -1
- package/dist/build/parser.js.map +1 -1
- package/dist/cli/index.js +25 -11
- package/dist/cli/index.js.map +1 -1
- package/dist/generated/build-stats.json +5 -5
- package/dist/generated/endpoints.json +29214 -20896
- package/dist/generated/schemas.d.ts +7530 -2789
- package/dist/generated/schemas.d.ts.map +1 -1
- package/dist/generated/schemas.js +1443 -590
- package/dist/generated/schemas.js.map +1 -1
- package/dist/generated/schemas.ts +1447 -596
- package/dist/server/factory.d.ts +2 -1
- package/dist/server/factory.d.ts.map +1 -1
- package/dist/server/factory.js +17 -206
- package/dist/server/factory.js.map +1 -1
- package/dist/server/factory.test.js +62 -4
- package/dist/server/factory.test.js.map +1 -1
- package/dist/server/http.d.ts +2 -1
- package/dist/server/http.d.ts.map +1 -1
- package/dist/server/http.js +8 -2
- package/dist/server/http.js.map +1 -1
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +9 -1388
- package/dist/server/index.js.map +1 -1
- package/dist/server/instructions.d.ts +13 -0
- package/dist/server/instructions.d.ts.map +1 -0
- package/dist/server/instructions.js +146 -0
- package/dist/server/instructions.js.map +1 -0
- package/dist/server/resources/handlers.d.ts +31 -0
- package/dist/server/resources/handlers.d.ts.map +1 -0
- package/dist/server/resources/handlers.js +113 -0
- package/dist/server/resources/handlers.js.map +1 -0
- package/dist/server/resources/handlers.test.d.ts +6 -0
- package/dist/server/resources/handlers.test.d.ts.map +1 -0
- package/dist/server/resources/handlers.test.js +203 -0
- package/dist/server/resources/handlers.test.js.map +1 -0
- package/dist/server/resources/native.d.ts +11 -0
- package/dist/server/resources/native.d.ts.map +1 -0
- package/dist/server/resources/native.js +31 -0
- package/dist/server/resources/native.js.map +1 -0
- package/dist/server/resources/tools.d.ts +11 -0
- package/dist/server/resources/tools.d.ts.map +1 -0
- package/dist/server/resources/tools.js +41 -0
- package/dist/server/resources/tools.js.map +1 -0
- package/dist/server/stdio.d.ts +2 -1
- package/dist/server/stdio.d.ts.map +1 -1
- package/dist/server/stdio.js +8 -2
- package/dist/server/stdio.js.map +1 -1
- package/dist/server/types.d.ts +36 -0
- package/dist/server/types.d.ts.map +1 -0
- package/dist/server/types.js +6 -0
- package/dist/server/types.js.map +1 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -106,12 +106,26 @@ Generated Artifacts (endpoints.json, schemas.ts)
|
|
|
106
106
|
The server provides a complete MCP implementation for accessing your OpenAPI-defined API:
|
|
107
107
|
|
|
108
108
|
- **Dual Transport Support**: Both Stdio (local) and HTTP (remote) transports
|
|
109
|
+
- **Flexible Resource Mode**: Native MCP resources OR tool-based emulation for Claude Desktop compatibility
|
|
109
110
|
- **Dynamic Resources**: All API endpoints exposed as MCP resources with full schema details
|
|
110
111
|
- **API Request Tool**: Fully typed `api_request` tool with automatic authentication and parameter validation
|
|
111
112
|
- **Stateless Architecture**: No session state required - all parameters provided per request
|
|
112
113
|
- **Zero Configuration**: Pre-processed data loads instantly, no config files needed
|
|
113
114
|
- **MCP Protocol Compliance**: Full implementation of MCP Streamable HTTP transport specification
|
|
114
115
|
|
|
116
|
+
#### Resource Modes
|
|
117
|
+
|
|
118
|
+
The server supports two modes for exposing API endpoints:
|
|
119
|
+
|
|
120
|
+
1. **Native Resources Mode** (Default): Uses native MCP resources for endpoint discovery
|
|
121
|
+
- Best for: MCP clients that fully support the resources capability
|
|
122
|
+
- Features: Hierarchical 3-tier navigation (tags → endpoints → details)
|
|
123
|
+
|
|
124
|
+
2. **Tool Emulation Mode**: Exposes resources through tools (`list_resources`, `read_resource`)
|
|
125
|
+
- **Required for Claude Desktop** (does not support native MCP resources)
|
|
126
|
+
- Provides identical functionality through tool-based interface
|
|
127
|
+
- Enable with `--emulate-resources-via-tools` flag
|
|
128
|
+
|
|
115
129
|
### Running the Server
|
|
116
130
|
|
|
117
131
|
```bash
|
|
@@ -141,6 +155,160 @@ The server uses a hostname-based routing structure where the target API hostname
|
|
|
141
155
|
- `POST /api.example.com/http` → Routes to `http://api.example.com`
|
|
142
156
|
- `POST /internal.api.company.com/http` → Routes to `http://internal.api.company.com`
|
|
143
157
|
|
|
158
|
+
## Setting Up with Claude Desktop
|
|
159
|
+
|
|
160
|
+
**IMPORTANT**: Claude Desktop **does not support native MCP resources**. You **MUST** use the `--emulate-resources-via-tools` flag for Claude Desktop.
|
|
161
|
+
|
|
162
|
+
### Claude Desktop Configuration
|
|
163
|
+
|
|
164
|
+
Claude Desktop uses a configuration file called `claude_desktop_config.json` located at:
|
|
165
|
+
|
|
166
|
+
- **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
167
|
+
- **Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
|
|
168
|
+
|
|
169
|
+
You can also access this file through Claude Desktop: **Settings** → **Developer** → **Edit Config**
|
|
170
|
+
|
|
171
|
+
### Configuration Example
|
|
172
|
+
|
|
173
|
+
Add this to your `claude_desktop_config.json`:
|
|
174
|
+
|
|
175
|
+
```json
|
|
176
|
+
{
|
|
177
|
+
"mcpServers": {
|
|
178
|
+
"ampeco-api": {
|
|
179
|
+
"command": "npx",
|
|
180
|
+
"args": [
|
|
181
|
+
"-y",
|
|
182
|
+
"@ampeco/public-api-mcp",
|
|
183
|
+
"--stdio",
|
|
184
|
+
"--hostname",
|
|
185
|
+
"https://api.example.com",
|
|
186
|
+
"--emulate-resources-via-tools"
|
|
187
|
+
],
|
|
188
|
+
"env": {
|
|
189
|
+
"AMPECO_BEARER_TOKEN": "your-token-here"
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
**Replace**:
|
|
197
|
+
- `https://api.example.com` with your actual API hostname
|
|
198
|
+
- `your-token-here` with your bearer token
|
|
199
|
+
|
|
200
|
+
**After editing the file**, restart Claude Desktop for changes to take effect
|
|
201
|
+
|
|
202
|
+
### What is Tool Emulation Mode?
|
|
203
|
+
|
|
204
|
+
When `--emulate-resources-via-tools` is enabled, the server provides two additional tools:
|
|
205
|
+
|
|
206
|
+
1. **`list_resources`**: Discover available API tags (e.g., Users, ChargingSessions)
|
|
207
|
+
- No parameters required
|
|
208
|
+
- Returns list of tags that group related endpoints
|
|
209
|
+
|
|
210
|
+
2. **`read_resource`**: Read endpoint definitions by URI
|
|
211
|
+
- Parameter: `uri` (e.g., `tag://Users` or `api://GET/users/{id}`)
|
|
212
|
+
- Returns full endpoint specification
|
|
213
|
+
|
|
214
|
+
These tools provide **identical functionality** to native MCP resources but work through the tools interface that Claude Desktop supports.
|
|
215
|
+
|
|
216
|
+
### Tool Schemas (Emulation Mode)
|
|
217
|
+
|
|
218
|
+
When `--emulate-resources-via-tools` is enabled, the following tools are available:
|
|
219
|
+
|
|
220
|
+
#### `list_resources` Tool
|
|
221
|
+
|
|
222
|
+
**Description**: List all available API tags. This emulates the native MCP resources/list functionality. Returns a list of tags that group related endpoints together (e.g., Users, ChargingSessions, Connectors). Each tag can then be read using the read_resource tool with tag:// URIs.
|
|
223
|
+
|
|
224
|
+
**Parameters**: None
|
|
225
|
+
|
|
226
|
+
**Returns**: JSON object with `resources` array containing:
|
|
227
|
+
```json
|
|
228
|
+
{
|
|
229
|
+
"resources": [
|
|
230
|
+
{
|
|
231
|
+
"uri": "tag://Users",
|
|
232
|
+
"name": "Users",
|
|
233
|
+
"description": "15 endpoints tagged with 'Users'",
|
|
234
|
+
"mimeType": "application/json"
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
"uri": "tag://ChargingSessions",
|
|
238
|
+
"name": "ChargingSessions",
|
|
239
|
+
"description": "8 endpoints tagged with 'ChargingSessions'",
|
|
240
|
+
"mimeType": "application/json"
|
|
241
|
+
}
|
|
242
|
+
]
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
**Example Usage**:
|
|
247
|
+
```
|
|
248
|
+
Call list_resources tool → Get list of all API tags
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
#### `read_resource` Tool
|
|
252
|
+
|
|
253
|
+
**Description**: Read a specific resource by URI. This emulates the native MCP resources/read functionality. Supports two URI formats: tag://{TagName} to read all endpoints within a tag, and api://{METHOD}{path} to read detailed endpoint specifications. You MUST use this tool to read endpoint definitions before calling api_request.
|
|
254
|
+
|
|
255
|
+
**Parameters**:
|
|
256
|
+
- `uri` (string, required): The resource URI to read
|
|
257
|
+
- Format: `tag://{TagName}` or `api://{METHOD}{path}`
|
|
258
|
+
- Examples: `"tag://Users"`, `"api://GET/users/{id}"`, `"api://POST/charging-sessions"`
|
|
259
|
+
|
|
260
|
+
**Returns** (for tag URIs):
|
|
261
|
+
```json
|
|
262
|
+
{
|
|
263
|
+
"tag": "Users",
|
|
264
|
+
"count": 15,
|
|
265
|
+
"endpoints": [
|
|
266
|
+
{
|
|
267
|
+
"uri": "api://GET/users/{id}",
|
|
268
|
+
"method": "GET",
|
|
269
|
+
"path": "/users/{id}",
|
|
270
|
+
"summary": "Get user by ID",
|
|
271
|
+
"operationId": "getUser"
|
|
272
|
+
}
|
|
273
|
+
]
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
**Returns** (for api URIs):
|
|
278
|
+
```json
|
|
279
|
+
{
|
|
280
|
+
"path": "/users/{id}",
|
|
281
|
+
"method": "GET",
|
|
282
|
+
"operationId": "getUser",
|
|
283
|
+
"summary": "Get user by ID",
|
|
284
|
+
"description": "Retrieves detailed information about a specific user",
|
|
285
|
+
"parameters": [...],
|
|
286
|
+
"requestBody": {...},
|
|
287
|
+
"responses": {...},
|
|
288
|
+
"security": "Include token in Authorization header as: Authorization: Bearer <token>"
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
**Example Usage**:
|
|
293
|
+
```
|
|
294
|
+
1. Call read_resource with uri="tag://Users" → Get all endpoints in Users tag
|
|
295
|
+
2. Call read_resource with uri="api://GET/users/{id}" → Get full endpoint specification
|
|
296
|
+
3. Call api_request with correct parameters → Make the actual API call
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### When to Use Each Mode
|
|
300
|
+
|
|
301
|
+
| Mode | Use For | Clients |
|
|
302
|
+
|------|---------|---------|
|
|
303
|
+
| **Native Resources** (default) | MCP clients that fully support resources | **Claude Code**, MCP Inspector |
|
|
304
|
+
| **Tool Emulation** (`--emulate-resources-via-tools`) | Clients without resource support | **Claude Desktop** |
|
|
305
|
+
|
|
306
|
+
**Important Distinction**:
|
|
307
|
+
- **Claude Code** (the CLI tool): Supports native MCP resources - use **default mode**
|
|
308
|
+
- **Claude Desktop** (the desktop app): Does NOT support resources - use **`--emulate-resources-via-tools`**
|
|
309
|
+
|
|
310
|
+
**Rule of thumb**: If you're using **Claude Desktop**, always add `--emulate-resources-via-tools`. If using **Claude Code**, use default mode.
|
|
311
|
+
|
|
144
312
|
## Setting Up with Claude Code
|
|
145
313
|
|
|
146
314
|
The MCP server supports two transport modes:
|
|
@@ -203,10 +371,15 @@ For production use without needing the source code:
|
|
|
203
371
|
#### Option 1: One-Liner (Recommended)
|
|
204
372
|
|
|
205
373
|
```bash
|
|
206
|
-
# Add production server with a single command
|
|
374
|
+
# Add production server with a single command (native resources)
|
|
207
375
|
claude mcp add --transport stdio ampeco-api \
|
|
208
376
|
--env AMPECO_BEARER_TOKEN=your-token-here \
|
|
209
377
|
-- npx -y @ampeco/public-api-mcp --stdio --hostname https://api.example.com
|
|
378
|
+
|
|
379
|
+
# With tool emulation mode (if client doesn't support resources)
|
|
380
|
+
claude mcp add --transport stdio ampeco-api \
|
|
381
|
+
--env AMPECO_BEARER_TOKEN=your-token-here \
|
|
382
|
+
-- npx -y @ampeco/public-api-mcp --stdio --hostname https://api.example.com --emulate-resources-via-tools
|
|
210
383
|
```
|
|
211
384
|
|
|
212
385
|
**Replace**:
|
|
@@ -217,11 +390,13 @@ claude mcp add --transport stdio ampeco-api \
|
|
|
217
390
|
- NPX downloads and caches the package automatically
|
|
218
391
|
- No source code or build required
|
|
219
392
|
- Works from any directory
|
|
393
|
+
- Add `--emulate-resources-via-tools` for Claude Desktop compatibility
|
|
220
394
|
|
|
221
395
|
#### Option 2: Manual Configuration
|
|
222
396
|
|
|
223
397
|
Create `.mcp.json` in your project or home directory:
|
|
224
398
|
|
|
399
|
+
**Native Resources Mode**:
|
|
225
400
|
```json
|
|
226
401
|
{
|
|
227
402
|
"mcpServers": {
|
|
@@ -243,6 +418,29 @@ Create `.mcp.json` in your project or home directory:
|
|
|
243
418
|
}
|
|
244
419
|
```
|
|
245
420
|
|
|
421
|
+
**Tool Emulation Mode** (for Claude Desktop):
|
|
422
|
+
```json
|
|
423
|
+
{
|
|
424
|
+
"mcpServers": {
|
|
425
|
+
"ampeco-api": {
|
|
426
|
+
"type": "stdio",
|
|
427
|
+
"command": "npx",
|
|
428
|
+
"args": [
|
|
429
|
+
"-y",
|
|
430
|
+
"@ampeco/public-api-mcp",
|
|
431
|
+
"--stdio",
|
|
432
|
+
"--hostname",
|
|
433
|
+
"https://api.example.com",
|
|
434
|
+
"--emulate-resources-via-tools"
|
|
435
|
+
],
|
|
436
|
+
"env": {
|
|
437
|
+
"AMPECO_BEARER_TOKEN": "your-token-here"
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
```
|
|
443
|
+
|
|
246
444
|
**Using environment variables** (more secure):
|
|
247
445
|
```bash
|
|
248
446
|
export AMPECO_BEARER_TOKEN="your-token-here"
|
|
@@ -263,13 +461,20 @@ npm install -g @ampeco/public-api-mcp
|
|
|
263
461
|
#### 2. Add to Claude Code (One-Liner)
|
|
264
462
|
|
|
265
463
|
```bash
|
|
266
|
-
# Add globally installed server
|
|
464
|
+
# Add globally installed server (native resources)
|
|
267
465
|
claude mcp add --transport stdio ampeco-api \
|
|
268
466
|
--env AMPECO_BEARER_TOKEN=your-token-here \
|
|
269
467
|
-- ampeco-api-mcp --stdio --hostname https://api.example.com
|
|
468
|
+
|
|
469
|
+
# With tool emulation mode (for Claude Desktop)
|
|
470
|
+
claude mcp add --transport stdio ampeco-api \
|
|
471
|
+
--env AMPECO_BEARER_TOKEN=your-token-here \
|
|
472
|
+
-- ampeco-api-mcp --stdio --hostname https://api.example.com --emulate-resources-via-tools
|
|
270
473
|
```
|
|
271
474
|
|
|
272
475
|
**Or manual configuration** in `.mcp.json`:
|
|
476
|
+
|
|
477
|
+
**Native Resources Mode**:
|
|
273
478
|
```json
|
|
274
479
|
{
|
|
275
480
|
"mcpServers": {
|
|
@@ -283,6 +488,25 @@ claude mcp add --transport stdio ampeco-api \
|
|
|
283
488
|
}
|
|
284
489
|
```
|
|
285
490
|
|
|
491
|
+
**Tool Emulation Mode** (for Claude Desktop):
|
|
492
|
+
```json
|
|
493
|
+
{
|
|
494
|
+
"mcpServers": {
|
|
495
|
+
"ampeco-api": {
|
|
496
|
+
"type": "stdio",
|
|
497
|
+
"command": "ampeco-api-mcp",
|
|
498
|
+
"args": [
|
|
499
|
+
"--stdio",
|
|
500
|
+
"--hostname",
|
|
501
|
+
"https://api.example.com",
|
|
502
|
+
"--emulate-resources-via-tools"
|
|
503
|
+
],
|
|
504
|
+
"env": {"AMPECO_BEARER_TOKEN": "your-token-here"}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
```
|
|
509
|
+
|
|
286
510
|
### HTTP Mode (Remote Server)
|
|
287
511
|
|
|
288
512
|
For shared server access or when multiple clients need to connect:
|
|
@@ -290,12 +514,19 @@ For shared server access or when multiple clients need to connect:
|
|
|
290
514
|
#### 1. Start the Server
|
|
291
515
|
|
|
292
516
|
```bash
|
|
293
|
-
# Development mode
|
|
517
|
+
# Development mode (native resources)
|
|
294
518
|
PORT=3001 npm run dev:http
|
|
295
519
|
|
|
296
|
-
#
|
|
520
|
+
# Development mode (tool emulation)
|
|
521
|
+
PORT=3001 npm start -- --http --port 3001 --emulate-resources-via-tools
|
|
522
|
+
|
|
523
|
+
# Production mode (native resources)
|
|
297
524
|
npm run build # First time only
|
|
298
525
|
PORT=3001 npm start -- --http --port 3001
|
|
526
|
+
|
|
527
|
+
# Production mode (tool emulation)
|
|
528
|
+
npm run build # First time only
|
|
529
|
+
PORT=3001 npm start -- --http --port 3001 --emulate-resources-via-tools
|
|
299
530
|
```
|
|
300
531
|
|
|
301
532
|
#### 2. Add to Claude Code (One-Liner)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"optimizer.d.ts","sourceRoot":"","sources":["../../src/build/optimizer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEpD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,iBAAiB,CAc/E;
|
|
1
|
+
{"version":3,"file":"optimizer.d.ts","sourceRoot":"","sources":["../../src/build/optimizer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEpD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,iBAAiB,CAc/E;AA8PD;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,GAAG;IACrE,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;CAC1B,CAaA"}
|
package/dist/build/optimizer.js
CHANGED
|
@@ -45,31 +45,91 @@ function optimizeDescription(description) {
|
|
|
45
45
|
.trim();
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
-
* Optimize parameters by
|
|
48
|
+
* Optimize parameters by grouping by type (path/query) and using object keys
|
|
49
|
+
*
|
|
50
|
+
* Transforms flat OpenAPI parameter array into a more intuitive structure:
|
|
51
|
+
* - Separates path and query parameters into distinct objects
|
|
52
|
+
* - Uses parameter name as the object key (eliminating redundant "name" field)
|
|
53
|
+
* - Maintains readable field names (type, description, required, etc.)
|
|
54
|
+
* - Reduces nesting while keeping the format clear and LLM-friendly
|
|
55
|
+
*
|
|
56
|
+
* Example:
|
|
57
|
+
* Before: [{"name": "userId", "in": "path", "required": true, "schema": {"type": "integer"}}]
|
|
58
|
+
* After: {"path": {"userId": {"type": "integer", "required": true}}}
|
|
49
59
|
*
|
|
50
60
|
* @param parameters - Array of parameters
|
|
51
|
-
* @returns Optimized parameters
|
|
61
|
+
* @returns Optimized parameters object with named keys
|
|
52
62
|
*/
|
|
53
63
|
function optimizeParameters(parameters) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
64
|
+
if (!parameters || parameters.length === 0) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const pathParams = {};
|
|
68
|
+
const queryParams = {};
|
|
69
|
+
for (const param of parameters) {
|
|
70
|
+
const optimized = {};
|
|
71
|
+
// Add description if present
|
|
59
72
|
if (param.description) {
|
|
60
73
|
optimized.description = optimizeDescription(param.description);
|
|
61
74
|
}
|
|
62
|
-
|
|
63
|
-
optimized.required = true; // Only include if true
|
|
64
|
-
}
|
|
75
|
+
// Add schema info
|
|
65
76
|
if (param.schema) {
|
|
66
|
-
|
|
77
|
+
const schema = param.schema;
|
|
78
|
+
// For simple types, flatten the schema into the parameter
|
|
79
|
+
if (schema.type && !schema.properties && !schema.items && !schema.enum && !schema.oneOf && !schema.anyOf) {
|
|
80
|
+
optimized.type = schema.type;
|
|
81
|
+
// Add format if present
|
|
82
|
+
if (schema.format) {
|
|
83
|
+
optimized.format = schema.format;
|
|
84
|
+
}
|
|
85
|
+
// Add constraints
|
|
86
|
+
if (schema.minimum !== undefined)
|
|
87
|
+
optimized.minimum = schema.minimum;
|
|
88
|
+
if (schema.maximum !== undefined)
|
|
89
|
+
optimized.maximum = schema.maximum;
|
|
90
|
+
if (schema.minLength !== undefined)
|
|
91
|
+
optimized.minLength = schema.minLength;
|
|
92
|
+
if (schema.maxLength !== undefined)
|
|
93
|
+
optimized.maxLength = schema.maxLength;
|
|
94
|
+
if (schema.pattern)
|
|
95
|
+
optimized.pattern = schema.pattern;
|
|
96
|
+
if (schema.default !== undefined)
|
|
97
|
+
optimized.default = schema.default;
|
|
98
|
+
if (schema.example !== undefined)
|
|
99
|
+
optimized.example = schema.example;
|
|
100
|
+
if (schema.nullable)
|
|
101
|
+
optimized.nullable = true;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
// For complex schemas (objects, arrays, enums), keep as nested schema
|
|
105
|
+
optimized.schema = optimizeSchema(schema);
|
|
106
|
+
}
|
|
67
107
|
}
|
|
68
|
-
if (
|
|
108
|
+
// Only add required flag if true (omit for optional params)
|
|
109
|
+
if (param.required) {
|
|
110
|
+
optimized.required = true;
|
|
111
|
+
}
|
|
112
|
+
// Add example if present and not already in schema
|
|
113
|
+
if (param.example !== undefined && !optimized.example) {
|
|
69
114
|
optimized.example = param.example;
|
|
70
115
|
}
|
|
71
|
-
|
|
72
|
-
|
|
116
|
+
// Group by parameter location, using parameter name as key
|
|
117
|
+
if (param.in === 'path') {
|
|
118
|
+
pathParams[param.name] = optimized;
|
|
119
|
+
}
|
|
120
|
+
else if (param.in === 'query') {
|
|
121
|
+
queryParams[param.name] = optimized;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Build result object with only non-empty groups
|
|
125
|
+
const result = {};
|
|
126
|
+
if (Object.keys(pathParams).length > 0) {
|
|
127
|
+
result.path = pathParams;
|
|
128
|
+
}
|
|
129
|
+
if (Object.keys(queryParams).length > 0) {
|
|
130
|
+
result.query = queryParams;
|
|
131
|
+
}
|
|
132
|
+
return Object.keys(result).length > 0 ? result : undefined;
|
|
73
133
|
}
|
|
74
134
|
/**
|
|
75
135
|
* Optimize request body by removing unnecessary fields
|
|
@@ -115,21 +175,22 @@ function optimizeMediaType(mediaType) {
|
|
|
115
175
|
/**
|
|
116
176
|
* Optimize responses by removing unnecessary fields
|
|
117
177
|
*
|
|
178
|
+
* Response content schemas are stripped to save tokens (70-80% reduction).
|
|
179
|
+
* Only the description is kept for each status code, which is sufficient for
|
|
180
|
+
* understanding what to expect. The actual response data is more useful than
|
|
181
|
+
* the schema definition when working with APIs.
|
|
182
|
+
*
|
|
118
183
|
* @param responses - Response definitions
|
|
119
|
-
* @returns Optimized responses
|
|
184
|
+
* @returns Optimized responses with content stripped
|
|
120
185
|
*/
|
|
121
186
|
function optimizeResponses(responses) {
|
|
122
187
|
const optimized = {};
|
|
123
188
|
for (const [statusCode, response] of Object.entries(responses)) {
|
|
189
|
+
// Only keep the description, strip all content schemas
|
|
124
190
|
optimized[statusCode] = {
|
|
125
191
|
description: optimizeDescription(response.description),
|
|
192
|
+
// content field is intentionally omitted to save tokens
|
|
126
193
|
};
|
|
127
|
-
if (response.content) {
|
|
128
|
-
optimized[statusCode].content = {};
|
|
129
|
-
for (const [mediaType, content] of Object.entries(response.content)) {
|
|
130
|
-
optimized[statusCode].content[mediaType] = optimizeMediaType(content);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
194
|
}
|
|
134
195
|
return optimized;
|
|
135
196
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"optimizer.js","sourceRoot":"","sources":["../../src/build/optimizer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAA2B;IAC1D,OAAO;QACL,GAAG,QAAQ;QACX,wBAAwB;QACxB,IAAI,EAAE,QAAQ,CAAC,IAAI;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1C,MAAM,gBAAgB,GAAG,CAAC,CAAC,YAAY,GAAG,aAAa,CAAC,GAAG,YAAY,CAAC,GAAG,GAAG,CAAC;IAE/E,OAAO;QACL,YAAY;QACZ,aAAa;QACb,gBAAgB,EAAE,IAAI,CAAC,KAAK,CAAC,gBAAgB,GAAG,GAAG,CAAC,GAAG,GAAG;KAC3D,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/build/parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAsBxE;
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/build/parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAsBxE;AAoFD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,GAAG,GAAG,KAAK,CAAC;IAChD,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,GAAG,CAAC;CAChB,CAAC,CA8BD;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAWjE"}
|
package/dist/build/parser.js
CHANGED
|
@@ -34,6 +34,77 @@ export async function parseAndDereference(filePath) {
|
|
|
34
34
|
throw new Error(`Failed to parse OpenAPI specification: ${error}`);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Resolve internal $ref pointers in a value
|
|
39
|
+
*
|
|
40
|
+
* @param value - Value that may contain $ref pointers
|
|
41
|
+
* @param api - Full API document to resolve references from
|
|
42
|
+
* @param visited - Set of visited references to prevent circular loops
|
|
43
|
+
* @returns Resolved value
|
|
44
|
+
*/
|
|
45
|
+
function resolveRef(value, api, visited = new Set()) {
|
|
46
|
+
if (!value || typeof value !== "object") {
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
// Handle $ref pointers
|
|
50
|
+
if (value.$ref && typeof value.$ref === "string") {
|
|
51
|
+
const ref = value.$ref;
|
|
52
|
+
// Skip external file references - these should have been bundled
|
|
53
|
+
// If they weren't bundled, they're missing files or bundler issues
|
|
54
|
+
// Only warn once per unique external reference
|
|
55
|
+
if (!ref.startsWith("#/")) {
|
|
56
|
+
// Silently skip external refs - they're handled by the bundler
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
// Prevent infinite loops
|
|
60
|
+
if (visited.has(ref)) {
|
|
61
|
+
// Circular references are expected in OpenAPI (e.g., ChargePoint -> evses -> ChargePoint)
|
|
62
|
+
// Silently return the unresolved value to break the cycle
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
visited.add(ref);
|
|
66
|
+
// Parse the JSON pointer (e.g., "#/components/parameters/user_filter")
|
|
67
|
+
const pointer = ref.replace(/^#\//, "").split("/");
|
|
68
|
+
// Navigate to the referenced value
|
|
69
|
+
let resolved = api;
|
|
70
|
+
for (const segment of pointer) {
|
|
71
|
+
// Handle URL-encoded segments
|
|
72
|
+
// First decode percent-encoded characters (e.g., %7B becomes {, %7D becomes })
|
|
73
|
+
// Then handle JSON Pointer encoding (e.g., ~1 becomes /, ~0 becomes ~)
|
|
74
|
+
const decodedSegment = decodeURIComponent(segment)
|
|
75
|
+
.replace(/~1/g, "/")
|
|
76
|
+
.replace(/~0/g, "~");
|
|
77
|
+
resolved = resolved?.[decodedSegment];
|
|
78
|
+
// If we encounter a $ref while navigating, resolve it first
|
|
79
|
+
// This handles cases where references point through other $refs
|
|
80
|
+
if (resolved?.$ref && typeof resolved.$ref === 'string') {
|
|
81
|
+
const nestedResolved = resolveRef(resolved, api, visited);
|
|
82
|
+
// If resolution returns the same object (circular ref), we can't navigate further
|
|
83
|
+
if (nestedResolved === resolved) {
|
|
84
|
+
// Circular reference - can't navigate deeper, return what we have
|
|
85
|
+
return resolved;
|
|
86
|
+
}
|
|
87
|
+
resolved = nestedResolved;
|
|
88
|
+
}
|
|
89
|
+
if (resolved === undefined) {
|
|
90
|
+
console.warn(`[Parser] Failed to resolve reference: ${ref}`);
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// Recursively resolve any nested $refs in the resolved value
|
|
95
|
+
return resolveRef(resolved, api, visited);
|
|
96
|
+
}
|
|
97
|
+
// Handle arrays
|
|
98
|
+
if (Array.isArray(value)) {
|
|
99
|
+
return value.map((item) => resolveRef(item, api, new Set(visited)));
|
|
100
|
+
}
|
|
101
|
+
// Handle objects
|
|
102
|
+
const resolved = {};
|
|
103
|
+
for (const [key, val] of Object.entries(value)) {
|
|
104
|
+
resolved[key] = resolveRef(val, api, new Set(visited));
|
|
105
|
+
}
|
|
106
|
+
return resolved;
|
|
107
|
+
}
|
|
37
108
|
/**
|
|
38
109
|
* Get all endpoints from the dereferenced OpenAPI document
|
|
39
110
|
*
|
|
@@ -53,10 +124,12 @@ export function extractEndpoints(api) {
|
|
|
53
124
|
for (const method of methods) {
|
|
54
125
|
const operation = pathItem[method];
|
|
55
126
|
if (operation) {
|
|
127
|
+
// Resolve all internal $ref pointers in the operation
|
|
128
|
+
const resolvedOperation = resolveRef(operation, api);
|
|
56
129
|
endpoints.push({
|
|
57
130
|
path,
|
|
58
131
|
method: method.toUpperCase(),
|
|
59
|
-
operation,
|
|
132
|
+
operation: resolvedOperation,
|
|
60
133
|
});
|
|
61
134
|
}
|
|
62
135
|
}
|
package/dist/build/parser.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/build/parser.ts"],"names":[],"mappings":"AAAA;;;;;;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