@kubb/plugin-mcp
Version:
Generator mcp
270 lines (267 loc) • 9.63 kB
JavaScript
const require_Server = require('./Server-DblKyNZ0.cjs');
let node_path = require("node:path");
node_path = require_Server.__toESM(node_path);
let _kubb_plugin_ts = require("@kubb/plugin-ts");
let _kubb_plugin_zod = require("@kubb/plugin-zod");
let _kubb_plugin_client_components = require("@kubb/plugin-client/components");
let _kubb_plugin_oas_generators = require("@kubb/plugin-oas/generators");
let _kubb_plugin_oas_hooks = require("@kubb/plugin-oas/hooks");
let _kubb_plugin_oas_utils = require("@kubb/plugin-oas/utils");
let _kubb_react_fabric = require("@kubb/react-fabric");
let _kubb_react_fabric_jsx_runtime = require("@kubb/react-fabric/jsx-runtime");
let _kubb_core_hooks = require("@kubb/core/hooks");
//#region src/generators/mcpGenerator.tsx
const mcpGenerator = (0, _kubb_plugin_oas_generators.createReactGenerator)({
name: "mcp",
Operation({ config, operation, generator, plugin }) {
const { options } = plugin;
const oas = (0, _kubb_plugin_oas_hooks.useOas)();
const { getSchemas, getName, getFile } = (0, _kubb_plugin_oas_hooks.useOperationManager)(generator);
const mcp = {
name: getName(operation, {
type: "function",
suffix: "handler"
}),
file: getFile(operation)
};
const type = {
file: getFile(operation, { pluginKey: [_kubb_plugin_ts.pluginTsName] }),
schemas: getSchemas(operation, {
pluginKey: [_kubb_plugin_ts.pluginTsName],
type: "type"
})
};
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
baseName: mcp.file.baseName,
path: mcp.file.path,
meta: mcp.file.meta,
banner: (0, _kubb_plugin_oas_utils.getBanner)({
oas,
output: options.output
}),
footer: (0, _kubb_plugin_oas_utils.getFooter)({
oas,
output: options.output
}),
children: [
options.client.importPath ? /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: "fetch",
path: options.client.importPath
}),
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["RequestConfig", "ResponseErrorConfig"],
path: options.client.importPath,
isTypeOnly: true
}),
options.client.dataReturnType === "full" && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["ResponseConfig"],
path: options.client.importPath,
isTypeOnly: true
})
] }) : /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["fetch"],
root: mcp.file.path,
path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts")
}),
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["RequestConfig", "ResponseErrorConfig"],
root: mcp.file.path,
path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts"),
isTypeOnly: true
}),
options.client.dataReturnType === "full" && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["ResponseConfig"],
root: mcp.file.path,
path: node_path.default.resolve(config.root, config.output.path, ".kubb/fetch.ts"),
isTypeOnly: true
})
] }),
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["buildFormData"],
root: mcp.file.path,
path: node_path.default.resolve(config.root, config.output.path, ".kubb/config.ts")
}),
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["CallToolResult"],
path: "@modelcontextprotocol/sdk/types",
isTypeOnly: true
}),
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.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__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_plugin_client_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 || "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 = (0, _kubb_plugin_oas_generators.createReactGenerator)({
name: "operations",
Operations({ operations, generator, plugin }) {
const pluginManager = (0, _kubb_core_hooks.usePluginManager)();
const { options } = plugin;
const oas = (0, _kubb_plugin_oas_hooks.useOas)();
const { getFile, getName, getSchemas } = (0, _kubb_plugin_oas_hooks.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: [_kubb_plugin_zod.pluginZodName]
}),
schemas: getSchemas(operation, {
pluginKey: [_kubb_plugin_zod.pluginZodName],
type: "function"
}),
file: getFile(operation, { pluginKey: [_kubb_plugin_zod.pluginZodName] })
},
type: { schemas: getSchemas(operation, {
pluginKey: [_kubb_plugin_ts.pluginTsName],
type: "type"
}) }
};
});
const imports = operationsMapped.flatMap(({ mcp, zod }) => {
return [/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: [mcp.name],
root: file.path,
path: mcp.file.path
}, mcp.name), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.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__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
baseName: file.baseName,
path: file.path,
meta: file.meta,
banner: (0, _kubb_plugin_oas_utils.getBanner)({
oas,
output: options.output,
config: pluginManager.config
}),
footer: (0, _kubb_plugin_oas_utils.getFooter)({
oas,
output: options.output
}),
children: [
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["McpServer"],
path: "@modelcontextprotocol/sdk/server/mcp"
}),
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["StdioServerTransport"],
path: "@modelcontextprotocol/sdk/server/stdio"
}),
imports,
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_Server.Server, {
name,
serverName: oas.api.info?.title,
serverVersion: oas.getVersion(),
operations: operationsMapped
})
]
}), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File, {
baseName: jsonFile.baseName,
path: jsonFile.path,
meta: jsonFile.meta,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
children: `
{
"mcpServers": {
"${oas.api.info?.title || "server"}": {
"type": "stdio",
"command": "npx",
"args": ["tsx", "${file.path}"]
}
}
}
`
})
})] });
}
});
//#endregion
Object.defineProperty(exports, 'mcpGenerator', {
enumerable: true,
get: function () {
return mcpGenerator;
}
});
Object.defineProperty(exports, 'serverGenerator', {
enumerable: true,
get: function () {
return serverGenerator;
}
});
//# sourceMappingURL=generators-dnrz3jPX.cjs.map