@n8n/n8n-nodes-langchain
Version:

120 lines • 4.02 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var utils_exports = {};
__export(utils_exports, {
McpToolkit: () => McpToolkit,
createCallTool: () => createCallTool,
getErrorDescriptionFromToolCall: () => getErrorDescriptionFromToolCall,
getSelectedTools: () => getSelectedTools,
mcpToolToDynamicTool: () => mcpToolToDynamicTool
});
module.exports = __toCommonJS(utils_exports);
var import_tools = require("@langchain/core/tools");
var import_types = require("@modelcontextprotocol/sdk/types.js");
var import_agents = require("@langchain/classic/agents");
var import_zod = require("zod");
var import_schemaParsing = require("../../../utils/schemaParsing");
function getSelectedTools({
mode,
includeTools,
excludeTools,
tools
}) {
switch (mode) {
case "selected": {
if (!includeTools?.length) return tools;
const include = new Set(includeTools);
return tools.filter((tool) => include.has(tool.name));
}
case "except": {
const except = new Set(excludeTools ?? []);
return tools.filter((tool) => !except.has(tool.name));
}
case "all":
default:
return tools;
}
}
const getErrorDescriptionFromToolCall = (result) => {
if (result && typeof result === "object") {
if ("content" in result && Array.isArray(result.content)) {
const errorMessage = result.content.find(
(content) => content && typeof content === "object" && typeof content.text === "string"
)?.text;
return errorMessage;
} else if ("toolResult" in result && typeof result.toolResult === "string") {
return result.toolResult;
}
if ("message" in result && typeof result.message === "string") {
return result.message;
}
}
return void 0;
};
const createCallTool = (name, client, timeout, onError) => async (args) => {
let result;
function handleError(error) {
const errorDescription = getErrorDescriptionFromToolCall(error) ?? `Failed to execute tool "${name}"`;
onError(errorDescription);
return errorDescription;
}
try {
result = await client.callTool({ name, arguments: args }, import_types.CompatibilityCallToolResultSchema, {
timeout
});
} catch (error) {
return handleError(error);
}
if (result.isError) {
return handleError(result);
}
if (result.toolResult !== void 0) {
return result.toolResult;
}
if (result.content !== void 0) {
return result.content;
}
return result;
};
function mcpToolToDynamicTool(tool, onCallTool) {
const rawSchema = (0, import_schemaParsing.convertJsonSchemaToZod)(tool.inputSchema);
const objectSchema = rawSchema instanceof import_zod.z.ZodObject ? rawSchema : import_zod.z.object({ value: rawSchema });
return new import_tools.DynamicStructuredTool({
name: tool.name,
description: tool.description ?? "",
schema: objectSchema,
func: onCallTool,
metadata: { isFromToolkit: true }
});
}
class McpToolkit extends import_agents.Toolkit {
constructor(tools) {
super();
this.tools = tools;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
McpToolkit,
createCallTool,
getErrorDescriptionFromToolCall,
getSelectedTools,
mcpToolToDynamicTool
});
//# sourceMappingURL=utils.js.map