@ttoss/http-server-mcp-openapi 0.2.15 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -19
- package/dist/index.cjs +278 -101
- package/dist/index.d.cts +159 -64
- package/dist/index.d.mts +159 -64
- package/dist/index.mjs +279 -103
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -5,6 +5,9 @@ Object.defineProperty(exports, Symbol.toStringTag, {
|
|
|
5
5
|
let _ttoss_http_server_mcp = require("@ttoss/http-server-mcp");
|
|
6
6
|
|
|
7
7
|
//#region src/schema.ts
|
|
8
|
+
var isRecord = value => {
|
|
9
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10
|
+
};
|
|
8
11
|
var getAlternativeSchemas = schema => {
|
|
9
12
|
if (Array.isArray(schema.oneOf) && schema.oneOf.length > 0) return schema.oneOf;
|
|
10
13
|
if (Array.isArray(schema.anyOf) && schema.anyOf.length > 0) return schema.anyOf;
|
|
@@ -26,36 +29,96 @@ var mergeResolvedSchemas = resolvedAlternatives => {
|
|
|
26
29
|
required: requiredIntersection && requiredIntersection.length > 0 ? requiredIntersection : void 0
|
|
27
30
|
};
|
|
28
31
|
};
|
|
32
|
+
var stripDotSlash = file => {
|
|
33
|
+
return file.replace(/^\.\//, "");
|
|
34
|
+
};
|
|
35
|
+
var findDocument = args => {
|
|
36
|
+
if (!args.documents) return void 0;
|
|
37
|
+
const wanted = stripDotSlash(args.file);
|
|
38
|
+
for (const [key, document] of Object.entries(args.documents)) if (stripDotSlash(key) === wanted) return document;
|
|
39
|
+
};
|
|
40
|
+
/** Follows an RFC 6901 JSON pointer (`/components/schemas/Tag`) into a value. */
|
|
41
|
+
var followPointer = args => {
|
|
42
|
+
const tokens = args.pointer.split("/").slice(1);
|
|
43
|
+
let current = args.root;
|
|
44
|
+
for (const rawToken of tokens) {
|
|
45
|
+
if (!isRecord(current) && !Array.isArray(current)) return void 0;
|
|
46
|
+
const token = decodeURIComponent(rawToken).replace(/~1/g, "/").replace(/~0/g, "~");
|
|
47
|
+
current = current[token];
|
|
48
|
+
}
|
|
49
|
+
return current;
|
|
50
|
+
};
|
|
51
|
+
/** Stable identity for each document, so cycle keys never collide across files. */
|
|
52
|
+
var documentIds = /* @__PURE__ */new WeakMap();
|
|
53
|
+
var nextDocumentId = 0;
|
|
54
|
+
var documentId = spec => {
|
|
55
|
+
const known = documentIds.get(spec);
|
|
56
|
+
if (known !== void 0) return known;
|
|
57
|
+
nextDocumentId += 1;
|
|
58
|
+
documentIds.set(spec, nextDocumentId);
|
|
59
|
+
return nextDocumentId;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Resolves a `$ref` to its target and the document the target lives in (the
|
|
63
|
+
* scope its own nested refs resolve against). A ref without a file part
|
|
64
|
+
* (`#/components/schemas/X`) points into the current document; one with a
|
|
65
|
+
* file part (`./tags.yaml#/components/schemas/Tag`) points into the matching
|
|
66
|
+
* entry of `documents`. Returns `undefined` when the target does not exist.
|
|
67
|
+
*/
|
|
68
|
+
var resolveRef = args => {
|
|
69
|
+
const hashIndex = args.ref.indexOf("#");
|
|
70
|
+
const file = hashIndex === -1 ? args.ref : args.ref.slice(0, hashIndex);
|
|
71
|
+
const pointer = hashIndex === -1 ? "" : args.ref.slice(hashIndex + 1);
|
|
72
|
+
const spec = file === "" ? args.scope.spec : findDocument({
|
|
73
|
+
file,
|
|
74
|
+
documents: args.scope.documents
|
|
75
|
+
});
|
|
76
|
+
if (!spec) return void 0;
|
|
77
|
+
const value = followPointer({
|
|
78
|
+
root: spec,
|
|
79
|
+
pointer
|
|
80
|
+
});
|
|
81
|
+
if (value === void 0) return void 0;
|
|
82
|
+
return {
|
|
83
|
+
value,
|
|
84
|
+
scope: {
|
|
85
|
+
spec,
|
|
86
|
+
documents: args.scope.documents
|
|
87
|
+
},
|
|
88
|
+
key: `${documentId(spec)}#${pointer}`
|
|
89
|
+
};
|
|
90
|
+
};
|
|
29
91
|
var dereferenceValue = args => {
|
|
30
92
|
const {
|
|
31
93
|
value,
|
|
32
|
-
|
|
94
|
+
scope,
|
|
33
95
|
seenRefs
|
|
34
96
|
} = args;
|
|
35
97
|
if (Array.isArray(value)) return value.map(item => {
|
|
36
98
|
return dereferenceValue({
|
|
37
99
|
value: item,
|
|
38
|
-
|
|
100
|
+
scope,
|
|
39
101
|
seenRefs
|
|
40
102
|
});
|
|
41
103
|
});
|
|
42
|
-
if (value
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (
|
|
104
|
+
if (isRecord(value)) {
|
|
105
|
+
if (typeof value.$ref === "string") {
|
|
106
|
+
const target = resolveRef({
|
|
107
|
+
ref: value.$ref,
|
|
108
|
+
scope
|
|
109
|
+
});
|
|
110
|
+
if (!target) return {};
|
|
111
|
+
if (seenRefs.has(target.key)) return {};
|
|
49
112
|
return dereferenceValue({
|
|
50
|
-
value:
|
|
51
|
-
|
|
52
|
-
seenRefs: new Set(seenRefs).add(
|
|
113
|
+
value: target.value,
|
|
114
|
+
scope: target.scope,
|
|
115
|
+
seenRefs: new Set(seenRefs).add(target.key)
|
|
53
116
|
});
|
|
54
117
|
}
|
|
55
118
|
const result = {};
|
|
56
|
-
for (const [key, entryValue] of Object.entries(
|
|
119
|
+
for (const [key, entryValue] of Object.entries(value)) result[key] = dereferenceValue({
|
|
57
120
|
value: entryValue,
|
|
58
|
-
|
|
121
|
+
scope,
|
|
59
122
|
seenRefs
|
|
60
123
|
});
|
|
61
124
|
return result;
|
|
@@ -67,12 +130,17 @@ var dereferenceValue = args => {
|
|
|
67
130
|
* `properties`, `items`, `oneOf`, `anyOf`, etc.), producing a self-contained
|
|
68
131
|
* schema safe to hand to an MCP client or LLM provider as a tool definition —
|
|
69
132
|
* provider tool schemas have no `components` section to resolve refs against.
|
|
133
|
+
*
|
|
134
|
+
* Refs into other files resolve against `documents`; see {@link OpenApiDocuments}.
|
|
70
135
|
*/
|
|
71
|
-
var dereferenceSchema = (schema, spec) => {
|
|
136
|
+
var dereferenceSchema = (schema, spec, documents) => {
|
|
72
137
|
if (!schema) return schema;
|
|
73
138
|
return dereferenceValue({
|
|
74
139
|
value: schema,
|
|
75
|
-
|
|
140
|
+
scope: {
|
|
141
|
+
spec,
|
|
142
|
+
documents
|
|
143
|
+
},
|
|
76
144
|
seenRefs: /* @__PURE__ */new Set()
|
|
77
145
|
});
|
|
78
146
|
};
|
|
@@ -81,25 +149,39 @@ var dereferenceSchema = (schema, spec) => {
|
|
|
81
149
|
* and merges `oneOf` / `anyOf` alternatives (union of properties, intersection
|
|
82
150
|
* of `required`) so the caller sees one flat property set.
|
|
83
151
|
*/
|
|
84
|
-
var resolveSchema = (schema, spec) => {
|
|
152
|
+
var resolveSchema = (schema, spec, documents) => {
|
|
85
153
|
if (!schema) return {};
|
|
86
154
|
if (typeof schema.$ref === "string") {
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
155
|
+
const target = resolveRef({
|
|
156
|
+
ref: schema.$ref,
|
|
157
|
+
scope: {
|
|
158
|
+
spec,
|
|
159
|
+
documents
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
return resolveSchema(target?.value, target?.scope.spec ?? spec, documents);
|
|
90
163
|
}
|
|
91
164
|
const alternatives = getAlternativeSchemas(schema);
|
|
92
165
|
if (alternatives) return mergeResolvedSchemas(alternatives.map(candidate => {
|
|
93
|
-
return resolveSchema(candidate, spec);
|
|
166
|
+
return resolveSchema(candidate, spec, documents);
|
|
94
167
|
}));
|
|
95
168
|
return schema;
|
|
96
169
|
};
|
|
97
|
-
/**
|
|
98
|
-
|
|
170
|
+
/**
|
|
171
|
+
* Follows a parameter `$ref` — into `components.parameters`, or into another
|
|
172
|
+
* file through `documents` — if present.
|
|
173
|
+
*/
|
|
174
|
+
var resolveParameter = (param, spec, documents) => {
|
|
99
175
|
if (!param) return {};
|
|
100
176
|
if (typeof param.$ref === "string") {
|
|
101
|
-
const
|
|
102
|
-
|
|
177
|
+
const target = resolveRef({
|
|
178
|
+
ref: param.$ref,
|
|
179
|
+
scope: {
|
|
180
|
+
spec,
|
|
181
|
+
documents
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
return isRecord(target?.value) ? target.value : {};
|
|
103
185
|
}
|
|
104
186
|
return param;
|
|
105
187
|
};
|
|
@@ -109,9 +191,9 @@ var buildPathFn = (pathTemplate, pathParams) => {
|
|
|
109
191
|
let result = pathTemplate;
|
|
110
192
|
for (const {
|
|
111
193
|
name,
|
|
112
|
-
|
|
194
|
+
argName
|
|
113
195
|
} of pathParams) {
|
|
114
|
-
const value = args[
|
|
196
|
+
const value = args[argName];
|
|
115
197
|
if (value !== void 0) result = result.replace(`{${name}}`, encodeURIComponent(String(value)));
|
|
116
198
|
}
|
|
117
199
|
return result;
|
|
@@ -122,9 +204,6 @@ var NON_EXPLODED_DELIMITERS = {
|
|
|
122
204
|
spaceDelimited: " ",
|
|
123
205
|
pipeDelimited: "|"
|
|
124
206
|
};
|
|
125
|
-
var isRecord = value => {
|
|
126
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
127
|
-
};
|
|
128
207
|
/**
|
|
129
208
|
* Appends a `deepObject` value as bracketed keys — `filters[documentId][$eq]`.
|
|
130
209
|
* OpenAPI only defines one level, but the APIs that ask for `deepObject`
|
|
@@ -212,7 +291,7 @@ var buildQueryFn = queryParams => {
|
|
|
212
291
|
return args => {
|
|
213
292
|
const search = new URLSearchParams();
|
|
214
293
|
for (const param of queryParams) {
|
|
215
|
-
const value = args[param.
|
|
294
|
+
const value = args[param.argName];
|
|
216
295
|
if (value === void 0 || value === null) continue;
|
|
217
296
|
appendQueryValue({
|
|
218
297
|
search,
|
|
@@ -225,8 +304,8 @@ var buildQueryFn = queryParams => {
|
|
|
225
304
|
};
|
|
226
305
|
};
|
|
227
306
|
/**
|
|
228
|
-
* Builds a function that maps
|
|
229
|
-
*
|
|
307
|
+
* Builds a function that maps tool args back to a request body keyed by the
|
|
308
|
+
* spec's property names, skipping `undefined` args. Returns `undefined` when the op has no body.
|
|
230
309
|
*/
|
|
231
310
|
var buildBodyFn = bodyProps => {
|
|
232
311
|
if (bodyProps.length === 0) return void 0;
|
|
@@ -234,8 +313,8 @@ var buildBodyFn = bodyProps => {
|
|
|
234
313
|
const body = {};
|
|
235
314
|
for (const {
|
|
236
315
|
snakeName,
|
|
237
|
-
|
|
238
|
-
} of bodyProps) if (args[
|
|
316
|
+
argName
|
|
317
|
+
} of bodyProps) if (args[argName] !== void 0) body[snakeName] = args[argName];
|
|
239
318
|
return body;
|
|
240
319
|
};
|
|
241
320
|
};
|
|
@@ -246,17 +325,93 @@ var DEFAULT_EXCLUDE_EXTENSION = "x-mcp-exclude";
|
|
|
246
325
|
var DEFAULT_SERVER_MANAGED_EXTENSION = "x-mcp-server-managed";
|
|
247
326
|
|
|
248
327
|
//#endregion
|
|
249
|
-
//#region src/
|
|
328
|
+
//#region src/parameters.ts
|
|
250
329
|
/**
|
|
251
330
|
* Folds `_` and `-` separators into camelCase. OpenAPI operation and parameter
|
|
252
|
-
* names may be snake_case (`agent_id`) or kebab-case (`list-tools`)
|
|
253
|
-
*
|
|
331
|
+
* names may be snake_case (`agent_id`) or kebab-case (`list-tools`); with the
|
|
332
|
+
* default `argumentNames: 'camelCase'` both are folded into tool arguments.
|
|
254
333
|
*/
|
|
255
334
|
var snakeToCamel = str => {
|
|
256
335
|
return str.replace(/[_-]([a-z])/g, (_, letter) => {
|
|
257
336
|
return letter.toUpperCase();
|
|
258
337
|
});
|
|
259
338
|
};
|
|
339
|
+
var verbatim = name => {
|
|
340
|
+
return name;
|
|
341
|
+
};
|
|
342
|
+
var argNameMapper = argumentNames => {
|
|
343
|
+
return argumentNames === "verbatim" ? verbatim : snakeToCamel;
|
|
344
|
+
};
|
|
345
|
+
/**
|
|
346
|
+
* Deduplicates parameter entries by `name`, keeping the last occurrence. When
|
|
347
|
+
* path-item-level and operation-level parameters are concatenated (operation
|
|
348
|
+
* last), this makes the operation-level entry win — as the OpenAPI spec requires.
|
|
349
|
+
*/
|
|
350
|
+
var dedupeByName = items => {
|
|
351
|
+
const byName = /* @__PURE__ */new Map();
|
|
352
|
+
for (const item of items) byName.set(item.name, item);
|
|
353
|
+
return [...byName.values()];
|
|
354
|
+
};
|
|
355
|
+
var extractPathParams = args => {
|
|
356
|
+
const toArgName = args.toArgName ?? snakeToCamel;
|
|
357
|
+
const flag = args.serverManagedExtension ?? "x-mcp-server-managed";
|
|
358
|
+
return dedupeByName((args.parameters || []).map(p => {
|
|
359
|
+
return resolveParameter(p, args.spec, args.documents);
|
|
360
|
+
}).filter(p => {
|
|
361
|
+
return p.in === "path";
|
|
362
|
+
}).map(p => {
|
|
363
|
+
return {
|
|
364
|
+
name: p.name || "",
|
|
365
|
+
argName: toArgName(p.name || ""),
|
|
366
|
+
serverManaged: Boolean(p[flag])
|
|
367
|
+
};
|
|
368
|
+
}));
|
|
369
|
+
};
|
|
370
|
+
var extractQueryParams = args => {
|
|
371
|
+
const toArgName = args.toArgName ?? snakeToCamel;
|
|
372
|
+
const flag = args.serverManagedExtension ?? "x-mcp-server-managed";
|
|
373
|
+
return dedupeByName((args.parameters || []).map(p => {
|
|
374
|
+
return resolveParameter(p, args.spec, args.documents);
|
|
375
|
+
}).filter(p => {
|
|
376
|
+
return p.in === "query";
|
|
377
|
+
}).map(p => {
|
|
378
|
+
return {
|
|
379
|
+
name: p.name || "",
|
|
380
|
+
argName: toArgName(p.name || ""),
|
|
381
|
+
description: p.description || "",
|
|
382
|
+
required: p.required || false,
|
|
383
|
+
type: p.schema?.type || "string",
|
|
384
|
+
style: p.style,
|
|
385
|
+
explode: p.explode,
|
|
386
|
+
serverManaged: Boolean(p[flag])
|
|
387
|
+
};
|
|
388
|
+
}));
|
|
389
|
+
};
|
|
390
|
+
/** Lists the path and query params flagged as server-managed. */
|
|
391
|
+
var collectServerManagedParameters = args => {
|
|
392
|
+
return [...args.pathParams.map(p => {
|
|
393
|
+
return {
|
|
394
|
+
...p,
|
|
395
|
+
in: "path"
|
|
396
|
+
};
|
|
397
|
+
}), ...args.queryParams.map(p => {
|
|
398
|
+
return {
|
|
399
|
+
...p,
|
|
400
|
+
in: "query"
|
|
401
|
+
};
|
|
402
|
+
})].filter(p => {
|
|
403
|
+
return p.serverManaged;
|
|
404
|
+
}).map(p => {
|
|
405
|
+
return {
|
|
406
|
+
name: p.name,
|
|
407
|
+
in: p.in,
|
|
408
|
+
argName: p.argName
|
|
409
|
+
};
|
|
410
|
+
});
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
//#endregion
|
|
414
|
+
//#region src/toolDefinitions.ts
|
|
260
415
|
/** Converts a camelCase `operationId` to a kebab-case tool name. */
|
|
261
416
|
var operationIdToToolName = operationId => {
|
|
262
417
|
return operationId.replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
|
|
@@ -321,23 +476,29 @@ var buildTypedProperty = param => {
|
|
|
321
476
|
};
|
|
322
477
|
};
|
|
323
478
|
var buildInputSchema = (pathParams, queryParams, bodyProps) => {
|
|
324
|
-
const
|
|
479
|
+
const modelPathParams = pathParams.filter(p => {
|
|
480
|
+
return !p.serverManaged;
|
|
481
|
+
});
|
|
482
|
+
const modelQueryParams = queryParams.filter(p => {
|
|
483
|
+
return !p.serverManaged;
|
|
484
|
+
});
|
|
485
|
+
const allParams = [...modelPathParams, ...modelQueryParams, ...bodyProps];
|
|
325
486
|
if (allParams.length === 0) return {
|
|
326
487
|
type: "object"
|
|
327
488
|
};
|
|
328
|
-
const requiredFields = [...
|
|
329
|
-
return p.
|
|
330
|
-
}), ...
|
|
489
|
+
const requiredFields = [...modelPathParams.map(p => {
|
|
490
|
+
return p.argName;
|
|
491
|
+
}), ...modelQueryParams.filter(p => {
|
|
331
492
|
return p.required;
|
|
332
493
|
}).map(p => {
|
|
333
|
-
return p.
|
|
494
|
+
return p.argName;
|
|
334
495
|
}), ...bodyProps.filter(p => {
|
|
335
496
|
return p.required;
|
|
336
497
|
}).map(p => {
|
|
337
|
-
return p.
|
|
498
|
+
return p.argName;
|
|
338
499
|
})];
|
|
339
500
|
const properties = {};
|
|
340
|
-
for (const param of allParams) properties[param.
|
|
501
|
+
for (const param of allParams) properties[param.argName] = "description" in param ? buildTypedProperty(param) : {
|
|
341
502
|
type: "string",
|
|
342
503
|
description: ""
|
|
343
504
|
};
|
|
@@ -349,48 +510,9 @@ var buildInputSchema = (pathParams, queryParams, bodyProps) => {
|
|
|
349
510
|
} : {})
|
|
350
511
|
};
|
|
351
512
|
};
|
|
352
|
-
/**
|
|
353
|
-
* Deduplicates parameter entries by `name`, keeping the last occurrence. When
|
|
354
|
-
* path-item-level and operation-level parameters are concatenated (operation
|
|
355
|
-
* last), this makes the operation-level entry win — as the OpenAPI spec requires.
|
|
356
|
-
*/
|
|
357
|
-
var dedupeByName = items => {
|
|
358
|
-
const byName = /* @__PURE__ */new Map();
|
|
359
|
-
for (const item of items) byName.set(item.name, item);
|
|
360
|
-
return [...byName.values()];
|
|
361
|
-
};
|
|
362
|
-
var extractPathParams = args => {
|
|
363
|
-
return dedupeByName((args.parameters || []).map(p => {
|
|
364
|
-
return resolveParameter(p, args.spec);
|
|
365
|
-
}).filter(p => {
|
|
366
|
-
return p.in === "path";
|
|
367
|
-
}).map(p => {
|
|
368
|
-
return {
|
|
369
|
-
name: p.name || "",
|
|
370
|
-
camelName: snakeToCamel(p.name || "")
|
|
371
|
-
};
|
|
372
|
-
}));
|
|
373
|
-
};
|
|
374
|
-
var extractQueryParams = args => {
|
|
375
|
-
return dedupeByName((args.parameters || []).map(p => {
|
|
376
|
-
return resolveParameter(p, args.spec);
|
|
377
|
-
}).filter(p => {
|
|
378
|
-
return p.in === "query";
|
|
379
|
-
}).map(p => {
|
|
380
|
-
return {
|
|
381
|
-
name: p.name || "",
|
|
382
|
-
camelName: snakeToCamel(p.name || ""),
|
|
383
|
-
description: p.description || "",
|
|
384
|
-
required: p.required || false,
|
|
385
|
-
type: p.schema?.type || "string",
|
|
386
|
-
style: p.style,
|
|
387
|
-
explode: p.explode
|
|
388
|
-
};
|
|
389
|
-
}));
|
|
390
|
-
};
|
|
391
513
|
var resolveBodySchema = args => {
|
|
392
514
|
const rawBodySchema = args.requestBody?.content?.["application/json"]?.schema;
|
|
393
|
-
return resolveSchema(dereferenceSchema(rawBodySchema, args.spec), args.spec);
|
|
515
|
+
return resolveSchema(dereferenceSchema(rawBodySchema, args.spec, args.documents), args.spec, args.documents);
|
|
394
516
|
};
|
|
395
517
|
/**
|
|
396
518
|
* snake_case names of every top-level property an operation's request schema
|
|
@@ -424,6 +546,7 @@ var flattenSingleAllOf = schema => {
|
|
|
424
546
|
};
|
|
425
547
|
};
|
|
426
548
|
var extractBodyProps = args => {
|
|
549
|
+
const toArgName = args.toArgName ?? snakeToCamel;
|
|
427
550
|
const bodySchema = resolveBodySchema(args);
|
|
428
551
|
if (!bodySchema?.properties) return [];
|
|
429
552
|
return Object.entries(bodySchema.properties).filter(([, value]) => {
|
|
@@ -432,7 +555,7 @@ var extractBodyProps = args => {
|
|
|
432
555
|
const val = flattenSingleAllOf(value);
|
|
433
556
|
return {
|
|
434
557
|
snakeName: key,
|
|
435
|
-
|
|
558
|
+
argName: toArgName(key),
|
|
436
559
|
description: typeof val.description === "string" ? val.description : "",
|
|
437
560
|
required: (bodySchema.required || []).includes(key),
|
|
438
561
|
type: typeof val.type === "string" ? val.type : void 0,
|
|
@@ -457,23 +580,37 @@ var processOperation = args => {
|
|
|
457
580
|
if (args.operation[args.options.excludeExtension]) return null;
|
|
458
581
|
const toolName = operationIdToToolName(args.operation.operationId);
|
|
459
582
|
const parameters = [...(args.pathItemParameters ?? []), ...(args.operation.parameters ?? [])];
|
|
583
|
+
const {
|
|
584
|
+
documents,
|
|
585
|
+
serverManagedExtension
|
|
586
|
+
} = args.options;
|
|
587
|
+
const toArgName = argNameMapper(args.options.argumentNames);
|
|
460
588
|
const pathParams = extractPathParams({
|
|
461
589
|
parameters,
|
|
462
|
-
spec: args.spec
|
|
590
|
+
spec: args.spec,
|
|
591
|
+
documents,
|
|
592
|
+
toArgName,
|
|
593
|
+
serverManagedExtension
|
|
463
594
|
});
|
|
464
595
|
const queryParams = extractQueryParams({
|
|
465
596
|
parameters,
|
|
466
|
-
spec: args.spec
|
|
597
|
+
spec: args.spec,
|
|
598
|
+
documents,
|
|
599
|
+
toArgName,
|
|
600
|
+
serverManagedExtension
|
|
467
601
|
});
|
|
468
602
|
const bodyProps = extractBodyProps({
|
|
469
603
|
requestBody: args.operation.requestBody,
|
|
470
604
|
spec: args.spec,
|
|
471
|
-
serverManagedExtension
|
|
605
|
+
serverManagedExtension,
|
|
606
|
+
documents,
|
|
607
|
+
toArgName
|
|
472
608
|
});
|
|
473
609
|
const inputSchema = buildInputSchema(pathParams, queryParams, bodyProps);
|
|
474
610
|
const acceptedBodyFields = extractAcceptedBodyFields({
|
|
475
611
|
requestBody: args.operation.requestBody,
|
|
476
|
-
spec: args.spec
|
|
612
|
+
spec: args.spec,
|
|
613
|
+
documents
|
|
477
614
|
});
|
|
478
615
|
return {
|
|
479
616
|
name: toolName,
|
|
@@ -486,7 +623,11 @@ var processOperation = args => {
|
|
|
486
623
|
query: buildQueryFn(queryParams),
|
|
487
624
|
body: buildBodyFn(bodyProps),
|
|
488
625
|
acceptedBodyFields,
|
|
489
|
-
extensions: extractExtensions(args.operation)
|
|
626
|
+
extensions: extractExtensions(args.operation),
|
|
627
|
+
serverManagedParameters: collectServerManagedParameters({
|
|
628
|
+
pathParams,
|
|
629
|
+
queryParams
|
|
630
|
+
})
|
|
490
631
|
};
|
|
491
632
|
};
|
|
492
633
|
var processPath = args => {
|
|
@@ -507,6 +648,15 @@ var processPath = args => {
|
|
|
507
648
|
}
|
|
508
649
|
return tools;
|
|
509
650
|
};
|
|
651
|
+
/** Applies the defaults to {@link OpenApiToToolsOptions}. */
|
|
652
|
+
var resolveOptions = (options = {}) => {
|
|
653
|
+
return {
|
|
654
|
+
excludeExtension: options.excludeExtension ?? "x-mcp-exclude",
|
|
655
|
+
serverManagedExtension: options.serverManagedExtension ?? "x-mcp-server-managed",
|
|
656
|
+
argumentNames: options.argumentNames ?? "camelCase",
|
|
657
|
+
documents: options.documents
|
|
658
|
+
};
|
|
659
|
+
};
|
|
510
660
|
/**
|
|
511
661
|
* Translates one or more OpenAPI documents into REST-backed MCP tool
|
|
512
662
|
* definitions. Each translatable operation (has an `operationId`, a supported
|
|
@@ -520,10 +670,7 @@ var processPath = args => {
|
|
|
520
670
|
* ```
|
|
521
671
|
*/
|
|
522
672
|
var openApiToToolDefinitions = args => {
|
|
523
|
-
const options =
|
|
524
|
-
excludeExtension: args.options?.excludeExtension ?? "x-mcp-exclude",
|
|
525
|
-
serverManagedExtension: args.options?.serverManagedExtension ?? "x-mcp-server-managed"
|
|
526
|
-
};
|
|
673
|
+
const options = resolveOptions(args.options);
|
|
527
674
|
const specs = Array.isArray(args.spec) ? args.spec : [args.spec];
|
|
528
675
|
const tools = [];
|
|
529
676
|
for (const spec of specs) {
|
|
@@ -540,13 +687,34 @@ var openApiToToolDefinitions = args => {
|
|
|
540
687
|
|
|
541
688
|
//#endregion
|
|
542
689
|
//#region src/registerOpenApiTools.ts
|
|
690
|
+
/** The text the default `toText` answers when the API returned no body. */
|
|
691
|
+
var NO_CONTENT_TEXT = "Succeeded. The operation returned no content.";
|
|
543
692
|
var defaultToText = data => {
|
|
693
|
+
if (data === void 0 || data === "") return NO_CONTENT_TEXT;
|
|
544
694
|
return typeof data === "string" ? data : JSON.stringify(data, null, 2);
|
|
545
695
|
};
|
|
546
696
|
/**
|
|
697
|
+
* Replaces whatever the model sent for server-managed parameters with the
|
|
698
|
+
* values `serverParameters` supplies, so the model can never set them.
|
|
699
|
+
*/
|
|
700
|
+
var applyServerParameters = async args => {
|
|
701
|
+
const managed = args.tool.serverManagedParameters;
|
|
702
|
+
if (managed.length === 0) return args.handlerArgs;
|
|
703
|
+
const result = {
|
|
704
|
+
...args.handlerArgs
|
|
705
|
+
};
|
|
706
|
+
for (const param of managed) delete result[param.argName];
|
|
707
|
+
const values = args.serverParameters ? await args.serverParameters({
|
|
708
|
+
tool: args.tool,
|
|
709
|
+
headers: args.headers
|
|
710
|
+
}) : {};
|
|
711
|
+
for (const param of managed) if (values[param.name] !== void 0) result[param.argName] = values[param.name];
|
|
712
|
+
return result;
|
|
713
|
+
};
|
|
714
|
+
/**
|
|
547
715
|
* Derives MCP tools from OpenAPI document(s) and registers each on the given
|
|
548
|
-
* MCP server. Every tool's handler resolves the incoming
|
|
549
|
-
*
|
|
716
|
+
* MCP server. Every tool's handler resolves the incoming args into a concrete
|
|
717
|
+
* HTTP request and delegates execution to `callApi`.
|
|
550
718
|
*
|
|
551
719
|
* @returns The list of {@link ToolDefinition} that were registered.
|
|
552
720
|
*
|
|
@@ -560,10 +728,10 @@ var defaultToText = data => {
|
|
|
560
728
|
* registerOpenApiTools({
|
|
561
729
|
* server,
|
|
562
730
|
* spec: myOpenApiDocument,
|
|
563
|
-
* callApi: async ({ method, url, body }) => {
|
|
731
|
+
* callApi: async ({ method, url, body, headers }) => {
|
|
564
732
|
* const res = await fetch(`https://api.example.com${url}`, {
|
|
565
733
|
* method,
|
|
566
|
-
* headers: { 'Content-Type': 'application/json' },
|
|
734
|
+
* headers: { ...headers, 'Content-Type': 'application/json' },
|
|
567
735
|
* body: body ? JSON.stringify(body) : undefined,
|
|
568
736
|
* });
|
|
569
737
|
* return res.json();
|
|
@@ -581,7 +749,14 @@ var registerOpenApiTools = args => {
|
|
|
581
749
|
name: tool.name,
|
|
582
750
|
description: tool.description,
|
|
583
751
|
inputSchema: tool.inputSchema,
|
|
584
|
-
handler: async
|
|
752
|
+
handler: async rawArgs => {
|
|
753
|
+
const headers = (0, _ttoss_http_server_mcp.getApiHeaders)();
|
|
754
|
+
const handlerArgs = await applyServerParameters({
|
|
755
|
+
tool,
|
|
756
|
+
handlerArgs: rawArgs,
|
|
757
|
+
headers,
|
|
758
|
+
serverParameters: args.serverParameters
|
|
759
|
+
});
|
|
585
760
|
const url = tool.path(handlerArgs) + (tool.query ? tool.query(handlerArgs) : "");
|
|
586
761
|
return {
|
|
587
762
|
content: [{
|
|
@@ -590,7 +765,8 @@ var registerOpenApiTools = args => {
|
|
|
590
765
|
method: tool.method,
|
|
591
766
|
url,
|
|
592
767
|
body: tool.body ? tool.body(handlerArgs) : void 0,
|
|
593
|
-
tool
|
|
768
|
+
tool,
|
|
769
|
+
headers
|
|
594
770
|
}))
|
|
595
771
|
}]
|
|
596
772
|
};
|
|
@@ -602,6 +778,7 @@ var registerOpenApiTools = args => {
|
|
|
602
778
|
//#endregion
|
|
603
779
|
exports.DEFAULT_EXCLUDE_EXTENSION = DEFAULT_EXCLUDE_EXTENSION;
|
|
604
780
|
exports.DEFAULT_SERVER_MANAGED_EXTENSION = DEFAULT_SERVER_MANAGED_EXTENSION;
|
|
781
|
+
exports.NO_CONTENT_TEXT = NO_CONTENT_TEXT;
|
|
605
782
|
exports.buildBodyFn = buildBodyFn;
|
|
606
783
|
exports.buildInputSchema = buildInputSchema;
|
|
607
784
|
exports.buildPathFn = buildPathFn;
|