第 21 期:无限工具箱 — MCP 协议原理与 n8n 集成架构

⏱ 预计阅读 9 分钟 更新于 2026/4/9

MCP 要解决什么问题?

在没有 MCP 之前,每个 AI 应用都需要为每个外部服务单独开发集成

graph TB
    subgraph "MCP 之前: N × M 集成噩梦"
        A1[Claude] --> S1[Gmail]
        A1 --> S2[Slack]
        A1 --> S3[GitHub]
        A2[ChatGPT] --> S1
        A2 --> S2
        A2 --> S3
        A3[n8n Agent] --> S1
        A3 --> S2
        A3 --> S3
    end
    
    subgraph "MCP 之后: N + M 标准化连接"
        B1[Claude] --> MCP["🔌 MCP 协议
(标准接口)"] B2[ChatGPT] --> MCP B3[n8n Agent] --> MCP MCP --> T1[Gmail MCP Server] MCP --> T2[Slack MCP Server] MCP --> T3[GitHub MCP Server] end style MCP fill:#22c55e,stroke:#16a34a,color:#fff

核心类比:MCP 之于 AI Agent,就像 USB 之于电脑——一个标准化接口协议,让任何 Agent 能即插即用地连接任何外部服务。


1. MCP 协议架构

graph TB
    subgraph "MCP 三层架构"
        Client["🤖 MCP Client
(AI Agent / n8n)"] Protocol["📡 MCP Protocol
JSON-RPC 2.0 over stdio/SSE"] Server["🔧 MCP Server
(工具提供者)"] Client -->|"① 发现可用工具"| Protocol Protocol -->|"列出 Tools/Resources"| Client Client -->|"② 调用工具"| Protocol Protocol -->|"执行并返回结果"| Client end subgraph "MCP Server 提供的三种能力" Tools["🔧 Tools
可执行的操作
如: 发邮件、查数据库"] Resources["📂 Resources
可读取的数据源
如: 文件内容、API 数据"] Prompts["📝 Prompts
预制的提示模板
如: 代码审查 Prompt"] end Server --> Tools & Resources & Prompts style Protocol fill:#f59e0b,stroke:#d97706,color:#fff

MCP 通信协议

传输方式 适用场景 特点
stdio 本地进程通信 最低延迟,适合桌面应用
SSE (Server-Sent Events) 远程网络通信 适合 Docker/云端部署
Streamable HTTP 新标准 (2025+) 双向流式,替代 SSE

2. n8n 的 MCP 双角色

n8n 在 MCP 生态中既是 Client(消费者) 又是 Server(提供者)

graph LR
    subgraph "n8n 作为 MCP Client"
        Agent[🤖 AI Agent] --> MCPClient[🔌 MCP Client Tool]
        MCPClient --> ExtServer["🔧 外部 MCP Server
(GitHub / Filesystem / DB)"] end subgraph "n8n 作为 MCP Server" ExtAgent["🤖 外部 Agent
(Claude / Cursor)"] --> MCPServer[🔌 MCP Server Trigger] MCPServer --> Workflow["⚙️ n8n 工作流
作为 Tool 暴露"] end style MCPClient fill:#3b82f6,stroke:#2563eb,color:#fff style MCPServer fill:#22c55e,stroke:#16a34a,color:#fff

3. MCP 与传统 API 的区别

维度 传统 REST API MCP Protocol
接口发现 需要读文档、手动对接 自动协商:MCP Server 主动列出能力
参数格式 每个 API 格式不同 JSON Schema 标准化描述
认证方式 OAuth/API Key 各不相同 统一的认证和授权层
给 AI 用 需要人工编写 Tool Description MCP Server 自带描述,AI 直接理解
双向通信 单向请求-响应 支持流式、通知、资源订阅
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// MCP Server 自描述示例 (Agent 自动获取)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
{
  "tools": [
    {
      "name": "search_issues",
      "description": "Search GitHub issues in a repository",
      "inputSchema": {
        "type": "object",
        "properties": {
          "repo": { "type": "string", "description": "owner/repo format" },
          "query": { "type": "string", "description": "Search keywords" },
          "state": { "enum": ["open", "closed", "all"] }
        },
        "required": ["repo", "query"]
      }
    }
  ]
}
// → Agent 读到这段描述后,自动知道如何调用此工具
// → 不需要人工编写 Tool Description!

MCP 生态全景

graph TB
    subgraph "MCP 生态 (2026 年现状)"
        direction TB
        
        subgraph "官方 Server"
            GH[🐙 GitHub]
            GD[📁 Google Drive]
            SL[💬 Slack]
            PG[🐘 PostgreSQL]
            FS[📂 Filesystem]
            PP[🌐 Puppeteer]
        end
        
        subgraph "社区 Server (1000+)"
            JI[📋 Jira]
            NO[📝 Notion]
            LI[🔗 Linear]
            ST[💳 Stripe]
            TW[🐦 Twitter/X]
            DI[🎮 Discord]
        end
        
        subgraph "MCP Clients"
            CL[Claude Desktop]
            CU[Cursor IDE]
            N8[n8n Agent]
            WI[Windsurf]
        end
        
        CL & CU & N8 & WI --> GH & GD & SL & PG & FS & PP & JI & NO
    end

下一步

Ep 22 将动手实战——给 n8n Agent 接入 MCP Client Tool,让它通过 MCP 协议连接 GitHub、Filesystem 等外部服务。