@n8n/n8n-nodes-langchain
Version:

335 lines • 10.9 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var McpClient_node_exports = {};
__export(McpClient_node_exports, {
McpClient: () => McpClient
});
module.exports = __toCommonJS(McpClient_node_exports);
var import_n8n_workflow = require("n8n-workflow");
var import_zod = require("zod");
var import_core = require("zod/v4/core");
var listSearch = __toESM(require("./listSearch"));
var resourceMapping = __toESM(require("./resourceMapping"));
var import_descriptions = require("../shared/descriptions");
var import_utils = 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: import_descriptions.credentials,
inputs: [import_n8n_workflow.NodeConnectionTypes.Main],
outputs: [import_n8n_workflow.NodeConnectionTypes.Main],
properties: [
(0, import_descriptions.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: 6e4,
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, import_utils.getAuthHeaders)(this, authentication);
const client = await (0, import_utils.connectMcpClient)({
serverTransport,
endpointUrl,
headers,
name: node.type,
version: node.typeVersion,
onUnauthorized: async (headers2) => await (0, import_utils.tryRefreshOAuth2Token)(this, authentication, headers2)
});
if (!client.ok) {
throw (0, import_utils.mapToNodeOperationError)(node, client.error);
}
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
},
void 0,
{
timeout: options.timeout ? Number(options.timeout) : void 0
}
);
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, import_n8n_workflow.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"),
void 0,
contentItem.mimeType
);
binaryIndex++;
continue;
}
content.push(contentItem);
}
returnData.push({
json: {
content: content.length > 0 ? content : void 0
},
binary: Object.keys(binary).length > 0 ? binary : void 0,
pairedItem: {
item: itemIndex
}
});
} catch (e) {
const errorMessage = e instanceof import_zod.ZodError ? (0, import_core.prettifyError)(e) : e instanceof Error ? e.message : String(e);
if (this.continueOnFail()) {
returnData.push({
json: {
error: {
message: errorMessage,
issues: e instanceof import_zod.ZodError ? e.issues : void 0
}
},
pairedItem: {
item: itemIndex
}
});
continue;
}
throw new import_n8n_workflow.NodeOperationError(node, errorMessage, {
itemIndex
});
}
}
return [returnData];
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
McpClient
});
//# sourceMappingURL=McpClient.node.js.map