"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.McpToolActionHandler = void 0;
const common_1 = require("@nestjs/common");
class McpToolActionHandler {
    constructor(serverName, toolNameRaw, mcpService, toolDescription) {
        this.serverName = serverName;
        this.toolNameRaw = toolNameRaw;
        this.mcpService = mcpService;
        this.toolDescription = toolDescription;
        this.logger = new common_1.Logger(McpToolActionHandler.name);
        this.toolName = `${serverName}__${toolNameRaw}`;
    }
    getMetadata() {
        return {
            name: this.toolName,
            description: this.toolDescription,
            arguments: [],
        };
    }
    getDefinition() {
        return `## ${this.toolName}

${this.toolDescription}

Executed via MCP server "${this.serverName}", tool "${this.toolNameRaw}".`;
    }
    async execute(args, _context) {
        try {
            const { stdout, stderr } = await this.mcpService.executeMcpTool(this.serverName, this.toolNameRaw, args);
            const unifiedOutput = stdout || stderr || '(Tool produced no output)';
            return {
                status: 'SUCCESS',
                summary: `MCP tool "${this.serverName}.${this.toolNameRaw}" executed.`,
                persisted_args: {
                    server_name: this.serverName,
                    tool_name: this.toolNameRaw,
                    arguments: JSON.stringify(args),
                    plain: unifiedOutput,
                },
                execution_log: {
                    output: unifiedOutput,
                },
            };
        }
        catch (error) {
            return {
                status: 'FAILURE',
                summary: `MCP tool "${this.serverName}.${this.toolNameRaw}" failed.`,
                error_message: error.message,
                persisted_args: {
                    server_name: this.serverName,
                    tool_name: this.toolNameRaw,
                    arguments: JSON.stringify(args),
                    plain: `Error: ${error.message}`,
                },
                execution_log: {
                    output: '',
                    error_message: error.message,
                },
            };
        }
    }
}
exports.McpToolActionHandler = McpToolActionHandler;
//# sourceMappingURL=mcp-tool-action.handler.js.map