@kubb/plugin-mcp 5.0.0-alpha.9 → 5.0.0-beta.15

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.js CHANGED
@@ -1,122 +1,1017 @@
1
1
  import "./chunk--u3MIqq1.js";
2
- import { n as camelCase } from "./Server-KWLMg0Lm.js";
3
- import { n as mcpGenerator, t as serverGenerator } from "./generators-TtEOkDB1.js";
4
2
  import path from "node:path";
5
- import { createPlugin, getBarrelFiles, getMode } from "@kubb/core";
3
+ import { ast, defineGenerator, definePlugin, defineResolver } from "@kubb/core";
4
+ import { functionPrinter, pluginTsName } from "@kubb/plugin-ts";
5
+ import { Const, File, Function, jsxRendererSync } from "@kubb/renderer-jsx";
6
+ import { Fragment, jsx, jsxs } from "@kubb/renderer-jsx/jsx-runtime";
7
+ import { pluginZodName } from "@kubb/plugin-zod";
6
8
  import { pluginClientName } from "@kubb/plugin-client";
7
9
  import { source } from "@kubb/plugin-client/templates/clients/axios.source";
8
10
  import { source as source$1 } from "@kubb/plugin-client/templates/clients/fetch.source";
9
11
  import { source as source$2 } from "@kubb/plugin-client/templates/config.source";
10
- import { OperationGenerator, pluginOasName } from "@kubb/plugin-oas";
11
- import { pluginTsName } from "@kubb/plugin-ts";
12
- import { pluginZodName } from "@kubb/plugin-zod";
12
+ //#region ../../internals/utils/src/casing.ts
13
+ /**
14
+ * Shared implementation for camelCase and PascalCase conversion.
15
+ * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
16
+ * and capitalizes each word according to `pascal`.
17
+ *
18
+ * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
19
+ */
20
+ function toCamelOrPascal(text, pascal) {
21
+ 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) => {
22
+ if (word.length > 1 && word === word.toUpperCase()) return word;
23
+ if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
24
+ return word.charAt(0).toUpperCase() + word.slice(1);
25
+ }).join("").replace(/[^a-zA-Z0-9]/g, "");
26
+ }
27
+ /**
28
+ * Splits `text` on `.` and applies `transformPart` to each segment.
29
+ * The last segment receives `isLast = true`, all earlier segments receive `false`.
30
+ * Segments are joined with `/` to form a file path.
31
+ *
32
+ * Only splits on dots followed by a letter so that version numbers
33
+ * embedded in operationIds (e.g. `v2025.0`) are kept intact.
34
+ */
35
+ function applyToFileParts(text, transformPart) {
36
+ const parts = text.split(/\.(?=[a-zA-Z])/);
37
+ return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
38
+ }
39
+ /**
40
+ * Converts `text` to camelCase.
41
+ * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
42
+ *
43
+ * @example
44
+ * camelCase('hello-world') // 'helloWorld'
45
+ * camelCase('pet.petId', { isFile: true }) // 'pet/petId'
46
+ */
47
+ function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
48
+ if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
49
+ prefix,
50
+ suffix
51
+ } : {}));
52
+ return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
53
+ }
54
+ //#endregion
55
+ //#region ../../internals/utils/src/reserved.ts
56
+ /**
57
+ * JavaScript and Java reserved words.
58
+ * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
59
+ */
60
+ const reservedWords = new Set([
61
+ "abstract",
62
+ "arguments",
63
+ "boolean",
64
+ "break",
65
+ "byte",
66
+ "case",
67
+ "catch",
68
+ "char",
69
+ "class",
70
+ "const",
71
+ "continue",
72
+ "debugger",
73
+ "default",
74
+ "delete",
75
+ "do",
76
+ "double",
77
+ "else",
78
+ "enum",
79
+ "eval",
80
+ "export",
81
+ "extends",
82
+ "false",
83
+ "final",
84
+ "finally",
85
+ "float",
86
+ "for",
87
+ "function",
88
+ "goto",
89
+ "if",
90
+ "implements",
91
+ "import",
92
+ "in",
93
+ "instanceof",
94
+ "int",
95
+ "interface",
96
+ "let",
97
+ "long",
98
+ "native",
99
+ "new",
100
+ "null",
101
+ "package",
102
+ "private",
103
+ "protected",
104
+ "public",
105
+ "return",
106
+ "short",
107
+ "static",
108
+ "super",
109
+ "switch",
110
+ "synchronized",
111
+ "this",
112
+ "throw",
113
+ "throws",
114
+ "transient",
115
+ "true",
116
+ "try",
117
+ "typeof",
118
+ "var",
119
+ "void",
120
+ "volatile",
121
+ "while",
122
+ "with",
123
+ "yield",
124
+ "Array",
125
+ "Date",
126
+ "hasOwnProperty",
127
+ "Infinity",
128
+ "isFinite",
129
+ "isNaN",
130
+ "isPrototypeOf",
131
+ "length",
132
+ "Math",
133
+ "name",
134
+ "NaN",
135
+ "Number",
136
+ "Object",
137
+ "prototype",
138
+ "String",
139
+ "toString",
140
+ "undefined",
141
+ "valueOf"
142
+ ]);
143
+ /**
144
+ * Returns `true` when `name` is a syntactically valid JavaScript variable name.
145
+ *
146
+ * @example
147
+ * ```ts
148
+ * isValidVarName('status') // true
149
+ * isValidVarName('class') // false (reserved word)
150
+ * isValidVarName('42foo') // false (starts with digit)
151
+ * ```
152
+ */
153
+ function isValidVarName(name) {
154
+ if (!name || reservedWords.has(name)) return false;
155
+ return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
156
+ }
157
+ //#endregion
158
+ //#region ../../internals/utils/src/urlPath.ts
159
+ /**
160
+ * Parses and transforms an OpenAPI/Swagger path string into various URL formats.
161
+ *
162
+ * @example
163
+ * const p = new URLPath('/pet/{petId}')
164
+ * p.URL // '/pet/:petId'
165
+ * p.template // '`/pet/${petId}`'
166
+ */
167
+ var URLPath = class {
168
+ /**
169
+ * The raw OpenAPI/Swagger path string, e.g. `/pet/{petId}`.
170
+ */
171
+ path;
172
+ #options;
173
+ constructor(path, options = {}) {
174
+ this.path = path;
175
+ this.#options = options;
176
+ }
177
+ /** Converts the OpenAPI path to Express-style colon syntax, e.g. `/pet/{petId}` → `/pet/:petId`.
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * new URLPath('/pet/{petId}').URL // '/pet/:petId'
182
+ * ```
183
+ */
184
+ get URL() {
185
+ return this.toURLPath();
186
+ }
187
+ /** Returns `true` when `path` is a fully-qualified URL (e.g. starts with `https://`).
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * new URLPath('https://petstore.swagger.io/v2/pet').isURL // true
192
+ * new URLPath('/pet/{petId}').isURL // false
193
+ * ```
194
+ */
195
+ get isURL() {
196
+ try {
197
+ return !!new URL(this.path).href;
198
+ } catch {
199
+ return false;
200
+ }
201
+ }
202
+ /**
203
+ * Converts the OpenAPI path to a TypeScript template literal string.
204
+ *
205
+ * @example
206
+ * new URLPath('/pet/{petId}').template // '`/pet/${petId}`'
207
+ * new URLPath('/account/monetary-accountID').template // '`/account/${monetaryAccountId}`'
208
+ */
209
+ get template() {
210
+ return this.toTemplateString();
211
+ }
212
+ /** Returns the path and its extracted params as a structured `URLObject`, or as a stringified expression when `stringify` is set.
213
+ *
214
+ * @example
215
+ * ```ts
216
+ * new URLPath('/pet/{petId}').object
217
+ * // { url: '/pet/:petId', params: { petId: 'petId' } }
218
+ * ```
219
+ */
220
+ get object() {
221
+ return this.toObject();
222
+ }
223
+ /** Returns a map of path parameter names, or `undefined` when the path has no parameters.
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * new URLPath('/pet/{petId}').params // { petId: 'petId' }
228
+ * new URLPath('/pet').params // undefined
229
+ * ```
230
+ */
231
+ get params() {
232
+ return this.toParamsObject();
233
+ }
234
+ #transformParam(raw) {
235
+ const param = isValidVarName(raw) ? raw : camelCase(raw);
236
+ return this.#options.casing === "camelcase" ? camelCase(param) : param;
237
+ }
238
+ /**
239
+ * Iterates over every `{param}` token in `path`, calling `fn` with the raw token and transformed name.
240
+ */
241
+ #eachParam(fn) {
242
+ for (const match of this.path.matchAll(/\{([^}]+)\}/g)) {
243
+ const raw = match[1];
244
+ fn(raw, this.#transformParam(raw));
245
+ }
246
+ }
247
+ toObject({ type = "path", replacer, stringify } = {}) {
248
+ const object = {
249
+ url: type === "path" ? this.toURLPath() : this.toTemplateString({ replacer }),
250
+ params: this.toParamsObject()
251
+ };
252
+ if (stringify) {
253
+ if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
254
+ if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
255
+ return `{ url: '${object.url}' }`;
256
+ }
257
+ return object;
258
+ }
259
+ /**
260
+ * Converts the OpenAPI path to a TypeScript template literal string.
261
+ * An optional `replacer` can transform each extracted parameter name before interpolation.
262
+ *
263
+ * @example
264
+ * new URLPath('/pet/{petId}').toTemplateString() // '`/pet/${petId}`'
265
+ */
266
+ toTemplateString({ prefix = "", replacer } = {}) {
267
+ return `\`${prefix}${this.path.split(/\{([^}]+)\}/).map((part, i) => {
268
+ if (i % 2 === 0) return part;
269
+ const param = this.#transformParam(part);
270
+ return `\${${replacer ? replacer(param) : param}}`;
271
+ }).join("")}\``;
272
+ }
273
+ /**
274
+ * Extracts all `{param}` segments from the path and returns them as a key-value map.
275
+ * An optional `replacer` transforms each parameter name in both key and value positions.
276
+ * Returns `undefined` when no path parameters are found.
277
+ *
278
+ * @example
279
+ * ```ts
280
+ * new URLPath('/pet/{petId}/tag/{tagId}').toParamsObject()
281
+ * // { petId: 'petId', tagId: 'tagId' }
282
+ * ```
283
+ */
284
+ toParamsObject(replacer) {
285
+ const params = {};
286
+ this.#eachParam((_raw, param) => {
287
+ const key = replacer ? replacer(param) : param;
288
+ params[key] = key;
289
+ });
290
+ return Object.keys(params).length > 0 ? params : void 0;
291
+ }
292
+ /** Converts the OpenAPI path to Express-style colon syntax.
293
+ *
294
+ * @example
295
+ * ```ts
296
+ * new URLPath('/pet/{petId}').toURLPath() // '/pet/:petId'
297
+ * ```
298
+ */
299
+ toURLPath() {
300
+ return this.path.replace(/\{([^}]+)\}/g, ":$1");
301
+ }
302
+ };
303
+ //#endregion
304
+ //#region ../../internals/shared/src/operation.ts
305
+ function getOperationLink(node, link) {
306
+ if (!link) return;
307
+ if (typeof link === "function") return link(node);
308
+ if (link === "urlPath") return node.path ? `{@link ${new URLPath(node.path).URL}}` : void 0;
309
+ return `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}`;
310
+ }
311
+ function buildOperationComments(node, options = {}) {
312
+ const { link = "pathTemplate", linkPosition = "afterDeprecated", splitLines = false } = options;
313
+ const linkComment = getOperationLink(node, link);
314
+ const filteredComments = (linkPosition === "beforeDeprecated" ? [
315
+ node.description && `@description ${node.description}`,
316
+ node.summary && `@summary ${node.summary}`,
317
+ linkComment,
318
+ node.deprecated && "@deprecated"
319
+ ] : [
320
+ node.description && `@description ${node.description}`,
321
+ node.summary && `@summary ${node.summary}`,
322
+ node.deprecated && "@deprecated",
323
+ linkComment
324
+ ]).filter((comment) => Boolean(comment));
325
+ if (!splitLines) return filteredComments;
326
+ return filteredComments.flatMap((text) => text.split(/\r?\n/).map((line) => line.trim())).filter((comment) => Boolean(comment));
327
+ }
328
+ function getOperationParameters(node, options = {}) {
329
+ const params = ast.caseParams(node.parameters, options.paramsCasing);
330
+ return {
331
+ path: params.filter((param) => param.in === "path"),
332
+ query: params.filter((param) => param.in === "query"),
333
+ header: params.filter((param) => param.in === "header"),
334
+ cookie: params.filter((param) => param.in === "cookie")
335
+ };
336
+ }
337
+ function getStatusCodeNumber(statusCode) {
338
+ const code = Number(statusCode);
339
+ return Number.isNaN(code) ? void 0 : code;
340
+ }
341
+ function isSuccessStatusCode(statusCode) {
342
+ const code = getStatusCodeNumber(statusCode);
343
+ return code !== void 0 && code >= 200 && code < 300;
344
+ }
345
+ function isErrorStatusCode(statusCode) {
346
+ const code = getStatusCodeNumber(statusCode);
347
+ return code !== void 0 && code >= 400;
348
+ }
349
+ function resolveErrorNames(node, resolver) {
350
+ return node.responses.filter((response) => isErrorStatusCode(response.statusCode)).map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
351
+ }
352
+ function resolveStatusCodeNames(node, resolver) {
353
+ return node.responses.map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
354
+ }
355
+ const typeNamesByResolver = /* @__PURE__ */ new WeakMap();
356
+ function resolveOperationTypeNames(node, resolver, options = {}) {
357
+ const cacheKey = `${node.operationId}\0${options.paramsCasing ?? ""}\0${options.order ?? ""}\0${options.responseStatusNames ?? ""}\0${(options.exclude ?? []).join(",")}`;
358
+ let byResolver = typeNamesByResolver.get(resolver);
359
+ if (byResolver) {
360
+ const cached = byResolver.get(cacheKey);
361
+ if (cached) return cached;
362
+ } else {
363
+ byResolver = /* @__PURE__ */ new Map();
364
+ typeNamesByResolver.set(resolver, byResolver);
365
+ }
366
+ const { path, query, header } = getOperationParameters(node, { paramsCasing: options.paramsCasing });
367
+ const responseStatusNames = options.responseStatusNames === "error" ? resolveErrorNames(node, resolver) : options.responseStatusNames === false ? [] : resolveStatusCodeNames(node, resolver);
368
+ const exclude = new Set(options.exclude ?? []);
369
+ const paramNames = [
370
+ ...path.map((param) => resolver.resolvePathParamsName(node, param)),
371
+ ...query.map((param) => resolver.resolveQueryParamsName(node, param)),
372
+ ...header.map((param) => resolver.resolveHeaderParamsName(node, param))
373
+ ];
374
+ const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : void 0, resolver.resolveResponseName(node)];
375
+ const result = (options.order === "body-response-first" ? [
376
+ ...bodyAndResponseNames,
377
+ ...paramNames,
378
+ ...responseStatusNames
379
+ ] : [
380
+ ...paramNames,
381
+ ...bodyAndResponseNames,
382
+ ...responseStatusNames
383
+ ]).filter((name) => Boolean(name) && !exclude.has(name));
384
+ byResolver.set(cacheKey, result);
385
+ return result;
386
+ }
387
+ function findSuccessStatusCode(responses) {
388
+ for (const response of responses) if (isSuccessStatusCode(response.statusCode)) return response.statusCode;
389
+ }
390
+ //#endregion
391
+ //#region ../../internals/shared/src/params.ts
392
+ function buildParamsMapping(originalParams, mappedParams) {
393
+ const mapping = {};
394
+ let hasChanged = false;
395
+ originalParams.forEach((param, i) => {
396
+ const mappedName = mappedParams[i]?.name ?? param.name;
397
+ mapping[param.name] = mappedName;
398
+ if (param.name !== mappedName) hasChanged = true;
399
+ });
400
+ return hasChanged ? mapping : void 0;
401
+ }
402
+ function buildTransformedParamsMapping(params, transformName) {
403
+ if (!params.length) return;
404
+ return buildParamsMapping(params, params.map((param) => ({
405
+ ...param,
406
+ name: transformName(param.name)
407
+ })));
408
+ }
409
+ //#endregion
410
+ //#region src/components/McpHandler.tsx
411
+ /**
412
+ * Generate a remapping statement: `const mappedX = x ? { "orig": x.camel, ... } : undefined`
413
+ */
414
+ function buildRemappingCode(mapping, varName, sourceName) {
415
+ return `const ${varName} = ${sourceName} ? { ${Object.entries(mapping).map(([orig, camel]) => `"${orig}": ${sourceName}.${camel}`).join(", ")} } : undefined`;
416
+ }
417
+ const declarationPrinter = functionPrinter({ mode: "declaration" });
418
+ function McpHandler({ name, node, resolver, baseURL, dataReturnType, paramsCasing }) {
419
+ const urlPath = new URLPath(node.path);
420
+ const contentType = node.requestBody?.content?.[0]?.contentType;
421
+ const isFormData = contentType === "multipart/form-data";
422
+ const { query: queryParams, header: headerParams } = getOperationParameters(node, { paramsCasing });
423
+ const { path: originalPathParams, query: originalQueryParams, header: originalHeaderParams } = getOperationParameters(node);
424
+ const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : void 0;
425
+ const responseName = resolver.resolveResponseName(node);
426
+ const errorResponses = node.responses.filter((r) => Number(r.statusCode) >= 400).map((r) => resolver.resolveResponseStatusName(node, r.statusCode));
427
+ const generics = [
428
+ responseName,
429
+ `ResponseErrorConfig<${errorResponses.length > 0 ? errorResponses.join(" | ") : "Error"}>`,
430
+ requestName || "unknown"
431
+ ].filter(Boolean);
432
+ const paramsNode = ast.createOperationParams(node, {
433
+ paramsType: "object",
434
+ pathParamsType: "inline",
435
+ resolver,
436
+ paramsCasing
437
+ });
438
+ const baseParamsSignature = declarationPrinter.print(paramsNode) ?? "";
439
+ const paramsSignature = baseParamsSignature ? `${baseParamsSignature}, request: RequestHandlerExtra<ServerRequest, ServerNotification>` : "request: RequestHandlerExtra<ServerRequest, ServerNotification>";
440
+ const pathParamsMapping = paramsCasing ? buildTransformedParamsMapping(originalPathParams, camelCase) : void 0;
441
+ const queryParamsMapping = paramsCasing ? buildTransformedParamsMapping(originalQueryParams, camelCase) : void 0;
442
+ const headerParamsMapping = paramsCasing ? buildTransformedParamsMapping(originalHeaderParams, camelCase) : void 0;
443
+ const contentTypeHeader = contentType && contentType !== "application/json" && contentType !== "multipart/form-data" ? `'Content-Type': '${contentType}'` : void 0;
444
+ const headers = [headerParams.length ? headerParamsMapping ? "...mappedHeaders" : "...headers" : void 0, contentTypeHeader].filter(Boolean);
445
+ const fetchConfig = [];
446
+ fetchConfig.push(`method: ${JSON.stringify(node.method.toUpperCase())}`);
447
+ fetchConfig.push(`url: ${urlPath.template}`);
448
+ if (baseURL) fetchConfig.push(`baseURL: \`${baseURL}\``);
449
+ if (queryParams.length) fetchConfig.push(queryParamsMapping ? "params: mappedParams" : "params");
450
+ if (requestName) fetchConfig.push(`data: ${isFormData ? "formData as FormData" : "requestData"}`);
451
+ if (headers.length) fetchConfig.push(`headers: { ${headers.join(", ")} }`);
452
+ const callToolResult = dataReturnType === "data" ? `return {
453
+ content: [
454
+ {
455
+ type: 'text',
456
+ text: JSON.stringify(res.data)
457
+ }
458
+ ],
459
+ structuredContent: { data: res.data }
460
+ }` : `return {
461
+ content: [
462
+ {
463
+ type: 'text',
464
+ text: JSON.stringify(res)
465
+ }
466
+ ],
467
+ structuredContent: { data: res.data }
468
+ }`;
469
+ return /* @__PURE__ */ jsx(File.Source, {
470
+ name,
471
+ isExportable: true,
472
+ isIndexable: true,
473
+ children: /* @__PURE__ */ jsxs(Function, {
474
+ name,
475
+ async: true,
476
+ export: true,
477
+ params: paramsSignature,
478
+ JSDoc: { comments: buildOperationComments(node) },
479
+ returnType: "Promise<CallToolResult>",
480
+ children: [
481
+ "",
482
+ /* @__PURE__ */ jsx("br", {}),
483
+ /* @__PURE__ */ jsx("br", {}),
484
+ pathParamsMapping && Object.entries(pathParamsMapping).filter(([originalName, camelCaseName]) => originalName !== camelCaseName && isValidVarName(originalName)).map(([originalName, camelCaseName]) => `const ${originalName} = ${camelCaseName}`).join("\n"),
485
+ pathParamsMapping && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("br", {}), /* @__PURE__ */ jsx("br", {})] }),
486
+ queryParamsMapping && queryParams.length > 0 && /* @__PURE__ */ jsxs(Fragment, { children: [
487
+ buildRemappingCode(queryParamsMapping, "mappedParams", "params"),
488
+ /* @__PURE__ */ jsx("br", {}),
489
+ /* @__PURE__ */ jsx("br", {})
490
+ ] }),
491
+ headerParamsMapping && headerParams.length > 0 && /* @__PURE__ */ jsxs(Fragment, { children: [
492
+ buildRemappingCode(headerParamsMapping, "mappedHeaders", "headers"),
493
+ /* @__PURE__ */ jsx("br", {}),
494
+ /* @__PURE__ */ jsx("br", {})
495
+ ] }),
496
+ requestName && "const requestData = data",
497
+ /* @__PURE__ */ jsx("br", {}),
498
+ isFormData && requestName && "const formData = buildFormData(requestData)",
499
+ /* @__PURE__ */ jsx("br", {}),
500
+ `const res = await fetch<${generics.join(", ")}>({ ${fetchConfig.join(", ")} }, request)`,
501
+ /* @__PURE__ */ jsx("br", {}),
502
+ callToolResult
503
+ ]
504
+ })
505
+ });
506
+ }
507
+ //#endregion
508
+ //#region src/utils.ts
509
+ /**
510
+ * Render a group param value — compose individual schemas into `z.object({ ... })`,
511
+ * or use a schema name string directly.
512
+ */
513
+ function zodGroupExpr(entry) {
514
+ if (typeof entry === "string") return entry;
515
+ return `z.object({ ${entry.map((p) => `${JSON.stringify(p.name)}: ${p.schemaName}`).join(", ")} })`;
516
+ }
517
+ /**
518
+ * Convert a SchemaNode type to an inline Zod expression string.
519
+ * Used as fallback when no named zod schema is available for a path parameter.
520
+ */
521
+ function zodExprFromSchemaNode(schema) {
522
+ let expr;
523
+ switch (schema.type) {
524
+ case "enum": {
525
+ const rawValues = schema.namedEnumValues?.length ? schema.namedEnumValues.map((v) => v.value) : (schema.enumValues ?? []).filter((v) => v !== null);
526
+ if (rawValues.length > 0 && rawValues.every((v) => typeof v === "string")) expr = `z.enum([${rawValues.map((v) => JSON.stringify(v)).join(", ")}])`;
527
+ else if (rawValues.length > 0) {
528
+ const literals = rawValues.map((v) => `z.literal(${JSON.stringify(v)})`);
529
+ expr = literals.length === 1 ? literals[0] : `z.union([${literals.join(", ")}])`;
530
+ } else expr = "z.string()";
531
+ break;
532
+ }
533
+ case "integer":
534
+ expr = "z.coerce.number()";
535
+ break;
536
+ case "number":
537
+ expr = "z.number()";
538
+ break;
539
+ case "boolean":
540
+ expr = "z.boolean()";
541
+ break;
542
+ case "array":
543
+ expr = "z.array(z.unknown())";
544
+ break;
545
+ default: expr = "z.string()";
546
+ }
547
+ if (schema.nullable) expr = `${expr}.nullable()`;
548
+ return expr;
549
+ }
550
+ //#endregion
551
+ //#region src/components/Server.tsx
552
+ const keysPrinter = functionPrinter({ mode: "keys" });
553
+ function Server({ name, serverName, serverVersion, paramsCasing, operations }) {
554
+ return /* @__PURE__ */ jsxs(File.Source, {
555
+ name,
556
+ isExportable: true,
557
+ isIndexable: true,
558
+ children: [
559
+ /* @__PURE__ */ jsx(Const, {
560
+ name: "server",
561
+ export: true,
562
+ children: `
563
+ new McpServer({
564
+ name: '${serverName}',
565
+ version: '${serverVersion}',
566
+ })
567
+ `
568
+ }),
569
+ operations.map(({ tool, mcp, zod, node }) => {
570
+ const { path: pathParams } = getOperationParameters(node, { paramsCasing });
571
+ const pathEntries = [];
572
+ const otherEntries = [];
573
+ for (const p of pathParams) {
574
+ const zodParam = zod.pathParams.find((zp) => zp.name === p.name);
575
+ pathEntries.push({
576
+ key: p.name,
577
+ value: zodParam ? zodParam.schemaName : zodExprFromSchemaNode(p.schema)
578
+ });
579
+ }
580
+ if (zod.requestName) otherEntries.push({
581
+ key: "data",
582
+ value: zod.requestName
583
+ });
584
+ if (zod.queryParams) otherEntries.push({
585
+ key: "params",
586
+ value: zodGroupExpr(zod.queryParams)
587
+ });
588
+ if (zod.headerParams) otherEntries.push({
589
+ key: "headers",
590
+ value: zodGroupExpr(zod.headerParams)
591
+ });
592
+ otherEntries.sort((a, b) => a.key.localeCompare(b.key));
593
+ const entries = [...pathEntries, ...otherEntries];
594
+ const paramsNode = entries.length ? ast.createFunctionParameters({ params: [ast.createParameterGroup({ properties: entries.map((e) => ast.createFunctionParameter({
595
+ name: e.key,
596
+ optional: false
597
+ })) })] }) : void 0;
598
+ const destructured = paramsNode ? keysPrinter.print(paramsNode) ?? "" : "";
599
+ const inputSchema = entries.length ? `{ ${entries.map((e) => `${e.key}: ${e.value}`).join(", ")} }` : void 0;
600
+ const outputSchema = zod.responseName;
601
+ const config = [
602
+ tool.title ? `title: ${JSON.stringify(tool.title)}` : null,
603
+ `description: ${JSON.stringify(tool.description)}`,
604
+ outputSchema ? `outputSchema: { data: ${outputSchema} }` : null
605
+ ].filter(Boolean).join(",\n ");
606
+ if (inputSchema) return `
607
+ server.registerTool(${JSON.stringify(tool.name)}, {
608
+ ${config},
609
+ inputSchema: ${inputSchema},
610
+ }, async (${destructured}, request) => {
611
+ return ${mcp.name}(${destructured}, request)
612
+ })
613
+ `;
614
+ return `
615
+ server.registerTool(${JSON.stringify(tool.name)}, {
616
+ ${config},
617
+ }, async (request) => {
618
+ return ${mcp.name}(request)
619
+ })
620
+ `;
621
+ }).filter(Boolean),
622
+ /* @__PURE__ */ jsx(Function, {
623
+ name: "startServer",
624
+ async: true,
625
+ export: true,
626
+ children: `try {
627
+ const transport = new StdioServerTransport()
628
+ await server.connect(transport)
629
+
630
+ } catch (error) {
631
+ console.error('Failed to start server:', error)
632
+ process.exit(1)
633
+ }`
634
+ })
635
+ ]
636
+ });
637
+ }
638
+ //#endregion
639
+ //#region src/generators/mcpGenerator.tsx
640
+ const mcpGenerator = defineGenerator({
641
+ name: "mcp",
642
+ renderer: jsxRendererSync,
643
+ operation(node, ctx) {
644
+ const { resolver, driver, root } = ctx;
645
+ const { output, client, paramsCasing, group } = ctx.options;
646
+ const pluginTs = driver.getPlugin(pluginTsName);
647
+ if (!pluginTs) return null;
648
+ const tsResolver = driver.getResolver(pluginTsName);
649
+ const importedTypeNames = resolveOperationTypeNames(node, tsResolver, {
650
+ paramsCasing,
651
+ responseStatusNames: "error"
652
+ });
653
+ const meta = {
654
+ name: resolver.resolveHandlerName(node),
655
+ file: resolver.resolveFile({
656
+ name: node.operationId,
657
+ extname: ".ts",
658
+ tag: node.tags[0] ?? "default",
659
+ path: node.path
660
+ }, {
661
+ root,
662
+ output,
663
+ group
664
+ }),
665
+ fileTs: tsResolver.resolveFile({
666
+ name: node.operationId,
667
+ extname: ".ts",
668
+ tag: node.tags[0] ?? "default",
669
+ path: node.path
670
+ }, {
671
+ root,
672
+ output: pluginTs.options?.output ?? output,
673
+ group: pluginTs.options?.group
674
+ })
675
+ };
676
+ return /* @__PURE__ */ jsxs(File, {
677
+ baseName: meta.file.baseName,
678
+ path: meta.file.path,
679
+ meta: meta.file.meta,
680
+ children: [
681
+ meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ jsx(File.Import, {
682
+ name: Array.from(new Set(importedTypeNames)).sort(),
683
+ root: meta.file.path,
684
+ path: meta.fileTs.path,
685
+ isTypeOnly: true
686
+ }),
687
+ /* @__PURE__ */ jsx(File.Import, {
688
+ name: [
689
+ "CallToolResult",
690
+ "ServerNotification",
691
+ "ServerRequest"
692
+ ],
693
+ path: "@modelcontextprotocol/sdk/types",
694
+ isTypeOnly: true
695
+ }),
696
+ /* @__PURE__ */ jsx(File.Import, {
697
+ name: ["RequestHandlerExtra"],
698
+ path: "@modelcontextprotocol/sdk/shared/protocol",
699
+ isTypeOnly: true
700
+ }),
701
+ /* @__PURE__ */ jsx(File.Import, {
702
+ name: ["buildFormData"],
703
+ root: meta.file.path,
704
+ path: path.resolve(root, ".kubb/config.ts")
705
+ }),
706
+ client.importPath ? /* @__PURE__ */ jsxs(Fragment, { children: [
707
+ /* @__PURE__ */ jsx(File.Import, {
708
+ name: [
709
+ "Client",
710
+ "RequestConfig",
711
+ "ResponseErrorConfig"
712
+ ],
713
+ path: client.importPath,
714
+ isTypeOnly: true
715
+ }),
716
+ /* @__PURE__ */ jsx(File.Import, {
717
+ name: "fetch",
718
+ path: client.importPath
719
+ }),
720
+ client.dataReturnType === "full" && /* @__PURE__ */ jsx(File.Import, {
721
+ name: ["ResponseConfig"],
722
+ path: client.importPath,
723
+ isTypeOnly: true
724
+ })
725
+ ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
726
+ /* @__PURE__ */ jsx(File.Import, {
727
+ name: [
728
+ "Client",
729
+ "RequestConfig",
730
+ "ResponseErrorConfig"
731
+ ],
732
+ root: meta.file.path,
733
+ path: path.resolve(root, ".kubb/fetch.ts"),
734
+ isTypeOnly: true
735
+ }),
736
+ /* @__PURE__ */ jsx(File.Import, {
737
+ name: ["fetch"],
738
+ root: meta.file.path,
739
+ path: path.resolve(root, ".kubb/fetch.ts")
740
+ }),
741
+ client.dataReturnType === "full" && /* @__PURE__ */ jsx(File.Import, {
742
+ name: ["ResponseConfig"],
743
+ root: meta.file.path,
744
+ path: path.resolve(root, ".kubb/fetch.ts"),
745
+ isTypeOnly: true
746
+ })
747
+ ] }),
748
+ /* @__PURE__ */ jsx(McpHandler, {
749
+ name: meta.name,
750
+ node,
751
+ resolver: tsResolver,
752
+ baseURL: client.baseURL,
753
+ dataReturnType: client.dataReturnType || "data",
754
+ paramsCasing
755
+ })
756
+ ]
757
+ });
758
+ }
759
+ });
760
+ //#endregion
761
+ //#region src/generators/serverGenerator.tsx
762
+ /**
763
+ * Default v5 server generator for `@kubb/plugin-mcp`.
764
+ *
765
+ * Uses individual zod schemas for each param (e.g. `createPetsPathUuidSchema`, `createPetsQueryOffsetSchema`)
766
+ * and `resolveResponseStatusName` for per-status response schemas.
767
+ * Query and header params are composed into `z.object({ ... })` from individual schemas.
768
+ */
769
+ const serverGenerator = defineGenerator({
770
+ name: "operations",
771
+ renderer: jsxRendererSync,
772
+ operations(nodes, ctx) {
773
+ const { config, resolver, plugin, driver, root, inputNode } = ctx;
774
+ const { output, paramsCasing, group } = ctx.options;
775
+ const pluginZod = driver.getPlugin(pluginZodName);
776
+ if (!pluginZod) return;
777
+ const zodResolver = driver.getResolver(pluginZodName);
778
+ const name = "server";
779
+ const serverFile = {
780
+ baseName: "server.ts",
781
+ path: path.resolve(root, output.path, "server.ts"),
782
+ meta: { pluginName: plugin.name }
783
+ };
784
+ const jsonFile = {
785
+ baseName: ".mcp.json",
786
+ path: path.resolve(root, output.path, ".mcp.json"),
787
+ meta: { pluginName: plugin.name }
788
+ };
789
+ const operationsMapped = nodes.map((node) => {
790
+ const { path: pathParams, query: queryParams, header: headerParams } = getOperationParameters(node, { paramsCasing });
791
+ const mcpFile = resolver.resolveFile({
792
+ name: node.operationId,
793
+ extname: ".ts",
794
+ tag: node.tags[0] ?? "default",
795
+ path: node.path
796
+ }, {
797
+ root,
798
+ output,
799
+ group
800
+ });
801
+ const zodFile = zodResolver.resolveFile({
802
+ name: node.operationId,
803
+ extname: ".ts",
804
+ tag: node.tags[0] ?? "default",
805
+ path: node.path
806
+ }, {
807
+ root,
808
+ output: pluginZod.options?.output ?? output,
809
+ group: pluginZod.options?.group
810
+ });
811
+ const requestName = node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName(node) : void 0;
812
+ const successStatus = findSuccessStatusCode(node.responses);
813
+ const responseName = successStatus ? zodResolver.resolveResponseStatusName(node, successStatus) : void 0;
814
+ const resolveParams = (params) => params.map((p) => ({
815
+ name: p.name,
816
+ schemaName: zodResolver.resolveParamName(node, p)
817
+ }));
818
+ return {
819
+ tool: {
820
+ name: node.operationId,
821
+ title: node.summary || void 0,
822
+ description: node.description || `Make a ${node.method.toUpperCase()} request to ${node.path}`
823
+ },
824
+ mcp: {
825
+ name: resolver.resolveHandlerName(node),
826
+ file: mcpFile
827
+ },
828
+ zod: {
829
+ pathParams: resolveParams(pathParams),
830
+ queryParams: queryParams.length ? resolveParams(queryParams) : void 0,
831
+ headerParams: headerParams.length ? resolveParams(headerParams) : void 0,
832
+ requestName,
833
+ responseName,
834
+ file: zodFile
835
+ },
836
+ node
837
+ };
838
+ });
839
+ const imports = operationsMapped.flatMap(({ mcp, zod }) => {
840
+ const zodNames = [
841
+ ...zod.pathParams.map((p) => p.schemaName),
842
+ ...(zod.queryParams ?? []).map((p) => p.schemaName),
843
+ ...(zod.headerParams ?? []).map((p) => p.schemaName),
844
+ zod.requestName,
845
+ zod.responseName
846
+ ].filter((name) => Boolean(name));
847
+ const uniqueNames = [...new Set(zodNames)].sort();
848
+ return [/* @__PURE__ */ jsx(File.Import, {
849
+ name: [mcp.name],
850
+ root: serverFile.path,
851
+ path: mcp.file.path
852
+ }, mcp.name), uniqueNames.length > 0 && /* @__PURE__ */ jsx(File.Import, {
853
+ name: uniqueNames,
854
+ root: serverFile.path,
855
+ path: zod.file.path
856
+ }, `zod-${mcp.name}`)].filter(Boolean);
857
+ });
858
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(File, {
859
+ baseName: serverFile.baseName,
860
+ path: serverFile.path,
861
+ meta: serverFile.meta,
862
+ banner: resolver.resolveBanner(inputNode, {
863
+ output,
864
+ config
865
+ }),
866
+ footer: resolver.resolveFooter(inputNode, {
867
+ output,
868
+ config
869
+ }),
870
+ children: [
871
+ /* @__PURE__ */ jsx(File.Import, {
872
+ name: ["McpServer"],
873
+ path: "@modelcontextprotocol/sdk/server/mcp"
874
+ }),
875
+ /* @__PURE__ */ jsx(File.Import, {
876
+ name: ["z"],
877
+ path: "zod"
878
+ }),
879
+ /* @__PURE__ */ jsx(File.Import, {
880
+ name: ["StdioServerTransport"],
881
+ path: "@modelcontextprotocol/sdk/server/stdio"
882
+ }),
883
+ imports,
884
+ /* @__PURE__ */ jsx(Server, {
885
+ name,
886
+ serverName: inputNode.meta?.title ?? "server",
887
+ serverVersion: inputNode.meta?.version ?? "0.0.0",
888
+ paramsCasing,
889
+ operations: operationsMapped
890
+ })
891
+ ]
892
+ }), /* @__PURE__ */ jsx(File, {
893
+ baseName: jsonFile.baseName,
894
+ path: jsonFile.path,
895
+ meta: jsonFile.meta,
896
+ children: /* @__PURE__ */ jsx(File.Source, {
897
+ name,
898
+ children: `
899
+ {
900
+ "mcpServers": {
901
+ "${inputNode.meta?.title || "server"}": {
902
+ "type": "stdio",
903
+ "command": "npx",
904
+ "args": ["tsx", "${path.relative(path.dirname(jsonFile.path), serverFile.path)}"]
905
+ }
906
+ }
907
+ }
908
+ `
909
+ })
910
+ })] });
911
+ }
912
+ });
913
+ //#endregion
914
+ //#region src/resolvers/resolverMcp.ts
915
+ /**
916
+ * Naming convention resolver for MCP plugin.
917
+ *
918
+ * Provides default naming helpers using camelCase with a `handler` suffix for functions.
919
+ *
920
+ * @example
921
+ * `resolverMcp.default('addPet', 'function') // → 'addPetHandler'`
922
+ */
923
+ const resolverMcp = defineResolver(() => ({
924
+ name: "default",
925
+ pluginName: "plugin-mcp",
926
+ default(name, type) {
927
+ if (type === "file") return camelCase(name, { isFile: true });
928
+ return camelCase(name, { suffix: "handler" });
929
+ },
930
+ resolveName(name) {
931
+ return this.default(name, "function");
932
+ },
933
+ resolvePathName(name, type) {
934
+ return this.default(name, type);
935
+ },
936
+ resolveHandlerName(node) {
937
+ return this.resolveName(node.operationId);
938
+ }
939
+ }));
940
+ //#endregion
13
941
  //#region src/plugin.ts
14
942
  const pluginMcpName = "plugin-mcp";
15
- const pluginMcp = createPlugin((options) => {
943
+ const pluginMcp = definePlugin((options) => {
16
944
  const { output = {
17
945
  path: "mcp",
18
946
  barrelType: "named"
19
- }, group, exclude = [], include, override = [], transformers = {}, generators = [mcpGenerator, serverGenerator].filter(Boolean), contentType, paramsCasing, client } = options;
947
+ }, group, exclude = [], include, override = [], paramsCasing, client, resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = options;
20
948
  const clientName = client?.client ?? "axios";
21
949
  const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : void 0);
950
+ const groupConfig = group ? {
951
+ ...group,
952
+ name: group.name ? group.name : (ctx) => {
953
+ if (group.type === "path") return `${ctx.group.split("/")[1]}`;
954
+ return `${camelCase(ctx.group)}Requests`;
955
+ }
956
+ } : void 0;
22
957
  return {
23
958
  name: pluginMcpName,
24
- options: {
25
- output,
26
- group,
27
- paramsCasing,
28
- client: {
29
- client: clientName,
30
- clientType: client?.clientType ?? "function",
31
- importPath: clientImportPath,
32
- dataReturnType: client?.dataReturnType ?? "data",
33
- bundle: client?.bundle,
34
- baseURL: client?.baseURL,
35
- paramsCasing: client?.paramsCasing
36
- }
37
- },
38
- pre: [
39
- pluginOasName,
40
- pluginTsName,
41
- pluginZodName
42
- ].filter(Boolean),
43
- resolvePath(baseName, pathMode, options) {
44
- const root = path.resolve(this.config.root, this.config.output.path);
45
- if ((pathMode ?? getMode(path.resolve(root, output.path))) === "single")
46
- /**
47
- * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
48
- * Other plugins then need to call addOrAppend instead of just add from the fileManager class
49
- */
50
- return path.resolve(root, output.path);
51
- if (group && (options?.group?.path || options?.group?.tag)) {
52
- const groupName = group?.name ? group.name : (ctx) => {
53
- if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
54
- return `${camelCase(ctx.group)}Requests`;
55
- };
56
- return path.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
57
- }
58
- return path.resolve(root, output.path, baseName);
59
- },
60
- resolveName(name, type) {
61
- const resolvedName = camelCase(name, { isFile: type === "file" });
62
- if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
63
- return resolvedName;
64
- },
65
- async install() {
66
- const root = path.resolve(this.config.root, this.config.output.path);
67
- const mode = getMode(path.resolve(root, output.path));
68
- const oas = await this.getOas();
69
- const baseURL = await this.getBaseURL();
70
- if (baseURL) this.plugin.options.client.baseURL = baseURL;
71
- const hasClientPlugin = !!this.driver.getPluginByName(pluginClientName);
72
- if (this.plugin.options.client.bundle && !hasClientPlugin && !this.plugin.options.client.importPath) await this.addFile({
959
+ options,
960
+ dependencies: [pluginTsName, pluginZodName],
961
+ hooks: { "kubb:plugin:setup"(ctx) {
962
+ const resolver = userResolver ? {
963
+ ...resolverMcp,
964
+ ...userResolver
965
+ } : resolverMcp;
966
+ ctx.setOptions({
967
+ output,
968
+ exclude,
969
+ include,
970
+ override,
971
+ group: groupConfig,
972
+ paramsCasing,
973
+ client: {
974
+ client: clientName,
975
+ clientType: client?.clientType ?? "function",
976
+ importPath: clientImportPath,
977
+ dataReturnType: client?.dataReturnType ?? "data",
978
+ bundle: client?.bundle,
979
+ baseURL: client?.baseURL,
980
+ paramsCasing: client?.paramsCasing
981
+ },
982
+ resolver
983
+ });
984
+ ctx.setResolver(resolver);
985
+ if (userTransformer) ctx.setTransformer(userTransformer);
986
+ ctx.addGenerator(mcpGenerator);
987
+ ctx.addGenerator(serverGenerator);
988
+ for (const gen of userGenerators) ctx.addGenerator(gen);
989
+ const root = path.resolve(ctx.config.root, ctx.config.output.path);
990
+ const hasClientPlugin = ctx.config.plugins?.some((p) => p.name === pluginClientName);
991
+ if (client?.bundle && !hasClientPlugin && !clientImportPath) ctx.injectFile({
73
992
  baseName: "fetch.ts",
74
993
  path: path.resolve(root, ".kubb/fetch.ts"),
75
- sources: [{
994
+ sources: [ast.createSource({
76
995
  name: "fetch",
77
- value: this.plugin.options.client.client === "fetch" ? source$1 : source,
996
+ nodes: [ast.createText(clientName === "fetch" ? source$1 : source)],
78
997
  isExportable: true,
79
998
  isIndexable: true
80
- }],
81
- imports: [],
82
- exports: []
999
+ })]
83
1000
  });
84
- if (!hasClientPlugin) await this.addFile({
1001
+ if (!hasClientPlugin) ctx.injectFile({
85
1002
  baseName: "config.ts",
86
1003
  path: path.resolve(root, ".kubb/config.ts"),
87
- sources: [{
1004
+ sources: [ast.createSource({
88
1005
  name: "config",
89
- value: source$2,
1006
+ nodes: [ast.createText(source$2)],
90
1007
  isExportable: false,
91
1008
  isIndexable: false
92
- }],
93
- imports: [],
94
- exports: []
1009
+ })]
95
1010
  });
96
- const files = await new OperationGenerator(this.plugin.options, {
97
- fabric: this.fabric,
98
- oas,
99
- driver: this.driver,
100
- events: this.events,
101
- plugin: this.plugin,
102
- contentType,
103
- exclude,
104
- include,
105
- override,
106
- mode
107
- }).build(...generators);
108
- await this.upsertFile(...files);
109
- const barrelFiles = await getBarrelFiles(this.fabric.files, {
110
- type: output.barrelType ?? "named",
111
- root,
112
- output,
113
- meta: { pluginName: this.plugin.name }
114
- });
115
- await this.upsertFile(...barrelFiles);
116
- }
1011
+ } }
117
1012
  };
118
1013
  });
119
1014
  //#endregion
120
- export { pluginMcp, pluginMcpName };
1015
+ export { McpHandler, Server, pluginMcp as default, pluginMcp, mcpGenerator, pluginMcpName, resolverMcp, serverGenerator };
121
1016
 
122
1017
  //# sourceMappingURL=index.js.map