@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 CHANGED
@@ -2,7 +2,7 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
- //#region \0rolldown/runtime.js
5
+ // \0rolldown/runtime.js
6
6
  var __create = Object.create;
7
7
  var __defProp = Object.defineProperty;
8
8
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -23,7 +23,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
23
  value: mod,
24
24
  enumerable: true
25
25
  }) : target, mod));
26
- //#endregion
27
26
  let kubb_kit = require("kubb/kit");
28
27
  let node_path = require("node:path");
29
28
  node_path = __toESM(node_path, 1);
@@ -31,7 +30,7 @@ let kubb_jsx = require("kubb/jsx");
31
30
  let kubb_jsx_jsx_runtime = require("kubb/jsx/jsx-runtime");
32
31
  let _kubb_plugin_zod = require("@kubb/plugin-zod");
33
32
  let _kubb_plugin_ts = require("@kubb/plugin-ts");
34
- //#region ../../internals/shared/src/operation.ts
33
+ // ../../internals/shared/src/operation.ts
35
34
  /**
36
35
  * Builds the `ResolverFileParams` every operation generator passes to
37
36
  * `resolver.file`: a file named `name`, tagged by the operation's first
@@ -153,8 +152,9 @@ function buildRequestParamsSignature(node, resolver, options = {}) {
153
152
  "body",
154
153
  "headers"
155
154
  ].filter((key) => groups[key]);
155
+ const optionsType = hasRequestContentTypeChoice(node) ? `Pick<${resolver.response.options(node)}, ${names.map((name) => `'${name}'`).join(" | ")}>` : resolver.response.options(node);
156
156
  return {
157
- signature: [names.length > 0 ? `{ ${names.join(", ")} }: ${resolver.response.options(node)}${isOptional ? " = {}" : ""}` : null, isConfigurable ? `config: ${buildRequestConfigType(node)} = {}` : null].filter(Boolean).join(", "),
157
+ signature: [names.length > 0 ? `{ ${names.join(", ")} }: ${optionsType}${isOptional ? " = {}" : ""}` : null, isConfigurable ? `config: ${buildRequestConfigType(node)} = {}` : null].filter(Boolean).join(", "),
158
158
  groups
159
159
  };
160
160
  }
@@ -202,6 +202,22 @@ function getOperationParameters(node) {
202
202
  operationParameterGroupsByNode.set(node, groups);
203
203
  return groups;
204
204
  }
205
+ /**
206
+ * Builds the combined `{ body, path, query, headers }` options object schema for an operation,
207
+ * referencing the already-resolved body and grouped param names. Shared by `@kubb/plugin-ts`'s
208
+ * `Options` type and `@kubb/plugin-zod`'s inferred options schema, so both printers emit the same
209
+ * shape from the same inputs. `primitive: 'object'` is a no-op for the TS printer and tells the Zod
210
+ * printer to emit `z.object(…)` rather than a record.
211
+ */
212
+ function hasRequestContentTypeChoice(node) {
213
+ return (node.requestBody?.content ?? []).filter((entry) => entry.schema).length > 1;
214
+ }
215
+ /**
216
+ * Casts a call argument to the client's parameter type; a `'plain'` client is non-generic.
217
+ */
218
+ function castToClientOptions(callArg, clientName, returnType = "full") {
219
+ return `${callArg} as Parameters<typeof ${clientName}${returnType === "plain" ? "" : "<true>"}>[0]`;
220
+ }
205
221
  function getStatusCodeNumber(statusCode) {
206
222
  const code = Number(statusCode);
207
223
  return Number.isNaN(code) ? null : code;
@@ -219,46 +235,7 @@ function getOperationSuccessResponses(node) {
219
235
  function getPrimarySuccessResponse(node) {
220
236
  return getOperationSuccessResponses(node)[0] ?? null;
221
237
  }
222
- //#endregion
223
- //#region ../../internals/utils/src/casing.ts
224
- /**
225
- * Shared implementation for camelCase and PascalCase conversion.
226
- * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
227
- * and capitalizes each word according to `pascal`.
228
- *
229
- * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
230
- */
231
- function convert(text, pascal) {
232
- 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) => {
233
- if (word.length > 1 && word === word.toUpperCase()) return word;
234
- return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
235
- }).join("").replace(/[^a-zA-Z0-9]/g, "");
236
- }
237
- const cache = /* @__PURE__ */ new Map();
238
- const cacheLimit = 1e4;
239
- function toCamelOrPascal(text, pascal) {
240
- const key = `${pascal ? "P" : "c"}${text}`;
241
- const cached = cache.get(key);
242
- if (cached !== void 0) return cached;
243
- const result = convert(text, pascal);
244
- if (cache.size >= cacheLimit) cache.clear();
245
- cache.set(key, result);
246
- return result;
247
- }
248
- /**
249
- * Converts `text` to camelCase.
250
- *
251
- * @example Word boundaries
252
- * `camelCase('hello-world') // 'helloWorld'`
253
- *
254
- * @example With a prefix
255
- * `camelCase('tag', { prefix: 'create' }) // 'createTag'`
256
- */
257
- function camelCase(text, { prefix = "", suffix = "" } = {}) {
258
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
259
- }
260
- //#endregion
261
- //#region ../../internals/utils/src/fs.ts
238
+ // ../../internals/utils/src/fs.ts
262
239
  function toSlash(p) {
263
240
  if (p.startsWith("\\\\?\\")) return p;
264
241
  return p.replaceAll("\\", "/");
@@ -277,8 +254,26 @@ function getRelativePath(rootDir, filePath) {
277
254
  const relativePath = node_path.posix.relative(toSlash(rootDir), toSlash(filePath));
278
255
  return relativePath.startsWith("../") ? relativePath : `./${relativePath}`;
279
256
  }
280
- //#endregion
281
- //#region ../../internals/shared/src/group.ts
257
+ // ../../internals/shared/src/resolver.ts
258
+ /**
259
+ * Builds the allocation key for a declaration an operation owns. `scope` names which declaration
260
+ * it is, so two of them never share an allocation even when a custom resolver gives both the same
261
+ * name. Leave `scope` empty for the operation's own declaration.
262
+ *
263
+ * @example
264
+ * ```ts
265
+ * resolver.resolveName({ key: operationKey(node, 'response', 'status', 404), name })
266
+ * ```
267
+ */
268
+ function operationKey(node, ...scope) {
269
+ return [
270
+ "operation",
271
+ node.method,
272
+ node.path,
273
+ ...scope.map(String)
274
+ ];
275
+ }
276
+ // ../../internals/shared/src/group.ts
282
277
  /**
283
278
  * Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
284
279
  * shared default naming so every plugin groups output consistently:
@@ -301,15 +296,14 @@ function createGroupConfig(group) {
301
296
  if (!group) return null;
302
297
  const defaultName = (ctx) => {
303
298
  if (group.type === "path") return `${ctx.group.split("/")[1]}`;
304
- return camelCase(ctx.group);
299
+ return (0, kubb_kit.camelCase)(ctx.group);
305
300
  };
306
301
  return {
307
302
  ...group,
308
303
  name: group.name ? group.name : defaultName
309
304
  };
310
305
  }
311
- //#endregion
312
- //#region src/components/McpHandler.tsx
306
+ // src/components/McpHandler.tsx
313
307
  const requestGroupOrder = [
314
308
  "path",
315
309
  "query",
@@ -324,6 +318,7 @@ function McpHandler({ name, clientName, node, resolver, returnType = "full" }) {
324
318
  "signal: ctx.mcpReq.signal",
325
319
  "throwOnError: true"
326
320
  ].join(", ")} }`;
321
+ const callArg = hasRequestContentTypeChoice(node) ? castToClientOptions(callConfig, clientName, returnType) : callConfig;
327
322
  const body = returnType === "data" ? "res" : "res.data";
328
323
  const callToolResult = `return {
329
324
  content: [
@@ -346,15 +341,14 @@ function McpHandler({ name, clientName, node, resolver, returnType = "full" }) {
346
341
  JSDoc: { comments: buildOperationComments(node) },
347
342
  returnType: "CallToolResult",
348
343
  children: [
349
- `const res = await ${clientName}(${callConfig})`,
344
+ `const res = await ${clientName}(${callArg})`,
350
345
  /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)("br", {}),
351
346
  callToolResult
352
347
  ]
353
348
  })
354
349
  });
355
350
  }
356
- //#endregion
357
- //#region src/components/Server.tsx
351
+ // src/components/Server.tsx
358
352
  /**
359
353
  * Composes individual param schemas into `z.object({ ... })`.
360
354
  */
@@ -443,8 +437,7 @@ return server`
443
437
  })
444
438
  ] });
445
439
  }
446
- //#endregion
447
- //#region ../../internals/client/src/resolveOperationTypes.ts
440
+ // ../../internals/client/src/resolveOperationTypes.ts
448
441
  /**
449
442
  * A `plugin-ts` shaped resolver whose operation type names point at the types `pluginZod({ inferred })`
450
443
  * exports next to each schema, so code written against `plugin-ts` names picks up the zod types.
@@ -454,6 +447,7 @@ function toZodTypeResolver(resolver) {
454
447
  return {
455
448
  ...resolver,
456
449
  file: resolver.file.bind(resolver),
450
+ memo: (key, compute) => resolver.memo(`zodTypes:${key}`, compute),
457
451
  param: {
458
452
  name: (node, p) => type(resolver.param.name(node, p)),
459
453
  path: (node, p) => type(resolver.param.path(node, p)),
@@ -510,8 +504,7 @@ function resolveOperationTypes(driver, pluginName) {
510
504
  group: pluginZod.options.group ?? void 0
511
505
  };
512
506
  }
513
- //#endregion
514
- //#region ../../internals/client/src/resolveClient.ts
507
+ // ../../internals/client/src/resolveClient.ts
515
508
  /**
516
509
  * Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
517
510
  * operation, shared so the selection rules and diagnostics stay in one place.
@@ -621,8 +614,7 @@ function resolveContractClient(options) {
621
614
  }
622
615
  return resolved;
623
616
  }
624
- //#endregion
625
- //#region ../../internals/client/src/resolveClientOperation.ts
617
+ // ../../internals/client/src/resolveClientOperation.ts
626
618
  function isRelativeImport(importPath) {
627
619
  return importPath.startsWith(".");
628
620
  }
@@ -669,8 +661,12 @@ function resolveClientOperation(options) {
669
661
  group: operationOptions.group ?? void 0
670
662
  });
671
663
  const { importPath } = operationOptions;
664
+ const name = resolver.name(node.operationId);
672
665
  return {
673
- name: resolver.name(node.operationId),
666
+ name: resolver.resolveName({
667
+ key: operationKey(node),
668
+ name
669
+ }),
674
670
  path: file.path,
675
671
  clientPath: importPath === void 0 ? node_path.default.resolve(root, ".kubb/client.ts") : resolveImportPath({
676
672
  importPath,
@@ -681,8 +677,7 @@ function resolveClientOperation(options) {
681
677
  };
682
678
  });
683
679
  }
684
- //#endregion
685
- //#region src/generators/mcpGenerator.tsx
680
+ // src/generators/mcpGenerator.tsx
686
681
  /**
687
682
  * Built-in operation generator for `@kubb/plugin-mcp`. Emits one MCP tool
688
683
  * handler per OpenAPI operation. The handler takes the grouped `{ path, query,
@@ -773,8 +768,7 @@ const mcpGenerator = (0, kubb_kit.defineGenerator)({
773
768
  });
774
769
  }
775
770
  });
776
- //#endregion
777
- //#region src/generators/serverGenerator.tsx
771
+ // src/generators/serverGenerator.tsx
778
772
  /**
779
773
  * Default v5 server generator for `@kubb/plugin-mcp`.
780
774
  *
@@ -947,17 +941,14 @@ const serverGenerator = (0, kubb_kit.defineGenerator)({
947
941
  ] });
948
942
  }
949
943
  });
950
- //#endregion
951
- //#region kubb-extension.json
944
+ // kubb-extension.json
952
945
  var compatibility = {
953
- "kubb": ">=5.8.3",
946
+ "kubb": ">=5.14.6",
954
947
  "node": ">=22.22"
955
948
  };
956
- //#endregion
957
- //#region package.json
958
- var version = "5.3.2";
959
- //#endregion
960
- //#region src/resolvers/resolverMcp.ts
949
+ // package.json
950
+ var version = "5.3.4";
951
+ // src/resolvers/resolverMcp.ts
961
952
  /**
962
953
  * Default resolver used by `@kubb/plugin-mcp`. Decides the names and file
963
954
  * paths for every generated MCP tool handler. Function names get a `Handler`
@@ -973,14 +964,17 @@ var version = "5.3.2";
973
964
  const resolverMcp = (0, kubb_kit.createResolver)({
974
965
  pluginName: "plugin-mcp",
975
966
  name(name) {
976
- return camelCase(name, { suffix: "handler" });
967
+ return this.memo(`name:${name}`, () => (0, kubb_kit.camelCase)(name, { suffix: "handler" }));
977
968
  },
978
969
  handler: { name(node) {
979
- return this.name(node.operationId);
970
+ const name = this.name(node.operationId);
971
+ return this.resolveName({
972
+ key: operationKey(node, "handler", "name"),
973
+ name
974
+ });
980
975
  } }
981
976
  });
982
- //#endregion
983
- //#region src/plugin.ts
977
+ // src/plugin.ts
984
978
  /**
985
979
  * Canonical plugin name for `@kubb/plugin-mcp`. Used for driver lookups and
986
980
  * cross-plugin dependency references.
@@ -1049,7 +1043,6 @@ const pluginMcp = (0, kubb_kit.defineExtension)({
1049
1043
  };
1050
1044
  }
1051
1045
  });
1052
- //#endregion
1053
1046
  exports.McpHandler = McpHandler;
1054
1047
  exports.Server = Server;
1055
1048
  exports.default = pluginMcp;