AI & Automation (vnROM)

Cover image for Biến OpenClaw / LobeHub thành "Senior n8n Engineer"
Duy Nghiện
Duy Nghiện

Posted on

Biến OpenClaw / LobeHub thành "Senior n8n Engineer"

Hello anh em 👋

Bài viết này dành cho ai đang dùng LobeHub / OpenClaw / Claude Code kết hợp với n8n và muốn AI agent của mình có thể tự tạo, sửa, xoá, chạy, debug workflow - tất cả qua giao thức MCP (Model Context Protocol), không cần mở trình duyệt n8n.

Mình sẽ chia sẻ toàn bộ quá trình từ phát hiện vấn đề → nghiên cứu → tự build giải pháp → triển khai thành công trên Raspberry Pi 4 chạy Docker.

Vấn đề: MCP mặc định thiếu gì?

Nếu AE đang dùng czlonkowski/n8n-mcp - một dự án MCP Server khá phổ biến cho n8n - thì AE sẽ nhận ra nó chỉ cho phép đọc và chạy workflow, cụ thể:

Thao tác n8n-mcp (của czlonkowski) AE thực sự cần?
Liệt kê workflow
Xem chi tiết workflow
Chạy (Execute) workflow
Bật/Tắt (Activate)
Tạo mới workflow
Sửa workflow
Xoá workflow
Test Webhook
Xem lịch sử chạy (Debug)

Nói cách khác: AI của AE có tay nhưng bị trói - biết nhìn, biết bấm nút "Run", nhưng không thể tự tạo hay sửa bất cứ thứ gì. Mỗi lần muốn thay đổi workflow, AE vẫn phải mở trình duyệt, vào n8n Editor, kéo thả bằng tay.

Điều này khá mất tay khi AE đang test các luồng phức tạp như Yandex Mail hay Microsoft Outlook - nơi AE cần liên tục tạo webhook, chỉnh sửa, trigger test, rồi debug lỗi. Làm thủ công sẽ mất hàng giờ, mà lại con 'non tech' nữa thì... :D


Giải pháp: n8n-custom-mcp là gì?

n8n-custom-mcp là MCP Server tự build, chạy bằng Docker, cung cấp 12 công cụ (tools) để AI agent trên LobeHub / OpenClaw / Claude Code quản lý n8n một cách toàn diện.

Bảng so sánh năng lực đầy đủ

Nhóm Tool Mô tả
Workflow CRUD list_workflows Liệt kê tất cả workflow
get_workflow Xem chi tiết JSON (nodes, connections)
create_workflow Tạo mới workflow từ JSON
update_workflow Sửa workflow (đổi tên, thêm/xoá node...)
delete_workflow Xoá workflow
activate_workflow Bật / Tắt workflow
Execution execute_workflow Chạy workflow theo ID
trigger_webhook Gọi webhook (hỗ trợ test mode!)
Monitoring list_executions Xem lịch sử chạy & lọc theo trạng thái
get_execution Debug chi tiết từng lần chạy (dữ liệu vào/ra, lỗi)
Discovery list_node_types Liệt kê các node đang cài trên n8n

Tóm lại: Từ "chỉ biết đọc và chạy", AI agent của AE giờ trở thành một Senior n8n Engineer - tự tạo workflow, tự test, tự debug, tự sửa lỗi. AE chỉ cần ra lệnh bằng tiếng mệ đẻ :))


Kiến trúc tổng quan

┌─────────────────────────────────────────────────────────┐
│                    LobeHub / OpenClaw                   │
│              (AI Agent + System Prompt)                 │
│                                                         │
│   ┌─────────────────┐    ┌───────────────────────────┐  │
│   │  n8n-skills     │    │  MCP Client               │  │
│   │  (Knowledge)    │    │  → http://host:3000/mcp   │  │
│   │  System Prompt  │    └──────────┬────────────────┘  │
│   └─────────────────┘               │                   │
└─────────────────────────────────────┼───────────────────┘
                                      │ HTTP (Streamable)
                    ┌─────────────────▼───────────────────┐
                    │       n8n-custom-mcp (Docker)       │
                    │                                     │
                    │  supergateway ← stdio → Node.js     │
                    │        :3000/mcp                    │
                    │                                     │
                    │  12 Tools (CRUD + Webhook + Debug)  │
                    └─────────────────┬───────────────────┘
                                      │ REST API (nội bộ Docker)
                    ┌─────────────────▼───────────────────┐
                    │         n8n Instance (Docker)       │
                    │              :5678                  │
                    │                                     │
                    │  ┌──────┐  ┌───────┐  ┌──────────┐  │
                    │  │ PgSQL│  │ Redis │  │ Worker   │  │
                    │  └──────┘  └───────┘  └──────────┘  │
                    └─────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

Ví dụ luồng hoạt động:

  1. AE nhắn với AI trên LobeHub: "Tạo một webhook workflow nhận email từ Outlook"
  2. AI dùng MCP gọi create_workflow → n8n tạo workflow mới
  3. AI gọi activate_workflow → Bật workflow
  4. AI gọi trigger_webhook (test mode) → Gửi request giả lập
  5. AI gọi list_executions → Kiểm tra kết quả
  6. Nếu lỗi → AI gọi get_execution → Đọc error → update_workflow → Tự sửa
  7. Lặp lại cho đến khi hoàn hảo

Hướng dẫn cài đặt chi tiết

Yêu cầu hệ thống

  • Docker & Docker Compose
  • n8n instance đang chạy (hoặc sẽ chạy cùng trong docker compose)
  • n8n API Key (tạo trong n8n → Settings → API)

Bước 1: Cấu trúc thư mục

n8n-stack/
├── docker-compose.yml
├── .env                        ← Chứa secrets
├── n8n-custom-mcp/             ← Clone từ GitHub
│   ├── package.json
│   ├── tsconfig.json
│   ├── Dockerfile
│   └── src/
│       └── index.ts            ← Core logic (12 tools)
├── db_data/
├── redis_data/
└── n8n_data/
Enter fullscreen mode Exit fullscreen mode

Bước 2: Clone repo

cd ~/n8n-stack
git clone https://github.com/duynghien/n8n-custom-mcp.git
Enter fullscreen mode Exit fullscreen mode

Bước 3: Tạo file .env

# .env
POSTGRES_USER=n8n
POSTGRES_PASSWORD=your_pg_password_here
POSTGRES_DB=n8n

N8N_ENCRYPTION_KEY=your_encryption_key_here
N8N_HOST=your-domain.com
WEBHOOK_URL=https://your-domain.com/

# Tạo API Key tại: n8n → Settings → API → Create API Key
N8N_API_KEY=your_n8n_api_key_here
Enter fullscreen mode Exit fullscreen mode

Bước 4: Cấu hình docker-compose.yml

services:
  # 1. Database PostgreSQL
  postgres:
    image: postgres:16
    restart: always
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    volumes:
      - ./db_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 5s
      timeout: 5s
      retries: 10

  # 2. Redis (Queue Manager)
  redis:
    image: redis:alpine
    restart: always
    volumes:
      - ./redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 10

  # 3. n8n Main Instance
  n8n:
    image: n8nio/n8n:latest
    restart: always
    ports:
      - "5678:5678"
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
      - EXECUTIONS_MODE=queue
      - QUEUE_BULL_REDIS_HOST=redis
      - QUEUE_BULL_REDIS_PORT=6379
      - WEBHOOK_URL=${WEBHOOK_URL}
      - N8N_HOST=${N8N_HOST}
      - N8N_SECURE_COOKIE=false
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    volumes:
      - ./n8n_data:/home/node/.n8n

  # 4. n8n Worker (xử lý tác vụ nặng)
  n8n-worker:
    image: n8nio/n8n:latest
    restart: always
    command: worker
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_PASSWORD}
      - N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
      - EXECUTIONS_MODE=queue
      - QUEUE_BULL_REDIS_HOST=redis
      - QUEUE_BULL_REDIS_PORT=6379
      - N8N_HOST=${N8N_HOST}
      - N8N_SECURE_COOKIE=false
    depends_on:
      n8n:
        condition: service_started
    volumes:
      - ./n8n_data:/home/node/.n8n

  # 5. n8n-custom-mcp (Cầu nối sức mạnh)
  n8n-mcp:
    build:
      context: ./n8n-custom-mcp
    restart: always
    ports:
      - "3000:3000"
    environment:
      - N8N_HOST=http://n8n:5678
      - N8N_API_KEY=${N8N_API_KEY}
    depends_on:
      n8n:
        condition: service_started
    command: >
      --stdio "node dist/index.js"
      --port 3000
      --outputTransport streamableHttp
      --streamableHttpPath /mcp
      --cors
Enter fullscreen mode Exit fullscreen mode

Bước 5: Build & chạy

docker compose up -d --build
Enter fullscreen mode Exit fullscreen mode

Kiểm tra tất cả container chạy ổn:

docker compose ps
Enter fullscreen mode Exit fullscreen mode

Output mong đợi:

NAME              STATUS
postgres          Up (healthy)
redis             Up (healthy)
n8n               Up
n8n-worker        Up
n8n-mcp           Up
Enter fullscreen mode Exit fullscreen mode

Bước 6: Kết nối với LobeHub/OpenClaw

Trong phần cấu hình MCP Plugin:

  • Type: MCP (Streamable HTTP)
  • URL: http://<IP-máy-chủ>:3000/mcp
  • Ví dụ: http://192.168.1.100:3000/mcp

Sau khi kết nối thành công, AE sẽ thấy 12 tools xuất hiện trong danh sách công cụ của Agent.


Nâng cấp AI Agent bằng n8n-skills

Đến đây AE đã có đôi tay (MCP tools). Giờ hãy thêm bộ não chuyên gia cho Agent.

Repo czlonkowski/n8n-skills cung cấp 7 bộ kiến thức chuyên sâu về n8n. Đây không phải phần mềm cài đặt - mà là tài liệu hướng dẫn để AE nhúng vào System Prompt của Agent.

Cách làm

1. Clone repo:

git clone https://github.com/czlonkowski/n8n-skills.git
Enter fullscreen mode Exit fullscreen mode

2. Mở 3 file SKILL.md quan trọng nhất:

n8n-skills/skills/
├── n8n-mcp-tools-expert/SKILL.md     ← Ưu tiên 1: Dạy AI dùng MCP tools đúng cách
├── n8n-workflow-patterns/SKILL.md    ← Ưu tiên 2: 5 mẫu workflow chuẩn
└── n8n-expression-syntax/SKILL.md    ← Ưu tiên 3: Cú pháp biểu thức n8n
Enter fullscreen mode Exit fullscreen mode

3. Copy nội dung và paste vào System Prompt của Agent trên LobeHub/OpenClaw.

Ví dụ System Prompt tối ưu

# Vai trò
AE là chuyên gia n8n workflow automation. AE có quyền truy cập n8n 
thông qua MCP Server với đầy đủ quyền CRUD, Testing và Debugging.

# Quy tắc quan trọng

## Webhook Data Access
- Dữ liệu webhook LUÔN nằm dưới `$json.body`
- Đúng: {{ $json.body.email }}
- Sai:  {{ $json.email }}

## Workflow Patterns
1. Webhook Processing: Webhook → Process → Respond to Webhook
2. HTTP API: Trigger → HTTP Request → Transform → Output
3. Database: Trigger → Query → Transform → Store
4. AI Agent: Trigger → AI Agent (tools) → Output
5. Scheduled: Schedule → Fetch → Process → Report

## Code Node
- Return format bắt buộc: [{json: {...}}]
- Dùng JavaScript cho 95% use case
- Python trong n8n KHÔNG có thư viện ngoài

## Quy trình làm việc
1. Nhận yêu cầu → Chọn pattern phù hợp
2. create_workflow → tạo workflow
3. activate_workflow → bật workflow
4. trigger_webhook (test_mode: true) → test thử
5. list_executions → kiểm tra kết quả
6. get_execution → nếu lỗi, đọc chi tiết lỗi
7. update_workflow → sửa lỗi
8. Lặp lại bước 4-7 cho đến khi OK
Enter fullscreen mode Exit fullscreen mode

Hiệu quả trước và sau

Tình huống Không có n8n-skills Có n8n-skills
Tạo webhook workflow AI viết $json.emaillỗi undefined AI viết $json.body.emailchạy đúng
Code node trả về data AI return {email: "..."}lỗi format AI return [{json: {email: "..."}}]chuẩn
Chọn pattern workflow AI đoán mò → thiếu node AI chọn đúng 1 trong 5 pattern → đủ node
Gặp lỗi validation AI bó tay, hỏi lại AE AI tự đọc error → tự sửa → tự chạy lại

Demo thực tế: Tạo & Debug Webhook Outlook

Đây là kịch bản thực tế mình đã chạy thành công trên hệ thống:

Bước 1: Ra lệnh cho AI

AE: "Tạo một webhook workflow nhận callback từ Microsoft Outlook. Khi nhận POST request, lấy ra email subject và sender, lưu vào Set node."

Bước 2: AI tự động thực hiện

AI gọi create_workflow:

{
  "name": "Outlook Webhook Handler",
  "nodes": [
    {
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "parameters": { "path": "outlook-callback", "httpMethod": "POST" },
      "position": [250, 300]
    },
    {
      "name": "Extract Data",
      "type": "n8n-nodes-base.set",
      "parameters": {
        "values": {
          "string": [
            { "name": "subject", "value": "={{ $json.body.subject }}" },
            { "name": "sender", "value": "={{ $json.body.sender }}" }
          ]
        }
      },
      "position": [450, 300]
    }
  ],
  "connections": {
    "Webhook": { "main": [[{ "node": "Extract Data", "type": "main", "index": 0 }]] }
  }
}
Enter fullscreen mode Exit fullscreen mode

AI gọi activate_workflow → Bật workflow.

Bước 3: AI tự test

AI gọi trigger_webhook:

{
  "webhook_path": "outlook-callback",
  "method": "POST",
  "test_mode": true,
  "body": {
    "subject": "Meeting Tomorrow",
    "sender": "[email protected]"
  }
}
Enter fullscreen mode Exit fullscreen mode

Bước 4: AI tự kiểm tra kết quả

AI gọi list_executions → Thấy execution mới nhất.

AI gọi get_execution → Đọc output:

{
  "status": "success",
  "data": {
    "Extract Data": {
      "subject": "Meeting Tomorrow",
      "sender": "[email protected]"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

AI trả lời: "Workflow đã chạy thành công! Subject và sender đã được extract chính xác."

Nếu gặp lỗi?

Giả sử AI viết sai expression thành $json.subject (thiếu .body):

  1. get_execution trả về subject: undefined
  2. AI nhận ra lỗi, gọi update_workflow → sửa thành $json.body.subject
  3. trigger_webhook lần 2 → thành công
  4. Toàn bộ quá trình debug tự động, không cần AE can thiệp!

Những gì AE đạt được sau bài viết này

12 MCP Tools - Full CRUD, Testing, Debugging cho n8n
Docker-ready - Chạy 1 lệnh docker compose up -d --build là xong
AI Agent thông minh - Tự tạo, tự test, tự debug workflow
Production-tested - Đã chạy thành công trên Raspberry Pi 4

Repo & Tài nguyên

Tài nguyên Link
n8n-custom-mcp (bài viết này) github.com/duynghien/n8n-custom-mcp
n8n-mcp (của czlonkowski) github.com/czlonkowski/n8n-mcp
n8n-skills (kiến thức) github.com/czlonkowski/n8n-skills

Một vài ý tưởng mở rộng trong tương lai:

  • Thêm tool search_templates - tìm workflow mẫu từ cộng đồng n8n
  • Thêm tool get_credentials - quản lý credentials qua MCP
  • Hỗ trợ SSE transport ngoài Streamable HTTP
  • Viết thêm System Prompt chuyên biệt cho từng use case (CRM, Email Marketing, DevOps...)

Top comments (0)