UNPKG

@n8n/n8n-nodes-langchain

Version:

![Banner image](https://user-images.githubusercontent.com/10284570/173569848-c624317f-42b1-45a6-ab09-f0ea3c247648.png)

339 lines 14.1 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.McpClient = void 0; const n8n_workflow_1 = require("n8n-workflow"); const zod_1 = require("zod"); const core_1 = require("zod/v4/core"); const listSearch = __importStar(require("./listSearch")); const resourceMapping = __importStar(require("./resourceMapping")); const descriptions_1 = require("../shared/descriptions"); const utils_1 = require("../shared/utils"); class McpClient { constructor() { this.description = { displayName: 'MCP Client', description: 'Standalone MCP Client', name: 'mcpClient', icon: { light: 'file:../mcp.svg', dark: 'file:../mcp.dark.svg', }, group: ['transform'], version: 1, defaults: { name: 'MCP Client', }, credentials: descriptions_1.credentials, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], properties: [ (0, descriptions_1.transportSelect)({ defaultOption: 'httpStreamable', }), { displayName: 'MCP Endpoint URL', name: 'endpointUrl', type: 'string', default: '', placeholder: 'e.g. https://my-mcp-server.ai/mcp', required: true, description: 'The URL of the MCP server to connect to', }, { 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', }, { displayName: 'Credentials', name: 'credentials', type: 'credentials', default: '', displayOptions: { show: { authentication: ['headerAuth', 'bearerAuth', 'mcpOAuth2Api', 'multipleHeadersAuth'], }, }, }, { displayName: 'Tool', name: 'tool', type: 'resourceLocator', default: { mode: 'list', value: '' }, required: true, description: 'The tool to use', modes: [ { displayName: 'From List', name: 'list', type: 'list', typeOptions: { searchListMethod: 'getTools', searchable: true, skipCredentialsCheckInRLC: true, }, }, { displayName: 'ID', name: 'id', type: 'string', }, ], }, { displayName: 'Input Mode', name: 'inputMode', type: 'options', default: 'manual', noDataExpression: true, options: [ { name: 'Manual', value: 'manual', description: 'Manually specify the input data for each tool parameter', }, { name: 'JSON', value: 'json', description: 'Specify the input data as a JSON object', }, ], }, { displayName: 'Parameters', name: 'parameters', type: 'resourceMapper', default: { mappingMode: 'defineBelow', value: null, }, noDataExpression: true, required: true, typeOptions: { loadOptionsDependsOn: ['tool.value'], resourceMapper: { resourceMapperMethod: 'getToolParameters', hideNoDataError: true, addAllFields: false, supportAutoMap: false, mode: 'add', fieldWords: { singular: 'parameter', plural: 'parameters', }, }, }, displayOptions: { show: { inputMode: ['manual'], }, }, }, { displayName: 'JSON', name: 'jsonInput', type: 'json', typeOptions: { rows: 5, }, default: '{\n "my_field_1": "value",\n "my_field_2": 1\n}\n', validateType: 'object', displayOptions: { show: { inputMode: ['json'], }, }, }, { displayName: 'Options', name: 'options', placeholder: 'Add Option', description: 'Additional options to add', type: 'collection', default: {}, options: [ { displayName: 'Convert to Binary', name: 'convertToBinary', type: 'boolean', default: true, description: 'Whether to convert images and audio to binary data. If false, images and audio will be returned as base64 encoded strings.', }, { displayName: 'Timeout', name: 'timeout', type: 'number', typeOptions: { minValue: 1, }, default: 60000, description: 'Time in ms to wait for tool calls to finish', }, ], }, ], }; this.methods = { listSearch, resourceMapping, }; } async execute() { const authentication = this.getNodeParameter('authentication', 0); const serverTransport = this.getNodeParameter('serverTransport', 0); const endpointUrl = this.getNodeParameter('endpointUrl', 0); const node = this.getNode(); const { headers } = await (0, utils_1.getAuthHeaders)(this, authentication); const client = await (0, utils_1.connectMcpClient)({ serverTransport, endpointUrl, headers, name: node.type, version: node.typeVersion, onUnauthorized: async (headers) => await (0, utils_1.tryRefreshOAuth2Token)(this, authentication, headers), }); if (!client.ok) { throw (0, utils_1.mapToNodeOperationError)(node, client.error); } try { const inputMode = this.getNodeParameter('inputMode', 0, 'manual'); const items = this.getInputData(); const returnData = []; for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { try { const tool = this.getNodeParameter('tool.value', itemIndex); const options = this.getNodeParameter('options', itemIndex); let parameters = {}; if (inputMode === 'manual') { parameters = this.getNodeParameter('parameters.value', itemIndex); } else { parameters = this.getNodeParameter('jsonInput', itemIndex); } const result = (await client.result.callTool({ name: tool, arguments: parameters, }, undefined, { timeout: options.timeout ? Number(options.timeout) : undefined, })); let binaryIndex = 0; const binary = {}; const content = []; const convertToBinary = options.convertToBinary ?? true; for (const contentItem of result.content) { if (contentItem.type === 'text') { content.push({ ...contentItem, text: (0, n8n_workflow_1.jsonParse)(contentItem.text, { fallbackValue: contentItem.text }), }); continue; } if (convertToBinary && (contentItem.type === 'image' || contentItem.type === 'audio')) { binary[`data_${binaryIndex}`] = await this.helpers.prepareBinaryData(Buffer.from(contentItem.data, 'base64'), undefined, contentItem.mimeType); binaryIndex++; continue; } content.push(contentItem); } const outputJson = {}; if ((0, utils_1.isStructuredContent)(result.structuredContent)) { outputJson.structuredContent = { ...result.structuredContent }; } if (content.length > 0) { outputJson.content = content; } returnData.push({ json: outputJson, binary: Object.keys(binary).length > 0 ? binary : undefined, pairedItem: { item: itemIndex, }, }); } catch (e) { const errorMessage = e instanceof zod_1.ZodError ? (0, core_1.prettifyError)(e) : e instanceof Error ? e.message : String(e); if (this.continueOnFail()) { returnData.push({ json: { error: { message: errorMessage, issues: e instanceof zod_1.ZodError ? e.issues : undefined, }, }, pairedItem: { item: itemIndex, }, }); continue; } throw new n8n_workflow_1.NodeOperationError(node, errorMessage, { itemIndex, }); } } return [returnData]; } finally { await client.result.close(); } } } exports.McpClient = McpClient; //# sourceMappingURL=McpClient.node.js.map