UNPKG

@kubb/plugin-mcp

Version:
259 lines (256 loc) 7.66 kB
import { t as __name } from "./chunk-eQyhnF5A.js"; import { t as Server } from "./Server-BolLh_dK.js"; import path from "node:path"; import { pluginTsName } from "@kubb/plugin-ts"; import { pluginZodName } from "@kubb/plugin-zod"; import { Client } from "@kubb/plugin-client/components"; import { createReactGenerator } from "@kubb/plugin-oas/generators"; import { useOas, useOperationManager } from "@kubb/plugin-oas/hooks"; import { getBanner, getFooter } from "@kubb/plugin-oas/utils"; import { File } from "@kubb/react-fabric"; import { Fragment, jsx, jsxs } from "@kubb/react-fabric/jsx-runtime"; import { usePluginManager } from "@kubb/core/hooks"; //#region src/generators/mcpGenerator.tsx const mcpGenerator = createReactGenerator({ name: "mcp", Operation({ config, operation, generator, plugin }) { const { options } = plugin; const oas = useOas(); const { getSchemas, getName, getFile } = useOperationManager(generator); const mcp = { name: getName(operation, { type: "function", suffix: "handler" }), file: getFile(operation) }; const type = { file: getFile(operation, { pluginKey: [pluginTsName] }), schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: "type" }) }; return /* @__PURE__ */ jsxs(File, { baseName: mcp.file.baseName, path: mcp.file.path, meta: mcp.file.meta, banner: getBanner({ oas, output: options.output }), footer: getFooter({ oas, output: options.output }), children: [ options.client.importPath ? /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(File.Import, { name: "fetch", path: options.client.importPath }), /* @__PURE__ */ jsx(File.Import, { name: ["RequestConfig", "ResponseErrorConfig"], path: options.client.importPath, isTypeOnly: true }), options.client.dataReturnType === "full" && /* @__PURE__ */ jsx(File.Import, { name: ["ResponseConfig"], path: options.client.importPath, isTypeOnly: true }) ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(File.Import, { name: ["fetch"], root: mcp.file.path, path: path.resolve(config.root, config.output.path, ".kubb/fetch.ts") }), /* @__PURE__ */ jsx(File.Import, { name: ["RequestConfig", "ResponseErrorConfig"], root: mcp.file.path, path: path.resolve(config.root, config.output.path, ".kubb/fetch.ts"), isTypeOnly: true }), options.client.dataReturnType === "full" && /* @__PURE__ */ jsx(File.Import, { name: ["ResponseConfig"], root: mcp.file.path, path: path.resolve(config.root, config.output.path, ".kubb/fetch.ts"), isTypeOnly: true }) ] }), /* @__PURE__ */ jsx(File.Import, { name: ["buildFormData"], root: mcp.file.path, path: path.resolve(config.root, config.output.path, ".kubb/config.ts") }), /* @__PURE__ */ jsx(File.Import, { name: ["CallToolResult"], path: "@modelcontextprotocol/sdk/types", isTypeOnly: true }), /* @__PURE__ */ jsx(File.Import, { name: [ type.schemas.request?.name, type.schemas.response.name, type.schemas.pathParams?.name, type.schemas.queryParams?.name, type.schemas.headerParams?.name, ...type.schemas.statusCodes?.map((item) => item.name) || [] ].filter(Boolean), root: mcp.file.path, path: type.file.path, isTypeOnly: true }), /* @__PURE__ */ jsxs(Client, { name: mcp.name, isConfigurable: false, returnType: "Promise<CallToolResult>", baseURL: options.client.baseURL, operation, typeSchemas: type.schemas, zodSchemas: void 0, dataReturnType: options.client.dataReturnType || "data", paramsType: "object", paramsCasing: "camelcase", pathParamsType: "object", parser: "client", children: [options.client.dataReturnType === "data" && `return { content: [ { type: 'text', text: JSON.stringify(res.data) } ] }`, options.client.dataReturnType === "full" && `return { content: [ { type: 'text', text: JSON.stringify(res) } ] }`] }) ] }); } }); //#endregion //#region src/generators/serverGenerator.tsx const serverGenerator = createReactGenerator({ name: "operations", Operations({ operations, generator, plugin }) { const pluginManager = usePluginManager(); const { options } = plugin; const oas = useOas(); const { getFile, getName, getSchemas } = useOperationManager(generator); const name = "server"; const file = pluginManager.getFile({ name, extname: ".ts", pluginKey: plugin.key }); const jsonFile = pluginManager.getFile({ name: ".mcp", extname: ".json", pluginKey: plugin.key }); const operationsMapped = operations.map((operation) => { return { tool: { name: operation.getOperationId() || operation.getSummary() || `${operation.method.toUpperCase()} ${operation.path}`, description: operation.getDescription() || `Make a ${operation.method.toUpperCase()} request to ${operation.path}` }, mcp: { name: getName(operation, { type: "function", suffix: "handler" }), file: getFile(operation) }, zod: { name: getName(operation, { type: "function", pluginKey: [pluginZodName] }), schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: "function" }), file: getFile(operation, { pluginKey: [pluginZodName] }) }, type: { schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: "type" }) } }; }); const imports = operationsMapped.flatMap(({ mcp, zod }) => { return [/* @__PURE__ */ jsx(File.Import, { name: [mcp.name], root: file.path, path: mcp.file.path }, mcp.name), /* @__PURE__ */ jsx(File.Import, { name: [ zod.schemas.request?.name, zod.schemas.pathParams?.name, zod.schemas.queryParams?.name, zod.schemas.headerParams?.name ].filter(Boolean), root: file.path, path: zod.file.path }, zod.name)]; }); return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: getBanner({ oas, output: options.output, config: pluginManager.config }), footer: getFooter({ oas, output: options.output }), children: [ /* @__PURE__ */ jsx(File.Import, { name: ["McpServer"], path: "@modelcontextprotocol/sdk/server/mcp" }), /* @__PURE__ */ jsx(File.Import, { name: ["StdioServerTransport"], path: "@modelcontextprotocol/sdk/server/stdio" }), imports, /* @__PURE__ */ jsx(Server, { name, serverName: oas.api.info?.title, serverVersion: oas.getVersion(), operations: operationsMapped }) ] }), /* @__PURE__ */ jsx(File, { baseName: jsonFile.baseName, path: jsonFile.path, meta: jsonFile.meta, children: /* @__PURE__ */ jsx(File.Source, { name, children: ` { "mcpServers": { "${oas.api.info?.title || "server"}": { "type": "stdio", "command": "npx", "args": ["tsx", "${file.path}"] } } } ` }) })] }); } }); //#endregion export { mcpGenerator as n, serverGenerator as t }; //# sourceMappingURL=generators-4PG9gQkq.js.map