@kubb/plugin-mcp
Version:
Generator mcp
181 lines (178 loc) • 7.13 kB
JavaScript
'use strict';
var chunkODMTYF4L_cjs = require('./chunk-ODMTYF4L.cjs');
var pluginOas = require('@kubb/plugin-oas');
var hooks = require('@kubb/plugin-oas/hooks');
var utils = require('@kubb/plugin-oas/utils');
var pluginTs = require('@kubb/plugin-ts');
var react = require('@kubb/react');
var components = require('@kubb/plugin-client/components');
var jsxRuntime = require('@kubb/react/jsx-runtime');
var pluginZod = require('@kubb/plugin-zod');
var mcpGenerator = pluginOas.createReactGenerator({
name: "mcp",
Operation({ operation }) {
const {
plugin: { options }
} = react.useApp();
const oas = hooks.useOas();
const { getSchemas, getName, getFile } = hooks.useOperationManager();
const mcp = {
name: getName(operation, { type: "function", suffix: "handler" }),
file: getFile(operation)
};
const type = {
file: getFile(operation, { pluginKey: [pluginTs.pluginTsName] }),
schemas: getSchemas(operation, { pluginKey: [pluginTs.pluginTsName], type: "type" })
};
return /* @__PURE__ */ jsxRuntime.jsxs(
react.File,
{
baseName: mcp.file.baseName,
path: mcp.file.path,
meta: mcp.file.meta,
banner: utils.getBanner({ oas, output: options.output }),
footer: utils.getFooter({ oas, output: options.output }),
children: [
/* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: ["CallToolResult"], path: "@modelcontextprotocol/sdk/types", isTypeOnly: true }),
/* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: "fetch", path: options.client.importPath }),
/* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: ["RequestConfig", "ResponseErrorConfig"], path: options.client.importPath, isTypeOnly: true }),
/* @__PURE__ */ jsxRuntime.jsx(
react.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__ */ jsxRuntime.jsxs(
components.Client,
{
name: mcp.name,
isConfigurable: false,
returnType: "Promise<CallToolResult>",
baseURL: options.client.baseURL,
operation,
typeSchemas: type.schemas,
zodSchemas: void 0,
dataReturnType: options.client.dataReturnType,
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)
}
]
}`
]
}
)
]
}
);
}
});
var serverGenerator = pluginOas.createReactGenerator({
name: "operations",
Operations({ operations, options }) {
const { pluginManager, plugin } = react.useApp();
const oas = hooks.useOas();
const { getFile, getName, getSchemas } = hooks.useOperationManager();
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: [pluginZod.pluginZodName]
}),
schemas: getSchemas(operation, { pluginKey: [pluginZod.pluginZodName], type: "function" }),
file: getFile(operation, { pluginKey: [pluginZod.pluginZodName] })
},
type: {
schemas: getSchemas(operation, { pluginKey: [pluginTs.pluginTsName], type: "type" })
}
};
});
const imports = operationsMapped.flatMap(({ mcp, zod }) => {
return [
/* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: [mcp.name], root: file.path, path: mcp.file.path }, mcp.name),
/* @__PURE__ */ jsxRuntime.jsx(
react.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__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
/* @__PURE__ */ jsxRuntime.jsxs(
react.File,
{
baseName: file.baseName,
path: file.path,
meta: file.meta,
banner: utils.getBanner({ oas, output: options.output, config: pluginManager.config }),
footer: utils.getFooter({ oas, output: options.output }),
children: [
/* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: ["McpServer"], path: "@modelcontextprotocol/sdk/server/mcp" }),
/* @__PURE__ */ jsxRuntime.jsx(react.File.Import, { name: ["StdioServerTransport"], path: "@modelcontextprotocol/sdk/server/stdio" }),
imports,
/* @__PURE__ */ jsxRuntime.jsx(chunkODMTYF4L_cjs.Server, { name, serverName: oas.api.info?.title, serverVersion: oas.getVersion(), operations: operationsMapped })
]
}
),
/* @__PURE__ */ jsxRuntime.jsx(react.File, { baseName: jsonFile.baseName, path: jsonFile.path, meta: jsonFile.meta, children: /* @__PURE__ */ jsxRuntime.jsx(react.File.Source, { name, children: `
{
"mcpServers": {
"${oas.api.info?.title || "server"}": {
"type": "stdio",
"command": "npx",
"args": ["tsx", "${file.path}"]
}
}
}
` }) })
] });
}
});
exports.mcpGenerator = mcpGenerator;
exports.serverGenerator = serverGenerator;
//# sourceMappingURL=chunk-MTZE6GGJ.cjs.map
//# sourceMappingURL=chunk-MTZE6GGJ.cjs.map