自动化与脚本
在 n8n AI Agent 的 Custom Code…
适用于 @n8n/n8n-nodes-langchain.toolCode——AI Agent 可调用的代码工具,不是工作流 Code 节点;需要返回字符串、读取 query/_query,或为 LLM 定义结构化参数 schema。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 59/100 ×0.15
适用场景
在 n8n AI Agent 的 Cus特定
解决思路
- 确认节点类型是 @n8n/n8n-nodes-langchain.toolCode,包为 @n8n/n8n-nodes-langchain;不要按 n8n-nodes-base.code 的 Code node 约定写。
- 记住调用方是 AI Agent(LangChain),输入来自 LLM 的 query(JS)或 _query(Python),返回必须是字符串。
- JS 中从固定变量 query 读取输入;Python 中从固定变量 _query 读取输入;不要重命名输入变量。
- 返回字符串;数字会被 n8n 自动转换为字符串;需要结构化结果时使用 JSON.stringify({...})。
- 不要返回 [{json:{...}}];这是 Code node 格式,会报 Wrong output type returned。
- 不要返回原始对象或数组;会报 The response property should be a string, but it is an object。
- 默认 unstructured 模式 specifyInputSchema:false 下,AI 传单个字符串;若需多字段,在代码中用 JSON.parse(query) 解析,并用 try/catch 捕获无效 JSON。
- unstructured 模式下,在工具 description 中写清楚如何调用,并包含 LLM 应发送的 JSON 字符串示例。
- structured 模式 specifyInputSchema:true 下,工具变为 LangChain DynamicStructuredTool;LLM 传经过验证的对象,直接读 query.price、query.months 等字段。
- structured schema 可用 schemaType:"fromJson" + jsonSchemaExample(n8n v≥1.3,粘贴示例 JSON 让 n8n 推断 schema)。
- structured schema 也可用 schemaType:"manual" + inputSchema,自行编写完整 JSON Schema。
- 多个类型化参数的生产工具优先用 structured 模式,避免 LLM 将数字字符串化或漏字段。
- 工具名必须匹配 [A-Za-z0-9_]+(v1.1+),无空格、无连字符、无 emoji。
- 使用动词性、可描述的名字如 calculate_car_loan、get_weather、search_orders;不要用 Code Tool / My Tool。
- 工具 description 要说明何时使用、发送什么;unstructured 模式包含 JSON 示例;structured 模式说明用途即可。
- 错误处理可 throw Error,n8n 会把消息返回给 agent;也可返回 JSON 字符串如 {"error":"..."} 作为工具结果。
- 错误消息写给 LLM:说明哪里错、合法调用长什么样,例如 price must be a number, e.g. 439900。
- 错误 Cannot assign to read only property 'name' / No execution data available:原因是代码体内调用 $fromAI();改为直接读取 query,或用 specifyInputSchema 处理结构化参数。
- 错误 Wrong output type returned:原因是返回了 [{json:{...}}] 工作流数组;改为返回字符串或 JSON.stringify。
- 错误 The response property should be a string, but it is an object:原因是返回普通对象或数组;改为 JSON.stringify 或字符串化。
- AI 从不调用工具:把工具名改为动词性名称,并在 description 中明确触发条件。
- AI 向 query 发送垃圾数据:在 description 中加入明确 JSON 示例,或切换到 specifyInputSchema:true 给 LLM 类型化 schema。
- 不要假设 Code node 的 helper 可用:Code Tool sandbox 不提供 $input.all/$input.first/$input.item、$node["NodeName"]、$json、$binary、$fromAI()、this.helpers.httpRequest()、$jmespath()、this.getContext(...)、$getWorkflowStaticData(...);DateTime(Luxon) 在 JS sandbox 可用。
- 将 Code Tool 限定为纯计算、解析、格式化、验证;需要 HTTP 调用时改用 HTTP Request Tool。
- 需要多个参数且有干净 $fromAI() 类型、访问 this.helpers、credentials 或其他节点、跨 agent 复用、或不想手写 JSON Schema 的结构化输入时,改用 toolWorkflow(Call Sub-workflow Tool)。
- 工具本质是单次 API 调用且需要按参数在 URL/query/body 绑定 $fromAI() 时,改用 HTTP Request Tool。
- 经验规则:如果发现自己想在 toolCode 中用 $fromAI(),大概率应改用 toolWorkflow。
- 通过 ai_tool 连接类型把 Code Tool 接入 AI Agent。
- 部署前检查:节点类型正确、工具名描述性 snake_case、description 说明触发条件与示例、从 query/_query 读取、无 $fromAI、无 $input/$json/$helpers、返回字符串、通过 ai_tool 连接接入 AI Agent、用 LLM 实际会发送的输入测试。
- 完整示例模式:配置 type 为 @n8n/n8n-nodes-langchain.toolCode、typeVersion 为 1.3、name 为 calculate_car_loan;jsCode 解析 query、校验参数、计算 annuity、返回 JSON.stringify 结果。
适用边界 · 注意事项
- 需要外部 HTTP/API 调用、凭证、访问其他节点、跨调用状态或多步可复用逻辑时,不用 Code Tool;改用 HTTP Request Tool、toolWorkflow、MCP 或数据库工具。
- 需要按参数绑定 $fromAI(),或需要工作流 Code node 的 $input/$json/$helpers 时,不用 Code Tool。
- 非 n8n AI Agent/LangChain 工具场景不适用;它专用于 @n8n/n8n-nodes-langchain.toolCode。