@kubb/plugin-mcp
Version:
Generator mcp
178 lines (176 loc) • 6.72 kB
JavaScript
import { Server } from './chunk-YP4TIQ5F.js';
import { createReactGenerator } from '@kubb/plugin-oas';
import { useOas, useOperationManager } from '@kubb/plugin-oas/hooks';
import { getFooter, getBanner } from '@kubb/plugin-oas/utils';
import { pluginTsName } from '@kubb/plugin-ts';
import { useApp, File } from '@kubb/react';
import { Client } from '@kubb/plugin-client/components';
import { jsxs, jsx, Fragment } from '@kubb/react/jsx-runtime';
import { pluginZodName } from '@kubb/plugin-zod';
var mcpGenerator = createReactGenerator({
name: "mcp",
Operation({ operation }) {
const {
plugin: { options }
} = useApp();
const oas = useOas();
const { getSchemas, getName, getFile } = useOperationManager();
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: [
/* @__PURE__ */ jsx(File.Import, { name: ["CallToolResult"], path: "@modelcontextprotocol/sdk/types", isTypeOnly: true }),
/* @__PURE__ */ jsx(File.Import, { name: "fetch", path: options.client.importPath }),
/* @__PURE__ */ jsx(File.Import, { name: ["RequestConfig", "ResponseErrorConfig"], path: options.client.importPath, 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,
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 = createReactGenerator({
name: "operations",
Operations({ operations, options }) {
const { pluginManager, plugin } = useApp();
const oas = useOas();
const { getFile, getName, getSchemas } = 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: [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}"]
}
}
}
` }) })
] });
}
});
export { mcpGenerator, serverGenerator };
//# sourceMappingURL=chunk-JTYWPGT3.js.map
//# sourceMappingURL=chunk-JTYWPGT3.js.map