学习路径 / 模块 5

MCP 协议

连接外部数据源与工具

Claude How To

MCP(模型上下文协议)

本文件夹包含模型上下文协议(MCP)服务器配置及其在 Claude Code 中使用的综合文档和示例。

概述

MCP(模型上下文协议)是一种标准化方式,使 Claude 能够访问外部工具、API 和实时数据源。与记忆(Memory)不同,MCP 提供对动态变化数据的实时访问。

核心特性:

  • 实时访问外部服务
  • 实时数据同步
  • 可扩展架构
  • 安全认证
  • 基于工具的交互

MCP 架构

graph TB
    A["Claude"]
    B["MCP 服务器"]
    C["外部服务"]

    A -->|请求: list_issues| B
    B -->|查询| C
    C -->|数据| B
    B -->|响应| A

    A -->|请求: create_issue| B
    B -->|操作| C
    C -->|结果| B
    B -->|响应| A

    style A fill:#e1f5fe,stroke:#333,color:#333
    style B fill:#f3e5f5,stroke:#333,color:#333
    style C fill:#e8f5e9,stroke:#333,color:#333

MCP 生态系统

graph TB
    A["Claude"] -->|MCP| B["文件系统<br/>MCP 服务器"]
    A -->|MCP| C["GitHub<br/>MCP 服务器"]
    A -->|MCP| D["数据库<br/>MCP 服务器"]
    A -->|MCP| E["Slack<br/>MCP 服务器"]
    A -->|MCP| F["Google Docs<br/>MCP 服务器"]

    B -->|文件 I/O| G["本地文件"]
    C -->|API| H["GitHub 仓库"]
    D -->|查询| I["PostgreSQL/MySQL"]
    E -->|消息| J["Slack 工作区"]
    F -->|文档| K["Google Drive"]

    style A fill:#e1f5fe,stroke:#333,color:#333
    style B fill:#f3e5f5,stroke:#333,color:#333
    style C fill:#f3e5f5,stroke:#333,color:#333
    style D fill:#f3e5f5,stroke:#333,color:#333
    style E fill:#f3e5f5,stroke:#333,color:#333
    style F fill:#f3e5f5,stroke:#333,color:#333
    style G fill:#e8f5e9,stroke:#333,color:#333
    style H fill:#e8f5e9,stroke:#333,color:#333
    style I fill:#e8f5e9,stroke:#333,color:#333
    style J fill:#e8f5e9,stroke:#333,color:#333
    style K fill:#e8f5e9,stroke:#333,color:#333

MCP 安装方法

Claude Code 支持多种 MCP 服务器连接的传输协议:

HTTP 传输(推荐)

# 基础 HTTP 连接
claude mcp add --transport http notion https://mcp.notion.com/mcp

# 带认证头的 HTTP
claude mcp add --transport http secure-api https://api.example.com/mcp \
  --header "Authorization: Bearer your-token"

Stdio 传输(本地)

用于本地运行的 MCP 服务器:

# 本地 Node.js 服务器
claude mcp add --transport stdio myserver -- npx @myorg/mcp-server

# 带环境变量
claude mcp add --transport stdio myserver --env KEY=value -- npx server

SSE 传输(已弃用)

Server-Sent Events 传输已被 http 取代,但仍然支持:

claude mcp add --transport sse legacy-server https://example.com/sse

WebSocket 传输

WebSocket 传输用于持久的双向连接:

claude mcp add --transport ws realtime-server wss://example.com/mcp
🔒

登录后查看完整内容

本篇还有约 20700 字的精彩内容