@kubb/plugin-mcp
Version:
Generator mcp
85 lines (83 loc) • 3.07 kB
JavaScript
import { mcpGenerator, serverGenerator } from "./generators-CFdUB1k-.js";
import "./Server-BX80OVzu.js";
import path from "node:path";
import { FileManager, PluginManager, createPlugin } from "@kubb/core";
import { camelCase } from "@kubb/core/transformers";
import { OperationGenerator, pluginOasName } from "@kubb/plugin-oas";
import { pluginTsName } from "@kubb/plugin-ts";
import { pluginZodName } from "@kubb/plugin-zod";
//#region src/plugin.ts
const pluginMcpName = "plugin-mcp";
const pluginMcp = createPlugin((options) => {
const { output = {
path: "mcp",
barrelType: "named"
}, group, exclude = [], include, override = [], transformers = {}, generators = [mcpGenerator, serverGenerator].filter(Boolean), contentType } = options;
return {
name: pluginMcpName,
options: {
output,
group,
client: {
importPath: "@kubb/plugin-client/clients/axios",
dataReturnType: "data",
...options.client
}
},
pre: [
pluginOasName,
pluginTsName,
pluginZodName
].filter(Boolean),
resolvePath(baseName, pathMode, options$1) {
const root = path.resolve(this.config.root, this.config.output.path);
if ((pathMode ?? FileManager.getMode(path.resolve(root, output.path))) === "single")
/**
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
*/
return path.resolve(root, output.path);
if (group && (options$1?.group?.path || options$1?.group?.tag)) {
const groupName = group?.name ? group.name : (ctx) => {
if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
return `${camelCase(ctx.group)}Requests`;
};
return path.resolve(root, output.path, groupName({ group: group.type === "path" ? options$1.group.path : options$1.group.tag }), baseName);
}
return path.resolve(root, output.path, baseName);
},
resolveName(name, type) {
const resolvedName = camelCase(name, { isFile: type === "file" });
if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
return resolvedName;
},
async buildStart() {
const [swaggerPlugin] = PluginManager.getDependedPlugins(this.plugins, [pluginOasName]);
const oas = await swaggerPlugin.context.getOas();
const root = path.resolve(this.config.root, this.config.output.path);
const mode = FileManager.getMode(path.resolve(root, output.path));
const files = await new OperationGenerator(this.plugin.options, {
oas,
pluginManager: this.pluginManager,
plugin: this.plugin,
contentType,
exclude,
include,
override,
mode
}).build(...generators);
await this.addFile(...files);
const barrelFiles = await this.fileManager.getBarrelFiles({
type: output.barrelType ?? "named",
root,
output,
meta: { pluginKey: this.plugin.key },
logger: this.logger
});
await this.addFile(...barrelFiles);
}
};
});
//#endregion
export { pluginMcp, pluginMcpName };
//# sourceMappingURL=index.js.map