@nestia/sdk
Version:
Nestia SDK and Swagger generator
79 lines • 4.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReflectMcpOperationAnalyzer = void 0;
const constants_1 = require("@nestjs/common/constants");
const ImportAnalyzer_1 = require("./ImportAnalyzer");
var ReflectMcpOperationAnalyzer;
(function (ReflectMcpOperationAnalyzer) {
ReflectMcpOperationAnalyzer.analyze = (ctx) => {
var _a, _b, _c;
var _d, _e, _f, _g, _h, _j, _k;
const route = Reflect.getMetadata("nestia/McpRoute", ctx.function);
if (route === undefined)
return null;
const errors = [];
const hasHttpRoute = Reflect.getMetadata(constants_1.PATH_METADATA, ctx.function) !== undefined ||
Reflect.getMetadata(constants_1.METHOD_METADATA, ctx.function) !== undefined;
const hasWebSocketRoute = Reflect.getMetadata("nestia/WebSocketRoute", ctx.function) !== undefined;
if (hasHttpRoute || hasWebSocketRoute)
errors.push("@McpRoute must not be combined with HTTP or WebSocket route decorators on the same method.");
const preconfigured = ((_d = Reflect.getMetadata("nestia/McpRoute/Parameters", ctx.controller.class.prototype, ctx.name)) !== null && _d !== void 0 ? _d : []).sort((a, b) => a.index - b.index);
if (preconfigured.length > 1)
errors.push("@McpRoute tools may declare at most one @McpRoute.Params() parameter.");
if (ctx.function.length > 1)
errors.push("@McpRoute tools must have 0 or 1 parameters (the MCP arguments object).");
const imports = [];
const parameters = preconfigured
.map((p) => {
const matched = ctx.metadata.parameters.find((m) => p.index === m.index);
if (matched === undefined) {
errors.push(`Unable to find parameter type of the ${p.index} (th) argument.`);
return null;
}
if (matched.type === null) {
errors.push(`Failed to analyze the parameter type of ${JSON.stringify(matched.name)}.`);
return null;
}
imports.push(...matched.imports);
return {
category: "params",
name: matched.name,
index: p.index,
type: matched.type,
imports: matched.imports,
description: matched.description,
jsDocTags: matched.jsDocTags,
};
})
.filter((p) => !!p);
if ((_b = (_a = ctx.metadata.success) === null || _a === void 0 ? void 0 : _a.imports) === null || _b === void 0 ? void 0 : _b.length)
imports.push(...ctx.metadata.success.imports);
if (errors.length) {
ctx.project.errors.push({
file: ctx.controller.file,
class: ctx.controller.class.name,
function: ctx.function.name,
from: ctx.name,
contents: errors,
});
return null;
}
return {
protocol: "mcp",
name: ctx.name,
toolName: route.name,
title: (_e = route.title) !== null && _e !== void 0 ? _e : null,
toolDescription: (_f = route.description) !== null && _f !== void 0 ? _f : null,
inputSchema: route.inputSchema,
outputSchema: (_g = route.outputSchema) !== null && _g !== void 0 ? _g : null,
annotations: (_h = route.annotations) !== null && _h !== void 0 ? _h : null,
function: ctx.function,
parameters,
returnType: (_j = (_c = ctx.metadata.success) === null || _c === void 0 ? void 0 : _c.type) !== null && _j !== void 0 ? _j : null,
imports: ImportAnalyzer_1.ImportAnalyzer.merge(imports),
description: (_k = ctx.metadata.description) !== null && _k !== void 0 ? _k : null,
jsDocTags: ctx.metadata.jsDocTags,
};
};
})(ReflectMcpOperationAnalyzer || (exports.ReflectMcpOperationAnalyzer = ReflectMcpOperationAnalyzer = {}));
//# sourceMappingURL=ReflectMcpOperationAnalyzer.js.map