MCP (Model Context Protocol)
概述
MCP (Model Context Protocol) 是 Anthropic 提出的开放协议标准,用于标准化 AI 模型与外部工具、数据源的连接。
核心定位
| 维度 | 描述 |
|---|---|
| 目的 | 统一 AI Agent 与工具的通信接口 |
| 范围 | Agent ↔ 工具/数据源 |
| 类型 | 开放协议 (Open Protocol) |
技术架构
┌─────────────────────────────────────────────┐
│ AI Agent │
├─────────────────────────────────────────────┤
│ Prompt / Task │
│ ↓ │
│ ┌─────────────────────────────────────┐ │
│ │ MCP Client │ │
│ │ - 连接 MCP Server │ │
│ │ - 请求工具列表 │ │
│ │ - 调用工具 │ │
│ └─────────────────────────────────────┘ │
└─────────────────────┬─────────────────────┘
│ MCP Protocol
┌─────────────────────▼─────────────────────┐
│ MCP Servers (插件) │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Filesystem│ │ Git │ │ Database │ │
│ │ Server │ │ Server │ │ Server │ │
│ └──────────┘ └──────────┘ └──────────┘ │
└───────────────────────────────────────────┘
核心能力
1. 工具发现 (Tool Discovery)
// 客户端请求可用工具
{
"jsonrpc": "2.0",
"method": "tools/list",
"id": 1
}
// 服务器返回工具列表
{
"jsonrpc": "2.0",
"result": {
"tools": [
{"name": "filesystem_read", "description": "Read file contents"},
{"name": "filesystem_write", "description": "Write file contents"}
]
}
}2. 工具调用 (Tool Calling)
// 调用工具
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "filesystem_read",
"arguments": {"path": "/project/main.py"}
}
}3. 资源访问 (Resource Access)
// 访问资源
{
"jsonrpc": "2.0",
"method": "resources/list"
}MCP vs 其他方案
| 维度 | MCP | OpenAI Plugin | LangChain Tools |
|---|---|---|---|
| 标准化 | 开放协议 | 专有 | 框架绑定 |
| 生态 | 快速扩张 | 受限 | 依赖框架 |
| 互操作性 | 跨模型 | 仅 OpenAI | 框架内 |
| 维护 | 社区 | Anthropic | LangChain |
生态系统
官方 MCP Servers
- Filesystem: 文件系统操作
- Git: Git 操作
- Memory: 持久记忆
- Web Search: 网络搜索
- Slack/Discord: 即时通讯
社区 MCP Servers
- 数据库连接
- API 网关
- 云服务集成
- 企业软件集成
与 A2A 的关系
| 协议 | 定位 | 使用场景 |
|---|---|---|
| MCP | Agent ↔ 工具 | 扩展 Agent 能力 |
| A2A | Agent ↔ Agent | 多 Agent 协作 |
最佳实践: 使用 MCP 扩展单 Agent 能力,使用 A2A 实现多 Agent 协作
开发指南
创建 MCP Server
from mcp.server import MCPServer
from mcp.types import Tool
server = MCPServer("my-server")
@server.tool()
def read_file(path: str) -> str:
"""Read contents of a file"""
with open(path, 'r') as f:
return f.read()
server.run()案例实例
2026-08-10 Linkly AI:本地文献库经 MCP 暴露给任意客户端
Linkly AI 是”本地资料库 → MCP Server → 任意 Agent 客户端”模式的文献管理实例:为个人 PDF 文献库建立三层索引(BM25 全文 + Outline 章节结构 + 向量语义),把 search(搜文献,返回文件列表和摘要)/ outline(看章节结构定位段落)/ read(精读指定章节原文) 三个渐进式工具暴露给 Claude、Cursor 等 MCP 客户端——由 AI 自主决定搜什么、读什么、读多少,引用落到本地真实文件的原话(反幻觉)。配置三步:导出 PDF 到统一目录 → 建索引 → 给 Agent 配 MCP(官方宣称 5 分钟完成)。见 Source: Linkly AI 文献工作流。
? 相关文章为产品方自述推广内容,“5 分钟配置""1-3 分钟建索引”等数字无第三方验证;Outline Index 对扫描件/双栏/公式密集 PDF 的鲁棒性未讨论。