AutomationFlowsAI & RAG › Conversational AI Agent with Custom JavaScript Tool

Conversational AI Agent with Custom JavaScript Tool

Original n8n title: Ai: Conversational Agent with Custom Tool Written in Javascript

Byn8n Team @n8n-team on n8n.io

This workflow implements a custom tool via JavaScript code which returns a random color to users and excludes the given colors.

Event trigger★★☆☆☆ complexityAI-powered6 nodesChat TriggerAgentOpenAI ChatTool Code
AI & RAG Trigger: Event Nodes: 6 Complexity: ★★☆☆☆ AI nodes: yes Added:

This workflow corresponds to n8n.io template #1963 — we link there as the canonical source.

This workflow follows the Agent → Chat Trigger recipe pattern — see all workflows that pair these two integrations.

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "912b279c-30e5-4991-92ab-040fc1e89c7a",
      "name": "When clicking \u2018Test workflow\u2019",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -60,
        0
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "749d8762-d213-4dd3-b404-4c6518fcd28f",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -60,
        200
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "1eeff044-b914-40f7-8d37-8b69007862cd",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        460,
        0
      ],
      "parameters": {
        "text": "={{ $json.chatInput }}",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 1.8
    },
    {
      "id": "ac34f7f0-d1dc-4ffb-8f49-6ddc925e97bc",
      "name": "Debug Input",
      "type": "n8n-nodes-base.set",
      "position": [
        160,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "25d97d59-b0cf-46ae-916d-18059b3d6847",
              "name": "chatInput",
              "type": "string",
              "value": "Return a random color but not green or blue"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "a410a0a5-1ea1-4ade-a32c-8f6fd959bae8",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        440,
        200
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "923b1597-2e9c-4c38-b3bb-7d6dffb52e4a",
      "name": "Code Tool",
      "type": "@n8n/n8n-nodes-langchain.toolCode",
      "position": [
        660,
        200
      ],
      "parameters": {
        "name": "my_color_selector",
        "jsCode": "const colors = [\n    'red',\n    'green',\n    'blue',\n    'yellow',\n    'pink',\n    'white',\n    'black',\n    'orange',\n    'brown',\n];\n\nconst ignoreColors = query.split(',').map((text) => text.trim());\n\n// remove all the colors that should be ignored\nconst availableColors = colors.filter((color) => {\n    return !ignoreColors.includes(color);\n});\n\n// Select a random color\nreturn availableColors[Math.floor(Math.random() * availableColors.length)];\n",
        "description": "Call this tool to get a random color. The input should be a string with comma-separated names of colors to exclude."
      },
      "typeVersion": 1.1
    }
  ],
  "connections": {
    "Code Tool": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Debug Input": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Test workflow\u2019": {
      "main": [
        [
          {
            "node": "Debug Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Credentials you'll need

Each integration node will prompt for credentials when you import. We strip credential IDs before publishing — you'll add your own.

Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

This workflow implements a custom tool via JavaScript code which returns a random color to users and excludes the given colors.

Source: https://n8n.io/workflows/1963/ — original creator credit. Request a take-down →

More AI & RAG workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

AI & RAG

Manual. Uses manualTrigger, chatTrigger, agent, lmChatOpenAi. Event-driven trigger; 6 nodes.

Chat Trigger, Agent, OpenAI Chat +1
AI & RAG

This beginner-friendly workflow demonstrates the core building blocks of n8n. It guides you through: Triggers – Start workflows manually, on a schedule, via webhooks, or through chat. Data processing

HTTP Request, Chat Trigger, Memory Buffer Window +3
AI & RAG

Query N8N Credentials With Ai Sql Agent. Uses manualTrigger, stickyNote, chatTrigger, toolCode. Event-driven trigger; 13 nodes.

Chat Trigger, Tool Code, OpenAI Chat +3
AI & RAG

Manual N8N Export. Uses manualTrigger, stickyNote, chatTrigger, toolCode. Event-driven trigger; 13 nodes.

Chat Trigger, Tool Code, OpenAI Chat +3
AI & RAG

This n8n workflow is a fun way to query and search over your credentials on your n8n instance.

Chat Trigger, Tool Code, OpenAI Chat +3