@dimassetoid/openapi-mcp 0.0.0-stage → 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 +144 -2
- package/dist/cli/update.d.ts +38 -0
- package/dist/cli/update.js +235 -0
- package/dist/engine/budget.d.ts +40 -0
- package/dist/engine/budget.js +96 -0
- package/dist/engine/example-generator.d.ts +47 -0
- package/dist/engine/example-generator.js +273 -0
- package/dist/engine/index-builder.d.ts +50 -0
- package/dist/engine/index-builder.js +155 -0
- package/dist/engine/index.d.ts +12 -0
- package/dist/engine/index.js +12 -0
- package/dist/engine/json.d.ts +1 -0
- package/dist/engine/json.js +3 -0
- package/dist/engine/lazy-expander.d.ts +37 -0
- package/dist/engine/lazy-expander.js +235 -0
- package/dist/engine/openapi-utils.d.ts +13 -0
- package/dist/engine/openapi-utils.js +48 -0
- package/dist/engine/query-sandbox.d.ts +29 -0
- package/dist/engine/query-sandbox.js +345 -0
- package/dist/engine/schema-index.d.ts +42 -0
- package/dist/engine/schema-index.js +145 -0
- package/dist/engine/source-map.d.ts +15 -0
- package/dist/engine/source-map.js +50 -0
- package/dist/engine/spec-catalog.d.ts +14 -0
- package/dist/engine/spec-catalog.js +99 -0
- package/dist/engine/spec-diagnostics.d.ts +28 -0
- package/dist/engine/spec-diagnostics.js +393 -0
- package/dist/engine/spec-diff.d.ts +32 -0
- package/dist/engine/spec-diff.js +406 -0
- package/dist/engine/spec-loader.d.ts +100 -0
- package/dist/engine/spec-loader.js +528 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +371 -0
- package/dist/mcp/prompts.d.ts +51 -0
- package/dist/mcp/prompts.js +161 -0
- package/dist/mcp/resources.d.ts +33 -0
- package/dist/mcp/resources.js +207 -0
- package/dist/mcp/server.d.ts +88 -0
- package/dist/mcp/server.js +588 -0
- package/dist/telemetry/telemetry.d.ts +90 -0
- package/dist/telemetry/telemetry.js +299 -0
- package/docs/feature/README.md +53 -0
- package/docs/feature/getting-started.md +167 -0
- package/docs/feature/index-and-prompts.md +53 -0
- package/docs/feature/openapi-query.md +53 -0
- package/docs/feature/spec-loader.md +34 -0
- package/package.json +77 -4
- package/skills/use-openapi-mcp/SKILL.md +43 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { isRecord } from './json.js';
|
|
2
|
+
import { mergeOperationParameters } from './openapi-utils.js';
|
|
3
|
+
import { escapeJsonPointer } from './source-map.js';
|
|
4
|
+
const DEFAULT_MAX_DEPTH = 8;
|
|
5
|
+
const DEFAULT_MAX_NODES = 256;
|
|
6
|
+
function pickPrimaryType(typeField) {
|
|
7
|
+
if (typeof typeField === 'string')
|
|
8
|
+
return typeField;
|
|
9
|
+
if (Array.isArray(typeField)) {
|
|
10
|
+
const nonNull = typeField.find((t) => typeof t === 'string' && t !== 'null');
|
|
11
|
+
if (typeof nonNull === 'string')
|
|
12
|
+
return nonNull;
|
|
13
|
+
const first = typeField.find((t) => typeof t === 'string');
|
|
14
|
+
if (typeof first === 'string')
|
|
15
|
+
return first;
|
|
16
|
+
}
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
function synthesizeString(schema) {
|
|
20
|
+
const format = typeof schema.format === 'string' ? schema.format.toLowerCase() : '';
|
|
21
|
+
switch (format) {
|
|
22
|
+
case 'uuid':
|
|
23
|
+
return '00000000-0000-4000-8000-000000000000';
|
|
24
|
+
case 'email':
|
|
25
|
+
case 'idn-email':
|
|
26
|
+
return 'user@example.com';
|
|
27
|
+
case 'date-time':
|
|
28
|
+
return '2026-01-01T00:00:00Z';
|
|
29
|
+
case 'date':
|
|
30
|
+
return '2026-01-01';
|
|
31
|
+
case 'time':
|
|
32
|
+
return '00:00:00Z';
|
|
33
|
+
case 'uri':
|
|
34
|
+
case 'url':
|
|
35
|
+
case 'iri':
|
|
36
|
+
return 'https://example.com/resource';
|
|
37
|
+
case 'ipv4':
|
|
38
|
+
return '192.0.2.1';
|
|
39
|
+
case 'ipv6':
|
|
40
|
+
return '2001:db8::1';
|
|
41
|
+
case 'hostname':
|
|
42
|
+
case 'idn-hostname':
|
|
43
|
+
return 'example.com';
|
|
44
|
+
case 'byte':
|
|
45
|
+
return 'ZXhhbXBsZQ==';
|
|
46
|
+
default: {
|
|
47
|
+
const minLen = typeof schema.minLength === 'number' && schema.minLength > 0 ? Math.min(64, Math.floor(schema.minLength)) : 0;
|
|
48
|
+
const base = 'string';
|
|
49
|
+
return minLen > base.length ? base.padEnd(minLen, 'x') : base;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function synthesizeNumber(schema, integer) {
|
|
54
|
+
const min = typeof schema.minimum === 'number' ? schema.minimum : undefined;
|
|
55
|
+
const exMin = typeof schema.exclusiveMinimum === 'number' ? schema.exclusiveMinimum : undefined;
|
|
56
|
+
const max = typeof schema.maximum === 'number' ? schema.maximum : undefined;
|
|
57
|
+
let value = integer ? 1 : 0;
|
|
58
|
+
if (exMin !== undefined) {
|
|
59
|
+
value = integer ? Math.floor(exMin) + 1 : exMin + 0.5;
|
|
60
|
+
}
|
|
61
|
+
else if (min !== undefined && value < min) {
|
|
62
|
+
value = integer ? Math.ceil(min) : min;
|
|
63
|
+
}
|
|
64
|
+
if (max !== undefined && value > max) {
|
|
65
|
+
value = integer ? Math.floor(max) : max;
|
|
66
|
+
}
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Deterministically synthesizes a valid sample JSON value from a resolved OpenAPI 3.0/3.1 schema.
|
|
71
|
+
* Safe against circular markers (`[Circular: ...]`) and bounded by depth and node count.
|
|
72
|
+
*/
|
|
73
|
+
export function generateSchemaExample(schema, options = {}) {
|
|
74
|
+
const maxDepth = options.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
75
|
+
const maxNodes = options.maxNodes ?? DEFAULT_MAX_NODES;
|
|
76
|
+
const includeReadOnly = options.includeReadOnly ?? true;
|
|
77
|
+
const includeWriteOnly = options.includeWriteOnly ?? true;
|
|
78
|
+
let nodes = 0;
|
|
79
|
+
function visit(node, depth) {
|
|
80
|
+
if (++nodes > maxNodes || depth > maxDepth)
|
|
81
|
+
return null;
|
|
82
|
+
if (typeof node === 'string' && node.startsWith('[Circular:'))
|
|
83
|
+
return null;
|
|
84
|
+
if (typeof node === 'boolean')
|
|
85
|
+
return node ? {} : null;
|
|
86
|
+
if (!isRecord(node))
|
|
87
|
+
return null;
|
|
88
|
+
if (node.example !== undefined)
|
|
89
|
+
return node.example;
|
|
90
|
+
if (Array.isArray(node.examples) && node.examples.length > 0)
|
|
91
|
+
return node.examples[0];
|
|
92
|
+
if (node.const !== undefined)
|
|
93
|
+
return node.const;
|
|
94
|
+
if (node.default !== undefined)
|
|
95
|
+
return node.default;
|
|
96
|
+
if (Array.isArray(node.enum) && node.enum.length > 0)
|
|
97
|
+
return node.enum[0];
|
|
98
|
+
if (Array.isArray(node.allOf) && node.allOf.length > 0) {
|
|
99
|
+
const merged = {};
|
|
100
|
+
let anyObject = false;
|
|
101
|
+
for (const branch of node.allOf) {
|
|
102
|
+
const sample = visit(branch, depth + 1);
|
|
103
|
+
if (isRecord(sample)) {
|
|
104
|
+
Object.assign(merged, sample);
|
|
105
|
+
anyObject = true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (anyObject)
|
|
109
|
+
return merged;
|
|
110
|
+
return visit(node.allOf[0], depth + 1);
|
|
111
|
+
}
|
|
112
|
+
const unionBranches = Array.isArray(node.oneOf)
|
|
113
|
+
? node.oneOf
|
|
114
|
+
: Array.isArray(node.anyOf)
|
|
115
|
+
? node.anyOf
|
|
116
|
+
: undefined;
|
|
117
|
+
if (unionBranches && unionBranches.length > 0) {
|
|
118
|
+
const nonNull = unionBranches.find((b) => isRecord(b) && b.type !== 'null') ?? unionBranches[0];
|
|
119
|
+
return visit(nonNull, depth + 1);
|
|
120
|
+
}
|
|
121
|
+
const primaryType = pickPrimaryType(node.type) ?? (isRecord(node.properties) ? 'object' : node.items !== undefined ? 'array' : undefined);
|
|
122
|
+
switch (primaryType) {
|
|
123
|
+
case 'object': {
|
|
124
|
+
const out = {};
|
|
125
|
+
if (isRecord(node.properties)) {
|
|
126
|
+
for (const [key, propSchema] of Object.entries(node.properties)) {
|
|
127
|
+
if (isRecord(propSchema)) {
|
|
128
|
+
if (!includeReadOnly && propSchema.readOnly === true)
|
|
129
|
+
continue;
|
|
130
|
+
if (!includeWriteOnly && propSchema.writeOnly === true)
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
out[key] = visit(propSchema, depth + 1);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
case 'array': {
|
|
139
|
+
if (Array.isArray(node.prefixItems) && node.prefixItems.length > 0) {
|
|
140
|
+
return node.prefixItems.map((item) => visit(item, depth + 1));
|
|
141
|
+
}
|
|
142
|
+
if (node.items === undefined)
|
|
143
|
+
return [];
|
|
144
|
+
const minItems = typeof node.minItems === 'number' && node.minItems > 0 ? Math.min(2, Math.floor(node.minItems)) : 1;
|
|
145
|
+
const arr = [];
|
|
146
|
+
for (let i = 0; i < minItems; i++) {
|
|
147
|
+
arr.push(visit(node.items, depth + 1));
|
|
148
|
+
}
|
|
149
|
+
return arr;
|
|
150
|
+
}
|
|
151
|
+
case 'string':
|
|
152
|
+
return synthesizeString(node);
|
|
153
|
+
case 'integer':
|
|
154
|
+
return synthesizeNumber(node, true);
|
|
155
|
+
case 'number':
|
|
156
|
+
return synthesizeNumber(node, false);
|
|
157
|
+
case 'boolean':
|
|
158
|
+
return true;
|
|
159
|
+
case 'null':
|
|
160
|
+
return null;
|
|
161
|
+
default:
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return visit(schema, 0);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Returns a single operation's complete contract with path-level + operation-level `parameters`
|
|
169
|
+
* merged (operation overrides path on matching `name`+`in`), effective `security`, and `sourceLocation`.
|
|
170
|
+
*/
|
|
171
|
+
export function getMergedOperation(spec, method, path, sourceLocations) {
|
|
172
|
+
const paths = isRecord(spec.paths) ? spec.paths : undefined;
|
|
173
|
+
const pathItem = paths && isRecord(paths[path]) ? paths[path] : undefined;
|
|
174
|
+
const lowerMethod = method.trim().toLowerCase();
|
|
175
|
+
const operation = pathItem && isRecord(pathItem[lowerMethod]) ? pathItem[lowerMethod] : undefined;
|
|
176
|
+
if (!pathItem || !operation)
|
|
177
|
+
return null;
|
|
178
|
+
const mergedParams = mergeOperationParameters(pathItem, operation);
|
|
179
|
+
const effectiveSecurity = Array.isArray(operation.security)
|
|
180
|
+
? operation.security
|
|
181
|
+
: Array.isArray(spec.security)
|
|
182
|
+
? spec.security
|
|
183
|
+
: undefined;
|
|
184
|
+
const sourceLocation = sourceLocations?.[`/paths/${escapeJsonPointer(path)}/${lowerMethod}`];
|
|
185
|
+
return {
|
|
186
|
+
method: lowerMethod.toUpperCase(),
|
|
187
|
+
path,
|
|
188
|
+
...(typeof operation.operationId === 'string' ? { operationId: operation.operationId } : {}),
|
|
189
|
+
...(typeof operation.summary === 'string' ? { summary: operation.summary } : {}),
|
|
190
|
+
...(typeof operation.description === 'string' ? { description: operation.description } : {}),
|
|
191
|
+
...(Array.isArray(operation.tags) ? { tags: operation.tags.filter((t) => typeof t === 'string') } : {}),
|
|
192
|
+
...(operation.deprecated === true ? { deprecated: true } : {}),
|
|
193
|
+
...(effectiveSecurity !== undefined ? { security: effectiveSecurity } : {}),
|
|
194
|
+
parameters: [...mergedParams.values()],
|
|
195
|
+
...(operation.requestBody !== undefined ? { requestBody: operation.requestBody } : {}),
|
|
196
|
+
...(operation.responses !== undefined ? { responses: operation.responses } : {}),
|
|
197
|
+
...(sourceLocation ? { sourceLocation } : {}),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function pickMediaContent(content) {
|
|
201
|
+
if (isRecord(content['application/json'])) {
|
|
202
|
+
return { mediaType: 'application/json', media: content['application/json'] };
|
|
203
|
+
}
|
|
204
|
+
const jsonLike = Object.keys(content).find((k) => k.includes('json') && isRecord(content[k]));
|
|
205
|
+
if (jsonLike)
|
|
206
|
+
return { mediaType: jsonLike, media: content[jsonLike] };
|
|
207
|
+
const firstKey = Object.keys(content).find((k) => isRecord(content[k]));
|
|
208
|
+
if (firstKey)
|
|
209
|
+
return { mediaType: firstKey, media: content[firstKey] };
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
function extractMediaExample(media, opts) {
|
|
213
|
+
if (media.example !== undefined)
|
|
214
|
+
return media.example;
|
|
215
|
+
if (isRecord(media.examples)) {
|
|
216
|
+
for (const ex of Object.values(media.examples)) {
|
|
217
|
+
if (isRecord(ex) && ex.value !== undefined)
|
|
218
|
+
return ex.value;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (media.schema !== undefined)
|
|
222
|
+
return generateSchemaExample(media.schema, opts);
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Generates deterministic request and response sample payloads for a given `method` + `path`
|
|
227
|
+
* (optionally filtered to a single response `statusCode`).
|
|
228
|
+
*/
|
|
229
|
+
export function generateOperationExamples(spec, method, path, statusCode) {
|
|
230
|
+
const op = getMergedOperation(spec, method, path);
|
|
231
|
+
if (!op)
|
|
232
|
+
return null;
|
|
233
|
+
let requestBody;
|
|
234
|
+
if (isRecord(op.requestBody) && isRecord(op.requestBody.content)) {
|
|
235
|
+
const picked = pickMediaContent(op.requestBody.content);
|
|
236
|
+
if (picked) {
|
|
237
|
+
requestBody = {
|
|
238
|
+
mediaType: picked.mediaType,
|
|
239
|
+
example: extractMediaExample(picked.media, { includeReadOnly: false, includeWriteOnly: true }),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const responses = {};
|
|
244
|
+
if (isRecord(op.responses)) {
|
|
245
|
+
const codes = statusCode ? [statusCode] : Object.keys(op.responses);
|
|
246
|
+
for (const code of codes) {
|
|
247
|
+
const res = op.responses[code];
|
|
248
|
+
if (!isRecord(res))
|
|
249
|
+
continue;
|
|
250
|
+
if (isRecord(res.content)) {
|
|
251
|
+
const picked = pickMediaContent(res.content);
|
|
252
|
+
if (picked) {
|
|
253
|
+
responses[code] = {
|
|
254
|
+
mediaType: picked.mediaType,
|
|
255
|
+
example: extractMediaExample(picked.media, { includeReadOnly: true, includeWriteOnly: false }),
|
|
256
|
+
};
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if (res.schema !== undefined) {
|
|
261
|
+
responses[code] = {
|
|
262
|
+
example: generateSchemaExample(res.schema, { includeReadOnly: true, includeWriteOnly: false }),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
method: op.method,
|
|
269
|
+
path: op.path,
|
|
270
|
+
...(requestBody ? { requestBody } : {}),
|
|
271
|
+
responses,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type SourceLocation } from './source-map.js';
|
|
2
|
+
export declare const HTTP_METHODS: readonly ["get", "post", "put", "patch", "delete", "options", "head", "trace"];
|
|
3
|
+
export interface EndpointSummaryItem {
|
|
4
|
+
method: string;
|
|
5
|
+
path: string;
|
|
6
|
+
operationId?: string;
|
|
7
|
+
summary: string;
|
|
8
|
+
tags?: string[];
|
|
9
|
+
deprecated?: boolean;
|
|
10
|
+
security?: string[];
|
|
11
|
+
statusCodes: string[];
|
|
12
|
+
errorStatusCodes: string[];
|
|
13
|
+
errorBodies: Record<string, {
|
|
14
|
+
schemaMediaTypes: string[];
|
|
15
|
+
hasSwaggerSchema?: boolean;
|
|
16
|
+
}>;
|
|
17
|
+
sourceLocation?: SourceLocation;
|
|
18
|
+
line: string;
|
|
19
|
+
}
|
|
20
|
+
export interface EndpointIndex {
|
|
21
|
+
title: string;
|
|
22
|
+
version: string;
|
|
23
|
+
openapiVersion: string;
|
|
24
|
+
specPath?: string;
|
|
25
|
+
totalEndpoints: number;
|
|
26
|
+
tags: string[];
|
|
27
|
+
endpointsWithErrorContracts: number;
|
|
28
|
+
endpointsMissingErrorContracts: string[];
|
|
29
|
+
lines: string[];
|
|
30
|
+
endpoints: EndpointSummaryItem[];
|
|
31
|
+
}
|
|
32
|
+
export interface EndpointSearchFilters {
|
|
33
|
+
q?: string;
|
|
34
|
+
tag?: string;
|
|
35
|
+
method?: string;
|
|
36
|
+
pathPrefix?: string;
|
|
37
|
+
operationId?: string;
|
|
38
|
+
missingErrorContracts?: boolean;
|
|
39
|
+
offset: number;
|
|
40
|
+
limit: number;
|
|
41
|
+
}
|
|
42
|
+
export declare function searchEndpoints(entries: EndpointSummaryItem[], filters: EndpointSearchFilters): {
|
|
43
|
+
endpoints: EndpointSummaryItem[];
|
|
44
|
+
totalMatches: number;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Builds a compact, token-lean index of all endpoints and their defined HTTP status codes
|
|
48
|
+
* (including all 4xx/5xx error responses) from a resolved OpenAPI specification.
|
|
49
|
+
*/
|
|
50
|
+
export declare function buildEndpointIndex(spec: Record<string, unknown>, specPath?: string, sourceLocations?: Readonly<Record<string, SourceLocation>>): EndpointIndex;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { isRecord } from './json.js';
|
|
2
|
+
import { extractSecuritySchemeNames, extractSpecMetadata, isErrorStatusCode } from './openapi-utils.js';
|
|
3
|
+
import { escapeJsonPointer } from './source-map.js';
|
|
4
|
+
export const HTTP_METHODS = [
|
|
5
|
+
'get',
|
|
6
|
+
'post',
|
|
7
|
+
'put',
|
|
8
|
+
'patch',
|
|
9
|
+
'delete',
|
|
10
|
+
'options',
|
|
11
|
+
'head',
|
|
12
|
+
'trace',
|
|
13
|
+
];
|
|
14
|
+
function sortStatusCodes(codes) {
|
|
15
|
+
return [...codes].sort((a, b) => {
|
|
16
|
+
const aNum = /^\d+$/.test(a) ? Number(a) : Number.POSITIVE_INFINITY;
|
|
17
|
+
const bNum = /^\d+$/.test(b) ? Number(b) : Number.POSITIVE_INFINITY;
|
|
18
|
+
if (aNum !== bNum)
|
|
19
|
+
return aNum - bNum;
|
|
20
|
+
if (a === 'default')
|
|
21
|
+
return 1;
|
|
22
|
+
if (b === 'default')
|
|
23
|
+
return -1;
|
|
24
|
+
return a.localeCompare(b);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
export function searchEndpoints(entries, filters) {
|
|
28
|
+
const query = filters.q?.toLowerCase();
|
|
29
|
+
const tag = filters.tag?.toLowerCase();
|
|
30
|
+
const method = filters.method?.toUpperCase();
|
|
31
|
+
const pathPrefix = filters.pathPrefix;
|
|
32
|
+
const operationId = filters.operationId?.toLowerCase();
|
|
33
|
+
const endpoints = [];
|
|
34
|
+
let totalMatches = 0;
|
|
35
|
+
for (const entry of entries) {
|
|
36
|
+
const tags = entry.tags ?? [];
|
|
37
|
+
if (method && entry.method !== method)
|
|
38
|
+
continue;
|
|
39
|
+
if (pathPrefix && !entry.path.startsWith(pathPrefix))
|
|
40
|
+
continue;
|
|
41
|
+
if (tag && !tags.some(value => value.toLowerCase() === tag))
|
|
42
|
+
continue;
|
|
43
|
+
if (operationId && entry.operationId?.toLowerCase() !== operationId)
|
|
44
|
+
continue;
|
|
45
|
+
if (filters.missingErrorContracts === true && entry.errorStatusCodes.length > 0)
|
|
46
|
+
continue;
|
|
47
|
+
if (filters.missingErrorContracts === false && entry.errorStatusCodes.length === 0)
|
|
48
|
+
continue;
|
|
49
|
+
if (query && ![entry.method, entry.path, entry.summary, entry.operationId ?? '', ...tags]
|
|
50
|
+
.some(value => value.toLowerCase().includes(query)))
|
|
51
|
+
continue;
|
|
52
|
+
if (totalMatches >= filters.offset && endpoints.length < filters.limit)
|
|
53
|
+
endpoints.push(entry);
|
|
54
|
+
totalMatches++;
|
|
55
|
+
}
|
|
56
|
+
return { endpoints, totalMatches };
|
|
57
|
+
}
|
|
58
|
+
function extractSummary(operation) {
|
|
59
|
+
if (typeof operation.summary === 'string' && operation.summary.trim()) {
|
|
60
|
+
return operation.summary.trim().replace(/\s+/g, ' ');
|
|
61
|
+
}
|
|
62
|
+
if (typeof operation.description === 'string' && operation.description.trim()) {
|
|
63
|
+
const firstLine = operation.description.trim().split(/\r?\n/)[0] ?? '';
|
|
64
|
+
if (firstLine.trim()) {
|
|
65
|
+
return firstLine.trim().slice(0, 120);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (typeof operation.operationId === 'string' && operation.operationId.trim()) {
|
|
69
|
+
return operation.operationId.trim();
|
|
70
|
+
}
|
|
71
|
+
return 'No summary';
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Builds a compact, token-lean index of all endpoints and their defined HTTP status codes
|
|
75
|
+
* (including all 4xx/5xx error responses) from a resolved OpenAPI specification.
|
|
76
|
+
*/
|
|
77
|
+
export function buildEndpointIndex(spec, specPath, sourceLocations) {
|
|
78
|
+
const { title, version, openapiVersion } = extractSpecMetadata(spec);
|
|
79
|
+
const pathsObj = isRecord(spec.paths) ? spec.paths : {};
|
|
80
|
+
const globalSecurity = extractSecuritySchemeNames(spec.security);
|
|
81
|
+
const endpoints = [];
|
|
82
|
+
const endpointsMissingErrorContracts = [];
|
|
83
|
+
const allTags = new Set();
|
|
84
|
+
for (const pathKey of Object.keys(pathsObj)) {
|
|
85
|
+
const pathItem = pathsObj[pathKey];
|
|
86
|
+
if (!isRecord(pathItem))
|
|
87
|
+
continue;
|
|
88
|
+
for (const httpMethod of HTTP_METHODS) {
|
|
89
|
+
const operation = pathItem[httpMethod];
|
|
90
|
+
if (!isRecord(operation))
|
|
91
|
+
continue;
|
|
92
|
+
const method = httpMethod.toUpperCase();
|
|
93
|
+
const summary = extractSummary(operation);
|
|
94
|
+
const responses = isRecord(operation.responses) ? operation.responses : {};
|
|
95
|
+
const statusCodes = sortStatusCodes(Object.keys(responses));
|
|
96
|
+
const errorStatusCodes = statusCodes.filter(isErrorStatusCode);
|
|
97
|
+
const errorBodies = Object.fromEntries(errorStatusCodes.map(code => {
|
|
98
|
+
const response = responses[code];
|
|
99
|
+
const content = isRecord(response) && isRecord(response.content) ? response.content : {};
|
|
100
|
+
const schemaMediaTypes = Object.keys(content).filter(type => {
|
|
101
|
+
const media = content[type];
|
|
102
|
+
return isRecord(media) && (isRecord(media.schema) || typeof media.schema === 'boolean');
|
|
103
|
+
});
|
|
104
|
+
const hasSwaggerSchema = spec.swagger === '2.0' && isRecord(response) && isRecord(response.schema);
|
|
105
|
+
return [code, { schemaMediaTypes, ...(hasSwaggerSchema ? { hasSwaggerSchema } : {}) }];
|
|
106
|
+
}));
|
|
107
|
+
if (errorStatusCodes.length === 0) {
|
|
108
|
+
endpointsMissingErrorContracts.push(`${method} ${pathKey}`);
|
|
109
|
+
}
|
|
110
|
+
const tags = Array.isArray(operation.tags)
|
|
111
|
+
? operation.tags.filter((t) => typeof t === 'string' && t.trim().length > 0)
|
|
112
|
+
: undefined;
|
|
113
|
+
if (tags) {
|
|
114
|
+
for (const t of tags)
|
|
115
|
+
allTags.add(t);
|
|
116
|
+
}
|
|
117
|
+
const operationId = typeof operation.operationId === 'string' && operation.operationId.trim()
|
|
118
|
+
? operation.operationId.trim()
|
|
119
|
+
: undefined;
|
|
120
|
+
const deprecated = operation.deprecated === true;
|
|
121
|
+
const opSecurity = extractSecuritySchemeNames(operation.security);
|
|
122
|
+
const effectiveSecurity = opSecurity !== undefined ? opSecurity : globalSecurity;
|
|
123
|
+
const includeSecurity = effectiveSecurity !== undefined && (effectiveSecurity.length > 0 || (opSecurity !== undefined && (globalSecurity?.length ?? 0) > 0));
|
|
124
|
+
const sourceLocation = sourceLocations?.[`/paths/${escapeJsonPointer(pathKey)}/${httpMethod}`];
|
|
125
|
+
const statusList = statusCodes.length > 0 ? statusCodes.join(', ') : 'none';
|
|
126
|
+
const line = `${method} ${pathKey} — ${summary} — [${statusList}]`;
|
|
127
|
+
endpoints.push({
|
|
128
|
+
method,
|
|
129
|
+
path: pathKey,
|
|
130
|
+
...(operationId ? { operationId } : {}),
|
|
131
|
+
summary,
|
|
132
|
+
...(tags && tags.length > 0 ? { tags } : {}),
|
|
133
|
+
...(deprecated ? { deprecated: true } : {}),
|
|
134
|
+
...(includeSecurity ? { security: effectiveSecurity } : {}),
|
|
135
|
+
statusCodes,
|
|
136
|
+
errorStatusCodes,
|
|
137
|
+
errorBodies,
|
|
138
|
+
...(sourceLocation ? { sourceLocation } : {}),
|
|
139
|
+
line,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
title,
|
|
145
|
+
version,
|
|
146
|
+
openapiVersion,
|
|
147
|
+
...(specPath ? { specPath } : {}),
|
|
148
|
+
totalEndpoints: endpoints.length,
|
|
149
|
+
tags: [...allTags].sort(),
|
|
150
|
+
endpointsWithErrorContracts: endpoints.length - endpointsMissingErrorContracts.length,
|
|
151
|
+
endpointsMissingErrorContracts,
|
|
152
|
+
lines: endpoints.map((e) => e.line),
|
|
153
|
+
endpoints,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { DEFAULT_SPEC_CANDIDATES, assertWorkspaceRoot, clearSpecCache, deepFreeze, findOpenApiSpecFile, getSpecCacheStats, loadOpenApiSpec, resolveSafeSpecPath, type FileFingerprint, type LoadOpenApiSpecOptions, type LoadedOpenApiSpec, } from './spec-loader.js';
|
|
2
|
+
export { buildRawEndpointSummary, clearLazySpecCache, expandPointerSubtree, loadRawOpenApiDocument, type LazySubtreeExpandResult, type RawOpenApiDocument, } from './lazy-expander.js';
|
|
3
|
+
export { extractSpecMetadata, type SpecMetadataSummary } from './openapi-utils.js';
|
|
4
|
+
export { HTTP_METHODS, buildEndpointIndex, searchEndpoints, type EndpointIndex, type EndpointSearchFilters, type EndpointSummaryItem, } from './index-builder.js';
|
|
5
|
+
export { DEFAULT_IGNORED_DIRS, listWorkspaceSpecs, scanWorkspaceSpecs, type WorkspaceSpecCatalog, } from './spec-catalog.js';
|
|
6
|
+
export { escapeJsonPointer, parseDocumentWithLocations, type ParsedDocumentWithLocations, type SourceLocation, } from './source-map.js';
|
|
7
|
+
export { buildSchemaIndex, searchSchemas, type SchemaIndex, type SchemaSearchFilters, type SchemaSummaryItem, type SchemaUsageRef, } from './schema-index.js';
|
|
8
|
+
export { DEFAULT_SANDBOX_TIMEOUT_MS, runSandboxQuery, validateSandboxCode, type SandboxMetadata, type SandboxQueryOptions, type SandboxQueryResult, type SandboxSpecMeta, } from './query-sandbox.js';
|
|
9
|
+
export { generateOperationExamples, generateSchemaExample, getMergedOperation, type ExampleGeneratorOptions, type MergedOperationContract, } from './example-generator.js';
|
|
10
|
+
export { diagnoseOpenApiSpec, type DiagnosticSeverity, type SpecDiagnosticIssue, type SpecDiagnosticsReport, } from './spec-diagnostics.js';
|
|
11
|
+
export { diffOpenApiSpecs, type DiffClassification, type SpecDiffChange, type SpecDiffReport, } from './spec-diff.js';
|
|
12
|
+
export { DEFAULT_MAX_BYTES, enforceByteBudget, type BudgetCheckResult, type BudgetExceededPayload, type BudgetExceededResult, type BudgetOkResult, } from './budget.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { DEFAULT_SPEC_CANDIDATES, assertWorkspaceRoot, clearSpecCache, deepFreeze, findOpenApiSpecFile, getSpecCacheStats, loadOpenApiSpec, resolveSafeSpecPath, } from './spec-loader.js';
|
|
2
|
+
export { buildRawEndpointSummary, clearLazySpecCache, expandPointerSubtree, loadRawOpenApiDocument, } from './lazy-expander.js';
|
|
3
|
+
export { extractSpecMetadata } from './openapi-utils.js';
|
|
4
|
+
export { HTTP_METHODS, buildEndpointIndex, searchEndpoints, } from './index-builder.js';
|
|
5
|
+
export { DEFAULT_IGNORED_DIRS, listWorkspaceSpecs, scanWorkspaceSpecs, } from './spec-catalog.js';
|
|
6
|
+
export { escapeJsonPointer, parseDocumentWithLocations, } from './source-map.js';
|
|
7
|
+
export { buildSchemaIndex, searchSchemas, } from './schema-index.js';
|
|
8
|
+
export { DEFAULT_SANDBOX_TIMEOUT_MS, runSandboxQuery, validateSandboxCode, } from './query-sandbox.js';
|
|
9
|
+
export { generateOperationExamples, generateSchemaExample, getMergedOperation, } from './example-generator.js';
|
|
10
|
+
export { diagnoseOpenApiSpec, } from './spec-diagnostics.js';
|
|
11
|
+
export { diffOpenApiSpecs, } from './spec-diff.js';
|
|
12
|
+
export { DEFAULT_MAX_BYTES, enforceByteBudget, } from './budget.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type EndpointIndex } from './index-builder.js';
|
|
2
|
+
import { type SourceLocation } from './source-map.js';
|
|
3
|
+
import { type FileFingerprint, type LoadOpenApiSpecOptions, type SpecLimits } from './spec-loader.js';
|
|
4
|
+
export interface RawOpenApiDocument {
|
|
5
|
+
entryPath: string;
|
|
6
|
+
rootDir: string;
|
|
7
|
+
entryDoc: Record<string, unknown>;
|
|
8
|
+
schema31: boolean;
|
|
9
|
+
entryFingerprint: FileFingerprint;
|
|
10
|
+
sourceLocations: Readonly<Record<string, SourceLocation>>;
|
|
11
|
+
parseFile: (canonicalPath: string) => unknown;
|
|
12
|
+
locations: Map<string, string>;
|
|
13
|
+
fromCache: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface LazySubtreeExpandResult {
|
|
16
|
+
pointer: string;
|
|
17
|
+
value: unknown;
|
|
18
|
+
nodeVisits: number;
|
|
19
|
+
expandedBytes: number;
|
|
20
|
+
fromCache: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare function clearLazySpecCache(): void;
|
|
23
|
+
/**
|
|
24
|
+
* Loads only the entry OpenAPI file without eagerly expanding nested or sibling $refs.
|
|
25
|
+
* External $ref files are parsed lazily on demand only when touched by a targeted subtree.
|
|
26
|
+
*/
|
|
27
|
+
export declare function loadRawOpenApiDocument(options?: LoadOpenApiSpecOptions): RawOpenApiDocument;
|
|
28
|
+
/**
|
|
29
|
+
* Builds the endpoint summary index from a raw OpenAPI document in O(endpoints) without
|
|
30
|
+
* expanding deep nested schemas on unrelated endpoints.
|
|
31
|
+
*/
|
|
32
|
+
export declare function buildRawEndpointSummary(rawDoc: RawOpenApiDocument): EndpointIndex;
|
|
33
|
+
/**
|
|
34
|
+
* Lazily resolves and expands only the $refs reachable from the given JSON Pointer
|
|
35
|
+
* (e.g. `/paths/~1users/get` or `/components/schemas/Order`).
|
|
36
|
+
*/
|
|
37
|
+
export declare function expandPointerSubtree(rawDoc: RawOpenApiDocument, pointer: string, limits?: SpecLimits): LazySubtreeExpandResult;
|