什么是MCP?

MCP(Model Context Protocol)是由Anthropic开发的开放标准协议,旨在让AI助手能够安全、标准化地连接到各种外部数据源和工具。简单来说,MCP就像是AI助手的“工具箱”,通过这个协议,AI 大模型可以访问文件系统、数据库、API服务等外部资源,大大扩展了AI的能力边界。


MCP架构原理

MCP采用客户端-服务器架构:

  • MCP Client:通常是AI助手(如Claude Desktop、Cline、Cursor等)
  • MCP Server:提供特定功能的服务程序
  • 传输层:负责客户端和服务器之间的通信

MCP传输层模式

1. Stdio (Standard Input/Output)

  • 工作原理:使用标准输入/输出流进行进程间直接通信,提供最优性能且无网络开销
  • 需要自己装mcp server的环境
  • 配置示例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    {
    "mcpServers": {
    "github": {
    "command": "npx",
    "args": ["-y", "@modelcontextprotocol/server-github"],
    "env": {
    "GITHUB_PERSONAL_ACCESS_TOKEN": "your_token"
    }
    }
    }
    }

2. SSE (Server-Sent Events)

  • 工作原理:使用HTTP POST进行客户端到服务器的消息传输,使用Server-Sent Events (SSE)处理服务器响应和流式数据
  • 不需要配置环境,用的是别人部署好的MCP Sever
  • 配置示例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    {
    "mcpServers": {
    "remote-service": {
    "command": "mcp-client",
    "args": ["--transport", "http", "--url", "https://api.example.com/mcp"],
    "env": {
    "API_KEY": "your_api_key"
    }
    }
    }
    }

3. Streamable HTTP

  • Streamable HTTP可以看作是HTTP+SSE的升级版,提供了更好的会话管理和连接复用能力
  • 配置示例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    {
    "mcpServers": {
    "streamable-service": {
    "command": "mcp-client",
    "args": ["--transport", "streamable-http", "--url", "https://api.example.com/mcp"],
    "env": {
    "API_KEY": "your_api_key"
    }
    }
    }
    }

常用MCP工具的配置方法

1. GitHub(streamableHttp)

  • GitHub MCP让AI直接与你的代码仓库交互,查看代码、创建Issue、管理Pull Request等。
  • 官方文档:https://github.com/github/github-mcp-server
  • 步骤
    1. 获取GitHub Token:https://github.com/settings/personal-access-tokens
    2. 在MCP配置文件中添加:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      - MCP配置:
      {
      "mcpServers": {
      "github-remote": {
      "url": "https://api.githubcopilot.com/mcp/",
      "type": "streamableHttp",
      "headers": {
      "Authorization": "Bearer [GitHub Token]"
      }
      }
      }
      }

2. 高德MCP(sse)

3. 12306(stdio)

  • 12306 MCP让AI帮助你查询火车票信息、车次时刻等。
  • 文档(非官方):https://github.com/Joooook/12306-mcp
  • 安装12306-mcp:
    1
    2
    3
    4
    5
    git clone https://github.com/Joooook/12306-mcp.git
    # 设置淘宝镜像,不然会卡住
    npm config set registry https://registry.npmmirror.com
    # 安装
    npm i 12306-mcp
  • MCP配置:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    {
    "mcpServers": {
    "12306-mcp": {
    "command": "npx",
    "args": [
    "-y",
    "12306-mcp"
    ]
    }
    }
    }

4. PubMed(stdio)

拓展阅读

更多MCP推荐:https://docs.anthropic.com/zh-CN/docs/claude-code/mcp