@kubb/plugin-mcp
Version:
Generator mcp
97 lines (92 loc) • 3.23 kB
JavaScript
;
var react = require('@kubb/react');
var utils = require('@kubb/plugin-oas/utils');
var oas = require('@kubb/oas');
var transformers = require('@kubb/core/transformers');
var jsxRuntime = require('@kubb/react/jsx-runtime');
// src/components/Server.tsx
function getParams({ schemas }) {
const pathParams = utils.getPathParams(schemas.pathParams, {
typed: false
});
return react.FunctionParams.factory({
data: {
mode: "object",
children: {
...Object.entries(pathParams).reduce((acc, [key, param]) => {
if (param && schemas.pathParams?.name) {
let suffix = ".shape";
if (oas.isNullable(schemas.pathParams.schema)) {
if (oas.isReference(schemas.pathParams)) {
suffix = ".unwrap().schema.unwrap().shape";
} else {
suffix = ".unwrap().shape";
}
} else {
if (oas.isReference(schemas.pathParams)) {
suffix = ".schema.shape";
}
}
param.value = `${schemas.pathParams?.name}${suffix}['${key}']`;
}
return {
...acc,
[transformers.camelCase(key)]: param
};
}, {}),
data: schemas.request?.name ? {
value: schemas.request?.name,
optional: utils.isOptional(schemas.request?.schema)
} : void 0,
params: schemas.queryParams?.name ? {
value: schemas.queryParams?.name,
optional: utils.isOptional(schemas.queryParams?.schema)
} : void 0,
headers: schemas.headerParams?.name ? {
value: schemas.headerParams?.name,
optional: utils.isOptional(schemas.headerParams?.schema)
} : void 0
}
}
});
}
function Server({ name, serverName, serverVersion, operations }) {
return /* @__PURE__ */ jsxRuntime.jsxs(react.File.Source, { name, isExportable: true, isIndexable: true, children: [
/* @__PURE__ */ jsxRuntime.jsx(react.Const, { name: "server", export: true, children: `
new McpServer({
name: '${serverName}',
version: '${serverVersion}',
})
` }),
operations.map(({ tool, mcp, zod }) => {
const paramsClient = getParams({ schemas: zod.schemas });
if (zod.schemas.request?.name || zod.schemas.headerParams?.name || zod.schemas.queryParams?.name || zod.schemas.pathParams?.name) {
return `
server.tool(${JSON.stringify(tool.name)}, ${JSON.stringify(tool.description)}, ${paramsClient.toObjectValue()}, async (${paramsClient.toObject()}) => {
return ${mcp.name}(${paramsClient.toObject()})
})
`;
}
return `
server.tool(${JSON.stringify(tool.name)}, ${JSON.stringify(tool.description)}, async () => {
return ${mcp.name}(${paramsClient.toObject()})
})
`;
}).filter(Boolean),
`
async function startServer() {
try {
const transport = new StdioServerTransport()
await server.connect(transport)
} catch (error) {
console.error('Failed to start server:', error)
process.exit(1)
}
}
startServer()
`
] });
}
exports.Server = Server;
//# sourceMappingURL=chunk-ODMTYF4L.cjs.map
//# sourceMappingURL=chunk-ODMTYF4L.cjs.map