"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.McpClientTool = void 0;
const ai_utilities_1 = require("@n8n/ai-utilities");
const n8n_workflow_1 = require("n8n-workflow");
const loadOptions_1 = require("./loadOptions");
const descriptions_1 = require("../shared/descriptions");
const runtime_1 = require("../shared/runtime");
function resolveConfigFromNodeParameters(ctx, itemIndex) {
    const node = ctx.getNode();
    const authentication = ctx.getNodeParameter('authentication', itemIndex);
    const timeout = ctx.getNodeParameter('options.timeout', itemIndex, 60000);
    let transport;
    let endpointUrl;
    if (node.typeVersion === 1) {
        transport = 'sse';
        endpointUrl = ctx.getNodeParameter('sseEndpoint', itemIndex);
    }
    else {
        transport = ctx.getNodeParameter('serverTransport', itemIndex);
        endpointUrl = ctx.getNodeParameter('endpointUrl', itemIndex);
    }
    return {
        authentication,
        transport,
        endpointUrl,
        timeout,
        toolFilter: {
            mode: ctx.getNodeParameter('include', itemIndex),
            includeTools: ctx.getNodeParameter('includeTools', itemIndex, []),
            excludeTools: ctx.getNodeParameter('excludeTools', itemIndex, []),
        },
    };
}
class McpClientTool {
    constructor() {
        this.description = {
            displayName: 'MCP Client Tool',
            name: 'mcpClientTool',
            icon: {
                light: 'file:../mcp.svg',
                dark: 'file:../mcp.dark.svg',
            },
            group: ['output'],
            version: [1, 1.1, 1.2, 1.3],
            defaultVersion: 1.3,
            description: 'Connect tools from an MCP Server',
            defaults: {
                name: 'MCP Client',
            },
            codex: {
                categories: ['AI'],
                subcategories: {
                    AI: ['Model Context Protocol'],
                },
                alias: ['Model Context Protocol', 'MCP Client'],
                resources: {
                    primaryDocumentation: [
                        {
                            url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.toolmcp/',
                        },
                    ],
                },
            },
            inputs: [],
            outputs: [{ type: n8n_workflow_1.NodeConnectionTypes.AiTool, displayName: 'Tools' }],
            credentials: descriptions_1.credentials,
            properties: [
                (0, ai_utilities_1.getConnectionHintNoticeField)([n8n_workflow_1.NodeConnectionTypes.AiAgent]),
                {
                    displayName: 'SSE Endpoint',
                    name: 'sseEndpoint',
                    type: 'string',
                    description: 'SSE Endpoint of your MCP server',
                    placeholder: 'e.g. https://my-mcp-server.ai/sse',
                    default: '',
                    required: true,
                    displayOptions: {
                        show: {
                            '@version': [1],
                        },
                    },
                },
                {
                    displayName: 'Endpoint',
                    name: 'endpointUrl',
                    type: 'string',
                    description: 'Endpoint of your MCP server',
                    placeholder: 'e.g. https://my-mcp-server.ai/mcp',
                    default: '',
                    required: true,
                    displayOptions: {
                        show: {
                            '@version': [{ _cnd: { gte: 1.1 } }],
                        },
                    },
                },
                (0, descriptions_1.transportSelect)({
                    defaultOption: 'sse',
                    displayOptions: {
                        show: {
                            '@version': [1.1],
                        },
                    },
                }),
                (0, descriptions_1.transportSelect)({
                    defaultOption: 'httpStreamable',
                    displayOptions: {
                        show: {
                            '@version': [{ _cnd: { gte: 1.2 } }],
                        },
                    },
                }),
                {
                    displayName: 'Authentication',
                    name: 'authentication',
                    type: 'options',
                    options: [
                        {
                            name: 'Bearer Auth',
                            value: 'bearerAuth',
                        },
                        {
                            name: 'Header Auth',
                            value: 'headerAuth',
                        },
                        {
                            name: 'None',
                            value: 'none',
                        },
                    ],
                    default: 'none',
                    description: 'The way to authenticate with your endpoint',
                    displayOptions: {
                        show: {
                            '@version': [{ _cnd: { lt: 1.2 } }],
                        },
                    },
                },
                {
                    displayName: 'Authentication',
                    name: 'authentication',
                    type: 'options',
                    options: [
                        {
                            name: 'Bearer Auth',
                            value: 'bearerAuth',
                        },
                        {
                            name: 'Header Auth',
                            value: 'headerAuth',
                        },
                        {
                            name: 'MCP OAuth2',
                            value: 'mcpOAuth2Api',
                        },
                        {
                            name: 'Multiple Headers Auth',
                            value: 'multipleHeadersAuth',
                        },
                        {
                            name: 'None',
                            value: 'none',
                        },
                    ],
                    default: 'none',
                    description: 'The way to authenticate with your endpoint',
                    displayOptions: {
                        show: {
                            '@version': [{ _cnd: { gte: 1.2 } }],
                        },
                    },
                },
                {
                    displayName: 'Credentials',
                    name: 'credentials',
                    type: 'credentials',
                    default: '',
                    displayOptions: {
                        show: {
                            authentication: ['headerAuth', 'bearerAuth', 'mcpOAuth2Api', 'multipleHeadersAuth'],
                        },
                    },
                },
                {
                    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: 'Also include all unchanged fields from the input',
                        },
                        {
                            name: 'Selected',
                            value: 'selected',
                            description: 'Also include the tools listed in the parameter "Tools to Include"',
                        },
                        {
                            name: 'All Except',
                            value: 'except',
                            description: 'Exclude the tools listed in the parameter "Tools to Exclude"',
                        },
                    ],
                },
                {
                    displayName: 'Tools to Include',
                    name: 'includeTools',
                    type: 'multiOptions',
                    default: [],
                    description: 'Choose from the list, or specify IDs using an <a href="https://docs.n8n.io/code/expressions/">expression</a>',
                    typeOptions: {
                        loadOptionsMethod: 'getTools',
                        loadOptionsDependsOn: ['sseEndpoint'],
                    },
                    displayOptions: {
                        show: {
                            include: ['selected'],
                        },
                    },
                },
                {
                    displayName: 'Tools to Exclude',
                    name: 'excludeTools',
                    type: 'multiOptions',
                    default: [],
                    description: '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: {
                getTools: loadOptions_1.getTools,
            },
        };
    }
    async supplyData(itemIndex) {
        return await (0, runtime_1.buildMcpToolkit)(this, itemIndex, resolveConfigFromNodeParameters(this, itemIndex));
    }
    async execute() {
        return await (0, runtime_1.executeMcpTool)(this, (itemIndex) => resolveConfigFromNodeParameters(this, itemIndex));
    }
}
exports.McpClientTool = McpClientTool;
//# sourceMappingURL=McpClientTool.node.js.map