@kubb/plugin-mcp
Version:
Generator mcp
90 lines (86 loc) • 2.83 kB
JavaScript
import { camelCase } from "@kubb/core/transformers";
import { getPathParams, isOptional } from "@kubb/plugin-oas/utils";
import { Const, File, FunctionParams } from "@kubb/react";
import { jsx, jsxs } from "@kubb/react/jsx-runtime";
import { isNullable, isReference } from "@kubb/oas";
//#region src/components/Server.tsx
function getParams({ schemas }) {
const pathParams = getPathParams(schemas.pathParams, { typed: false });
return FunctionParams.factory({ data: {
mode: "object",
children: {
...Object.entries(pathParams).reduce((acc, [key, param]) => {
if (param && schemas.pathParams?.name) {
let suffix = ".shape";
if (isNullable(schemas.pathParams.schema)) if (isReference(schemas.pathParams)) suffix = ".unwrap().schema.unwrap().shape";
else suffix = ".unwrap().shape";
else if (isReference(schemas.pathParams)) suffix = ".schema.shape";
param.value = `${schemas.pathParams?.name}${suffix}['${key}']`;
}
return {
...acc,
[camelCase(key)]: param
};
}, {}),
data: schemas.request?.name ? {
value: schemas.request?.name,
optional: isOptional(schemas.request?.schema)
} : void 0,
params: schemas.queryParams?.name ? {
value: schemas.queryParams?.name,
optional: isOptional(schemas.queryParams?.schema)
} : void 0,
headers: schemas.headerParams?.name ? {
value: schemas.headerParams?.name,
optional: isOptional(schemas.headerParams?.schema)
} : void 0
}
} });
}
function Server({ name, serverName, serverVersion, operations }) {
return /* @__PURE__ */ jsxs(File.Source, {
name,
isExportable: true,
isIndexable: true,
children: [
/* @__PURE__ */ jsx(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()
`
]
});
}
//#endregion
export { Server };
//# sourceMappingURL=Server-BX80OVzu.js.map