如何设置 n8n MCP Server (2026)
通过 MCP 将 n8n 自动化工作流连接至 AI Agent
开始使用 Hypereal 构建
通过单个 API 访问 Kling、Flux、Sora、Veo 等。免费积分开始,扩展到数百万。
无需信用卡 • 10万+ 开发者 • 企业级服务
如何设置 n8n MCP Server (2026)
Model Context Protocol (MCP) 允许像 Claude Code、Cursor 和 Cline 这样的 AI Agent 连接到外部工具和服务。通过将 n8n 设置为 MCP server,你可以让你的 AI 助手访问数百个集成——从 Slack 和 Google Sheets 到数据库和自定义 API——这一切都通过 n8n 的可视化工作流构建器实现。
本指南将带你从零开始设置 n8n MCP server,将其连接到 AI 工具,并构建实用的自动化工作流。
什么是 MCP,为什么要选择 n8n?
Model Context Protocol (MCP)
MCP 是一个开放标准(由 Anthropic 创建),它定义了 AI 模型如何连接到外部工具。MCP 不再需要硬编码集成,而是让你将工具公开为一个标准化的接口,任何兼容的 AI Agent 都可以使用。
为什么在 MCP 中使用 n8n?
n8n 是一个工作流自动化平台(类似于 Zapier,但可自托管且开源),拥有 400 多个内置集成。通过将 n8n 工作流公开为 MCP 工具,你的 AI Agent 将获得以下访问权限:
| 类别 | 集成示例 |
|---|---|
| 通讯 | Slack, Discord, Email, Telegram |
| 生产力 | Google Sheets, Notion, Airtable, Todoist |
| 开发 | GitHub, GitLab, Jira, Linear |
| 数据库 | PostgreSQL, MySQL, MongoDB, Supabase |
| 云服务 | AWS, GCP, Azure, Cloudflare |
| 金融 | Stripe, QuickBooks, PayPal |
| CRM | Salesforce, HubSpot, Pipedrive |
| 自定义 | 任何 REST API, webhooks, 脚本 |
前提条件
- 已安装 Docker 和 Docker Compose
- 用于 MCP bridge 的 Node.js 18+
- 一个支持 MCP 的 AI 工具 (Claude Code, Cursor, Cline 等)
- 对 n8n 有基本了解(可选,我们会涵盖基础知识)
第 1 步:部署 n8n
运行 n8n 最简单的方法是使用 Docker:
# 为 n8n 数据创建目录
mkdir -p ~/n8n-data
# 使用 Docker 运行 n8n
docker run -d \
--name n8n \
-p 5678:5678 \
-v ~/n8n-data:/home/node/.n8n \
-e N8N_SECURE_COOKIE=false \
-e WEBHOOK_URL=http://localhost:5678/ \
n8nio/n8n:latest
或者使用 Docker Compose 进行更稳健的设置:
# docker-compose.yml
version: '3.8'
services:
n8n:
image: n8nio/n8n:latest
ports:
- "5678:5678"
environment:
- N8N_SECURE_COOKIE=false
- WEBHOOK_URL=http://localhost:5678/
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=admin
- N8N_BASIC_AUTH_PASSWORD=changeme
volumes:
- n8n_data:/home/node/.n8n
restart: unless-stopped
volumes:
n8n_data:
docker compose up -d
打开 http://localhost:5678 访问 n8n 编辑器。
第 2 步:为 MCP 创建 n8n 工作流
每个你想作为 MCP 工具公开的 n8n 工作流都需要一个 Webhook 触发器。这就是 MCP bridge 调用该工作流的方式。
示例 1:Slack 消息发送器
- 在 n8n 中,点击 Add Workflow
- 添加一个 Webhook 节点作为触发器:
- Method: POST
- Path:
/mcp/send-slack-message
- 添加一个 Slack 节点:
- Action: Send Message
- Channel:
{{ $json.channel }} - Message:
{{ $json.message }}
- 激活 workflow (Activate)
使用 curl 测试:
curl -X POST http://localhost:5678/webhook/mcp/send-slack-message \
-H "Content-Type: application/json" \
-d '{"channel": "#general", "message": "Hello from MCP!"}'
示例 2:Google Sheets 查询
- 创建一个带有 Webhook 触发器的新工作流:
- Method: POST
- Path:
/mcp/lookup-spreadsheet
- 添加一个 Google Sheets 节点:
- Action: Read Rows
- Spreadsheet ID: 你的表格-id
- Range:
Sheet1!A:Z
- 添加一个 Filter 节点来匹配查询请求
- 添加一个 Respond to Webhook 节点来返回结果
- 激活 workflow
示例 3:数据库查询
Webhook (POST /mcp/query-database)
→ PostgreSQL (Execute Query)
→ Respond to Webhook (Return Results)
第 3 步:安装 n8n MCP Bridge
MCP bridge 在 MCP 协议和 n8n webhook 调用之间进行转换。安装社区 bridge 包:
npm install -g @anthropic-ai/mcp-server-n8n
或者使用社区包:
npm install -g n8n-mcp-server
第 4 步:配置 MCP Server
创建一个配置文件,将 n8n 工作流映射到 MCP 工具:
{
"n8n_base_url": "http://localhost:5678",
"n8n_api_key": "your-n8n-api-key",
"tools": [
{
"name": "send_slack_message",
"description": "Send a message to a Slack channel",
"webhook_path": "/webhook/mcp/send-slack-message",
"parameters": {
"channel": {
"type": "string",
"description": "Slack channel name (e.g., #general)",
"required": true
},
"message": {
"type": "string",
"description": "Message text to send",
"required": true
}
}
},
{
"name": "lookup_spreadsheet",
"description": "Look up data in the team Google Spreadsheet",
"webhook_path": "/webhook/mcp/lookup-spreadsheet",
"parameters": {
"query": {
"type": "string",
"description": "Search term to look up",
"required": true
}
}
},
{
"name": "query_database",
"description": "Run a read-only SQL query against the project database",
"webhook_path": "/webhook/mcp/query-database",
"parameters": {
"sql": {
"type": "string",
"description": "SQL SELECT query to execute",
"required": true
}
}
},
{
"name": "create_github_issue",
"description": "Create a GitHub issue in the project repository",
"webhook_path": "/webhook/mcp/create-github-issue",
"parameters": {
"title": {
"type": "string",
"description": "Issue title",
"required": true
},
"body": {
"type": "string",
"description": "Issue description in markdown",
"required": true
},
"labels": {
"type": "string",
"description": "Comma-separated labels",
"required": false
}
}
}
]
}
将其保存为 ~/.config/n8n-mcp/config.json。
第 5 步:连接到 Claude Code
将 n8n MCP server 添加到 Claude Code 的 MCP 配置中。
编辑 ~/.claude/mcp_servers.json:
{
"mcpServers": {
"n8n": {
"command": "n8n-mcp-server",
"args": ["--config", "/Users/yourname/.config/n8n-mcp/config.json"]
}
}
}
重启 Claude Code:
claude
Claude Code 现在可以访问你的 n8n 工具了。测试一下:
You: 向 #dev-updates 发送一条 Slack 消息,内容为 "v2.1.0 部署完成"
Claude Code 将调用 send_slack_message 工具,触发 n8n 工作流,从而发送 Slack 消息。
第 6 步:连接到 Cursor 或 Cline
Cursor MCP 配置
在 Cursor 中,前往 Settings > MCP Servers 并添加:
{
"n8n": {
"command": "n8n-mcp-server",
"args": ["--config", "/Users/yourname/.config/n8n-mcp/config.json"]
}
}
Cline MCP 配置
在 Cline 的设置面板中,添加 MCP server:
{
"mcpServers": {
"n8n": {
"command": "n8n-mcp-server",
"args": ["--config", "/Users/yourname/.config/n8n-mcp/config.json"]
}
}
}
实际应用案例
案例 1:AI 驱动的部署通知
当 Claude Code 完成一项任务时,它可以通知你的团队:
You: 修复 src/api/users.ts 中的分页 bug,提交修复代码,
并向 #dev-updates 发送一条包含摘要的 Slack 消息。
Claude Code 将:
- 修复代码
- 创建 Git commit
- 调用
send_slack_messageMCP 工具通知团队
案例 2:数据驱动开发
You: 在电子表格中查找收入前 10 名的客户,
并创建一个显示它们的仪表板组件。
Claude Code 将:
- 调用
lookup_spreadsheet获取数据 - 使用真实数据结构生成 React 组件
案例 3:故障分类自动化
You: 审查错误日志,识别根本原因并修复它,
然后创建一个 GitHub issue 记录发生的情况。
案例 4:基于数据库信息的编码
You: 查询数据库中 users 表的 schema,
并生成一个匹配它的 TypeScript 接口。
构建更复杂的 n8n 工作流
多步工作流:Bug 报告流水线
Webhook (POST /mcp/report-bug)
→ Create GitHub Issue
→ Create Jira Ticket
→ Send Slack Notification to #bugs
→ Log to Google Sheet
→ Respond to Webhook (Return Issue URL)
这个单一的 MCP 工具触发了一个跨越四个服务的完整 bug 报告流水线。
带有 MCP 触发器的定时工作流
将 MCP 触发的工作流与 n8n 的调度功能结合:
n8n Cron (每天上午 9 点)
→ 查询数据库错误
→ 如果错误数 > 阈值
→ 通过 Slack 发送告警
→ 创建摘要报告
安全最佳实践
| 实践方法 | 实现方式 |
|---|---|
| 使用 API 密钥 | 设置 N8N_BASIC_AUTH_ACTIVE=true 并使用凭据 |
| 限制 SQL 查询 | 在数据库工具中仅允许 SELECT 语句 |
| 验证输入 | 在 n8n 工作流中添加验证节点 |
| 使用 HTTPS | 将 n8n 放在带有 TLS 的反向代理之后 |
| 限制网络权限 | 在内部网络运行 n8n,不要公开暴露 |
| 审计日志 | 为所有 MCP 触发的工作流启用 n8n 执行日志 |
故障排除
| 问题 | 解决方案 |
|---|---|
| "MCP server not found" | 验证 server 是否已安装:which n8n-mcp-server |
| "Connection refused" | 检查 n8n 是否正在运行:docker ps |
| "Webhook not found" | 确保工作流在 n8n 中已激活 (Activated) |
| "Authentication failed" | 验证 MCP 配置中的 API key 是否与 n8n 匹配 |
| 工具未出现 | 修改配置后重启 Claude Code / Cursor |
| 超时错误 | 在 n8n webhook 节点设置中增加超时时间 |
结论
将 n8n 设置为 MCP server 可将你的 AI 编码助手转变为强大的自动化枢纽。你的 AI Agent 不再需要手动切换工具,而是可以直接通过自然语言命令发送 Slack 消息、查询数据库、创建 issue 以及触发复杂的工作流。
如果你的 n8n 自动化工作流涉及图像、视频或音频生成等 AI 媒体任务,Hypereal AI 提供的 REST API 能够与 n8n 的 HTTP request 节点完美集成。通过你连接了 MCP 的 AI 助手,即可构建端到端的 AI 媒体流水线。
