"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.McpRegistryClientTool = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const runtime_1 = require("../shared/runtime");
const types_1 = require("../shared/types");
class McpRegistryClientTool {
    constructor() {
        this.description = {
            displayName: 'MCP Registry Client (internal)',
            name: 'mcpRegistryClientTool',
            hidden: true,
            group: ['output'],
            version: 1,
            description: 'Runtime backing for MCP registry-derived nodes',
            defaults: {
                name: 'MCP Registry Client',
            },
            codex: {
                categories: ['AI'],
                subcategories: {
                    AI: ['Model Context Protocol'],
                },
                alias: ['MCP', 'Model Context Protocol'],
            },
            inputs: [],
            outputs: [{ type: n8n_workflow_1.NodeConnectionTypes.AiTool, displayName: 'Tools' }],
            credentials: [
                {
                    name: 'mcpOAuth2Api',
                    required: true,
                },
            ],
            properties: [
                {
                    displayName: 'Endpoint URL',
                    name: 'endpointUrl',
                    type: 'hidden',
                    default: '',
                },
                {
                    displayName: 'Server Transport',
                    name: 'serverTransport',
                    type: 'hidden',
                    default: 'httpStreamable',
                },
                {
                    displayName: 'Tools to Include',
                    name: 'include',
                    type: 'options',
                    description: 'How to select the tools you want to be exposed to the AI Agent',
                    default: 'all',
                    options: [
                        {
                            name: 'All',
                            value: 'all',
                            description: 'Expose every tool from the MCP server',
                        },
                        {
                            name: 'Selected',
                            value: 'selected',
                            description: 'Only expose the tools listed in "Tools to Include"',
                        },
                        {
                            name: 'All Except',
                            value: 'except',
                            description: 'Expose all tools except those listed in "Tools to Exclude"',
                        },
                    ],
                },
                {
                    displayName: 'Tools to Include',
                    name: 'includeTools',
                    type: 'multiOptions',
                    default: [],
                    description: 'Tools from the MCP server to expose to the agent. Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.',
                    typeOptions: {
                        loadOptionsMethod: 'getTools',
                    },
                    displayOptions: {
                        show: {
                            include: ['selected'],
                        },
                    },
                },
                {
                    displayName: 'Tools to Exclude',
                    name: 'excludeTools',
                    type: 'multiOptions',
                    default: [],
                    description: 'Tools from the MCP server to hide from the agent. Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.',
                    typeOptions: {
                        loadOptionsMethod: 'getTools',
                    },
                    displayOptions: {
                        show: {
                            include: ['except'],
                        },
                    },
                },
                {
                    displayName: 'Options',
                    name: 'options',
                    placeholder: 'Add Option',
                    description: 'Additional options to add',
                    type: 'collection',
                    default: {},
                    options: [
                        {
                            displayName: 'Timeout',
                            name: 'timeout',
                            type: 'number',
                            typeOptions: {
                                minValue: 1,
                            },
                            default: 60000,
                            description: 'Time in ms to wait for tool calls to finish',
                        },
                    ],
                },
            ],
        };
        this.methods = {
            loadOptions: {
                async getTools() {
                    const authentication = getCredentialType(this);
                    return await (0, runtime_1.loadMcpToolOptions)(this, {
                        authentication,
                        transport: this.getNodeParameter('serverTransport'),
                        endpointUrl: this.getNodeParameter('endpointUrl'),
                        timeout: this.getNodeParameter('options.timeout', 60000),
                    });
                },
            },
        };
    }
    async supplyData(itemIndex) {
        return await (0, runtime_1.buildMcpToolkit)(this, itemIndex, resolveConfig(this, itemIndex));
    }
    async execute() {
        return await (0, runtime_1.executeMcpTool)(this, (itemIndex) => resolveConfig(this, itemIndex));
    }
}
exports.McpRegistryClientTool = McpRegistryClientTool;
function resolveConfig(ctx, itemIndex) {
    const authentication = getCredentialType(ctx);
    return {
        authentication,
        transport: ctx.getNodeParameter('serverTransport', itemIndex),
        endpointUrl: ctx.getNodeParameter('endpointUrl', itemIndex),
        timeout: ctx.getNodeParameter('options.timeout', itemIndex, 60000),
        toolFilter: {
            mode: ctx.getNodeParameter('include', itemIndex),
            includeTools: ctx.getNodeParameter('includeTools', itemIndex, []),
            excludeTools: ctx.getNodeParameter('excludeTools', itemIndex, []),
        },
    };
}
function getCredentialType(ctx) {
    const node = ctx.getNode();
    const credentials = node.credentials ?? {};
    const credentialType = Object.keys(credentials).find((credentialType) => (0, types_1.isMcpOAuth2Authentication)(credentialType));
    if (!credentialType) {
        throw new n8n_workflow_1.NodeOperationError(node, 'No MCP OAuth2 credential type found');
    }
    return credentialType;
}
//# sourceMappingURL=McpRegistryClientTool.node.js.map