fastmcp 4.24.4 → 4.24.5

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.
@@ -2,630 +2,727 @@ import {
2
2
  FastMCP,
3
3
  UserError,
4
4
  jsonSchemaAdapter
5
- } from "../chunk-D2CVYAA5.js";
5
+ } from "../chunk-UYV7MFKA.js";
6
6
  import "../chunk-CAQLTSJ5.js";
7
7
  import "../chunk-OKKFL2M4.js";
8
8
 
9
9
  // src/openapi/loadSpec.ts
10
10
  import SwaggerParser from "@apidevtools/swagger-parser";
11
- async function loadSpec(spec) {
12
- const document = await SwaggerParser.bundle(
13
- spec
11
+
12
+ // src/openapi/schemas.ts
13
+ var SCHEMA_MAP_KEYS = /* @__PURE__ */ new Set([
14
+ "$defs",
15
+ "definitions",
16
+ "dependentSchemas",
17
+ "patternProperties",
18
+ "properties"
19
+ ]);
20
+ var DATA_KEYS = /* @__PURE__ */ new Set([
21
+ "const",
22
+ "default",
23
+ "enum",
24
+ "example",
25
+ "examples"
26
+ ]);
27
+ var STRIP_KEYS = /* @__PURE__ */ new Set(["example", "examples"]);
28
+ function buildFlatSchema(route, sharedDefs) {
29
+ const byName = /* @__PURE__ */ new Map();
30
+ for (const param of route.parameters) {
31
+ const list = byName.get(param.name) ?? [];
32
+ list.push(param);
33
+ byName.set(param.name, list);
34
+ }
35
+ const {
36
+ bodyEncoding,
37
+ properties: bodyProperties,
38
+ unsupportedBodyContentType,
39
+ wholeBodyKey
40
+ } = extractBodyProperties(
41
+ route.method === "get" ? void 0 : route.requestBody,
42
+ sharedDefs
14
43
  );
15
- if (!document.openapi?.startsWith("3.")) {
16
- throw new Error(
17
- `fromOpenAPI only supports OpenAPI 3.x documents (found ${document.openapi ?? document.swagger ?? "an unrecognized version"}). Swagger 2.0 is not supported.`
18
- );
44
+ const properties = /* @__PURE__ */ Object.create(null);
45
+ const required = [];
46
+ const parameterMap = /* @__PURE__ */ Object.create(null);
47
+ const used = new Set(bodyProperties.keys());
48
+ for (const [name, occurrences] of byName) {
49
+ if (occurrences.length === 1 && !bodyProperties.has(name)) {
50
+ used.add(name);
51
+ }
52
+ }
53
+ const allocateAlias = (base) => {
54
+ let key = base;
55
+ let suffix = 1;
56
+ while (used.has(key)) {
57
+ suffix += 1;
58
+ key = `${base}_${suffix}`;
59
+ }
60
+ used.add(key);
61
+ return key;
62
+ };
63
+ for (const [name, occurrences] of byName) {
64
+ const collides = name === "__proto__" || occurrences.length > 1 || bodyProperties.has(name);
65
+ for (const param of occurrences) {
66
+ const base = collides ? `${name}__${param.in}` : name;
67
+ const key = collides ? allocateAlias(base) : name;
68
+ properties[key] = rewriteComponentRefs(
69
+ param.schema ?? { type: "string" }
70
+ );
71
+ parameterMap[key] = {
72
+ allowReserved: param.allowReserved,
73
+ explode: param.explode,
74
+ in: param.in,
75
+ name,
76
+ style: param.style
77
+ };
78
+ if (param.in === "path" || param.required) {
79
+ required.push(key);
80
+ }
81
+ }
82
+ }
83
+ for (const [name, { required: isRequired, schema }] of bodyProperties) {
84
+ const key = name === "__proto__" ? allocateAlias(`${name}__body`) : name;
85
+ properties[key] = rewriteComponentRefs(schema);
86
+ parameterMap[key] = { in: "body", name };
87
+ if (isRequired) {
88
+ required.push(key);
89
+ }
90
+ }
91
+ const flatSchema = {
92
+ additionalProperties: false,
93
+ properties,
94
+ type: "object",
95
+ ...required.length > 0 ? { required } : {}
96
+ };
97
+ const usedDefs = sharedDefs && filterReferencedDefs(properties, sharedDefs);
98
+ if (usedDefs) {
99
+ flatSchema.$defs = usedDefs;
19
100
  }
20
101
  return {
21
- document,
22
- origin: typeof spec === "string" && isHttpUrl(spec) ? spec : void 0
102
+ bodyEncoding,
103
+ flatSchema,
104
+ parameterMap,
105
+ unsupportedBodyContentType,
106
+ wholeBodyKey
23
107
  };
24
108
  }
25
- function isHttpUrl(value) {
26
- return /^https?:\/\//i.test(value);
109
+ function buildSharedDefs(document) {
110
+ const schemas = document.components?.schemas;
111
+ if (!schemas || Object.keys(schemas).length === 0) {
112
+ return void 0;
113
+ }
114
+ return rewriteNode(schemas, "schemaMap");
27
115
  }
28
-
29
- // src/openapi/naming.ts
30
- var MAX_NAME_LENGTH = 56;
31
- var MAX_BASE_LENGTH = MAX_NAME_LENGTH - 5;
32
- function generateNames(routes, mcpNames) {
33
- const names = /* @__PURE__ */ new Map();
34
- const used = /* @__PURE__ */ new Set();
35
- for (const route of routes) {
36
- const base = slugify(baseNameFor(route, mcpNames));
37
- let candidate = base;
38
- let suffix = 1;
39
- while (used.has(candidate)) {
40
- suffix += 1;
41
- candidate = `${base}_${suffix}`;
42
- }
43
- used.add(candidate);
44
- names.set(route, candidate);
116
+ var SUCCESS_STATUS_PATTERN = /^2\d\d$/;
117
+ var MAX_OUTPUT_SCHEMA_DEFS = 50;
118
+ function buildOutputSchema(route, sharedDefs) {
119
+ const successEntry = Object.entries(route.responses ?? {}).find(
120
+ ([code]) => SUCCESS_STATUS_PATTERN.test(code)
121
+ );
122
+ const declaredSchema = findMediaType(
123
+ successEntry?.[1].content,
124
+ "application/json"
125
+ )?.schema;
126
+ if (!declaredSchema) {
127
+ return void 0;
45
128
  }
46
- return names;
129
+ const schema = resolveComponentRef(declaredSchema, sharedDefs);
130
+ const rewritten = rewriteComponentRefs(schema, true);
131
+ const { type } = rewritten;
132
+ const isObjectShaped = type === "object" || Array.isArray(type) && type.includes("object");
133
+ if (!isObjectShaped) {
134
+ return void 0;
135
+ }
136
+ const normalized = { ...rewritten, type: "object" };
137
+ const usedDefs = sharedDefs && filterReferencedDefs(normalized, sharedDefs);
138
+ if (usedDefs && Object.keys(usedDefs).length > MAX_OUTPUT_SCHEMA_DEFS) {
139
+ return void 0;
140
+ }
141
+ return {
142
+ ...normalized,
143
+ ...usedDefs ? {
144
+ $defs: rewriteNode(usedDefs, "schemaMap", true)
145
+ } : {}
146
+ };
47
147
  }
48
- function baseNameFor(route, mcpNames) {
49
- if (route.operationId) {
50
- const override = mcpNames && Object.hasOwn(mcpNames, route.operationId) ? mcpNames[route.operationId] : void 0;
51
- return override ?? route.operationId.split("__")[0];
148
+ function resolveComponentRef(schema, sharedDefs) {
149
+ const seen = /* @__PURE__ */ new Set();
150
+ let current = schema;
151
+ let ref = current.$ref;
152
+ while (typeof ref === "string") {
153
+ const name = componentSchemaName(ref);
154
+ const target = name === void 0 ? void 0 : sharedDefs?.[name];
155
+ if (name === void 0 || target === void 0 || seen.has(name)) {
156
+ break;
157
+ }
158
+ seen.add(name);
159
+ current = target;
160
+ ref = current.$ref;
52
161
  }
53
- return route.summary || `${route.method}_${route.path}`;
162
+ return current;
54
163
  }
55
- function slugify(value) {
56
- const slug = value.replace(/[^a-zA-Z0-9_]+/g, "_").replace(/_+/g, "_").replace(/^_|_$/g, "").slice(0, MAX_BASE_LENGTH);
57
- return slug || "operation";
164
+ function rewriteComponentRefs(value, stripExamples = false) {
165
+ return rewriteNode(value, "schema", stripExamples);
58
166
  }
59
-
60
- // src/openapi/requestBuilder.ts
61
- async function executeRequest(options) {
62
- const baseUrl = resolveBaseUrl(
63
- options.servers,
64
- options.origin,
65
- options.baseUrlOverride
66
- );
67
- const pathParams = /* @__PURE__ */ Object.create(null);
68
- const queryParts = [];
69
- const headers = new Headers(await resolveHeaders(options.headers));
70
- const bodyProps = /* @__PURE__ */ Object.create(null);
71
- for (const [key, value] of Object.entries(options.args)) {
72
- const mapping = options.parameterMap[key];
73
- if (!mapping || value === void 0) {
74
- continue;
167
+ function childMode(key) {
168
+ if (DATA_KEYS.has(key)) {
169
+ return "data";
170
+ }
171
+ return SCHEMA_MAP_KEYS.has(key) ? "schemaMap" : "schema";
172
+ }
173
+ function componentSchemaName(ref) {
174
+ for (const prefix of ["#/components/schemas/", "#/$defs/"]) {
175
+ if (ref.startsWith(prefix)) {
176
+ return ref.slice(prefix.length).replaceAll("~1", "/").replaceAll("~0", "~");
75
177
  }
76
- switch (mapping.in) {
77
- case "body":
78
- bodyProps[mapping.name] = value;
79
- break;
80
- case "cookie": {
81
- const pair = `${mapping.name}=${encodeCookieValue(mapping, value)}`;
82
- const existing = headers.get("cookie");
83
- headers.set("cookie", existing ? `${existing}; ${pair}` : pair);
84
- break;
85
- }
86
- case "header":
87
- headers.set(mapping.name, String(value));
88
- break;
89
- case "path":
90
- pathParams[mapping.name] = encodePathValue(
91
- value,
92
- mapping.style,
93
- mapping.explode
94
- );
95
- break;
96
- case "query": {
97
- const query = new URLSearchParams();
98
- appendQueryValue(
99
- query,
100
- mapping.name,
101
- mapping.style,
102
- value,
103
- mapping.explode
104
- );
105
- if (query.size > 0) {
106
- queryParts.push(serializeQuery(query, mapping.allowReserved));
107
- }
108
- break;
109
- }
178
+ }
179
+ return void 0;
180
+ }
181
+ function extractBodyProperties(requestBody, sharedDefs) {
182
+ const properties = /* @__PURE__ */ new Map();
183
+ const content = requestBody?.content;
184
+ if (!content) {
185
+ return { properties };
186
+ }
187
+ const json = findMediaType(content, "application/json");
188
+ const form = findMediaType(content, "application/x-www-form-urlencoded");
189
+ if (json === void 0 && form === void 0) {
190
+ const contentTypes = Object.keys(content);
191
+ return contentTypes.length > 0 ? { properties, unsupportedBodyContentType: contentTypes[0] } : { properties };
192
+ }
193
+ const bodyEncoding = json === void 0 ? "form" : "json";
194
+ const declaredSchema = bodyEncoding === "json" ? json?.schema : form?.schema;
195
+ if (!declaredSchema) {
196
+ return { bodyEncoding, properties };
197
+ }
198
+ const schema = bodyEncoding === "form" ? resolveComponentRef(declaredSchema, sharedDefs) : declaredSchema;
199
+ const schemaProperties = schema.properties;
200
+ if (schema.type === "object" && schemaProperties && (bodyEncoding === "form" || schema.additionalProperties === false || Object.keys(schemaProperties).length > 0)) {
201
+ const requiredNames = new Set(
202
+ schema.required ?? []
203
+ );
204
+ for (const [name, propertySchema] of Object.entries(schemaProperties)) {
205
+ properties.set(name, {
206
+ required: requiredNames.has(name),
207
+ schema: propertySchema
208
+ });
110
209
  }
210
+ return { bodyEncoding, properties };
111
211
  }
112
- let path = options.route.path;
113
- for (const [name, value] of Object.entries(pathParams)) {
114
- path = path.replaceAll(`{${name}}`, value);
212
+ if (bodyEncoding === "form") {
213
+ return {
214
+ properties,
215
+ unsupportedBodyContentType: "application/x-www-form-urlencoded"
216
+ };
115
217
  }
116
- assertNoSubstitutedDotSegments(options.route, path);
117
- const url = new URL(baseUrl.replace(/\/$/, "") + path);
118
- url.search = queryParts.join("&");
119
- let body;
120
- const requiredJsonBody = options.bodyEncoding === "json" && options.route.requestBody?.required === true && !options.wholeBodyKey;
121
- if (Object.keys(bodyProps).length > 0 || requiredJsonBody) {
122
- const payload = options.wholeBodyKey ? bodyProps[options.wholeBodyKey] : bodyProps;
123
- if (options.bodyEncoding === "form") {
124
- if (!headers.has("content-type")) {
125
- headers.set("content-type", "application/x-www-form-urlencoded");
126
- }
127
- body = encodeFormBody(payload);
128
- } else {
129
- if (!headers.has("content-type")) {
130
- headers.set("content-type", "application/json");
218
+ properties.set("body", {
219
+ required: requestBody?.required ?? false,
220
+ schema
221
+ });
222
+ return { bodyEncoding, properties, wholeBodyKey: "body" };
223
+ }
224
+ function filterReferencedDefs(node, allDefs) {
225
+ const referenced = /* @__PURE__ */ new Set();
226
+ const stack = [node];
227
+ while (stack.length > 0) {
228
+ const current = stack.pop();
229
+ if (Array.isArray(current)) {
230
+ stack.push(...current);
231
+ continue;
232
+ }
233
+ if (!current || typeof current !== "object") {
234
+ continue;
235
+ }
236
+ for (const [key, value] of Object.entries(
237
+ current
238
+ )) {
239
+ if (key === "$ref" && typeof value === "string" && value.startsWith("#/$defs/")) {
240
+ const [first = ""] = value.slice("#/$defs/".length).split("/");
241
+ const name = first.replaceAll("~1", "/").replaceAll("~0", "~");
242
+ if (allDefs[name] && !referenced.has(name)) {
243
+ referenced.add(name);
244
+ stack.push(allDefs[name]);
245
+ }
246
+ continue;
131
247
  }
132
- body = JSON.stringify(payload);
248
+ stack.push(value);
133
249
  }
134
250
  }
135
- const response = await options.fetchImpl(url.toString(), {
136
- body,
137
- headers,
138
- method: options.route.method.toUpperCase(),
139
- signal: options.signal
140
- });
141
- const text = await response.text();
142
- if (!response.ok) {
143
- throw new UserError(
144
- `${options.route.method.toUpperCase()} ${path} failed with ${response.status}: ${text.slice(0, 2e3)}`
145
- );
251
+ if (referenced.size === 0) {
252
+ return void 0;
146
253
  }
147
- const mediaType = response.headers.get("content-type")?.split(";")[0];
148
- if (mediaType?.toLowerCase().includes("json")) {
149
- try {
150
- const json = JSON.parse(text);
151
- return { json, text: JSON.stringify(json, null, 2) };
152
- } catch {
153
- return { text };
154
- }
254
+ return Object.fromEntries(
255
+ [...referenced].map((name) => [name, allDefs[name]])
256
+ );
257
+ }
258
+ function findMediaType(content, mediaType) {
259
+ if (!content) {
260
+ return void 0;
155
261
  }
156
- return { text };
262
+ const key = Object.hasOwn(content, mediaType) ? mediaType : Object.keys(content).find(
263
+ (candidate) => candidate.split(";")[0].trim().toLowerCase() === mediaType
264
+ );
265
+ return key === void 0 ? void 0 : content[key];
157
266
  }
158
- function resolveBaseUrl(servers, origin, overrideUrl) {
159
- if (overrideUrl) {
160
- return overrideUrl.replace(/\/$/, "");
267
+ function normalizeExclusiveBound(schema, exclusiveKey, boundKey) {
268
+ const { [exclusiveKey]: exclusive, ...rest } = schema;
269
+ if (typeof exclusive !== "boolean") {
270
+ return schema;
161
271
  }
162
- const server = servers?.[0];
163
- if (!server) {
164
- throw new Error(
165
- "The OpenAPI document has no `servers` entry. Pass `baseUrl` to fromOpenAPI() explicitly."
166
- );
272
+ if (!exclusive || typeof rest[boundKey] !== "number") {
273
+ return rest;
167
274
  }
168
- let url = server.url;
169
- for (const [name, variable] of Object.entries(server.variables ?? {})) {
170
- url = url.replaceAll(`{${name}}`, () => variable.default);
275
+ const { [boundKey]: bound, ...unbounded } = rest;
276
+ return { ...unbounded, [exclusiveKey]: bound };
277
+ }
278
+ function normalizeExclusiveBounds(schema) {
279
+ return normalizeExclusiveBound(
280
+ normalizeExclusiveBound(schema, "exclusiveMinimum", "minimum"),
281
+ "exclusiveMaximum",
282
+ "maximum"
283
+ );
284
+ }
285
+ function normalizeNullable(schema) {
286
+ if (!("nullable" in schema)) {
287
+ return schema;
171
288
  }
172
- try {
173
- return new URL(url).toString().replace(/\/$/, "");
174
- } catch {
175
- if (!origin) {
176
- throw new Error(
177
- `The OpenAPI document's servers[0].url ("${url}") is relative, and the spec was not loaded from an http(s) URL, so it cannot be resolved to an absolute address. Pass \`baseUrl\` to fromOpenAPI() explicitly.`
178
- );
179
- }
180
- return new URL(url, origin).toString().replace(/\/$/, "");
289
+ const { nullable, ...rest } = schema;
290
+ if (nullable !== true) {
291
+ return rest;
181
292
  }
182
- }
183
- function appendBracketPairs(params, key, value) {
184
- if (Array.isArray(value)) {
185
- for (const item of value) {
186
- if (item !== null && typeof item === "object") {
187
- appendBracketPairs(params, `${key}[]`, item);
188
- } else {
189
- params.append(key, String(item));
190
- }
191
- }
192
- return;
293
+ const { type } = rest;
294
+ if (typeof type === "string") {
295
+ return { ...rest, type: [type, "null"] };
193
296
  }
194
- if (value !== null && typeof value === "object") {
195
- for (const [subKey, subValue] of Object.entries(
196
- value
197
- )) {
198
- if (subValue !== void 0) {
199
- appendBracketPairs(params, `${key}[${subKey}]`, subValue);
200
- }
201
- }
202
- return;
297
+ if (Array.isArray(type)) {
298
+ return { ...rest, type: [.../* @__PURE__ */ new Set(["null", ...type])] };
203
299
  }
204
- params.append(key, String(value));
300
+ return rest;
205
301
  }
206
- function appendQueryValue(query, name, style, value, explode) {
207
- if (style === "deepObject") {
208
- appendBracketPairs(query, name, value);
209
- return;
210
- }
211
- if (style === "spaceDelimited" || style === "pipeDelimited") {
212
- const items = Array.isArray(value) ? value : [value];
213
- const separator = style === "spaceDelimited" ? " " : "|";
214
- query.append(name, items.map(String).join(separator));
215
- return;
302
+ function rewriteNode(value, mode, stripExamples = false) {
303
+ if (mode === "data") {
304
+ return value;
216
305
  }
217
- if ((style === void 0 || style === "form") && value !== null && typeof value === "object" && !Array.isArray(value)) {
218
- const entries = Object.entries(value);
219
- if (explode === false) {
220
- if (entries.length > 0) {
221
- query.append(
222
- name,
223
- entries.flatMap(([key, item]) => [key, String(item)]).join(",")
224
- );
225
- }
226
- } else {
227
- for (const [key, item] of entries) {
228
- query.append(key, String(item));
229
- }
230
- }
231
- return;
306
+ if (Array.isArray(value)) {
307
+ return value.map((item) => rewriteNode(item, "schema", stripExamples));
232
308
  }
233
- if ((style === void 0 || style === "form") && explode === false && Array.isArray(value) && value.length > 0) {
234
- query.append(name, value.map(String).join(","));
235
- return;
309
+ if (!value || typeof value !== "object") {
310
+ return value;
236
311
  }
237
- for (const item of Array.isArray(value) ? value : [value]) {
238
- query.append(name, String(item));
312
+ const entries = Object.entries(value).filter(
313
+ ([key]) => !stripExamples || mode !== "schema" || !STRIP_KEYS.has(key)
314
+ ).map(([key, entryValue]) => {
315
+ if (mode === "schemaMap") {
316
+ return [key, rewriteNode(entryValue, "schema", stripExamples)];
317
+ }
318
+ if (key === "$ref" && typeof entryValue === "string" && entryValue.startsWith("#/components/schemas/")) {
319
+ return [key, entryValue.replace("#/components/schemas/", "#/$defs/")];
320
+ }
321
+ return [key, rewriteNode(entryValue, childMode(key), stripExamples)];
322
+ });
323
+ const rewritten = Object.fromEntries(entries);
324
+ return mode === "schemaMap" ? rewritten : normalizeExclusiveBounds(normalizeNullable(rewritten));
325
+ }
326
+
327
+ // src/openapi/loadSpec.ts
328
+ async function loadSpec(spec) {
329
+ const document = await SwaggerParser.bundle(
330
+ spec
331
+ );
332
+ if (!document.openapi?.startsWith("3.")) {
333
+ throw new Error(
334
+ `fromOpenAPI only supports OpenAPI 3.x documents (found ${document.openapi ?? document.swagger ?? "an unrecognized version"}). Swagger 2.0 is not supported.`
335
+ );
239
336
  }
337
+ resolveBundledPointerRefs(document);
338
+ return {
339
+ document,
340
+ origin: typeof spec === "string" && isHttpUrl(spec) ? spec : void 0
341
+ };
240
342
  }
241
- function assertNoSubstitutedDotSegments(route, resolvedPath) {
242
- const templateSegments = new Set(route.path.split("/"));
243
- for (const segment of resolvedPath.split("/")) {
244
- if ((segment === "." || segment === "..") && !templateSegments.has(segment)) {
245
- throw new UserError(
246
- `${route.method.toUpperCase()} ${route.path}: path parameter values must not produce a "${segment}" path segment.`
343
+ function isHttpUrl(value) {
344
+ return /^https?:\/\//i.test(value);
345
+ }
346
+ function resolveBundledPointerRefs(document) {
347
+ const hoisted = /* @__PURE__ */ new Map();
348
+ let counter = 0;
349
+ const resolvePointer = (pointer) => {
350
+ let segments;
351
+ try {
352
+ segments = pointer.slice(2).split("/").map(
353
+ (segment) => decodeURIComponent(
354
+ segment.replaceAll("~1", "/").replaceAll("~0", "~")
355
+ )
247
356
  );
357
+ } catch {
358
+ return void 0;
248
359
  }
249
- }
250
- }
251
- function encodeCookieValue(mapping, value) {
252
- return (Array.isArray(value) ? value : [value]).map((item) => {
253
- const text = String(item);
254
- if (mapping.style !== "cookie") {
255
- return encodePathComponent(text);
360
+ let node = document;
361
+ for (const segment of segments) {
362
+ if (!node || typeof node !== "object" || !Object.hasOwn(node, segment)) {
363
+ return void 0;
364
+ }
365
+ node = node[segment];
256
366
  }
257
- if (/[;,]/.test(text)) {
258
- throw new UserError(
259
- `Cookie parameter "${mapping.name}" uses style "cookie" and its value contains ";" or ","`
260
- );
367
+ return node;
368
+ };
369
+ const hoist = (ref) => {
370
+ const existing = hoisted.get(ref);
371
+ if (existing !== void 0) {
372
+ return existing;
261
373
  }
262
- return text;
263
- }).join(",");
264
- }
265
- function encodeFormBody(payload) {
266
- const params = new URLSearchParams();
267
- if (payload && typeof payload === "object" && !Array.isArray(payload)) {
268
- for (const [key, value] of Object.entries(
269
- payload
270
- )) {
271
- if (value === void 0) {
272
- continue;
374
+ const target = resolvePointer(ref);
375
+ if (!target || typeof target !== "object") {
376
+ return void 0;
377
+ }
378
+ const schemas = (document.components ??= {}).schemas ??= {};
379
+ let name;
380
+ do {
381
+ name = `__bundle_${counter++}`;
382
+ } while (Object.hasOwn(schemas, name));
383
+ hoisted.set(ref, name);
384
+ schemas[name] = target;
385
+ walk(target, "schema");
386
+ return name;
387
+ };
388
+ const rewriteRef = (record) => {
389
+ const ref = record.$ref;
390
+ if (typeof ref !== "string" || !ref.startsWith("#/") || ref.startsWith("#/components/schemas/") || ref.startsWith("#/$defs/")) {
391
+ return;
392
+ }
393
+ const name = hoist(ref);
394
+ if (name !== void 0) {
395
+ record.$ref = `#/components/schemas/${name}`;
396
+ }
397
+ };
398
+ const childMode2 = (key, mode) => {
399
+ if (mode === "schemaMap") {
400
+ return "schema";
401
+ }
402
+ if (mode === "schema") {
403
+ return SCHEMA_MAP_KEYS.has(key) ? "schemaMap" : "schema";
404
+ }
405
+ if (key === "schema") {
406
+ return "schema";
407
+ }
408
+ if (mode === "document" && key === "components") {
409
+ return "components";
410
+ }
411
+ return mode === "components" && key === "schemas" ? "schemaMap" : "document";
412
+ };
413
+ const walk = (value, mode) => {
414
+ if (Array.isArray(value)) {
415
+ for (const item of value) {
416
+ walk(item, mode);
273
417
  }
274
- appendBracketPairs(params, key, value);
418
+ return;
275
419
  }
276
- }
277
- return params.toString();
278
- }
279
- function encodePathComponent(value) {
280
- return encodeURIComponent(value).replace(
281
- /[!'()*]/g,
282
- (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`
283
- );
420
+ if (!value || typeof value !== "object") {
421
+ return;
422
+ }
423
+ const record = value;
424
+ for (const [key, entryValue] of Object.entries(record)) {
425
+ if (mode === "schema" && key === "$ref") {
426
+ rewriteRef(record);
427
+ } else if (mode !== "schema" || !DATA_KEYS.has(key)) {
428
+ walk(entryValue, childMode2(key, mode));
429
+ }
430
+ }
431
+ };
432
+ walk(document, "document");
284
433
  }
285
- function encodePathValue(value, style, explode) {
286
- if ((style === void 0 || style === "simple") && Array.isArray(value) && value.every((item) => item === null || typeof item !== "object")) {
287
- return value.map((item) => encodePathComponent(String(item))).join(",");
288
- }
289
- if ((style === void 0 || style === "simple") && value !== null && typeof value === "object" && !Array.isArray(value)) {
290
- const entries = Object.entries(value);
291
- if (entries.every(([, item]) => item === null || typeof item !== "object")) {
292
- return entries.map(([key, item]) => {
293
- const encodedKey = encodePathComponent(key);
294
- const encodedValue = encodePathComponent(String(item));
295
- return explode === true ? `${encodedKey}=${encodedValue}` : `${encodedKey},${encodedValue}`;
296
- }).join(",");
434
+
435
+ // src/openapi/naming.ts
436
+ var MAX_NAME_LENGTH = 56;
437
+ var MAX_BASE_LENGTH = MAX_NAME_LENGTH - 5;
438
+ function generateNames(routes, mcpNames) {
439
+ const names = /* @__PURE__ */ new Map();
440
+ const used = /* @__PURE__ */ new Set();
441
+ for (const route of routes) {
442
+ const base = slugify(baseNameFor(route, mcpNames));
443
+ let candidate = base;
444
+ let suffix = 1;
445
+ while (used.has(candidate)) {
446
+ suffix += 1;
447
+ candidate = `${base}_${suffix}`;
297
448
  }
449
+ used.add(candidate);
450
+ names.set(route, candidate);
298
451
  }
299
- return encodeURIComponent(String(value));
452
+ return names;
300
453
  }
301
- async function resolveHeaders(headers) {
302
- if (!headers) {
303
- return {};
454
+ function baseNameFor(route, mcpNames) {
455
+ if (route.operationId) {
456
+ const override = mcpNames && Object.hasOwn(mcpNames, route.operationId) ? mcpNames[route.operationId] : void 0;
457
+ return override ?? route.operationId.split("__")[0];
304
458
  }
305
- return typeof headers === "function" ? await headers() : { ...headers };
459
+ return route.summary || `${route.method}_${route.path}`;
306
460
  }
307
- function serializeQuery(query, allowReserved) {
308
- const encoded = query.toString();
309
- if (!allowReserved) {
310
- return encoded;
311
- }
312
- return encoded.split("&").map((pair) => {
313
- const [name, value] = pair.split("=");
314
- const expanded = value.replace(
315
- /\+|%25([\da-f]{2})|%(21|24|27|28|29|2c|2f|3a|3b|3f|40|7e)/gi,
316
- (match, triple) => triple ? `%${triple}` : match === "+" ? "%20" : decodeURIComponent(match)
317
- );
318
- return `${name}=${expanded}`;
319
- }).join("&");
461
+ function slugify(value) {
462
+ const slug = value.replace(/[^a-zA-Z0-9_]+/g, "_").replace(/_+/g, "_").replace(/^_|_$/g, "").slice(0, MAX_BASE_LENGTH);
463
+ return slug || "operation";
320
464
  }
321
465
 
322
- // src/openapi/schemas.ts
323
- var SCHEMA_MAP_KEYS = /* @__PURE__ */ new Set([
324
- "$defs",
325
- "definitions",
326
- "dependentSchemas",
327
- "patternProperties",
328
- "properties"
329
- ]);
330
- var DATA_KEYS = /* @__PURE__ */ new Set(["const", "default", "enum", "example", "examples"]);
331
- var STRIP_KEYS = /* @__PURE__ */ new Set(["example", "examples"]);
332
- function buildFlatSchema(route, sharedDefs) {
333
- const byName = /* @__PURE__ */ new Map();
334
- for (const param of route.parameters) {
335
- const list = byName.get(param.name) ?? [];
336
- list.push(param);
337
- byName.set(param.name, list);
338
- }
339
- const {
340
- bodyEncoding,
341
- properties: bodyProperties,
342
- unsupportedBodyContentType,
343
- wholeBodyKey
344
- } = extractBodyProperties(
345
- route.method === "get" ? void 0 : route.requestBody,
346
- sharedDefs
466
+ // src/openapi/requestBuilder.ts
467
+ async function executeRequest(options) {
468
+ const baseUrl = resolveBaseUrl(
469
+ options.servers,
470
+ options.origin,
471
+ options.baseUrlOverride
347
472
  );
348
- const properties = /* @__PURE__ */ Object.create(null);
349
- const required = [];
350
- const parameterMap = /* @__PURE__ */ Object.create(null);
351
- const used = new Set(bodyProperties.keys());
352
- for (const [name, occurrences] of byName) {
353
- if (occurrences.length === 1 && !bodyProperties.has(name)) {
354
- used.add(name);
355
- }
356
- }
357
- const allocateAlias = (base) => {
358
- let key = base;
359
- let suffix = 1;
360
- while (used.has(key)) {
361
- suffix += 1;
362
- key = `${base}_${suffix}`;
473
+ const pathParams = /* @__PURE__ */ Object.create(null);
474
+ const queryParts = [];
475
+ const headers = new Headers(await resolveHeaders(options.headers));
476
+ const bodyProps = /* @__PURE__ */ Object.create(null);
477
+ for (const [key, value] of Object.entries(options.args)) {
478
+ const mapping = options.parameterMap[key];
479
+ if (!mapping || value === void 0) {
480
+ continue;
363
481
  }
364
- used.add(key);
365
- return key;
366
- };
367
- for (const [name, occurrences] of byName) {
368
- const collides = name === "__proto__" || occurrences.length > 1 || bodyProperties.has(name);
369
- for (const param of occurrences) {
370
- const base = collides ? `${name}__${param.in}` : name;
371
- const key = collides ? allocateAlias(base) : name;
372
- properties[key] = rewriteComponentRefs(
373
- param.schema ?? { type: "string" }
374
- );
375
- parameterMap[key] = {
376
- allowReserved: param.allowReserved,
377
- explode: param.explode,
378
- in: param.in,
379
- name,
380
- style: param.style
381
- };
382
- if (param.in === "path" || param.required) {
383
- required.push(key);
482
+ switch (mapping.in) {
483
+ case "body":
484
+ bodyProps[mapping.name] = value;
485
+ break;
486
+ case "cookie": {
487
+ const pair = `${mapping.name}=${encodeCookieValue(mapping, value)}`;
488
+ const existing = headers.get("cookie");
489
+ headers.set("cookie", existing ? `${existing}; ${pair}` : pair);
490
+ break;
491
+ }
492
+ case "header":
493
+ headers.set(mapping.name, String(value));
494
+ break;
495
+ case "path":
496
+ pathParams[mapping.name] = encodePathValue(
497
+ value,
498
+ mapping.style,
499
+ mapping.explode
500
+ );
501
+ break;
502
+ case "query": {
503
+ const query = new URLSearchParams();
504
+ appendQueryValue(
505
+ query,
506
+ mapping.name,
507
+ mapping.style,
508
+ value,
509
+ mapping.explode
510
+ );
511
+ if (query.size > 0) {
512
+ queryParts.push(serializeQuery(query, mapping.allowReserved));
513
+ }
514
+ break;
384
515
  }
385
516
  }
386
517
  }
387
- for (const [name, { required: isRequired, schema }] of bodyProperties) {
388
- const key = name === "__proto__" ? allocateAlias(`${name}__body`) : name;
389
- properties[key] = rewriteComponentRefs(schema);
390
- parameterMap[key] = { in: "body", name };
391
- if (isRequired) {
392
- required.push(key);
518
+ let path = options.route.path;
519
+ for (const [name, value] of Object.entries(pathParams)) {
520
+ path = path.replaceAll(`{${name}}`, value);
521
+ }
522
+ assertNoSubstitutedDotSegments(options.route, path);
523
+ const url = new URL(baseUrl.replace(/\/$/, "") + path);
524
+ url.search = queryParts.join("&");
525
+ let body;
526
+ const requiredJsonBody = options.bodyEncoding === "json" && options.route.requestBody?.required === true && !options.wholeBodyKey;
527
+ if (Object.keys(bodyProps).length > 0 || requiredJsonBody) {
528
+ const payload = options.wholeBodyKey ? bodyProps[options.wholeBodyKey] : bodyProps;
529
+ if (options.bodyEncoding === "form") {
530
+ if (!headers.has("content-type")) {
531
+ headers.set("content-type", "application/x-www-form-urlencoded");
532
+ }
533
+ body = encodeFormBody(payload);
534
+ } else {
535
+ if (!headers.has("content-type")) {
536
+ headers.set("content-type", "application/json");
537
+ }
538
+ body = JSON.stringify(payload);
393
539
  }
394
540
  }
395
- const flatSchema = {
396
- additionalProperties: false,
397
- properties,
398
- type: "object",
399
- ...required.length > 0 ? { required } : {}
400
- };
401
- const usedDefs = sharedDefs && filterReferencedDefs(properties, sharedDefs);
402
- if (usedDefs) {
403
- flatSchema.$defs = usedDefs;
541
+ const response = await options.fetchImpl(url.toString(), {
542
+ body,
543
+ headers,
544
+ method: options.route.method.toUpperCase(),
545
+ signal: options.signal
546
+ });
547
+ const text = await response.text();
548
+ if (!response.ok) {
549
+ throw new UserError(
550
+ `${options.route.method.toUpperCase()} ${path} failed with ${response.status}: ${text.slice(0, 2e3)}`
551
+ );
404
552
  }
405
- return {
406
- bodyEncoding,
407
- flatSchema,
408
- parameterMap,
409
- unsupportedBodyContentType,
410
- wholeBodyKey
411
- };
412
- }
413
- function buildSharedDefs(document) {
414
- const schemas = document.components?.schemas;
415
- if (!schemas || Object.keys(schemas).length === 0) {
416
- return void 0;
553
+ const mediaType = response.headers.get("content-type")?.split(";")[0];
554
+ if (mediaType?.toLowerCase().includes("json")) {
555
+ try {
556
+ const json = JSON.parse(text);
557
+ return { json, text: JSON.stringify(json, null, 2) };
558
+ } catch {
559
+ return { text };
560
+ }
417
561
  }
418
- return rewriteNode(schemas, "schemaMap");
562
+ return { text };
419
563
  }
420
- var SUCCESS_STATUS_PATTERN = /^2\d\d$/;
421
- var MAX_OUTPUT_SCHEMA_DEFS = 50;
422
- function buildOutputSchema(route, sharedDefs) {
423
- const successEntry = Object.entries(route.responses ?? {}).find(
424
- ([code]) => SUCCESS_STATUS_PATTERN.test(code)
425
- );
426
- const declaredSchema = findMediaType(
427
- successEntry?.[1].content,
428
- "application/json"
429
- )?.schema;
430
- if (!declaredSchema) {
431
- return void 0;
564
+ function resolveBaseUrl(servers, origin, overrideUrl) {
565
+ if (overrideUrl) {
566
+ return overrideUrl.replace(/\/$/, "");
432
567
  }
433
- const schema = resolveComponentRef(declaredSchema, sharedDefs);
434
- const rewritten = rewriteComponentRefs(schema, true);
435
- const { type } = rewritten;
436
- const isObjectShaped = type === "object" || Array.isArray(type) && type.includes("object");
437
- if (!isObjectShaped) {
438
- return void 0;
568
+ const server = servers?.[0];
569
+ if (!server) {
570
+ throw new Error(
571
+ "The OpenAPI document has no `servers` entry. Pass `baseUrl` to fromOpenAPI() explicitly."
572
+ );
439
573
  }
440
- const normalized = { ...rewritten, type: "object" };
441
- const usedDefs = sharedDefs && filterReferencedDefs(normalized, sharedDefs);
442
- if (usedDefs && Object.keys(usedDefs).length > MAX_OUTPUT_SCHEMA_DEFS) {
443
- return void 0;
574
+ let url = server.url;
575
+ for (const [name, variable] of Object.entries(server.variables ?? {})) {
576
+ url = url.replaceAll(`{${name}}`, () => variable.default);
444
577
  }
445
- return {
446
- ...normalized,
447
- ...usedDefs ? {
448
- $defs: rewriteNode(usedDefs, "schemaMap", true)
449
- } : {}
450
- };
451
- }
452
- function resolveComponentRef(schema, sharedDefs) {
453
- const seen = /* @__PURE__ */ new Set();
454
- let current = schema;
455
- let ref = current.$ref;
456
- while (typeof ref === "string") {
457
- const name = componentSchemaName(ref);
458
- const target = name === void 0 ? void 0 : sharedDefs?.[name];
459
- if (name === void 0 || target === void 0 || seen.has(name)) {
460
- break;
578
+ try {
579
+ return new URL(url).toString().replace(/\/$/, "");
580
+ } catch {
581
+ if (!origin) {
582
+ throw new Error(
583
+ `The OpenAPI document's servers[0].url ("${url}") is relative, and the spec was not loaded from an http(s) URL, so it cannot be resolved to an absolute address. Pass \`baseUrl\` to fromOpenAPI() explicitly.`
584
+ );
461
585
  }
462
- seen.add(name);
463
- current = target;
464
- ref = current.$ref;
586
+ return new URL(url, origin).toString().replace(/\/$/, "");
465
587
  }
466
- return current;
467
588
  }
468
- function rewriteComponentRefs(value, stripExamples = false) {
469
- return rewriteNode(value, "schema", stripExamples);
470
- }
471
- function childMode(key) {
472
- if (DATA_KEYS.has(key)) {
473
- return "data";
589
+ function appendBracketPairs(params, key, value) {
590
+ if (Array.isArray(value)) {
591
+ for (const item of value) {
592
+ if (item !== null && typeof item === "object") {
593
+ appendBracketPairs(params, `${key}[]`, item);
594
+ } else {
595
+ params.append(key, String(item));
596
+ }
597
+ }
598
+ return;
474
599
  }
475
- return SCHEMA_MAP_KEYS.has(key) ? "schemaMap" : "schema";
476
- }
477
- function componentSchemaName(ref) {
478
- for (const prefix of ["#/components/schemas/", "#/$defs/"]) {
479
- if (ref.startsWith(prefix)) {
480
- return ref.slice(prefix.length).replaceAll("~1", "/").replaceAll("~0", "~");
600
+ if (value !== null && typeof value === "object") {
601
+ for (const [subKey, subValue] of Object.entries(
602
+ value
603
+ )) {
604
+ if (subValue !== void 0) {
605
+ appendBracketPairs(params, `${key}[${subKey}]`, subValue);
606
+ }
481
607
  }
608
+ return;
482
609
  }
483
- return void 0;
610
+ params.append(key, String(value));
484
611
  }
485
- function extractBodyProperties(requestBody, sharedDefs) {
486
- const properties = /* @__PURE__ */ new Map();
487
- const content = requestBody?.content;
488
- if (!content) {
489
- return { properties };
490
- }
491
- const json = findMediaType(content, "application/json");
492
- const form = findMediaType(content, "application/x-www-form-urlencoded");
493
- if (json === void 0 && form === void 0) {
494
- const contentTypes = Object.keys(content);
495
- return contentTypes.length > 0 ? { properties, unsupportedBodyContentType: contentTypes[0] } : { properties };
612
+ function appendQueryValue(query, name, style, value, explode) {
613
+ if (style === "deepObject") {
614
+ appendBracketPairs(query, name, value);
615
+ return;
496
616
  }
497
- const bodyEncoding = json === void 0 ? "form" : "json";
498
- const declaredSchema = bodyEncoding === "json" ? json?.schema : form?.schema;
499
- if (!declaredSchema) {
500
- return { bodyEncoding, properties };
617
+ if (style === "spaceDelimited" || style === "pipeDelimited") {
618
+ const items = Array.isArray(value) ? value : [value];
619
+ const separator = style === "spaceDelimited" ? " " : "|";
620
+ query.append(name, items.map(String).join(separator));
621
+ return;
501
622
  }
502
- const schema = bodyEncoding === "form" ? resolveComponentRef(declaredSchema, sharedDefs) : declaredSchema;
503
- const schemaProperties = schema.properties;
504
- if (schema.type === "object" && schemaProperties && (bodyEncoding === "form" || schema.additionalProperties === false || Object.keys(schemaProperties).length > 0)) {
505
- const requiredNames = new Set(
506
- schema.required ?? []
507
- );
508
- for (const [name, propertySchema] of Object.entries(schemaProperties)) {
509
- properties.set(name, {
510
- required: requiredNames.has(name),
511
- schema: propertySchema
512
- });
623
+ if ((style === void 0 || style === "form") && value !== null && typeof value === "object" && !Array.isArray(value)) {
624
+ const entries = Object.entries(value);
625
+ if (explode === false) {
626
+ if (entries.length > 0) {
627
+ query.append(
628
+ name,
629
+ entries.flatMap(([key, item]) => [key, String(item)]).join(",")
630
+ );
631
+ }
632
+ } else {
633
+ for (const [key, item] of entries) {
634
+ query.append(key, String(item));
635
+ }
513
636
  }
514
- return { bodyEncoding, properties };
637
+ return;
515
638
  }
516
- if (bodyEncoding === "form") {
517
- return {
518
- properties,
519
- unsupportedBodyContentType: "application/x-www-form-urlencoded"
520
- };
639
+ if ((style === void 0 || style === "form") && explode === false && Array.isArray(value) && value.length > 0) {
640
+ query.append(name, value.map(String).join(","));
641
+ return;
642
+ }
643
+ for (const item of Array.isArray(value) ? value : [value]) {
644
+ query.append(name, String(item));
521
645
  }
522
- properties.set("body", {
523
- required: requestBody?.required ?? false,
524
- schema
525
- });
526
- return { bodyEncoding, properties, wholeBodyKey: "body" };
527
646
  }
528
- function filterReferencedDefs(node, allDefs) {
529
- const referenced = /* @__PURE__ */ new Set();
530
- const stack = [node];
531
- while (stack.length > 0) {
532
- const current = stack.pop();
533
- if (Array.isArray(current)) {
534
- stack.push(...current);
535
- continue;
647
+ function assertNoSubstitutedDotSegments(route, resolvedPath) {
648
+ const templateSegments = new Set(route.path.split("/"));
649
+ for (const segment of resolvedPath.split("/")) {
650
+ if ((segment === "." || segment === "..") && !templateSegments.has(segment)) {
651
+ throw new UserError(
652
+ `${route.method.toUpperCase()} ${route.path}: path parameter values must not produce a "${segment}" path segment.`
653
+ );
536
654
  }
537
- if (!current || typeof current !== "object") {
538
- continue;
655
+ }
656
+ }
657
+ function encodeCookieValue(mapping, value) {
658
+ return (Array.isArray(value) ? value : [value]).map((item) => {
659
+ const text = String(item);
660
+ if (mapping.style !== "cookie") {
661
+ return encodePathComponent(text);
662
+ }
663
+ if (/[;,]/.test(text)) {
664
+ throw new UserError(
665
+ `Cookie parameter "${mapping.name}" uses style "cookie" and its value contains ";" or ","`
666
+ );
539
667
  }
668
+ return text;
669
+ }).join(",");
670
+ }
671
+ function encodeFormBody(payload) {
672
+ const params = new URLSearchParams();
673
+ if (payload && typeof payload === "object" && !Array.isArray(payload)) {
540
674
  for (const [key, value] of Object.entries(
541
- current
675
+ payload
542
676
  )) {
543
- if (key === "$ref" && typeof value === "string" && value.startsWith("#/$defs/")) {
544
- const [first = ""] = value.slice("#/$defs/".length).split("/");
545
- const name = first.replaceAll("~1", "/").replaceAll("~0", "~");
546
- if (allDefs[name] && !referenced.has(name)) {
547
- referenced.add(name);
548
- stack.push(allDefs[name]);
549
- }
677
+ if (value === void 0) {
550
678
  continue;
551
679
  }
552
- stack.push(value);
680
+ appendBracketPairs(params, key, value);
553
681
  }
554
682
  }
555
- if (referenced.size === 0) {
556
- return void 0;
557
- }
558
- return Object.fromEntries(
559
- [...referenced].map((name) => [name, allDefs[name]])
560
- );
683
+ return params.toString();
561
684
  }
562
- function findMediaType(content, mediaType) {
563
- if (!content) {
564
- return void 0;
565
- }
566
- const key = Object.hasOwn(content, mediaType) ? mediaType : Object.keys(content).find(
567
- (candidate) => candidate.split(";")[0].trim().toLowerCase() === mediaType
685
+ function encodePathComponent(value) {
686
+ return encodeURIComponent(value).replace(
687
+ /[!'()*]/g,
688
+ (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`
568
689
  );
569
- return key === void 0 ? void 0 : content[key];
570
690
  }
571
- function normalizeExclusiveBound(schema, exclusiveKey, boundKey) {
572
- const { [exclusiveKey]: exclusive, ...rest } = schema;
573
- if (typeof exclusive !== "boolean") {
574
- return schema;
691
+ function encodePathValue(value, style, explode) {
692
+ if ((style === void 0 || style === "simple") && Array.isArray(value) && value.every((item) => item === null || typeof item !== "object")) {
693
+ return value.map((item) => encodePathComponent(String(item))).join(",");
575
694
  }
576
- if (!exclusive || typeof rest[boundKey] !== "number") {
577
- return rest;
695
+ if ((style === void 0 || style === "simple") && value !== null && typeof value === "object" && !Array.isArray(value)) {
696
+ const entries = Object.entries(value);
697
+ if (entries.every(([, item]) => item === null || typeof item !== "object")) {
698
+ return entries.map(([key, item]) => {
699
+ const encodedKey = encodePathComponent(key);
700
+ const encodedValue = encodePathComponent(String(item));
701
+ return explode === true ? `${encodedKey}=${encodedValue}` : `${encodedKey},${encodedValue}`;
702
+ }).join(",");
703
+ }
578
704
  }
579
- const { [boundKey]: bound, ...unbounded } = rest;
580
- return { ...unbounded, [exclusiveKey]: bound };
581
- }
582
- function normalizeExclusiveBounds(schema) {
583
- return normalizeExclusiveBound(
584
- normalizeExclusiveBound(schema, "exclusiveMinimum", "minimum"),
585
- "exclusiveMaximum",
586
- "maximum"
587
- );
705
+ return encodeURIComponent(String(value));
588
706
  }
589
- function normalizeNullable(schema) {
590
- if (!("nullable" in schema)) {
591
- return schema;
592
- }
593
- const { nullable, ...rest } = schema;
594
- if (nullable !== true) {
595
- return rest;
596
- }
597
- const { type } = rest;
598
- if (typeof type === "string") {
599
- return { ...rest, type: [type, "null"] };
600
- }
601
- if (Array.isArray(type)) {
602
- return { ...rest, type: [.../* @__PURE__ */ new Set(["null", ...type])] };
707
+ async function resolveHeaders(headers) {
708
+ if (!headers) {
709
+ return {};
603
710
  }
604
- return rest;
711
+ return typeof headers === "function" ? await headers() : { ...headers };
605
712
  }
606
- function rewriteNode(value, mode, stripExamples = false) {
607
- if (mode === "data") {
608
- return value;
609
- }
610
- if (Array.isArray(value)) {
611
- return value.map((item) => rewriteNode(item, "schema", stripExamples));
612
- }
613
- if (!value || typeof value !== "object") {
614
- return value;
713
+ function serializeQuery(query, allowReserved) {
714
+ const encoded = query.toString();
715
+ if (!allowReserved) {
716
+ return encoded;
615
717
  }
616
- const entries = Object.entries(value).filter(
617
- ([key]) => !stripExamples || mode !== "schema" || !STRIP_KEYS.has(key)
618
- ).map(([key, entryValue]) => {
619
- if (mode === "schemaMap") {
620
- return [key, rewriteNode(entryValue, "schema", stripExamples)];
621
- }
622
- if (key === "$ref" && typeof entryValue === "string" && entryValue.startsWith("#/components/schemas/")) {
623
- return [key, entryValue.replace("#/components/schemas/", "#/$defs/")];
624
- }
625
- return [key, rewriteNode(entryValue, childMode(key), stripExamples)];
626
- });
627
- const rewritten = Object.fromEntries(entries);
628
- return mode === "schemaMap" ? rewritten : normalizeExclusiveBounds(normalizeNullable(rewritten));
718
+ return encoded.split("&").map((pair) => {
719
+ const [name, value] = pair.split("=");
720
+ const expanded = value.replace(
721
+ /\+|%25([\da-f]{2})|%(21|24|27|28|29|2c|2f|3a|3b|3f|40|7e)/gi,
722
+ (match, triple) => triple ? `%${triple}` : match === "+" ? "%20" : decodeURIComponent(match)
723
+ );
724
+ return `${name}=${expanded}`;
725
+ }).join("&");
629
726
  }
630
727
 
631
728
  // src/openapi/resourceMapping.ts