@kubb/plugin-mcp 5.3.2 → 5.3.4
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/dist/index.cjs +69 -76
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -16
- package/dist/index.d.ts +0 -16
- package/dist/index.js +68 -75
- package/dist/index.js.map +1 -1
- package/kubb-extension.json +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { Diagnostics, Resolver, Url, addGenerator, ast, createResolver, defineExtension, defineGenerator, setMacros, setResolver, useConfig } from "kubb/kit";
|
|
1
|
+
import { Diagnostics, Resolver, Url, addGenerator, ast, camelCase, createResolver, defineExtension, defineGenerator, setMacros, setResolver, useConfig } from "kubb/kit";
|
|
2
2
|
import path, { posix } from "node:path";
|
|
3
3
|
import { Const, File, Function, jsxRenderer } from "kubb/jsx";
|
|
4
4
|
import { Fragment, jsx, jsxs } from "kubb/jsx/jsx-runtime";
|
|
5
5
|
import { pluginZodName } from "@kubb/plugin-zod";
|
|
6
6
|
import { pluginTsName } from "@kubb/plugin-ts";
|
|
7
|
-
|
|
8
|
-
//#region ../../internals/shared/src/operation.ts
|
|
7
|
+
// ../../internals/shared/src/operation.ts
|
|
9
8
|
/**
|
|
10
9
|
* Builds the `ResolverFileParams` every operation generator passes to
|
|
11
10
|
* `resolver.file`: a file named `name`, tagged by the operation's first
|
|
@@ -127,8 +126,9 @@ function buildRequestParamsSignature(node, resolver, options = {}) {
|
|
|
127
126
|
"body",
|
|
128
127
|
"headers"
|
|
129
128
|
].filter((key) => groups[key]);
|
|
129
|
+
const optionsType = hasRequestContentTypeChoice(node) ? `Pick<${resolver.response.options(node)}, ${names.map((name) => `'${name}'`).join(" | ")}>` : resolver.response.options(node);
|
|
130
130
|
return {
|
|
131
|
-
signature: [names.length > 0 ? `{ ${names.join(", ")} }: ${
|
|
131
|
+
signature: [names.length > 0 ? `{ ${names.join(", ")} }: ${optionsType}${isOptional ? " = {}" : ""}` : null, isConfigurable ? `config: ${buildRequestConfigType(node)} = {}` : null].filter(Boolean).join(", "),
|
|
132
132
|
groups
|
|
133
133
|
};
|
|
134
134
|
}
|
|
@@ -176,6 +176,22 @@ function getOperationParameters(node) {
|
|
|
176
176
|
operationParameterGroupsByNode.set(node, groups);
|
|
177
177
|
return groups;
|
|
178
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Builds the combined `{ body, path, query, headers }` options object schema for an operation,
|
|
181
|
+
* referencing the already-resolved body and grouped param names. Shared by `@kubb/plugin-ts`'s
|
|
182
|
+
* `Options` type and `@kubb/plugin-zod`'s inferred options schema, so both printers emit the same
|
|
183
|
+
* shape from the same inputs. `primitive: 'object'` is a no-op for the TS printer and tells the Zod
|
|
184
|
+
* printer to emit `z.object(…)` rather than a record.
|
|
185
|
+
*/
|
|
186
|
+
function hasRequestContentTypeChoice(node) {
|
|
187
|
+
return (node.requestBody?.content ?? []).filter((entry) => entry.schema).length > 1;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Casts a call argument to the client's parameter type; a `'plain'` client is non-generic.
|
|
191
|
+
*/
|
|
192
|
+
function castToClientOptions(callArg, clientName, returnType = "full") {
|
|
193
|
+
return `${callArg} as Parameters<typeof ${clientName}${returnType === "plain" ? "" : "<true>"}>[0]`;
|
|
194
|
+
}
|
|
179
195
|
function getStatusCodeNumber(statusCode) {
|
|
180
196
|
const code = Number(statusCode);
|
|
181
197
|
return Number.isNaN(code) ? null : code;
|
|
@@ -193,46 +209,7 @@ function getOperationSuccessResponses(node) {
|
|
|
193
209
|
function getPrimarySuccessResponse(node) {
|
|
194
210
|
return getOperationSuccessResponses(node)[0] ?? null;
|
|
195
211
|
}
|
|
196
|
-
|
|
197
|
-
//#region ../../internals/utils/src/casing.ts
|
|
198
|
-
/**
|
|
199
|
-
* Shared implementation for camelCase and PascalCase conversion.
|
|
200
|
-
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
201
|
-
* and capitalizes each word according to `pascal`.
|
|
202
|
-
*
|
|
203
|
-
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
204
|
-
*/
|
|
205
|
-
function convert(text, pascal) {
|
|
206
|
-
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
|
|
207
|
-
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
208
|
-
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
209
|
-
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
210
|
-
}
|
|
211
|
-
const cache = /* @__PURE__ */ new Map();
|
|
212
|
-
const cacheLimit = 1e4;
|
|
213
|
-
function toCamelOrPascal(text, pascal) {
|
|
214
|
-
const key = `${pascal ? "P" : "c"}${text}`;
|
|
215
|
-
const cached = cache.get(key);
|
|
216
|
-
if (cached !== void 0) return cached;
|
|
217
|
-
const result = convert(text, pascal);
|
|
218
|
-
if (cache.size >= cacheLimit) cache.clear();
|
|
219
|
-
cache.set(key, result);
|
|
220
|
-
return result;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Converts `text` to camelCase.
|
|
224
|
-
*
|
|
225
|
-
* @example Word boundaries
|
|
226
|
-
* `camelCase('hello-world') // 'helloWorld'`
|
|
227
|
-
*
|
|
228
|
-
* @example With a prefix
|
|
229
|
-
* `camelCase('tag', { prefix: 'create' }) // 'createTag'`
|
|
230
|
-
*/
|
|
231
|
-
function camelCase(text, { prefix = "", suffix = "" } = {}) {
|
|
232
|
-
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
233
|
-
}
|
|
234
|
-
//#endregion
|
|
235
|
-
//#region ../../internals/utils/src/fs.ts
|
|
212
|
+
// ../../internals/utils/src/fs.ts
|
|
236
213
|
function toSlash(p) {
|
|
237
214
|
if (p.startsWith("\\\\?\\")) return p;
|
|
238
215
|
return p.replaceAll("\\", "/");
|
|
@@ -251,8 +228,26 @@ function getRelativePath(rootDir, filePath) {
|
|
|
251
228
|
const relativePath = posix.relative(toSlash(rootDir), toSlash(filePath));
|
|
252
229
|
return relativePath.startsWith("../") ? relativePath : `./${relativePath}`;
|
|
253
230
|
}
|
|
254
|
-
|
|
255
|
-
|
|
231
|
+
// ../../internals/shared/src/resolver.ts
|
|
232
|
+
/**
|
|
233
|
+
* Builds the allocation key for a declaration an operation owns. `scope` names which declaration
|
|
234
|
+
* it is, so two of them never share an allocation even when a custom resolver gives both the same
|
|
235
|
+
* name. Leave `scope` empty for the operation's own declaration.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* resolver.resolveName({ key: operationKey(node, 'response', 'status', 404), name })
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
function operationKey(node, ...scope) {
|
|
243
|
+
return [
|
|
244
|
+
"operation",
|
|
245
|
+
node.method,
|
|
246
|
+
node.path,
|
|
247
|
+
...scope.map(String)
|
|
248
|
+
];
|
|
249
|
+
}
|
|
250
|
+
// ../../internals/shared/src/group.ts
|
|
256
251
|
/**
|
|
257
252
|
* Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
|
|
258
253
|
* shared default naming so every plugin groups output consistently:
|
|
@@ -282,8 +277,7 @@ function createGroupConfig(group) {
|
|
|
282
277
|
name: group.name ? group.name : defaultName
|
|
283
278
|
};
|
|
284
279
|
}
|
|
285
|
-
|
|
286
|
-
//#region src/components/McpHandler.tsx
|
|
280
|
+
// src/components/McpHandler.tsx
|
|
287
281
|
const requestGroupOrder = [
|
|
288
282
|
"path",
|
|
289
283
|
"query",
|
|
@@ -298,6 +292,7 @@ function McpHandler({ name, clientName, node, resolver, returnType = "full" }) {
|
|
|
298
292
|
"signal: ctx.mcpReq.signal",
|
|
299
293
|
"throwOnError: true"
|
|
300
294
|
].join(", ")} }`;
|
|
295
|
+
const callArg = hasRequestContentTypeChoice(node) ? castToClientOptions(callConfig, clientName, returnType) : callConfig;
|
|
301
296
|
const body = returnType === "data" ? "res" : "res.data";
|
|
302
297
|
const callToolResult = `return {
|
|
303
298
|
content: [
|
|
@@ -320,15 +315,14 @@ function McpHandler({ name, clientName, node, resolver, returnType = "full" }) {
|
|
|
320
315
|
JSDoc: { comments: buildOperationComments(node) },
|
|
321
316
|
returnType: "CallToolResult",
|
|
322
317
|
children: [
|
|
323
|
-
`const res = await ${clientName}(${
|
|
318
|
+
`const res = await ${clientName}(${callArg})`,
|
|
324
319
|
/* @__PURE__ */ jsx("br", {}),
|
|
325
320
|
callToolResult
|
|
326
321
|
]
|
|
327
322
|
})
|
|
328
323
|
});
|
|
329
324
|
}
|
|
330
|
-
|
|
331
|
-
//#region src/components/Server.tsx
|
|
325
|
+
// src/components/Server.tsx
|
|
332
326
|
/**
|
|
333
327
|
* Composes individual param schemas into `z.object({ ... })`.
|
|
334
328
|
*/
|
|
@@ -417,8 +411,7 @@ return server`
|
|
|
417
411
|
})
|
|
418
412
|
] });
|
|
419
413
|
}
|
|
420
|
-
|
|
421
|
-
//#region ../../internals/client/src/resolveOperationTypes.ts
|
|
414
|
+
// ../../internals/client/src/resolveOperationTypes.ts
|
|
422
415
|
/**
|
|
423
416
|
* A `plugin-ts` shaped resolver whose operation type names point at the types `pluginZod({ inferred })`
|
|
424
417
|
* exports next to each schema, so code written against `plugin-ts` names picks up the zod types.
|
|
@@ -428,6 +421,7 @@ function toZodTypeResolver(resolver) {
|
|
|
428
421
|
return {
|
|
429
422
|
...resolver,
|
|
430
423
|
file: resolver.file.bind(resolver),
|
|
424
|
+
memo: (key, compute) => resolver.memo(`zodTypes:${key}`, compute),
|
|
431
425
|
param: {
|
|
432
426
|
name: (node, p) => type(resolver.param.name(node, p)),
|
|
433
427
|
path: (node, p) => type(resolver.param.path(node, p)),
|
|
@@ -484,8 +478,7 @@ function resolveOperationTypes(driver, pluginName) {
|
|
|
484
478
|
group: pluginZod.options.group ?? void 0
|
|
485
479
|
};
|
|
486
480
|
}
|
|
487
|
-
|
|
488
|
-
//#region ../../internals/client/src/resolveClient.ts
|
|
481
|
+
// ../../internals/client/src/resolveClient.ts
|
|
489
482
|
/**
|
|
490
483
|
* Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
|
|
491
484
|
* operation, shared so the selection rules and diagnostics stay in one place.
|
|
@@ -595,8 +588,7 @@ function resolveContractClient(options) {
|
|
|
595
588
|
}
|
|
596
589
|
return resolved;
|
|
597
590
|
}
|
|
598
|
-
|
|
599
|
-
//#region ../../internals/client/src/resolveClientOperation.ts
|
|
591
|
+
// ../../internals/client/src/resolveClientOperation.ts
|
|
600
592
|
function isRelativeImport(importPath) {
|
|
601
593
|
return importPath.startsWith(".");
|
|
602
594
|
}
|
|
@@ -643,8 +635,12 @@ function resolveClientOperation(options) {
|
|
|
643
635
|
group: operationOptions.group ?? void 0
|
|
644
636
|
});
|
|
645
637
|
const { importPath } = operationOptions;
|
|
638
|
+
const name = resolver.name(node.operationId);
|
|
646
639
|
return {
|
|
647
|
-
name: resolver.
|
|
640
|
+
name: resolver.resolveName({
|
|
641
|
+
key: operationKey(node),
|
|
642
|
+
name
|
|
643
|
+
}),
|
|
648
644
|
path: file.path,
|
|
649
645
|
clientPath: importPath === void 0 ? path.resolve(root, ".kubb/client.ts") : resolveImportPath({
|
|
650
646
|
importPath,
|
|
@@ -655,8 +651,7 @@ function resolveClientOperation(options) {
|
|
|
655
651
|
};
|
|
656
652
|
});
|
|
657
653
|
}
|
|
658
|
-
|
|
659
|
-
//#region src/generators/mcpGenerator.tsx
|
|
654
|
+
// src/generators/mcpGenerator.tsx
|
|
660
655
|
/**
|
|
661
656
|
* Built-in operation generator for `@kubb/plugin-mcp`. Emits one MCP tool
|
|
662
657
|
* handler per OpenAPI operation. The handler takes the grouped `{ path, query,
|
|
@@ -747,8 +742,7 @@ const mcpGenerator = defineGenerator({
|
|
|
747
742
|
});
|
|
748
743
|
}
|
|
749
744
|
});
|
|
750
|
-
|
|
751
|
-
//#region src/generators/serverGenerator.tsx
|
|
745
|
+
// src/generators/serverGenerator.tsx
|
|
752
746
|
/**
|
|
753
747
|
* Default v5 server generator for `@kubb/plugin-mcp`.
|
|
754
748
|
*
|
|
@@ -921,17 +915,14 @@ const serverGenerator = defineGenerator({
|
|
|
921
915
|
] });
|
|
922
916
|
}
|
|
923
917
|
});
|
|
924
|
-
|
|
925
|
-
//#region kubb-extension.json
|
|
918
|
+
// kubb-extension.json
|
|
926
919
|
var compatibility = {
|
|
927
|
-
"kubb": ">=5.
|
|
920
|
+
"kubb": ">=5.14.6",
|
|
928
921
|
"node": ">=22.22"
|
|
929
922
|
};
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
//#endregion
|
|
934
|
-
//#region src/resolvers/resolverMcp.ts
|
|
923
|
+
// package.json
|
|
924
|
+
var version = "5.3.4";
|
|
925
|
+
// src/resolvers/resolverMcp.ts
|
|
935
926
|
/**
|
|
936
927
|
* Default resolver used by `@kubb/plugin-mcp`. Decides the names and file
|
|
937
928
|
* paths for every generated MCP tool handler. Function names get a `Handler`
|
|
@@ -947,14 +938,17 @@ var version = "5.3.2";
|
|
|
947
938
|
const resolverMcp = createResolver({
|
|
948
939
|
pluginName: "plugin-mcp",
|
|
949
940
|
name(name) {
|
|
950
|
-
return camelCase(name, { suffix: "handler" });
|
|
941
|
+
return this.memo(`name:${name}`, () => camelCase(name, { suffix: "handler" }));
|
|
951
942
|
},
|
|
952
943
|
handler: { name(node) {
|
|
953
|
-
|
|
944
|
+
const name = this.name(node.operationId);
|
|
945
|
+
return this.resolveName({
|
|
946
|
+
key: operationKey(node, "handler", "name"),
|
|
947
|
+
name
|
|
948
|
+
});
|
|
954
949
|
} }
|
|
955
950
|
});
|
|
956
|
-
|
|
957
|
-
//#region src/plugin.ts
|
|
951
|
+
// src/plugin.ts
|
|
958
952
|
/**
|
|
959
953
|
* Canonical plugin name for `@kubb/plugin-mcp`. Used for driver lookups and
|
|
960
954
|
* cross-plugin dependency references.
|
|
@@ -1023,7 +1017,6 @@ const pluginMcp = defineExtension({
|
|
|
1023
1017
|
};
|
|
1024
1018
|
}
|
|
1025
1019
|
});
|
|
1026
|
-
//#endregion
|
|
1027
1020
|
export { McpHandler, Server, pluginMcp as default, pluginMcp, mcpGenerator, pluginMcpName, resolverMcp, serverGenerator };
|
|
1028
1021
|
|
|
1029
1022
|
//# sourceMappingURL=index.js.map
|