@vitemcp/server 1.7.0 → 1.8.1
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 +52 -0
- package/dist/ViteMCP.cjs +4 -1328
- package/dist/ViteMCP.cjs.map +1 -1
- package/dist/ViteMCP.js +13 -1337
- package/dist/ViteMCP.js.map +1 -1
- package/dist/chunk-2624N2BP.cjs +1351 -0
- package/dist/chunk-2624N2BP.cjs.map +1 -0
- package/dist/chunk-3CGG26M4.js +1351 -0
- package/dist/chunk-3CGG26M4.js.map +1 -0
- package/dist/openapi/index.cjs +1023 -0
- package/dist/openapi/index.cjs.map +1 -0
- package/dist/openapi/index.d.cts +142 -0
- package/dist/openapi/index.d.ts +142 -0
- package/dist/openapi/index.js +1023 -0
- package/dist/openapi/index.js.map +1 -0
- package/package.json +18 -2
|
@@ -0,0 +1,1023 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UserError,
|
|
3
|
+
ViteMCP,
|
|
4
|
+
jsonSchemaAdapter
|
|
5
|
+
} from "../chunk-3CGG26M4.js";
|
|
6
|
+
import "../chunk-GCCQBATS.js";
|
|
7
|
+
|
|
8
|
+
// src/openapi/parameters.ts
|
|
9
|
+
var appendQueryParameter = (query, parameter, value) => {
|
|
10
|
+
const style = parameter.style ?? "form";
|
|
11
|
+
const explode = parameter.explode ?? style === "form";
|
|
12
|
+
const name = parameter.name;
|
|
13
|
+
if (value === null || value === void 0) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (style === "deepObject") {
|
|
17
|
+
if (isRecord(value)) {
|
|
18
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
19
|
+
if (entry !== void 0) {
|
|
20
|
+
query.append(`${name}[${key}]`, stringify(entry));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (explode) {
|
|
27
|
+
if (Array.isArray(value)) {
|
|
28
|
+
for (const entry of value) {
|
|
29
|
+
query.append(name, stringify(entry));
|
|
30
|
+
}
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (isRecord(value)) {
|
|
34
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
35
|
+
if (entry !== void 0) {
|
|
36
|
+
query.append(key, stringify(entry));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
query.append(name, stringify(value));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const separator = DELIMITERS[style] ?? ",";
|
|
45
|
+
if (Array.isArray(value)) {
|
|
46
|
+
query.append(name, value.map(stringify).join(separator));
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
if (isRecord(value)) {
|
|
50
|
+
query.append(
|
|
51
|
+
name,
|
|
52
|
+
Object.entries(value).flatMap(
|
|
53
|
+
([key, entry]) => entry === void 0 ? [] : [key, stringify(entry)]
|
|
54
|
+
).join(separator)
|
|
55
|
+
);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
query.append(name, stringify(value));
|
|
59
|
+
};
|
|
60
|
+
var toQueryString = (query) => query.toString().replaceAll("+", "%20");
|
|
61
|
+
var serializePathParameter = (parameter, value) => {
|
|
62
|
+
const style = parameter.style ?? "simple";
|
|
63
|
+
const explode = parameter.explode ?? false;
|
|
64
|
+
const body = serializeSimple(value, explode, parameter.name, style);
|
|
65
|
+
if (style === "label") {
|
|
66
|
+
return `.${body}`;
|
|
67
|
+
}
|
|
68
|
+
if (style === "matrix") {
|
|
69
|
+
return explode && (Array.isArray(value) || isRecord(value)) ? body : `;${parameter.name}=${body}`;
|
|
70
|
+
}
|
|
71
|
+
return body;
|
|
72
|
+
};
|
|
73
|
+
var serializeSimpleParameter = (parameter, value) => {
|
|
74
|
+
const explode = parameter.explode ?? false;
|
|
75
|
+
if (Array.isArray(value)) {
|
|
76
|
+
return value.map(stringify).join(",");
|
|
77
|
+
}
|
|
78
|
+
if (isRecord(value)) {
|
|
79
|
+
return Object.entries(value).flatMap(
|
|
80
|
+
([key, entry]) => entry === void 0 ? [] : [
|
|
81
|
+
explode ? `${key}=${stringify(entry)}` : `${key},${stringify(entry)}`
|
|
82
|
+
]
|
|
83
|
+
).join(",");
|
|
84
|
+
}
|
|
85
|
+
return stringify(value);
|
|
86
|
+
};
|
|
87
|
+
var DELIMITERS = {
|
|
88
|
+
form: ",",
|
|
89
|
+
pipeDelimited: "|",
|
|
90
|
+
simple: ",",
|
|
91
|
+
spaceDelimited: " "
|
|
92
|
+
};
|
|
93
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
94
|
+
var serializeSimple = (value, explode, name, style) => {
|
|
95
|
+
const encode = (entry) => encodeURIComponent(stringify(entry));
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
if (style === "matrix" && explode) {
|
|
98
|
+
return value.map((entry) => `;${name}=${encode(entry)}`).join("");
|
|
99
|
+
}
|
|
100
|
+
if (style === "label" && explode) {
|
|
101
|
+
return value.map(encode).join(".");
|
|
102
|
+
}
|
|
103
|
+
return value.map(encode).join(",");
|
|
104
|
+
}
|
|
105
|
+
if (isRecord(value)) {
|
|
106
|
+
const entries = Object.entries(value).filter(
|
|
107
|
+
([, entry]) => entry !== void 0
|
|
108
|
+
);
|
|
109
|
+
if (style === "matrix" && explode) {
|
|
110
|
+
return entries.map(([key, entry]) => `;${key}=${encode(entry)}`).join("");
|
|
111
|
+
}
|
|
112
|
+
if (explode) {
|
|
113
|
+
return entries.map(([key, entry]) => `${encodeURIComponent(key)}=${encode(entry)}`).join(style === "label" ? "." : ",");
|
|
114
|
+
}
|
|
115
|
+
return entries.flatMap(([key, entry]) => [encodeURIComponent(key), encode(entry)]).join(",");
|
|
116
|
+
}
|
|
117
|
+
return encode(value);
|
|
118
|
+
};
|
|
119
|
+
var stringify = (value) => typeof value === "object" && value !== null ? JSON.stringify(value) : String(value);
|
|
120
|
+
|
|
121
|
+
// src/openapi/executeRequest.ts
|
|
122
|
+
var MAX_INLINE_MEDIA_BYTES = 8 * 1024 * 1024;
|
|
123
|
+
var executeRequest = async (options) => {
|
|
124
|
+
const { args, binding, context, route } = options;
|
|
125
|
+
const pathValues = /* @__PURE__ */ new Map();
|
|
126
|
+
const query = new URLSearchParams();
|
|
127
|
+
const cookies = [];
|
|
128
|
+
const headers = new Headers();
|
|
129
|
+
const bodyValues = {};
|
|
130
|
+
for (const [key, value] of Object.entries(args)) {
|
|
131
|
+
const binder = binding.parameters[key];
|
|
132
|
+
if (!binder || value === void 0) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (binder.kind === "body") {
|
|
136
|
+
bodyValues[binder.name] = value;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
const { parameter } = binder;
|
|
140
|
+
switch (parameter.in) {
|
|
141
|
+
case "cookie":
|
|
142
|
+
cookies.push(
|
|
143
|
+
`${parameter.name}=${encodeCookieValue(
|
|
144
|
+
serializeSimpleParameter(parameter, value)
|
|
145
|
+
)}`
|
|
146
|
+
);
|
|
147
|
+
break;
|
|
148
|
+
case "header":
|
|
149
|
+
setHeader(
|
|
150
|
+
headers,
|
|
151
|
+
parameter.name,
|
|
152
|
+
serializeSimpleParameter(parameter, value)
|
|
153
|
+
);
|
|
154
|
+
break;
|
|
155
|
+
case "path":
|
|
156
|
+
pathValues.set(
|
|
157
|
+
parameter.name,
|
|
158
|
+
serializePathParameter(parameter, value)
|
|
159
|
+
);
|
|
160
|
+
break;
|
|
161
|
+
case "query":
|
|
162
|
+
appendQueryParameter(query, parameter, value);
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
for (const [name, value] of Object.entries(
|
|
167
|
+
await resolve(options.query, context)
|
|
168
|
+
)) {
|
|
169
|
+
query.delete(name);
|
|
170
|
+
query.append(name, value);
|
|
171
|
+
}
|
|
172
|
+
if (cookies.length > 0) {
|
|
173
|
+
setHeader(headers, "cookie", cookies.join("; "));
|
|
174
|
+
}
|
|
175
|
+
const body = buildBody(binding, bodyValues, headers);
|
|
176
|
+
for (const [name, value] of Object.entries(
|
|
177
|
+
await resolve(options.headers, context)
|
|
178
|
+
)) {
|
|
179
|
+
setHeader(headers, name, value);
|
|
180
|
+
}
|
|
181
|
+
const url = new URL(buildPath(route.path, pathValues), `${options.baseUrl}/`);
|
|
182
|
+
url.search = toQueryString(query);
|
|
183
|
+
const response = await options.fetchImpl(url, {
|
|
184
|
+
body,
|
|
185
|
+
headers,
|
|
186
|
+
method: route.method.toUpperCase(),
|
|
187
|
+
// Forwarded so an abandoned tool call stops the upstream request too,
|
|
188
|
+
// rather than leaving it running behind a settled promise.
|
|
189
|
+
signal: context.signal
|
|
190
|
+
});
|
|
191
|
+
return await readResponse(response, options);
|
|
192
|
+
};
|
|
193
|
+
var resolveBaseUrl = (servers, origin, override) => {
|
|
194
|
+
if (override) {
|
|
195
|
+
return trimSlash(override);
|
|
196
|
+
}
|
|
197
|
+
const server = servers?.[0];
|
|
198
|
+
if (!server?.url) {
|
|
199
|
+
throw new Error(
|
|
200
|
+
"The OpenAPI document declares no `servers`, so there is no address to call. Pass `baseUrl` to fromOpenAPI()."
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
let url = server.url;
|
|
204
|
+
for (const [name, variable] of Object.entries(server.variables ?? {})) {
|
|
205
|
+
url = url.replaceAll(`{${name}}`, variable.default);
|
|
206
|
+
}
|
|
207
|
+
try {
|
|
208
|
+
return trimSlash(new URL(url).toString());
|
|
209
|
+
} catch {
|
|
210
|
+
if (!origin) {
|
|
211
|
+
throw new Error(
|
|
212
|
+
`The OpenAPI document's server URL ("${url}") is relative, and the document was not loaded from an http(s) URL, so there is nothing to resolve it against. Pass \`baseUrl\` to fromOpenAPI().`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
return trimSlash(new URL(url, origin).toString());
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
var buildBody = (binding, values, headers) => {
|
|
219
|
+
const encoding = binding.bodyEncoding;
|
|
220
|
+
if (!encoding || Object.keys(values).length === 0) {
|
|
221
|
+
return void 0;
|
|
222
|
+
}
|
|
223
|
+
const payload = binding.wholeBodyKey ? values[binding.wholeBodyKey] : values;
|
|
224
|
+
if (payload === void 0) {
|
|
225
|
+
return void 0;
|
|
226
|
+
}
|
|
227
|
+
if (!headers.has("content-type")) {
|
|
228
|
+
headers.set("content-type", encoding.contentType);
|
|
229
|
+
}
|
|
230
|
+
if (encoding.kind === "text") {
|
|
231
|
+
return typeof payload === "string" ? payload : JSON.stringify(payload);
|
|
232
|
+
}
|
|
233
|
+
if (encoding.kind === "form") {
|
|
234
|
+
if (!isRecord2(payload)) {
|
|
235
|
+
return typeof payload === "string" ? payload : JSON.stringify(payload);
|
|
236
|
+
}
|
|
237
|
+
const form = new URLSearchParams();
|
|
238
|
+
for (const [name, value] of Object.entries(payload)) {
|
|
239
|
+
if (value === void 0) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
for (const entry of Array.isArray(value) ? value : [value]) {
|
|
243
|
+
form.append(
|
|
244
|
+
name,
|
|
245
|
+
typeof entry === "object" && entry !== null ? JSON.stringify(entry) : String(entry)
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return form.toString();
|
|
250
|
+
}
|
|
251
|
+
return JSON.stringify(payload);
|
|
252
|
+
};
|
|
253
|
+
var buildPath = (template, values) => {
|
|
254
|
+
let path = template;
|
|
255
|
+
for (const [name, value] of values) {
|
|
256
|
+
path = path.replaceAll(`{${name}}`, value);
|
|
257
|
+
}
|
|
258
|
+
return path.replace(/^\//, "");
|
|
259
|
+
};
|
|
260
|
+
var resolve = async (source, context) => {
|
|
261
|
+
if (!source) {
|
|
262
|
+
return {};
|
|
263
|
+
}
|
|
264
|
+
return typeof source === "function" ? await source(context) : source;
|
|
265
|
+
};
|
|
266
|
+
var readResponse = async (response, options) => {
|
|
267
|
+
const { binding, maxResponseCharacters, route } = options;
|
|
268
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
269
|
+
const label = `${route.method.toUpperCase()} ${route.path}`;
|
|
270
|
+
if (!response.ok) {
|
|
271
|
+
throw new UserError(
|
|
272
|
+
`${label} failed with ${response.status} ${response.statusText}: ${truncate(
|
|
273
|
+
await response.text(),
|
|
274
|
+
2e3
|
|
275
|
+
)}`
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
if (/^(?:audio|image)\//.test(contentType) && !binding.outputSchema) {
|
|
279
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
280
|
+
if (bytes.byteLength <= MAX_INLINE_MEDIA_BYTES) {
|
|
281
|
+
const mimeType = contentType.split(";")[0].trim();
|
|
282
|
+
return contentType.startsWith("image/") ? { data: toBase64(bytes), mimeType, type: "image" } : { data: toBase64(bytes), mimeType, type: "audio" };
|
|
283
|
+
}
|
|
284
|
+
throw new UserError(
|
|
285
|
+
`${label} returned ${bytes.byteLength} bytes of ${contentType}, too large to return inline.`
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
const text = await response.text();
|
|
289
|
+
const parsed = /\bjson\b/.test(contentType) ? tryParse(text.length <= maxResponseCharacters ? text : void 0) : void 0;
|
|
290
|
+
if (isRecord2(parsed)) {
|
|
291
|
+
return { structuredContent: parsed };
|
|
292
|
+
}
|
|
293
|
+
if (binding.outputSchema) {
|
|
294
|
+
throw new UserError(
|
|
295
|
+
`${label} declares a JSON object response but returned ${contentType || "an untyped body"}. Set \`outputSchema: false\` if this document's response schemas do not match the API.`
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
if (text === "") {
|
|
299
|
+
return `${response.status} ${response.statusText}`.trim();
|
|
300
|
+
}
|
|
301
|
+
return parsed === void 0 ? truncate(text, maxResponseCharacters) : truncate(JSON.stringify(parsed, null, 2), maxResponseCharacters);
|
|
302
|
+
};
|
|
303
|
+
var encodeCookieValue = (value) => (
|
|
304
|
+
// eslint-disable-next-line no-control-regex
|
|
305
|
+
value.replace(
|
|
306
|
+
/[\u0000-\u001F\u007F ",;\\]/g,
|
|
307
|
+
(character) => encodeURIComponent(character)
|
|
308
|
+
)
|
|
309
|
+
);
|
|
310
|
+
var isRecord2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
311
|
+
var setHeader = (headers, name, value) => {
|
|
312
|
+
try {
|
|
313
|
+
headers.set(name, value);
|
|
314
|
+
} catch {
|
|
315
|
+
throw new UserError(
|
|
316
|
+
`"${name}" is not a valid header name, or the value given for it is not a valid header value.`
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
var toBase64 = (bytes) => {
|
|
321
|
+
if (typeof Buffer !== "undefined") {
|
|
322
|
+
return Buffer.from(bytes).toString("base64");
|
|
323
|
+
}
|
|
324
|
+
let binary = "";
|
|
325
|
+
for (const byte of bytes) {
|
|
326
|
+
binary += String.fromCharCode(byte);
|
|
327
|
+
}
|
|
328
|
+
return btoa(binary);
|
|
329
|
+
};
|
|
330
|
+
var trimSlash = (value) => value.replace(/\/+$/, "");
|
|
331
|
+
var truncate = (text, limit) => text.length <= limit ? text : `${text.slice(0, limit)}
|
|
332
|
+
|
|
333
|
+
[truncated after ${limit} characters]`;
|
|
334
|
+
var tryParse = (text) => {
|
|
335
|
+
if (text === void 0) {
|
|
336
|
+
return void 0;
|
|
337
|
+
}
|
|
338
|
+
try {
|
|
339
|
+
return JSON.parse(text);
|
|
340
|
+
} catch {
|
|
341
|
+
return void 0;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
// src/openapi/loadSpec.ts
|
|
346
|
+
var loadSpec = async (spec) => {
|
|
347
|
+
if (typeof spec !== "string") {
|
|
348
|
+
return {
|
|
349
|
+
document: hasExternalRef(spec) ? await bundle(spec) : assertOpenApi3(spec)
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
const origin = isHttpUrl(spec) ? spec : void 0;
|
|
353
|
+
const parsed = parseJson(await readSpecText(spec));
|
|
354
|
+
if (!parsed || hasExternalRef(parsed)) {
|
|
355
|
+
return { document: await bundle(spec), origin };
|
|
356
|
+
}
|
|
357
|
+
return { document: assertOpenApi3(parsed), origin };
|
|
358
|
+
};
|
|
359
|
+
var assertOpenApi3 = (document) => {
|
|
360
|
+
if (!document?.openapi?.startsWith("3.")) {
|
|
361
|
+
const found = document?.openapi ?? (document?.swagger ? `Swagger ${document.swagger}` : void 0);
|
|
362
|
+
throw new Error(
|
|
363
|
+
`fromOpenAPI supports OpenAPI 3.x documents only (found ${found ?? "no recognisable version field"}). Convert a Swagger 2.0 document first \u2014 https://converter.swagger.io converts one in place.`
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
return document;
|
|
367
|
+
};
|
|
368
|
+
var bundle = async (spec) => {
|
|
369
|
+
let parser;
|
|
370
|
+
try {
|
|
371
|
+
parser = await import("@apidevtools/swagger-parser");
|
|
372
|
+
} catch {
|
|
373
|
+
throw new Error(
|
|
374
|
+
'This document is YAML or has external $refs, which needs the "@apidevtools/swagger-parser" package. Install it with: npm install @apidevtools/swagger-parser'
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
const SwaggerParser = unwrapDefault(unwrapDefault(parser));
|
|
378
|
+
return assertOpenApi3(await SwaggerParser.bundle(spec));
|
|
379
|
+
};
|
|
380
|
+
var hasExternalRef = (root) => {
|
|
381
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
382
|
+
const stack = [root];
|
|
383
|
+
while (stack.length > 0) {
|
|
384
|
+
const node = stack.pop();
|
|
385
|
+
if (!node || typeof node !== "object" || seen.has(node)) {
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
seen.add(node);
|
|
389
|
+
if (Array.isArray(node)) {
|
|
390
|
+
for (const entry of node) {
|
|
391
|
+
stack.push(entry);
|
|
392
|
+
}
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
for (const [key, value] of Object.entries(node)) {
|
|
396
|
+
if (key === "$ref" && typeof value === "string") {
|
|
397
|
+
if (!value.startsWith("#")) {
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
stack.push(value);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return false;
|
|
406
|
+
};
|
|
407
|
+
var isHttpUrl = (value) => value.startsWith("http://") || value.startsWith("https://");
|
|
408
|
+
var parseJson = (text) => {
|
|
409
|
+
try {
|
|
410
|
+
const value = JSON.parse(text);
|
|
411
|
+
return value && typeof value === "object" ? value : void 0;
|
|
412
|
+
} catch {
|
|
413
|
+
return void 0;
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
var readSpecText = async (spec) => {
|
|
417
|
+
if (isHttpUrl(spec)) {
|
|
418
|
+
const response = await fetch(spec, {
|
|
419
|
+
headers: { accept: "application/json, text/yaml, */*" }
|
|
420
|
+
});
|
|
421
|
+
if (!response.ok) {
|
|
422
|
+
throw new Error(
|
|
423
|
+
`Failed to fetch the OpenAPI document from ${spec}: ${response.status} ${response.statusText}`
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
return await response.text();
|
|
427
|
+
}
|
|
428
|
+
if (spec.trimStart().startsWith("{")) {
|
|
429
|
+
return spec;
|
|
430
|
+
}
|
|
431
|
+
const { readFile } = await import("fs/promises");
|
|
432
|
+
return await readFile(spec, "utf8");
|
|
433
|
+
};
|
|
434
|
+
var unwrapDefault = (value) => typeof value === "object" && value !== null && "default" in value ? value.default : value;
|
|
435
|
+
|
|
436
|
+
// src/openapi/routes.ts
|
|
437
|
+
var HTTP_METHODS = ["get", "post", "put", "patch", "delete"];
|
|
438
|
+
var MAX_REF_HOPS = 32;
|
|
439
|
+
var extractRoutes = (document) => {
|
|
440
|
+
const routes = [];
|
|
441
|
+
for (const [path, pathItem] of Object.entries(document.paths ?? {})) {
|
|
442
|
+
if (!pathItem || typeof pathItem !== "object") {
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
const pathLevelParameters = (pathItem.parameters ?? []).map(
|
|
446
|
+
(parameter) => resolve2(document, parameter)
|
|
447
|
+
);
|
|
448
|
+
for (const method of HTTP_METHODS) {
|
|
449
|
+
const operation = pathItem[method];
|
|
450
|
+
if (!operation) {
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
const responses = {};
|
|
454
|
+
for (const [status, response] of Object.entries(
|
|
455
|
+
operation.responses ?? {}
|
|
456
|
+
)) {
|
|
457
|
+
responses[status] = resolve2(document, response) ?? {};
|
|
458
|
+
}
|
|
459
|
+
routes.push({
|
|
460
|
+
deprecated: operation.deprecated ?? false,
|
|
461
|
+
description: operation.description,
|
|
462
|
+
method,
|
|
463
|
+
operationId: operation.operationId,
|
|
464
|
+
parameters: mergeParameters(
|
|
465
|
+
pathLevelParameters,
|
|
466
|
+
(operation.parameters ?? []).map(
|
|
467
|
+
(parameter) => resolve2(document, parameter)
|
|
468
|
+
)
|
|
469
|
+
),
|
|
470
|
+
path,
|
|
471
|
+
requestBody: operation.requestBody ? resolve2(document, operation.requestBody) : void 0,
|
|
472
|
+
responses,
|
|
473
|
+
servers: operation.servers ?? pathItem.servers,
|
|
474
|
+
summary: operation.summary,
|
|
475
|
+
tags: operation.tags ?? []
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
return routes;
|
|
480
|
+
};
|
|
481
|
+
var toOperationSummary = (route) => ({
|
|
482
|
+
deprecated: route.deprecated,
|
|
483
|
+
description: route.description,
|
|
484
|
+
method: route.method,
|
|
485
|
+
operationId: route.operationId,
|
|
486
|
+
path: route.path,
|
|
487
|
+
summary: route.summary,
|
|
488
|
+
tags: route.tags
|
|
489
|
+
});
|
|
490
|
+
var mergeParameters = (pathLevel, operationLevel) => {
|
|
491
|
+
const overridden = new Set(
|
|
492
|
+
operationLevel.map((parameter) => `${parameter.in}:${parameter.name}`)
|
|
493
|
+
);
|
|
494
|
+
return [
|
|
495
|
+
...pathLevel.filter(
|
|
496
|
+
(parameter) => !overridden.has(`${parameter.in}:${parameter.name}`)
|
|
497
|
+
),
|
|
498
|
+
...operationLevel
|
|
499
|
+
].filter((parameter) => parameter?.name && parameter.in);
|
|
500
|
+
};
|
|
501
|
+
var resolve2 = (document, value) => {
|
|
502
|
+
let node = value;
|
|
503
|
+
for (let hop = 0; hop < MAX_REF_HOPS; hop++) {
|
|
504
|
+
if (!node || typeof node !== "object" || !("$ref" in node)) {
|
|
505
|
+
return node;
|
|
506
|
+
}
|
|
507
|
+
const pointer = node.$ref;
|
|
508
|
+
if (!pointer.startsWith("#")) {
|
|
509
|
+
throw new Error(`Unresolved external $ref after bundling: ${pointer}`);
|
|
510
|
+
}
|
|
511
|
+
node = readPointer(document, pointer);
|
|
512
|
+
}
|
|
513
|
+
throw new Error(
|
|
514
|
+
`$ref chain longer than ${MAX_REF_HOPS} hops, which a cyclic document is the only way to produce.`
|
|
515
|
+
);
|
|
516
|
+
};
|
|
517
|
+
var readPointer = (root, pointer) => {
|
|
518
|
+
const path = pointer.slice(1);
|
|
519
|
+
if (path === "" || path === "/") {
|
|
520
|
+
return root;
|
|
521
|
+
}
|
|
522
|
+
let node = root;
|
|
523
|
+
for (const rawSegment of path.slice(1).split("/")) {
|
|
524
|
+
const segment = decodeSegment(rawSegment);
|
|
525
|
+
node = node?.[segment];
|
|
526
|
+
}
|
|
527
|
+
return node;
|
|
528
|
+
};
|
|
529
|
+
var decodeSegment = (segment) => {
|
|
530
|
+
let decoded = segment;
|
|
531
|
+
try {
|
|
532
|
+
decoded = decodeURIComponent(segment);
|
|
533
|
+
} catch {
|
|
534
|
+
}
|
|
535
|
+
return decoded.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
// src/openapi/naming.ts
|
|
539
|
+
var MAX_NAME_LENGTH = 64;
|
|
540
|
+
var MAX_BASE_LENGTH = MAX_NAME_LENGTH - 5;
|
|
541
|
+
var generateToolNames = (routes, override) => {
|
|
542
|
+
const names = /* @__PURE__ */ new Map();
|
|
543
|
+
const taken = /* @__PURE__ */ new Set();
|
|
544
|
+
for (const route of routes) {
|
|
545
|
+
const base = slugify(baseNameFor(route, override));
|
|
546
|
+
let name = base;
|
|
547
|
+
for (let suffix = 2; taken.has(name); suffix++) {
|
|
548
|
+
name = `${base}_${suffix}`;
|
|
549
|
+
}
|
|
550
|
+
taken.add(name);
|
|
551
|
+
names.set(route, name);
|
|
552
|
+
}
|
|
553
|
+
return names;
|
|
554
|
+
};
|
|
555
|
+
var baseNameFor = (route, override) => {
|
|
556
|
+
const explicit = typeof override === "function" ? override(toOperationSummary(route)) : route.operationId ? override?.[route.operationId] : void 0;
|
|
557
|
+
if (explicit) {
|
|
558
|
+
return explicit;
|
|
559
|
+
}
|
|
560
|
+
if (route.operationId) {
|
|
561
|
+
return route.operationId.split("__")[0];
|
|
562
|
+
}
|
|
563
|
+
return route.summary || `${route.method}_${route.path}`;
|
|
564
|
+
};
|
|
565
|
+
var slugify = (value) => {
|
|
566
|
+
const slug = value.replace(/[^a-zA-Z0-9_-]+/g, "_").replace(/_+/g, "_").replace(/^[_-]+|[_-]+$/g, "").slice(0, MAX_BASE_LENGTH).replace(/[_-]+$/, "");
|
|
567
|
+
return slug || "operation";
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
// src/openapi/normalize.ts
|
|
571
|
+
var SCHEMA_MAP_KEYWORDS = /* @__PURE__ */ new Set([
|
|
572
|
+
"$defs",
|
|
573
|
+
"definitions",
|
|
574
|
+
"dependentSchemas",
|
|
575
|
+
"patternProperties",
|
|
576
|
+
"properties"
|
|
577
|
+
]);
|
|
578
|
+
var VALUE_KEYWORDS = /* @__PURE__ */ new Set([
|
|
579
|
+
"const",
|
|
580
|
+
"default",
|
|
581
|
+
"enum",
|
|
582
|
+
"example",
|
|
583
|
+
"examples"
|
|
584
|
+
]);
|
|
585
|
+
var createSchemaNormalizer = (document) => {
|
|
586
|
+
const definitions = {};
|
|
587
|
+
const namesByPointer = /* @__PURE__ */ new Map();
|
|
588
|
+
const takenNames = /* @__PURE__ */ new Set();
|
|
589
|
+
const hoist = (pointer) => {
|
|
590
|
+
const existing = namesByPointer.get(pointer);
|
|
591
|
+
if (existing !== void 0) {
|
|
592
|
+
return existing;
|
|
593
|
+
}
|
|
594
|
+
const target = readPointer2(document, pointer);
|
|
595
|
+
if (!target || typeof target !== "object") {
|
|
596
|
+
return void 0;
|
|
597
|
+
}
|
|
598
|
+
const segments = pointer.split("/");
|
|
599
|
+
const base = slugify2(decodeSegment2(segments[segments.length - 1] ?? "")) || "schema";
|
|
600
|
+
let name = base;
|
|
601
|
+
for (let suffix = 2; takenNames.has(name); suffix++) {
|
|
602
|
+
name = `${base}_${suffix}`;
|
|
603
|
+
}
|
|
604
|
+
takenNames.add(name);
|
|
605
|
+
namesByPointer.set(pointer, name);
|
|
606
|
+
definitions[name] = {};
|
|
607
|
+
definitions[name] = normalize(target);
|
|
608
|
+
return name;
|
|
609
|
+
};
|
|
610
|
+
const normalize = (value) => {
|
|
611
|
+
const rewritten = rewrite(value);
|
|
612
|
+
return rewritten && typeof rewritten === "object" && !Array.isArray(rewritten) ? rewritten : {};
|
|
613
|
+
};
|
|
614
|
+
const rewrite = (value) => {
|
|
615
|
+
if (Array.isArray(value)) {
|
|
616
|
+
return value.map((entry) => rewrite(entry));
|
|
617
|
+
}
|
|
618
|
+
if (!value || typeof value !== "object") {
|
|
619
|
+
return value;
|
|
620
|
+
}
|
|
621
|
+
const source = value;
|
|
622
|
+
const result = {};
|
|
623
|
+
for (const [key, entry] of Object.entries(source)) {
|
|
624
|
+
if (key === "$ref" && typeof entry === "string") {
|
|
625
|
+
const name = entry.startsWith("#") ? hoist(toPointer(entry)) : void 0;
|
|
626
|
+
if (name !== void 0) {
|
|
627
|
+
result.$ref = `#/$defs/${name}`;
|
|
628
|
+
}
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
631
|
+
if (VALUE_KEYWORDS.has(key)) {
|
|
632
|
+
result[key] = entry;
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
635
|
+
if (SCHEMA_MAP_KEYWORDS.has(key) && entry && typeof entry === "object") {
|
|
636
|
+
result[key] = Object.fromEntries(
|
|
637
|
+
Object.entries(entry).map(
|
|
638
|
+
([name, schema]) => [name, rewrite(schema)]
|
|
639
|
+
)
|
|
640
|
+
);
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
result[key] = rewrite(entry);
|
|
644
|
+
}
|
|
645
|
+
return normalizeKeywords(result);
|
|
646
|
+
};
|
|
647
|
+
return { definitions, normalize };
|
|
648
|
+
};
|
|
649
|
+
var collectDefinitions = (schema, definitions) => {
|
|
650
|
+
const referenced = /* @__PURE__ */ new Set();
|
|
651
|
+
const stack = [schema];
|
|
652
|
+
while (stack.length > 0) {
|
|
653
|
+
const node = stack.pop();
|
|
654
|
+
if (Array.isArray(node)) {
|
|
655
|
+
for (const entry of node) {
|
|
656
|
+
stack.push(entry);
|
|
657
|
+
}
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
if (!node || typeof node !== "object") {
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
for (const [key, value] of Object.entries(node)) {
|
|
664
|
+
if (key === "$ref" && typeof value === "string") {
|
|
665
|
+
const name = value.startsWith("#/$defs/") ? value.slice("#/$defs/".length) : void 0;
|
|
666
|
+
if (name !== void 0 && name in definitions && !referenced.has(name)) {
|
|
667
|
+
referenced.add(name);
|
|
668
|
+
stack.push(definitions[name]);
|
|
669
|
+
}
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
stack.push(value);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
if (referenced.size === 0) {
|
|
676
|
+
return void 0;
|
|
677
|
+
}
|
|
678
|
+
return Object.fromEntries(
|
|
679
|
+
[...referenced].sort().map((name) => [name, definitions[name]])
|
|
680
|
+
);
|
|
681
|
+
};
|
|
682
|
+
var normalizeKeywords = (schema) => {
|
|
683
|
+
if (!("nullable" in schema) && !("required" in schema) && typeof schema.exclusiveMaximum !== "boolean" && typeof schema.exclusiveMinimum !== "boolean") {
|
|
684
|
+
return schema;
|
|
685
|
+
}
|
|
686
|
+
const result = { ...schema };
|
|
687
|
+
if ("required" in result && !Array.isArray(result.required)) {
|
|
688
|
+
delete result.required;
|
|
689
|
+
}
|
|
690
|
+
for (const [flag, bound] of [
|
|
691
|
+
["exclusiveMaximum", "maximum"],
|
|
692
|
+
["exclusiveMinimum", "minimum"]
|
|
693
|
+
]) {
|
|
694
|
+
if (typeof result[flag] !== "boolean") {
|
|
695
|
+
continue;
|
|
696
|
+
}
|
|
697
|
+
const inclusive = result[bound];
|
|
698
|
+
if (result[flag] === true && typeof inclusive === "number") {
|
|
699
|
+
result[flag] = inclusive;
|
|
700
|
+
delete result[bound];
|
|
701
|
+
} else {
|
|
702
|
+
delete result[flag];
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
if ("nullable" in result) {
|
|
706
|
+
const nullable = result.nullable;
|
|
707
|
+
const type = result.type;
|
|
708
|
+
delete result.nullable;
|
|
709
|
+
if (nullable === true && typeof type === "string") {
|
|
710
|
+
result.type = [type, "null"];
|
|
711
|
+
} else if (nullable === true && Array.isArray(type)) {
|
|
712
|
+
result.type = type.includes("null") ? type : [...type, "null"];
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
return result;
|
|
716
|
+
};
|
|
717
|
+
var readPointer2 = (root, pointer) => {
|
|
718
|
+
if (pointer === "") {
|
|
719
|
+
return root;
|
|
720
|
+
}
|
|
721
|
+
let node = root;
|
|
722
|
+
for (const segment of pointer.split("/").slice(1)) {
|
|
723
|
+
node = node?.[decodeSegment2(segment)];
|
|
724
|
+
}
|
|
725
|
+
return node;
|
|
726
|
+
};
|
|
727
|
+
var decodeSegment2 = (segment) => {
|
|
728
|
+
let decoded = segment;
|
|
729
|
+
try {
|
|
730
|
+
decoded = decodeURIComponent(segment);
|
|
731
|
+
} catch {
|
|
732
|
+
}
|
|
733
|
+
return decoded.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
734
|
+
};
|
|
735
|
+
var slugify2 = (value) => value.replace(/[^a-zA-Z0-9_.-]+/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "");
|
|
736
|
+
var toPointer = (ref) => ref.slice(1);
|
|
737
|
+
|
|
738
|
+
// src/openapi/schemas.ts
|
|
739
|
+
var buildToolBinding = (route, normalizer, withOutputSchema) => {
|
|
740
|
+
const body = describeBody(route, normalizer);
|
|
741
|
+
const properties = {};
|
|
742
|
+
const parameters = {};
|
|
743
|
+
const required = [];
|
|
744
|
+
const byName = /* @__PURE__ */ new Map();
|
|
745
|
+
for (const parameter of route.parameters) {
|
|
746
|
+
byName.set(parameter.name, [
|
|
747
|
+
...byName.get(parameter.name) ?? [],
|
|
748
|
+
parameter
|
|
749
|
+
]);
|
|
750
|
+
}
|
|
751
|
+
for (const [name, occurrences] of byName) {
|
|
752
|
+
const collides = occurrences.length > 1 || body.properties.has(name);
|
|
753
|
+
for (const parameter of occurrences) {
|
|
754
|
+
const key = collides ? `${name}__${parameter.in}` : name;
|
|
755
|
+
properties[key] = describeParameter(parameter, normalizer);
|
|
756
|
+
parameters[key] = { kind: "parameter", parameter };
|
|
757
|
+
if (parameter.in === "path" || parameter.required) {
|
|
758
|
+
required.push(key);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
for (const [name, property] of body.properties) {
|
|
763
|
+
properties[name] = property.schema;
|
|
764
|
+
parameters[name] = { kind: "body", name };
|
|
765
|
+
if (property.required) {
|
|
766
|
+
required.push(name);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
const inputSchema = {
|
|
770
|
+
additionalProperties: false,
|
|
771
|
+
properties,
|
|
772
|
+
type: "object",
|
|
773
|
+
...required.length > 0 ? { required: required.sort() } : {}
|
|
774
|
+
};
|
|
775
|
+
const definitions = collectDefinitions(properties, normalizer.definitions);
|
|
776
|
+
if (definitions) {
|
|
777
|
+
inputSchema.$defs = definitions;
|
|
778
|
+
}
|
|
779
|
+
return {
|
|
780
|
+
bodyEncoding: body.encoding,
|
|
781
|
+
caveat: body.caveat,
|
|
782
|
+
inputSchema,
|
|
783
|
+
outputSchema: withOutputSchema ? buildOutputSchema(route, normalizer) : void 0,
|
|
784
|
+
parameters,
|
|
785
|
+
wholeBodyKey: body.wholeBodyKey
|
|
786
|
+
};
|
|
787
|
+
};
|
|
788
|
+
var describeBody = (route, normalizer) => {
|
|
789
|
+
const properties = /* @__PURE__ */ new Map();
|
|
790
|
+
const content = route.method === "get" ? void 0 : route.requestBody?.content;
|
|
791
|
+
if (!content || Object.keys(content).length === 0) {
|
|
792
|
+
return { properties };
|
|
793
|
+
}
|
|
794
|
+
const contentType = selectContentType(Object.keys(content));
|
|
795
|
+
if (!contentType) {
|
|
796
|
+
return {
|
|
797
|
+
caveat: `This operation sends a ${Object.keys(content).sort().join(" or ")} body, which this tool cannot construct.`,
|
|
798
|
+
properties
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
const kind = encodingKindFor(contentType);
|
|
802
|
+
const encoding = { contentType, kind };
|
|
803
|
+
const required = route.requestBody?.required ?? false;
|
|
804
|
+
const rawSchema = content[contentType]?.schema;
|
|
805
|
+
const schema = rawSchema ? normalizer.normalize(rawSchema) : void 0;
|
|
806
|
+
if (kind === "text") {
|
|
807
|
+
properties.set("body", { required, schema: schema ?? { type: "string" } });
|
|
808
|
+
return { encoding, properties, wholeBodyKey: "body" };
|
|
809
|
+
}
|
|
810
|
+
if (!schema) {
|
|
811
|
+
return { encoding, properties };
|
|
812
|
+
}
|
|
813
|
+
const object = flattenObject(schema, normalizer.definitions);
|
|
814
|
+
if (object) {
|
|
815
|
+
for (const [name, property] of Object.entries(object.properties)) {
|
|
816
|
+
properties.set(name, {
|
|
817
|
+
required: object.required.has(name),
|
|
818
|
+
schema: property
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
return { encoding, properties };
|
|
822
|
+
}
|
|
823
|
+
properties.set("body", { required, schema });
|
|
824
|
+
return { encoding, properties, wholeBodyKey: "body" };
|
|
825
|
+
};
|
|
826
|
+
var buildOutputSchema = (route, normalizer) => {
|
|
827
|
+
const status = Object.keys(route.responses).filter((code) => /^2\d\d$/.test(code)).sort()[0];
|
|
828
|
+
const content = status ? route.responses[status]?.content : void 0;
|
|
829
|
+
const contentType = content && findJsonContentType(Object.keys(content));
|
|
830
|
+
const raw = contentType ? content?.[contentType]?.schema : void 0;
|
|
831
|
+
if (!raw) {
|
|
832
|
+
return void 0;
|
|
833
|
+
}
|
|
834
|
+
const schema = normalizer.normalize(raw);
|
|
835
|
+
if (!resolvesToObject(schema, normalizer.definitions)) {
|
|
836
|
+
return void 0;
|
|
837
|
+
}
|
|
838
|
+
const definitions = collectDefinitions(schema, normalizer.definitions);
|
|
839
|
+
return {
|
|
840
|
+
...schema,
|
|
841
|
+
type: "object",
|
|
842
|
+
...definitions ? { $defs: definitions } : {}
|
|
843
|
+
};
|
|
844
|
+
};
|
|
845
|
+
var describeParameter = (parameter, normalizer) => {
|
|
846
|
+
const schema = normalizer.normalize(parameter.schema ?? { type: "string" });
|
|
847
|
+
const description = [
|
|
848
|
+
parameter.deprecated ? "Deprecated." : void 0,
|
|
849
|
+
parameter.description ?? schema.description
|
|
850
|
+
].filter(Boolean).join(" ");
|
|
851
|
+
return description ? { ...schema, description } : schema;
|
|
852
|
+
};
|
|
853
|
+
var flattenObject = (schema, definitions, depth = 0) => {
|
|
854
|
+
if (!schema || depth > 8) {
|
|
855
|
+
return void 0;
|
|
856
|
+
}
|
|
857
|
+
if (typeof schema.$ref === "string" && schema.$ref.startsWith("#/$defs/")) {
|
|
858
|
+
return flattenObject(
|
|
859
|
+
definitions[schema.$ref.slice("#/$defs/".length)],
|
|
860
|
+
definitions,
|
|
861
|
+
depth + 1
|
|
862
|
+
);
|
|
863
|
+
}
|
|
864
|
+
if (Array.isArray(schema.allOf) && schema.allOf.length > 0) {
|
|
865
|
+
const properties2 = {};
|
|
866
|
+
const required = /* @__PURE__ */ new Set();
|
|
867
|
+
for (const branch of schema.allOf) {
|
|
868
|
+
const part = flattenObject(
|
|
869
|
+
branch,
|
|
870
|
+
definitions,
|
|
871
|
+
depth + 1
|
|
872
|
+
);
|
|
873
|
+
if (!part) {
|
|
874
|
+
return void 0;
|
|
875
|
+
}
|
|
876
|
+
Object.assign(properties2, part.properties);
|
|
877
|
+
for (const name of part.required) {
|
|
878
|
+
required.add(name);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
return { properties: properties2, required };
|
|
882
|
+
}
|
|
883
|
+
const properties = schema.properties;
|
|
884
|
+
if (!properties || typeof properties !== "object" || Array.isArray(properties)) {
|
|
885
|
+
return void 0;
|
|
886
|
+
}
|
|
887
|
+
return {
|
|
888
|
+
properties,
|
|
889
|
+
required: new Set(
|
|
890
|
+
Array.isArray(schema.required) ? schema.required : []
|
|
891
|
+
)
|
|
892
|
+
};
|
|
893
|
+
};
|
|
894
|
+
var encodingKindFor = (contentType) => {
|
|
895
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
896
|
+
return "form";
|
|
897
|
+
}
|
|
898
|
+
return contentType.startsWith("text/") || contentType.includes("xml") ? "text" : "json";
|
|
899
|
+
};
|
|
900
|
+
var findJsonContentType = (available) => available.find((type) => type === "application/json") ?? available.find((type) => /^application\/([\w.+-]+\+)?json\b/.test(type));
|
|
901
|
+
var resolvesToObject = (schema, definitions, depth = 0) => {
|
|
902
|
+
if (!schema || depth > 8) {
|
|
903
|
+
return false;
|
|
904
|
+
}
|
|
905
|
+
if (typeof schema.$ref === "string" && schema.$ref.startsWith("#/$defs/")) {
|
|
906
|
+
return resolvesToObject(
|
|
907
|
+
definitions[schema.$ref.slice("#/$defs/".length)],
|
|
908
|
+
definitions,
|
|
909
|
+
depth + 1
|
|
910
|
+
);
|
|
911
|
+
}
|
|
912
|
+
for (const keyword of ["allOf", "anyOf", "oneOf"]) {
|
|
913
|
+
const branches = schema[keyword];
|
|
914
|
+
if (Array.isArray(branches) && branches.length > 0) {
|
|
915
|
+
return branches.every(
|
|
916
|
+
(branch) => resolvesToObject(branch, definitions, depth + 1)
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
return schema.type === "object" || schema.properties !== void 0;
|
|
921
|
+
};
|
|
922
|
+
var selectContentType = (available) => findJsonContentType(available) ?? available.find((type) => type === "application/x-www-form-urlencoded") ?? available.find((type) => type.startsWith("text/") || type.includes("xml"));
|
|
923
|
+
|
|
924
|
+
// src/openapi/selection.ts
|
|
925
|
+
var DEFAULT_MAX_TOOLS = 40;
|
|
926
|
+
var METHOD_ORDER = {
|
|
927
|
+
delete: 4,
|
|
928
|
+
get: 0,
|
|
929
|
+
patch: 3,
|
|
930
|
+
post: 1,
|
|
931
|
+
put: 2
|
|
932
|
+
};
|
|
933
|
+
var selectRoutes = (routes, options) => {
|
|
934
|
+
const { exclude, include, maxTools } = options;
|
|
935
|
+
let selected = include ? routes.filter((route) => include(toOperationSummary(route))) : routes.filter((route) => !route.deprecated);
|
|
936
|
+
if (exclude) {
|
|
937
|
+
selected = selected.filter((route) => !exclude(toOperationSummary(route)));
|
|
938
|
+
}
|
|
939
|
+
selected.sort(
|
|
940
|
+
(a, b) => METHOD_ORDER[a.method] - METHOD_ORDER[b.method] || a.path.localeCompare(b.path)
|
|
941
|
+
);
|
|
942
|
+
const limit = maxTools ?? (include || exclude ? void 0 : DEFAULT_MAX_TOOLS);
|
|
943
|
+
if (limit !== void 0 && selected.length > limit) {
|
|
944
|
+
throw new Error(
|
|
945
|
+
`fromOpenAPI selected ${selected.length} operations, over the limit of ${limit}` + (maxTools === void 0 ? " that applies when no selection is given. Turning a large document into a tool per operation produces a list most clients cannot work with, so this stops rather than guessing. Pass `include`/`exclude` to choose the operations you want, or `maxTools` to raise the limit deliberately." : ". Narrow the selection with `include`/`exclude`, or raise `maxTools`.")
|
|
946
|
+
);
|
|
947
|
+
}
|
|
948
|
+
return selected;
|
|
949
|
+
};
|
|
950
|
+
|
|
951
|
+
// src/openapi/fromOpenAPI.ts
|
|
952
|
+
var ANNOTATIONS = {
|
|
953
|
+
delete: { destructiveHint: true, idempotentHint: true, readOnlyHint: false },
|
|
954
|
+
get: { idempotentHint: true, readOnlyHint: true },
|
|
955
|
+
patch: { destructiveHint: true, idempotentHint: false, readOnlyHint: false },
|
|
956
|
+
post: { destructiveHint: false, idempotentHint: false, readOnlyHint: false },
|
|
957
|
+
put: { destructiveHint: true, idempotentHint: true, readOnlyHint: false }
|
|
958
|
+
};
|
|
959
|
+
var DEFAULT_MAX_RESPONSE_CHARACTERS = 1e6;
|
|
960
|
+
var fromOpenAPI = async (options) => {
|
|
961
|
+
const { document, origin } = await loadSpec(options.spec);
|
|
962
|
+
const selected = selectRoutes(extractRoutes(document), options);
|
|
963
|
+
const names = generateToolNames(selected, options.toolNames);
|
|
964
|
+
const normalizer = createSchemaNormalizer(document);
|
|
965
|
+
const server = options.server ?? new ViteMCP({
|
|
966
|
+
description: document.info?.description,
|
|
967
|
+
name: options.name ?? document.info?.title ?? "OpenAPI",
|
|
968
|
+
version: options.version ?? semanticVersion(document.info?.version)
|
|
969
|
+
});
|
|
970
|
+
for (const route of selected) {
|
|
971
|
+
const name = names.get(route);
|
|
972
|
+
if (!name) {
|
|
973
|
+
continue;
|
|
974
|
+
}
|
|
975
|
+
const binding = buildToolBinding(
|
|
976
|
+
route,
|
|
977
|
+
normalizer,
|
|
978
|
+
options.outputSchema ?? false
|
|
979
|
+
);
|
|
980
|
+
const baseUrl = resolveBaseUrl(
|
|
981
|
+
route.servers ?? document.servers,
|
|
982
|
+
origin,
|
|
983
|
+
options.baseUrl
|
|
984
|
+
);
|
|
985
|
+
server.addTool({
|
|
986
|
+
annotations: {
|
|
987
|
+
...ANNOTATIONS[route.method],
|
|
988
|
+
openWorldHint: true,
|
|
989
|
+
...route.summary ? { title: route.summary } : {}
|
|
990
|
+
},
|
|
991
|
+
description: describe(route, binding.caveat),
|
|
992
|
+
execute: (args, context) => executeRequest({
|
|
993
|
+
args,
|
|
994
|
+
baseUrl,
|
|
995
|
+
binding,
|
|
996
|
+
context,
|
|
997
|
+
fetchImpl: options.fetch ?? globalThis.fetch,
|
|
998
|
+
headers: options.headers,
|
|
999
|
+
maxResponseCharacters: options.maxResponseCharacters ?? DEFAULT_MAX_RESPONSE_CHARACTERS,
|
|
1000
|
+
query: options.query,
|
|
1001
|
+
route
|
|
1002
|
+
}),
|
|
1003
|
+
name,
|
|
1004
|
+
outputSchema: binding.outputSchema ? jsonSchemaAdapter(binding.outputSchema) : void 0,
|
|
1005
|
+
parameters: jsonSchemaAdapter(binding.inputSchema),
|
|
1006
|
+
timeoutMs: options.timeoutMs
|
|
1007
|
+
});
|
|
1008
|
+
}
|
|
1009
|
+
return server;
|
|
1010
|
+
};
|
|
1011
|
+
var describe = (route, caveat) => [
|
|
1012
|
+
route.deprecated ? "Deprecated." : void 0,
|
|
1013
|
+
route.summary,
|
|
1014
|
+
route.description !== route.summary ? route.description : void 0,
|
|
1015
|
+
caveat,
|
|
1016
|
+
`${route.method.toUpperCase()} ${route.path}`
|
|
1017
|
+
].filter(Boolean).join("\n\n");
|
|
1018
|
+
var semanticVersion = (version) => version && /^\d+\.\d+\.\d+$/.test(version) ? version : "1.0.0";
|
|
1019
|
+
export {
|
|
1020
|
+
DEFAULT_MAX_TOOLS,
|
|
1021
|
+
fromOpenAPI
|
|
1022
|
+
};
|
|
1023
|
+
//# sourceMappingURL=index.js.map
|