JavaScript is disabled. Some features may not work.
n8n-code-tool — ★ 5.5K GitHub Stars — Install Guide | SkillsNav
🇺🇸 English🇨🇳 中文
SkillsNav
Home

n8n-code-tool

★ 5.5K repogenerationN/AIntermediateClaude
🤖 AI Summary

This skill provides expert guidance on writing code specifically for the `@n8n/n8n-nodes-langchain.toolCode` node, which is invoked by an AI Agent (not the regular workflow Code node). It clarifies the distinct runtime contract: the tool receives a `query` string/object from the LLM and must return data in the `[{json: {...}}]` format.

How to Install

Claude Code:
git clone --depth 1 https://github.com/czlonkowski/n8n-skills.git && cp n8n-skills/skills/n8n-code-tool ~/.claude/skills/n8n-code-tool -r
# n8n Custom Code Tool Expert guidance for writing code inside `@n8n/n8n-nodes-langchain.toolCode` — the tool an AI Agent can invoke, **not** the regular workflow Code node. --- ## ⚠️ This is NOT the Code node The Custom Code Tool looks like a Code node in the editor — same JavaScript editor, similar layout — but it is a **completely different node** from a different package with a **different runtime contract**. | | Code node | Custom Code Tool | |---|---|---| | **Node type** | `n8n-nodes-base.code` | `@n8n/n8n-nodes-langchain.toolCode` | | **Package** | `n8n-nodes-base` | `@n8n/n8n-nodes-langchain` | | **Invoked by** | Previous node (workflow flow) | AI Agent (LangChain) | | **Input** | `$input.all()` — item stream | `query` — string or object from LLM | | **Return** | `[{json: {...}}]` (items array) | **A string** | | **`$fromAI()`** | N/A | **Not available** (see Errors) | | **`$helpers`** | Full helpers incl. httpRequest | Not exposed to the tool sandbox | | **State** | Per-run execution data | No `getContext`, no `$getWorkflowStaticData` | **If you treat it like a Code node, it fails.** The rest of this skill covers the Code Tool's actual contract. --- ## Quick Start ### Minimal JavaScript Code Tool ```javascript // `query` is whatever the AI sent (a string by default) return `You asked: ${query}`; ``` ### Minimal Python Code Tool ```python # `_query` is whatever the AI sent (a string by default) return f"You asked: {_query}" ``` ### Essential Rules 1. **Return a string.** Numbers are auto-converted. Anything else throws `"The response property should be a string, but it is an object"`. 2. **Input variable is fixed**: `query` (JS), `_query` (Python). You cannot rename it. 3. **Do NOT use `$fromAI()`** inside the Code Tool sandbox — it throws `"No execution data available"`. 4. **Do NOT use `[{json: {...}}]`** return format — that's for Code nodes. Throws `"Wrong output type returned"`. 5. **Use a descriptive tool name** (letters/numbers/underscores, v1.1+). The agent calls the tool by its name. 6. **Write a precise description** — the LLM decides whether to invoke the tool based on it. --- ## The Two Input Modes The Code Tool has two input shapes, controlled by `specifyInputSchema`: ### Mode 1: Unstructured (default, `specifyInputSchema: false`) The AI passes **a single string** as `query`. If you need multiple fields, the AI has to stuff them into that one string and you parse them out. In practice, LLMs will happily pass a JSON string if your description tells them to. ```javascript // Parse a JSON string the AI sent let params; try { params = typeof query === 'string' ? JSON.parse(query) : query; } catch (e) { throw new Error('Expected a JSON object. Parser said: ' + e.message); } const price = Number(params.price); const months = Number(params.months); // ... return JSON.stringify({ monthly_payment: /* ... */ }); ``` **Pros**: simplest to set up, one field to describe. **Cons**: no schema validation — if the LLM

Details

Category Coding → generation
Sourceczlonkowski/n8n-skills
SKILL.mdView on GitHub →
Repo Stars★ 5.5K
Est. per Skill369 (shared across 15 skills from this repo)
DifficultyIntermediate
Risk LevelN/A

Related Skills

Works Well With

Skills from the same repository — often designed to work together