@kubb/plugin-mcp 5.0.0-alpha.9 → 5.0.0-beta.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.js CHANGED
@@ -1,122 +1,955 @@
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, jsxRenderer } 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.getParams();
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.getParams()
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}').getParams()
281
+ * // { petId: 'petId', tagId: 'tagId' }
282
+ * ```
283
+ */
284
+ getParams(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 src/utils.ts
305
+ /**
306
+ * Find the first 2xx response status code from an operation's responses.
307
+ */
308
+ function findSuccessStatusCode(responses) {
309
+ for (const res of responses) {
310
+ const code = Number(res.statusCode);
311
+ if (code >= 200 && code < 300) return res.statusCode;
312
+ }
313
+ }
314
+ /**
315
+ * Render a group param value — compose individual schemas into `z.object({ ... })`,
316
+ * or use a schema name string directly.
317
+ */
318
+ function zodGroupExpr(entry) {
319
+ if (typeof entry === "string") return entry;
320
+ return `z.object({ ${entry.map((p) => `${JSON.stringify(p.name)}: ${p.schemaName}`).join(", ")} })`;
321
+ }
322
+ /**
323
+ * Build JSDoc comment lines from an OperationNode.
324
+ */
325
+ function getComments(node) {
326
+ return [
327
+ node.description && `@description ${node.description}`,
328
+ node.summary && `@summary ${node.summary}`,
329
+ node.deprecated && "@deprecated",
330
+ `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}`
331
+ ].filter((x) => Boolean(x));
332
+ }
333
+ /**
334
+ * Build a mapping of original param names → camelCase names.
335
+ * Returns `undefined` when no names actually change (no remapping needed).
336
+ */
337
+ function getParamsMapping(params) {
338
+ if (!params.length) return;
339
+ const mapping = {};
340
+ let hasDifference = false;
341
+ for (const p of params) {
342
+ const camelName = camelCase(p.name);
343
+ mapping[p.name] = camelName;
344
+ if (p.name !== camelName) hasDifference = true;
345
+ }
346
+ return hasDifference ? mapping : void 0;
347
+ }
348
+ /**
349
+ * Convert a SchemaNode type to an inline Zod expression string.
350
+ * Used as fallback when no named zod schema is available for a path parameter.
351
+ */
352
+ function zodExprFromSchemaNode(schema) {
353
+ let expr;
354
+ switch (schema.type) {
355
+ case "enum": {
356
+ const rawValues = schema.namedEnumValues?.length ? schema.namedEnumValues.map((v) => v.value) : (schema.enumValues ?? []).filter((v) => v !== null);
357
+ if (rawValues.length > 0 && rawValues.every((v) => typeof v === "string")) expr = `z.enum([${rawValues.map((v) => JSON.stringify(v)).join(", ")}])`;
358
+ else if (rawValues.length > 0) {
359
+ const literals = rawValues.map((v) => `z.literal(${JSON.stringify(v)})`);
360
+ expr = literals.length === 1 ? literals[0] : `z.union([${literals.join(", ")}])`;
361
+ } else expr = "z.string()";
362
+ break;
363
+ }
364
+ case "integer":
365
+ expr = "z.coerce.number()";
366
+ break;
367
+ case "number":
368
+ expr = "z.number()";
369
+ break;
370
+ case "boolean":
371
+ expr = "z.boolean()";
372
+ break;
373
+ case "array":
374
+ expr = "z.array(z.unknown())";
375
+ break;
376
+ default: expr = "z.string()";
377
+ }
378
+ if (schema.nullable) expr = `${expr}.nullable()`;
379
+ return expr;
380
+ }
381
+ //#endregion
382
+ //#region src/components/McpHandler.tsx
383
+ /**
384
+ * Generate a remapping statement: `const mappedX = x ? { "orig": x.camel, ... } : undefined`
385
+ */
386
+ function buildRemappingCode(mapping, varName, sourceName) {
387
+ return `const ${varName} = ${sourceName} ? { ${Object.entries(mapping).map(([orig, camel]) => `"${orig}": ${sourceName}.${camel}`).join(", ")} } : undefined`;
388
+ }
389
+ const declarationPrinter = functionPrinter({ mode: "declaration" });
390
+ function McpHandler({ name, node, resolver, baseURL, dataReturnType, paramsCasing }) {
391
+ const urlPath = new URLPath(node.path);
392
+ const contentType = node.requestBody?.content?.[0]?.contentType;
393
+ const isFormData = contentType === "multipart/form-data";
394
+ const casedParams = ast.caseParams(node.parameters, paramsCasing);
395
+ const queryParams = casedParams.filter((p) => p.in === "query");
396
+ const headerParams = casedParams.filter((p) => p.in === "header");
397
+ const originalPathParams = node.parameters.filter((p) => p.in === "path");
398
+ const originalQueryParams = node.parameters.filter((p) => p.in === "query");
399
+ const originalHeaderParams = node.parameters.filter((p) => p.in === "header");
400
+ const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : void 0;
401
+ const responseName = resolver.resolveResponseName(node);
402
+ const errorResponses = node.responses.filter((r) => Number(r.statusCode) >= 400).map((r) => resolver.resolveResponseStatusName(node, r.statusCode));
403
+ const generics = [
404
+ responseName,
405
+ `ResponseErrorConfig<${errorResponses.length > 0 ? errorResponses.join(" | ") : "Error"}>`,
406
+ requestName || "unknown"
407
+ ].filter(Boolean);
408
+ const paramsNode = ast.createOperationParams(node, {
409
+ paramsType: "object",
410
+ pathParamsType: "inline",
411
+ resolver,
412
+ paramsCasing
413
+ });
414
+ const baseParamsSignature = declarationPrinter.print(paramsNode) ?? "";
415
+ const paramsSignature = baseParamsSignature ? `${baseParamsSignature}, request: RequestHandlerExtra<ServerRequest, ServerNotification>` : "request: RequestHandlerExtra<ServerRequest, ServerNotification>";
416
+ const pathParamsMapping = paramsCasing ? getParamsMapping(originalPathParams) : void 0;
417
+ const queryParamsMapping = paramsCasing ? getParamsMapping(originalQueryParams) : void 0;
418
+ const headerParamsMapping = paramsCasing ? getParamsMapping(originalHeaderParams) : void 0;
419
+ const contentTypeHeader = contentType && contentType !== "application/json" && contentType !== "multipart/form-data" ? `'Content-Type': '${contentType}'` : void 0;
420
+ const headers = [headerParams.length ? headerParamsMapping ? "...mappedHeaders" : "...headers" : void 0, contentTypeHeader].filter(Boolean);
421
+ const fetchConfig = [];
422
+ fetchConfig.push(`method: ${JSON.stringify(node.method.toUpperCase())}`);
423
+ fetchConfig.push(`url: ${urlPath.template}`);
424
+ if (baseURL) fetchConfig.push(`baseURL: \`${baseURL}\``);
425
+ if (queryParams.length) fetchConfig.push(queryParamsMapping ? "params: mappedParams" : "params");
426
+ if (requestName) fetchConfig.push(`data: ${isFormData ? "formData as FormData" : "requestData"}`);
427
+ if (headers.length) fetchConfig.push(`headers: { ${headers.join(", ")} }`);
428
+ const callToolResult = dataReturnType === "data" ? `return {
429
+ content: [
430
+ {
431
+ type: 'text',
432
+ text: JSON.stringify(res.data)
433
+ }
434
+ ],
435
+ structuredContent: { data: res.data }
436
+ }` : `return {
437
+ content: [
438
+ {
439
+ type: 'text',
440
+ text: JSON.stringify(res)
441
+ }
442
+ ],
443
+ structuredContent: { data: res.data }
444
+ }`;
445
+ return /* @__PURE__ */ jsx(File.Source, {
446
+ name,
447
+ isExportable: true,
448
+ isIndexable: true,
449
+ children: /* @__PURE__ */ jsxs(Function, {
450
+ name,
451
+ async: true,
452
+ export: true,
453
+ params: paramsSignature,
454
+ JSDoc: { comments: getComments(node) },
455
+ returnType: "Promise<CallToolResult>",
456
+ children: [
457
+ "",
458
+ /* @__PURE__ */ jsx("br", {}),
459
+ /* @__PURE__ */ jsx("br", {}),
460
+ pathParamsMapping && Object.entries(pathParamsMapping).filter(([originalName, camelCaseName]) => originalName !== camelCaseName && isValidVarName(originalName)).map(([originalName, camelCaseName]) => `const ${originalName} = ${camelCaseName}`).join("\n"),
461
+ pathParamsMapping && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("br", {}), /* @__PURE__ */ jsx("br", {})] }),
462
+ queryParamsMapping && queryParams.length > 0 && /* @__PURE__ */ jsxs(Fragment, { children: [
463
+ buildRemappingCode(queryParamsMapping, "mappedParams", "params"),
464
+ /* @__PURE__ */ jsx("br", {}),
465
+ /* @__PURE__ */ jsx("br", {})
466
+ ] }),
467
+ headerParamsMapping && headerParams.length > 0 && /* @__PURE__ */ jsxs(Fragment, { children: [
468
+ buildRemappingCode(headerParamsMapping, "mappedHeaders", "headers"),
469
+ /* @__PURE__ */ jsx("br", {}),
470
+ /* @__PURE__ */ jsx("br", {})
471
+ ] }),
472
+ requestName && "const requestData = data",
473
+ /* @__PURE__ */ jsx("br", {}),
474
+ isFormData && requestName && "const formData = buildFormData(requestData)",
475
+ /* @__PURE__ */ jsx("br", {}),
476
+ `const res = await fetch<${generics.join(", ")}>({ ${fetchConfig.join(", ")} }, request)`,
477
+ /* @__PURE__ */ jsx("br", {}),
478
+ callToolResult
479
+ ]
480
+ })
481
+ });
482
+ }
483
+ //#endregion
484
+ //#region src/components/Server.tsx
485
+ const keysPrinter = functionPrinter({ mode: "keys" });
486
+ function Server({ name, serverName, serverVersion, paramsCasing, operations }) {
487
+ return /* @__PURE__ */ jsxs(File.Source, {
488
+ name,
489
+ isExportable: true,
490
+ isIndexable: true,
491
+ children: [
492
+ /* @__PURE__ */ jsx(Const, {
493
+ name: "server",
494
+ export: true,
495
+ children: `
496
+ new McpServer({
497
+ name: '${serverName}',
498
+ version: '${serverVersion}',
499
+ })
500
+ `
501
+ }),
502
+ operations.map(({ tool, mcp, zod, node }) => {
503
+ const pathParams = ast.caseParams(node.parameters, paramsCasing).filter((p) => p.in === "path");
504
+ const pathEntries = [];
505
+ const otherEntries = [];
506
+ for (const p of pathParams) {
507
+ const zodParam = zod.pathParams.find((zp) => zp.name === p.name);
508
+ pathEntries.push({
509
+ key: p.name,
510
+ value: zodParam ? zodParam.schemaName : zodExprFromSchemaNode(p.schema)
511
+ });
512
+ }
513
+ if (zod.requestName) otherEntries.push({
514
+ key: "data",
515
+ value: zod.requestName
516
+ });
517
+ if (zod.queryParams) otherEntries.push({
518
+ key: "params",
519
+ value: zodGroupExpr(zod.queryParams)
520
+ });
521
+ if (zod.headerParams) otherEntries.push({
522
+ key: "headers",
523
+ value: zodGroupExpr(zod.headerParams)
524
+ });
525
+ otherEntries.sort((a, b) => a.key.localeCompare(b.key));
526
+ const entries = [...pathEntries, ...otherEntries];
527
+ const paramsNode = entries.length ? ast.createFunctionParameters({ params: [ast.createParameterGroup({ properties: entries.map((e) => ast.createFunctionParameter({
528
+ name: e.key,
529
+ optional: false
530
+ })) })] }) : void 0;
531
+ const destructured = paramsNode ? keysPrinter.print(paramsNode) ?? "" : "";
532
+ const inputSchema = entries.length ? `{ ${entries.map((e) => `${e.key}: ${e.value}`).join(", ")} }` : void 0;
533
+ const outputSchema = zod.responseName;
534
+ const config = [
535
+ tool.title ? `title: ${JSON.stringify(tool.title)}` : null,
536
+ `description: ${JSON.stringify(tool.description)}`,
537
+ outputSchema ? `outputSchema: { data: ${outputSchema} }` : null
538
+ ].filter(Boolean).join(",\n ");
539
+ if (inputSchema) return `
540
+ server.registerTool(${JSON.stringify(tool.name)}, {
541
+ ${config},
542
+ inputSchema: ${inputSchema},
543
+ }, async (${destructured}, request) => {
544
+ return ${mcp.name}(${destructured}, request)
545
+ })
546
+ `;
547
+ return `
548
+ server.registerTool(${JSON.stringify(tool.name)}, {
549
+ ${config},
550
+ }, async (request) => {
551
+ return ${mcp.name}(request)
552
+ })
553
+ `;
554
+ }).filter(Boolean),
555
+ /* @__PURE__ */ jsx(Function, {
556
+ name: "startServer",
557
+ async: true,
558
+ export: true,
559
+ children: `try {
560
+ const transport = new StdioServerTransport()
561
+ await server.connect(transport)
562
+
563
+ } catch (error) {
564
+ console.error('Failed to start server:', error)
565
+ process.exit(1)
566
+ }`
567
+ })
568
+ ]
569
+ });
570
+ }
571
+ //#endregion
572
+ //#region src/generators/mcpGenerator.tsx
573
+ const mcpGenerator = defineGenerator({
574
+ name: "mcp",
575
+ renderer: jsxRenderer,
576
+ operation(node, ctx) {
577
+ const { resolver, driver, root } = ctx;
578
+ const { output, client, paramsCasing, group } = ctx.options;
579
+ const pluginTs = driver.getPlugin(pluginTsName);
580
+ if (!pluginTs) return null;
581
+ const tsResolver = driver.getResolver(pluginTsName);
582
+ const casedParams = ast.caseParams(node.parameters, paramsCasing);
583
+ const pathParams = casedParams.filter((p) => p.in === "path");
584
+ const queryParams = casedParams.filter((p) => p.in === "query");
585
+ const headerParams = casedParams.filter((p) => p.in === "header");
586
+ const importedTypeNames = [
587
+ ...pathParams.map((p) => tsResolver.resolvePathParamsName(node, p)),
588
+ ...queryParams.map((p) => tsResolver.resolveQueryParamsName(node, p)),
589
+ ...headerParams.map((p) => tsResolver.resolveHeaderParamsName(node, p)),
590
+ node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : void 0,
591
+ tsResolver.resolveResponseName(node),
592
+ ...node.responses.filter((r) => Number(r.statusCode) >= 400).map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode))
593
+ ].filter(Boolean);
594
+ const meta = {
595
+ name: resolver.resolveName(node.operationId),
596
+ file: resolver.resolveFile({
597
+ name: node.operationId,
598
+ extname: ".ts",
599
+ tag: node.tags[0] ?? "default",
600
+ path: node.path
601
+ }, {
602
+ root,
603
+ output,
604
+ group
605
+ }),
606
+ fileTs: tsResolver.resolveFile({
607
+ name: node.operationId,
608
+ extname: ".ts",
609
+ tag: node.tags[0] ?? "default",
610
+ path: node.path
611
+ }, {
612
+ root,
613
+ output: pluginTs.options?.output ?? output,
614
+ group: pluginTs.options?.group
615
+ })
616
+ };
617
+ return /* @__PURE__ */ jsxs(File, {
618
+ baseName: meta.file.baseName,
619
+ path: meta.file.path,
620
+ meta: meta.file.meta,
621
+ children: [
622
+ meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ jsx(File.Import, {
623
+ name: Array.from(new Set(importedTypeNames)).sort(),
624
+ root: meta.file.path,
625
+ path: meta.fileTs.path,
626
+ isTypeOnly: true
627
+ }),
628
+ /* @__PURE__ */ jsx(File.Import, {
629
+ name: [
630
+ "CallToolResult",
631
+ "ServerNotification",
632
+ "ServerRequest"
633
+ ],
634
+ path: "@modelcontextprotocol/sdk/types",
635
+ isTypeOnly: true
636
+ }),
637
+ /* @__PURE__ */ jsx(File.Import, {
638
+ name: ["RequestHandlerExtra"],
639
+ path: "@modelcontextprotocol/sdk/shared/protocol",
640
+ isTypeOnly: true
641
+ }),
642
+ /* @__PURE__ */ jsx(File.Import, {
643
+ name: ["buildFormData"],
644
+ root: meta.file.path,
645
+ path: path.resolve(root, ".kubb/config.ts")
646
+ }),
647
+ client.importPath ? /* @__PURE__ */ jsxs(Fragment, { children: [
648
+ /* @__PURE__ */ jsx(File.Import, {
649
+ name: [
650
+ "Client",
651
+ "RequestConfig",
652
+ "ResponseErrorConfig"
653
+ ],
654
+ path: client.importPath,
655
+ isTypeOnly: true
656
+ }),
657
+ /* @__PURE__ */ jsx(File.Import, {
658
+ name: "fetch",
659
+ path: client.importPath
660
+ }),
661
+ client.dataReturnType === "full" && /* @__PURE__ */ jsx(File.Import, {
662
+ name: ["ResponseConfig"],
663
+ path: client.importPath,
664
+ isTypeOnly: true
665
+ })
666
+ ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
667
+ /* @__PURE__ */ jsx(File.Import, {
668
+ name: [
669
+ "Client",
670
+ "RequestConfig",
671
+ "ResponseErrorConfig"
672
+ ],
673
+ root: meta.file.path,
674
+ path: path.resolve(root, ".kubb/fetch.ts"),
675
+ isTypeOnly: true
676
+ }),
677
+ /* @__PURE__ */ jsx(File.Import, {
678
+ name: ["fetch"],
679
+ root: meta.file.path,
680
+ path: path.resolve(root, ".kubb/fetch.ts")
681
+ }),
682
+ client.dataReturnType === "full" && /* @__PURE__ */ jsx(File.Import, {
683
+ name: ["ResponseConfig"],
684
+ root: meta.file.path,
685
+ path: path.resolve(root, ".kubb/fetch.ts"),
686
+ isTypeOnly: true
687
+ })
688
+ ] }),
689
+ /* @__PURE__ */ jsx(McpHandler, {
690
+ name: meta.name,
691
+ node,
692
+ resolver: tsResolver,
693
+ baseURL: client.baseURL,
694
+ dataReturnType: client.dataReturnType || "data",
695
+ paramsCasing
696
+ })
697
+ ]
698
+ });
699
+ }
700
+ });
701
+ //#endregion
702
+ //#region src/generators/serverGenerator.tsx
703
+ /**
704
+ * Default v5 server generator for `@kubb/plugin-mcp`.
705
+ *
706
+ * Uses individual zod schemas for each param (e.g. `createPetsPathUuidSchema`, `createPetsQueryOffsetSchema`)
707
+ * and `resolveResponseStatusName` for per-status response schemas.
708
+ * Query and header params are composed into `z.object({ ... })` from individual schemas.
709
+ */
710
+ const serverGenerator = defineGenerator({
711
+ name: "operations",
712
+ renderer: jsxRenderer,
713
+ operations(nodes, ctx) {
714
+ const { adapter, config, resolver, plugin, driver, root } = ctx;
715
+ const { output, paramsCasing, group } = ctx.options;
716
+ const pluginZod = driver.getPlugin(pluginZodName);
717
+ if (!pluginZod) return;
718
+ const zodResolver = driver.getResolver(pluginZodName);
719
+ const name = "server";
720
+ const serverFile = {
721
+ baseName: "server.ts",
722
+ path: path.resolve(root, output.path, "server.ts"),
723
+ meta: { pluginName: plugin.name }
724
+ };
725
+ const jsonFile = {
726
+ baseName: ".mcp.json",
727
+ path: path.resolve(root, output.path, ".mcp.json"),
728
+ meta: { pluginName: plugin.name }
729
+ };
730
+ const operationsMapped = nodes.map((node) => {
731
+ const casedParams = ast.caseParams(node.parameters, paramsCasing);
732
+ const pathParams = casedParams.filter((p) => p.in === "path");
733
+ const queryParams = casedParams.filter((p) => p.in === "query");
734
+ const headerParams = casedParams.filter((p) => p.in === "header");
735
+ const mcpFile = resolver.resolveFile({
736
+ name: node.operationId,
737
+ extname: ".ts",
738
+ tag: node.tags[0] ?? "default",
739
+ path: node.path
740
+ }, {
741
+ root,
742
+ output,
743
+ group
744
+ });
745
+ const zodFile = zodResolver.resolveFile({
746
+ name: node.operationId,
747
+ extname: ".ts",
748
+ tag: node.tags[0] ?? "default",
749
+ path: node.path
750
+ }, {
751
+ root,
752
+ output: pluginZod.options?.output ?? output,
753
+ group: pluginZod.options?.group
754
+ });
755
+ const requestName = node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName(node) : void 0;
756
+ const successStatus = findSuccessStatusCode(node.responses);
757
+ const responseName = successStatus ? zodResolver.resolveResponseStatusName(node, successStatus) : void 0;
758
+ const resolveParams = (params) => params.map((p) => ({
759
+ name: p.name,
760
+ schemaName: zodResolver.resolveParamName(node, p)
761
+ }));
762
+ return {
763
+ tool: {
764
+ name: node.operationId,
765
+ title: node.summary || void 0,
766
+ description: node.description || `Make a ${node.method.toUpperCase()} request to ${node.path}`
767
+ },
768
+ mcp: {
769
+ name: resolver.resolveName(node.operationId),
770
+ file: mcpFile
771
+ },
772
+ zod: {
773
+ pathParams: resolveParams(pathParams),
774
+ queryParams: queryParams.length ? resolveParams(queryParams) : void 0,
775
+ headerParams: headerParams.length ? resolveParams(headerParams) : void 0,
776
+ requestName,
777
+ responseName,
778
+ file: zodFile
779
+ },
780
+ node
781
+ };
782
+ });
783
+ const imports = operationsMapped.flatMap(({ mcp, zod }) => {
784
+ const zodNames = [
785
+ ...zod.pathParams.map((p) => p.schemaName),
786
+ ...(zod.queryParams ?? []).map((p) => p.schemaName),
787
+ ...(zod.headerParams ?? []).map((p) => p.schemaName),
788
+ zod.requestName,
789
+ zod.responseName
790
+ ].filter(Boolean);
791
+ const uniqueNames = [...new Set(zodNames)].sort();
792
+ return [/* @__PURE__ */ jsx(File.Import, {
793
+ name: [mcp.name],
794
+ root: serverFile.path,
795
+ path: mcp.file.path
796
+ }, mcp.name), uniqueNames.length > 0 && /* @__PURE__ */ jsx(File.Import, {
797
+ name: uniqueNames,
798
+ root: serverFile.path,
799
+ path: zod.file.path
800
+ }, `zod-${mcp.name}`)].filter(Boolean);
801
+ });
802
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(File, {
803
+ baseName: serverFile.baseName,
804
+ path: serverFile.path,
805
+ meta: serverFile.meta,
806
+ banner: resolver.resolveBanner(adapter.inputNode, {
807
+ output,
808
+ config
809
+ }),
810
+ footer: resolver.resolveFooter(adapter.inputNode, {
811
+ output,
812
+ config
813
+ }),
814
+ children: [
815
+ /* @__PURE__ */ jsx(File.Import, {
816
+ name: ["McpServer"],
817
+ path: "@modelcontextprotocol/sdk/server/mcp"
818
+ }),
819
+ /* @__PURE__ */ jsx(File.Import, {
820
+ name: ["z"],
821
+ path: "zod"
822
+ }),
823
+ /* @__PURE__ */ jsx(File.Import, {
824
+ name: ["StdioServerTransport"],
825
+ path: "@modelcontextprotocol/sdk/server/stdio"
826
+ }),
827
+ imports,
828
+ /* @__PURE__ */ jsx(Server, {
829
+ name,
830
+ serverName: adapter.inputNode?.meta?.title ?? "server",
831
+ serverVersion: adapter.inputNode?.meta?.version ?? "0.0.0",
832
+ paramsCasing,
833
+ operations: operationsMapped
834
+ })
835
+ ]
836
+ }), /* @__PURE__ */ jsx(File, {
837
+ baseName: jsonFile.baseName,
838
+ path: jsonFile.path,
839
+ meta: jsonFile.meta,
840
+ children: /* @__PURE__ */ jsx(File.Source, {
841
+ name,
842
+ children: `
843
+ {
844
+ "mcpServers": {
845
+ "${adapter.inputNode?.meta?.title || "server"}": {
846
+ "type": "stdio",
847
+ "command": "npx",
848
+ "args": ["tsx", "${path.relative(path.dirname(jsonFile.path), serverFile.path)}"]
849
+ }
850
+ }
851
+ }
852
+ `
853
+ })
854
+ })] });
855
+ }
856
+ });
857
+ //#endregion
858
+ //#region src/resolvers/resolverMcp.ts
859
+ /**
860
+ * Naming convention resolver for MCP plugin.
861
+ *
862
+ * Provides default naming helpers using camelCase with a `handler` suffix for functions.
863
+ *
864
+ * @example
865
+ * `resolverMcp.default('addPet', 'function') // → 'addPetHandler'`
866
+ */
867
+ const resolverMcp = defineResolver((ctx) => ({
868
+ name: "default",
869
+ pluginName: "plugin-mcp",
870
+ default(name, type) {
871
+ if (type === "file") return camelCase(name, { isFile: true });
872
+ return camelCase(name, { suffix: "handler" });
873
+ },
874
+ resolveName(name) {
875
+ return ctx.default(name, "function");
876
+ }
877
+ }));
878
+ //#endregion
13
879
  //#region src/plugin.ts
14
880
  const pluginMcpName = "plugin-mcp";
15
- const pluginMcp = createPlugin((options) => {
881
+ const pluginMcp = definePlugin((options) => {
16
882
  const { output = {
17
883
  path: "mcp",
18
884
  barrelType: "named"
19
- }, group, exclude = [], include, override = [], transformers = {}, generators = [mcpGenerator, serverGenerator].filter(Boolean), contentType, paramsCasing, client } = options;
885
+ }, group, exclude = [], include, override = [], paramsCasing, client, resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = options;
20
886
  const clientName = client?.client ?? "axios";
21
887
  const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : void 0);
888
+ const groupConfig = group ? {
889
+ ...group,
890
+ name: group.name ? group.name : (ctx) => {
891
+ if (group.type === "path") return `${ctx.group.split("/")[1]}`;
892
+ return `${camelCase(ctx.group)}Requests`;
893
+ }
894
+ } : void 0;
22
895
  return {
23
896
  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({
897
+ options,
898
+ dependencies: [pluginTsName, pluginZodName],
899
+ hooks: { "kubb:plugin:setup"(ctx) {
900
+ const resolver = userResolver ? {
901
+ ...resolverMcp,
902
+ ...userResolver
903
+ } : resolverMcp;
904
+ ctx.setOptions({
905
+ output,
906
+ exclude,
907
+ include,
908
+ override,
909
+ group: groupConfig,
910
+ paramsCasing,
911
+ client: {
912
+ client: clientName,
913
+ clientType: client?.clientType ?? "function",
914
+ importPath: clientImportPath,
915
+ dataReturnType: client?.dataReturnType ?? "data",
916
+ bundle: client?.bundle,
917
+ baseURL: client?.baseURL,
918
+ paramsCasing: client?.paramsCasing
919
+ },
920
+ resolver
921
+ });
922
+ ctx.setResolver(resolver);
923
+ if (userTransformer) ctx.setTransformer(userTransformer);
924
+ ctx.addGenerator(mcpGenerator);
925
+ ctx.addGenerator(serverGenerator);
926
+ for (const gen of userGenerators) ctx.addGenerator(gen);
927
+ const root = path.resolve(ctx.config.root, ctx.config.output.path);
928
+ const hasClientPlugin = ctx.config.plugins?.some((p) => p.name === pluginClientName);
929
+ if (client?.bundle && !hasClientPlugin && !clientImportPath) ctx.injectFile({
73
930
  baseName: "fetch.ts",
74
931
  path: path.resolve(root, ".kubb/fetch.ts"),
75
- sources: [{
932
+ sources: [ast.createSource({
76
933
  name: "fetch",
77
- value: this.plugin.options.client.client === "fetch" ? source$1 : source,
934
+ nodes: [ast.createText(clientName === "fetch" ? source$1 : source)],
78
935
  isExportable: true,
79
936
  isIndexable: true
80
- }],
81
- imports: [],
82
- exports: []
937
+ })]
83
938
  });
84
- if (!hasClientPlugin) await this.addFile({
939
+ if (!hasClientPlugin) ctx.injectFile({
85
940
  baseName: "config.ts",
86
941
  path: path.resolve(root, ".kubb/config.ts"),
87
- sources: [{
942
+ sources: [ast.createSource({
88
943
  name: "config",
89
- value: source$2,
944
+ nodes: [ast.createText(source$2)],
90
945
  isExportable: false,
91
946
  isIndexable: false
92
- }],
93
- imports: [],
94
- exports: []
95
- });
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 }
947
+ })]
114
948
  });
115
- await this.upsertFile(...barrelFiles);
116
- }
949
+ } }
117
950
  };
118
951
  });
119
952
  //#endregion
120
- export { pluginMcp, pluginMcpName };
953
+ export { McpHandler, Server, pluginMcp as default, pluginMcp, mcpGenerator, pluginMcpName, resolverMcp, serverGenerator };
121
954
 
122
955
  //# sourceMappingURL=index.js.map