n8n工作流集成服务器

一个将n8n自动化工作流作为MCP工具暴露的协议服务器,支持与AI助手和其他MCP客户端集成。

  • by trieder83
  • Shell
  • 混合部署
  • 更新于
在 GitHub 查看 trieder83/mcp-n8n-server

使用说明

用于n8n工作流集成的MCP服务器

该项目提供了一个模型上下文协议(MCP)服务器,该服务器使用流式HTTP(服务器发送事件)将n8n工作流作为MCP工具公开。它允许您将n8n自动化工作流与AI助手和其他MCP客户端集成。

!!由于goose_1.12.1_amd64.deb已损坏,请使用goose_1-12.0_amd64.deb

特性

  • 将n8n webhooks作为MCP工具公开
  • 支持具有自定义输入模式的多个工作流
  • 身份验证支持(基本身份验证和自定义标头)
  • Docker容器化
  • 使用NodePort服务进行Kubernetes部署
  • 可通过环境变量或配置文件进行配置
  • MCP通信的SSE(服务器发送事件)传输
  • 与Codename Goose、Claude Desktop和其他MCP客户端兼容

目录

建筑

┌─────────────┐         ┌──────────────┐         ┌─────────┐
│ MCP Client  │ ◄─HTTP─►│  MCP Server  │ ◄─HTTP─► │   n8n   │
│ (AI Model)  │         │   (Python)   │         │         │
│   Goose     │         │  Port 8000   │         │Webhooks │
└─────────────┘         └──────────────┘         └─────────┘
      │                        │                       │
      │ POST /mcp             │ POST /webhook/{id}    │
      │ (JSON-RPC 2.0)        │                       │
      └───────────────────────┘                       │
         Streamable HTTP                              │
                                                      │
                              HTTP POST ──────────────┘

使用代号Goose快速入门

开始使用Codename Goose的最快方法:

  1. 启动MCP服务器:
docker run -d -p 8000:8000 \
  -e N8N_BASE_URL=http://your-n8n-host:31678 \
  -v $(pwd)/workflows.json:/app/workflows.json:ro \
  trieder83/mcp-n8n-server:latest
  1. 配置鹅 (~/.config/goose/config.yaml):
# Edit Goose configuration
goose configure

# Or manually add to ~/.config/goose/config.yaml:
# Add this under 'extensions:' section:
extensions:
  mcp-n8n-server:
    available_tools: []
    bundled: null
    description: n8n workflow integration via MCP
    enabled: true
    env_keys: []
    envs: {}
    headers: {}
    name: mcp-n8n-server
    timeout: 300
    type: streamable_http
    uri: http://localhost:8000/mcp
  1. 开始在Goose中使用工作流:
goose
# Inside Goose:
# "Run the chat_workflow with message 'Hello n8n!'"

看 配置MCP客户端 有关详细设置说明的部分。

配置

工作流配置

工作流通过JSON文件配置(workflows.json)或环境变量。每个工作流都可以指定:

  • name:MCP工具的唯一标识符
  • webhook_id:n8n webhook ID(来自webhook URL)
  • description:工作流程描述
  • input_schema:输入参数的JSON模式
  • required_fields:必填输入字段列表
  • auth_type:身份验证类型(“基本”、“标头”或“无”)
  • basic_auth:基本身份验证的Base64编码凭据
  • auth_header_name:用于身份验证的自定义标头名称
  • auth_header_value:用于身份验证的自定义标头值
  • custom_headers:其他自定义标头(键值对)

工作流配置示例

[
  {
    "name": "chat_workflow",
    "webhook_id": "919df572-8dcd-4cd6-b592-9e90ba0db414",
    "description": "Process chat input through n8n workflow",
    "input_schema": {
      "chatInput": {
        "type": "string",
        "description": "The chat message to process"
      }
    },
    "required_fields": ["chatInput"]
  },
  {
    "name": "secure_workflow",
    "webhook_id": "secure-webhook-id",
    "description": "Workflow with Basic authentication",
    "auth_type": "basic",
    "basic_auth": "dXNlcm5hbWU6cGFzc3dvcmQ=",
    "input_schema": {
      "message": {
        "type": "string",
        "description": "Message to send"
      }
    },
    "required_fields": ["message"]
  }
]

环境变量

变量 描述 默认值
SERVER_HOST 服务器绑定地址 0.0.0.0
SERVER_PORT 服务器端口 8000
N8N_BASE_URL n8n实例基URL http://localhost:31678
N8N_WORKFLOWS_CONFIG 工作流JSON文件的路径 workflows.json
N8N_WORKFLOWS_JSON JSON作为字符串的工作流(替代文件) -
N8N_AUTH_TYPE 全局身份验证类型(基本/头/无) none
N8N_BASIC_AUTH 全局基本身份验证(base64) -
N8N_AUTH_HEADER_NAME 全局身份验证标头名称 Authorization
N8N_AUTH_HEADER_VALUE 全局身份验证标头值 -

HTTP配置

此MCP服务器使用 可流式传输的HTTP 作为MCP通信的传输协议。它使用JSON-RPC 2.0在标准HTTP POST请求上实现MCP协议。

传输类型

  • 运输: http (流式HTTP)
  • 协议:HTTP/HTTPS
  • 类型:基于HTTP POST的JSON-RPC 2.0
  • 格式:请求-响应模式

HTTP端点

服务器公开以下HTTP端点:

端点 方法 目的 描述
/mcp POST MCP协议 MCP JSON-RPC 2.0消息的主要端点
/ GET 运行状况检查 返回服务器运行状况和工作流计数
/health GET 健康检查 与 / -返回详细的健康信息
/ready GET 就绪检查 如果加载了工作流,则返回200,否则返回503

服务器配置

# Default configuration
SERVER_HOST=0.0.0.0  # Bind to all interfaces
SERVER_PORT=8000     # HTTP port

# The server runs on plain HTTP (not HTTPS) by default
# Access via: http://localhost:8000

HTTP传输协议

MCP服务器使用JSON-RPC 2.0实现流式HTTP传输:

Client                          Server
  │                               │
  │ POST /mcp (initialize)        │
  ├──────────────────────────────>│
  │ <── JSON Response ───────────│
  │                               │
  │ POST /mcp (tools/list)        │
  ├──────────────────────────────>│
  │ <── JSON Response ───────────│
  │                               │
  │ POST /mcp (tools/call)        │
  ├──────────────────────────────>│
  │ <── JSON Response ───────────│
  │                               │

它是如何工作的:

  1. 初始化:客户端发送 initialize 方法to /mcp 端点
  2. 列出工具:客户端发送 tools/list 获取可用工作流
  3. 呼叫工具:客户端发送 tools/call 执行工作流
  4. 响应:服务器与JSON-RPC结果同步响应

支持的MCP方法:

  • initialize -初始化MCP连接
  • tools/list -列出所有可用的n8n工作流
  • tools/call -执行特定工作流

MCP客户端配置示例:

{
  "url": "http://localhost:8000/mcp",
  "transport": "http",
  "description": "n8n MCP server with HTTP transport"
}

验证:

# Test initialization
curl -X POST http://localhost:8000/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}'

# List tools
curl -X POST http://localhost:8000/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'

# Call a tool
curl -X POST http://localhost:8000/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"chat_workflow","arguments":{"chatInput":"test"}}}'

港口暴露

地方发展:

  • 服务器在上运行 http://0.0.0.0:8000
  • 可通过以下方式访问 http://localhost:8000

Docker:

  • 集装箱港口: 8000
  • 主机映射: -p 8000:8000
  • 访问方式: http://localhost:8000

库贝内特斯:

  • 服务端口: 8000
  • 节点端口: 30888 (配置于 k8s/service.yaml)
  • 访问方式: http://<node-ip>:30888

添加HTTPS支持

对于使用HTTPS的生产部署:

选项1:使用反向代理(推荐)

# Nginx example
server {
    listen 443 ssl;
    server_name mcp.example.com;

    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

选项2:带TLS的Kubernetes入口

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: n8n-mcp-server
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
spec:
  ingressClassName: nginx
  tls:
  - hosts:
    - mcp.example.com
    secretName: mcp-tls
  rules:
  - host: mcp.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: n8n-mcp-server
            port:
              number: 8000

健康检查

服务器提供专用的健康检查端点:

# Basic health check
curl http://localhost:8000/health

# Expected response
{
  "status": "healthy",
  "service": "n8n-mcp-server",
  "workflows_loaded": 4,
  "n8n_base_url": "http://localhost:31678"
}

# Readiness check (returns 503 if no workflows loaded)
curl http://localhost:8000/ready

# Expected response when ready
{
  "status": "ready"
}

Kubernetes探测器:

  • 生命探测: GET /health -检查服务器是否正在运行
  • 就绪探针: GET /ready -检查工作流是否已加载,服务器是否已准备好接受请求

本地运行

先决条件

  • Python 3.11+
  • 启用了webhooks的n8n实例

设置

  1. 克隆存储库:
git clone <repository-url>
cd mcp-n8n-server
  1. 安装依赖项:
pip install -r requirements.txt
  1. 在中配置您的工作流 workflows.json

  2. 设置环境变量(可选):

cp .env.example .env
# Edit .env with your configuration
  1. 运行服务器:
python server.py

服务器将在以下时间可用 http://localhost:8000

Docker部署

构建Docker镜像

docker build --network=host -t trieder83/mcp-n8n-server:latest .
# Or use make
make docker-build

推送到Docker Hub

docker push trieder83/mcp-n8n-server:latest
# Or use make
make docker-push

运行容器

docker run -d \
  --name n8n-mcp-server \
  -p 8000:8000 \
  -e N8N_BASE_URL=http://your-n8n-instance:31678 \
  -v $(pwd)/workflows.json:/app/workflows.json:ro \
  trieder83/mcp-n8n-server:latest
# Or use make
make docker-run

Kubernetes部署

先决条件

  • Kubernetes集群(v1.20+)
  • kubectl已配置

部署到Kubernetes

  1. 在中更新ConfigMap k8s/deployment.yaml 与您的工作流程

  2. 更新n8n基本URL:

# In k8s/deployment.yaml, update:
- name: N8N_BASE_URL
  value: "http://your-n8n-service:31678"
  1. (可选)配置身份验证密钥:
# Create Basic Auth secret
echo -n "username:password" | base64
kubectl create secret generic n8n-mcp-secrets \
  --from-literal=basic-auth='<base64-encoded-credentials>'

# Or create header auth secret
kubectl create secret generic n8n-mcp-secrets \
  --from-literal=auth-token='your-api-key'
  1. 部署:
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
  1. 检查部署:
kubectl get pods -l app=n8n-mcp-server
kubectl get svc n8n-mcp-server

该服务将在NodePort 30800上公开。通过以下方式访问:

http://<node-ip>:30800

访问服务

# Get node IP
kubectl get nodes -o wide

# Access the service
curl http://<node-ip>:30800/

测试

直接测试n8n Webhook

curl -X POST \
  -H "Content-Type: application/json" \
  http://localhost:31678/webhook/919df572-8dcd-4cd6-b592-9e90ba0db414 \
  -d '{"chatInput":"test"}'

测试MCP服务器

MCP服务器使用SSE传输。使用MCP客户端连接或使用curl进行测试:

# List available tools
curl http://localhost:8000/messages

配置MCP客户端

代号鹅配置

使用此n8n MCP服务器 代号鹅,您需要在Goose的MCP设置中配置它。

1.地方发展设置

在以下位置编辑Goose配置文件 ~/.config/goose/config.yaml:

extensions:
  mcp-n8n-server:
    available_tools: []
    bundled: null
    description: n8n workflow integration via MCP
    enabled: true
    env_keys: []
    envs: {}
    headers: {}
    name: mcp-n8n-server
    timeout: 300
    type: streamable_http
    uri: http://localhost:8000/mcp

2.Kubernetes/远程设置

如果您的MCP服务器在Kubernetes中运行NodePort:

extensions:
  mcp-n8n-server:
    available_tools: []
    bundled: null
    description: n8n workflow integration via Kubernetes
    enabled: true
    env_keys: []
    envs: {}
    headers: {}
    name: mcp-n8n-server
    timeout: 300
    type: streamable_http
    uri: http://<kubernetes-node-ip>:30888/mcp

3.使用Docker Compose(推荐本地开发)

创建一个 docker-compose.yml 用于同时运行Goose和MCP服务器的文件:

version: '3.8'

services:
  mcp-server:
    image: trieder83/mcp-n8n-server:latest
    container_name: n8n-mcp-server
    ports:
      - "8000:8000"
    environment:
      - N8N_BASE_URL=http://your-n8n-instance:31678
      - N8N_WORKFLOWS_CONFIG=/config/workflows.json
    volumes:
      - ./workflows.json:/config/workflows.json:ro
    restart: unless-stopped

然后添加到 ~/.config/goose/config.yaml:

extensions:
  mcp-n8n-server:
    type: streamable_http
    uri: http://mcp-server:8000/mcp  # Or http://localhost:8000/mcp
    enabled: true
    timeout: 300

4.验证连接

配置后,启动Goose并验证连接:

goose

在Goose界面中,您应该看到n8n工作流作为工具可用。您可以将它们与以下内容一起列出:

list tools

5.在Goose中使用n8n工作流

连接后,您可以直接从Goose调用n8n工作流:

use tool chat_workflow with {"chatInput": "Hello from Goose!"}

或者简单地让Goose使用工作流:

Run the chat_workflow with the message "analyze this data"

Goose将通过MCP服务器自动调用相应的n8n工作流。

其他MCP客户端

此服务器使用HTTP传输实现标准MCP协议,并应与任何兼容MCP的客户端一起工作:

  • 克劳德桌面:添加到 claude_desktop_config.json
  • Continue.dev:在“继续”设置中配置
  • 定制客户:连接到 http://host:port/mcp

Claude Desktop示例(~/Library/Application Support/Claude/claude_desktop_config.json 在macOS上):

{
  "mcpServers": {
    "n8n-workflows": {
      "url": "http://localhost:8000/mcp",
      "transport": "http",
      "description": "n8n workflow integration"
    }
  }
}

工作流身份验证

基本认证

对于需要基本身份验证的工作流:

  1. 生成base64凭据:
echo -n "username:password" | base64
  1. 添加到工作流配置:
{
  "name": "secure_workflow",
  "auth_type": "basic",
  "basic_auth": "dXNlcm5hbWU6cGFzc3dvcmQ=",
  ...
}

标头身份验证

对于需要自定义标头身份验证的工作流:

{
  "name": "api_workflow",
  "auth_type": "header",
  "auth_header_name": "X-API-Key",
  "auth_header_value": "your-api-key-here",
  ...
}

全局身份验证与工作流特定身份验证

  • 通过环境变量设置全局身份验证(适用于所有工作流)
  • 使用中的工作流特定设置进行覆盖 workflows.json
  • 工作流特定设置优先于全局设置

监控

日志

在Kubernetes中查看日志:

kubectl logs -f deployment/n8n-mcp-server

在Docker中查看日志:

docker logs -f n8n-mcp-server

健康检查

服务器包括Kubernetes的健康检查端点:

  • 实时性探测:端口8000上的HTTP GET
  • 就绪探测:端口8000上的HTTP GET

故障排除

常见问题

  1. 拒绝连接n8n

    • 检查一下 N8N_BASE_URL 是正确的
    • 验证n8n是否可以从容器/吊舱访问
    • 在Kubernetes中,使用服务DNS名称
  2. 找不到工作流

    • 验证工作流配置是否已加载
    • 检查日志中的配置错误
    • 确保 workflows.json 正确安装
  3. 身份验证失败

    • 验证凭据是否正确进行base64编码
    • 检查身份验证标头是否符合n8n要求
    • 查看日志以查找与身份验证相关的错误
  4. 端口冲突

    • 改变 SERVER_PORT 如果8000正在使用中
    • 如果30888冲突,请更新service.yaml中的NodePort
  5. 鹅未连接到MCP服务器

    • 验证MCP服务器是否正在运行: curl -X POST http://localhost:8000/mcp -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}'
    • 请在以下网址查看您的Goose配置 ~/.config/goose/config.yaml
    • 验证扩展是否已启用: enabled: true
    • 检查URI是否正确: uri: http://localhost:8000/mcp (或K8s的节点端口30888)
    • 验证类型: type: streamable_http
    • 在配置更改后尝试重新启动Goose
    • 检查鹅日志:运行 goose 使用详细模式或检查控制台输出
  6. Goose找不到n8n工作流

    • 检查工作流是否已加载: docker logs n8n-mcp-server
    • 使用鹅 list tools 查看可用工具的命令
    • 验证MCP服务器是否可以访问n8n(使用curl进行测试)
  7. Docker无法在本地主机上访问n8n

    • 使用 host.docker.internal 而不是 localhost 在Docker中
    • 集 N8N_BASE_URL=http://host.docker.internal:31678
    • 在Linux上,您可能需要使用 --network=host 对于集装箱

发展

项目结构

mcp-n8n-server/
├── server.py                  # Main MCP server implementation
├── requirements.txt           # Python dependencies
├── workflows.json             # Workflow configuration examples
├── Dockerfile                 # Docker image definition
├── docker-compose.yml         # Docker Compose configuration
├── .dockerignore              # Docker build exclusions
├── .env.example               # Environment variables template
├── .gitignore                 # Git exclusions
├── Makefile                   # Build and deployment commands
├── build-and-push.sh          # Automated build and push script
├── goose-mcp-config.json      # Example Goose MCP configuration
├── k8s/                       # Kubernetes manifests
│   ├── deployment.yaml        # Deployment + ConfigMap + Secret
│   └── service.yaml           # Service (NodePort 30888)
└── README.md                  # This file

添加新工作流

  1. 将工作流配置添加到 workflows.json
  2. 重新启动服务器以加载新配置
  3. 工作流将自动作为MCP工具公开

扩展服务器

服务器可以扩展到:

  • 为n8n工作流结果添加资源提供程序
  • 实施工作流指导的提示模板
  • 为常用工作流添加缓存
  • 支持带有GET请求的webhooks

许可证

MIT许可证

贡献

欢迎投稿!请打开问题或提交拉取请求。

支持

对于问题和疑问:

  • GitHub问题: /问题
  • n8n文件:https://docs.n8n.io/
  • MCP协议:https://modelcontextprotocol.io/

测试

鹅跑-供应商ollama-模型qwen3:4b-t“问n8n工作流什么糖果颜色最好”