AI Agent Protocol (MCP & A2A)
Core
AI Agent "đối thoại" với thế giới ngoài thế nào? Như Internet cần HTTP, AI Agent cần protocol chuẩn. Chương này giới thiệu 2 protocol mainstream: MCP + A2A, giải bài toán AI-tool và Agent-Agent communication.
0. Protocol là gì?
Protocol = bộ rule chuẩn cho các system/program "hiểu" + "communicate" nhau.
0.1 Sao cần protocol?
Tưởng gửi quà cho bạn cần điền địa chỉ. Nếu mỗi người viết format khác, shipper không giao được. Protocol = chuẩn "viết địa chỉ thế nào".
Computer cũng vậy. 2 program communicate phải agree:
- Data format gì? (JSON? binary?)
- Lập connection thế nào? (handshake)
- Lỗi thì sao? (error handling)
0.2 Protocol thường gặp
| Protocol | Use | Bạn dùng hàng ngày |
|---|---|---|
| HTTP | Web transfer | Browser mở web |
| HTTPS | HTTP encrypt | Banking, payment |
| TCP/IP | Internet base | Mọi network communication |
| DNS | Domain resolve | google.com → IP |
| SMTP | Send email | Gửi mail |
| WebSocket | Realtime 2-way | Chat, online game |
| SSH | Remote login secure | Connect server |
| FTP | File transfer | Upload/download |
Đây là nền tảng Internet. Không có chúng = không xem web, không gửi mail.
0.3 Giá trị protocol
Core value: standardization + interoperability.
- Standardization: cùng rule, giảm chi phí communicate
- Interoperability: vendor/stack khác nhau seamless integrate
Vd HTTP: Chrome access Nginx server, Python crawler crawl Java website. Chrome + Nginx không cần "biết nhau", chỉ cần đều theo HTTP.
0.4 AI Agent cũng cần protocol
AI Agent muốn "làm việc" cần:
- Gọi external tool (xem thời tiết, gửi mail, op DB)
- Cộng tác Agent khác (chia việc, làm task phức tạp)
Cần protocol chuẩn cho "AI gọi tool", "Agent đối thoại". Đây là nguồn gốc MCP + A2A.
1. Layer của Agent protocol
| Layer | Protocol | Vấn đề | Ẩn dụ |
|---|---|---|---|
| 1 | Function Call | AI gọi local function thế nào | Não phát lệnh |
| 2 | MCP | AI connect external tool/data | Cổng USB-C |
| 3 | A2A | Agent communicate Agent | Slack/Teams nội bộ |
Đọc table
Layer 1 (Function Call): capability LLM cơ bản — output JSON trigger function. Là "nền protocol", nhưng giống capability hơn standard protocol.
Layer 2 (MCP): Model Context Protocol, Anthropic release 2024-11. Chuẩn hoá cách AI connect external tool + data, như USB-C thống nhất cổng sạc.
Layer 3 (A2A): Agent-to-Agent, Google release 2025-04. Cho các Agent discover-communicate-collaborate, như nội bộ enterprise.
2. MCP (Model Context Protocol)
2.1 Info
| Item | Content |
|---|---|
| Full | Model Context Protocol |
| Initiator | Anthropic |
| Release | 2024-11-25 |
| Docs | modelcontextprotocol.io |
| License | MIT |
| GitHub | github.com/modelcontextprotocol |
Sao gọi "Context Protocol"?
Context = key cho LLM hiểu task. Core idea MCP: AI dynamically lấy context cần, không phải nhét hết vào Prompt.
Vd AI cần đọc 1 file → không cần bạn copy-paste content, mà qua MCP access file system trực tiếp.
2.2 Background
2024, với Claude 3.5 Sonnet, Anthropic phát hiện vấn đề: mỗi tool phải tích hợp riêng.
Tưởng:
- AI đọc GitHub repo → viết integration GitHub
- AI query DB → viết integration DB
- AI op file system → viết integration FS
Mỗi integration lặp code: auth, error, data transform...
Anthropic viết:
"Introducing MCP, an open protocol that standardizes how applications provide context to LLMs."
Mục tiêu: tool dev viết 1 lần, mọi AI app support MCP đều dùng được.
2.3 MCP là gì?
3 capability core:
| Capability | EN | Use | Vd |
|---|---|---|---|
| Tools | — | Function AI gọi | Query weather, send mail |
| Resources | — | Data AI đọc | File content, DB record |
| Prompts | — | Template prompt | Code review, writing template |
2.4 MCP internal
MCP 之前,AI 只能"看"和"说",有了 MCP,AI 终于可以"动手"了。它让 AI 可以操纵各种程序,真正帮你干活。
使用 MCP 非常简单,只需要配置一个 mcp.json 文件,就可以在你的 IDE 里使用各种 MCP 工具。
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/home/user/projects"
]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": "your-token-here"
}
},
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgresql://user:pass@localhost/db"
}
}
}
}假设你有一个天气 API,想把它封装成 MCP Server 让 AI 可以调用。下面以 Node.js 为例演示:
import { Server } from '@modelcontextprotocol/sdk/server/index.js'
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
// 1. 创建 MCP Server
const server = new Server({
name: 'weather-server',
version: '1.0.0'
}, {
capabilities: { tools: {} }
})
// 2. 定义工具列表
server.setRequestHandler('tools/list', async () => ({
tools: [{
name: 'get_weather',
description: '获取指定城市的天气信息',
inputSchema: {
type: 'object',
properties: {
city: { type: 'string', description: '城市名称' }
},
required: ['city']
}
}]
}))
// 3. 实现工具调用逻辑
server.setRequestHandler('tools/call', async (request) => {
const { name, arguments: args } = request.params
if (name === 'get_weather') {
// 调用你的天气 API
const response = await fetch(
`https://api.weather.com/v1/current?city=${args.city}`
)
const data = await response.json()
return {
content: [{
type: 'text',
text: JSON.stringify(data)
}]
}
}
})
// 4. 启动服务(stdio 方式)
const transport = new StdioServerTransport()
await server.connect(transport)MCP Server 作为子进程运行,通过标准输入输出通信
优点:简单、安全、适合本地工具
缺点:只能本地使用,不支持远程
MCP Server 作为 HTTP 服务运行,支持 SSE 推送
优点:支持远程访问、多客户端共享
缺点:需要部署服务器、配置认证
// Server 发送 initialize 请求
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {},
"resources": {},
"prompts": {}
},
"serverInfo": {
"name": "filesystem",
"version": "1.0.0"
}
}
}技术深究:JSON-RPC 2.0 消息格式
{
"jsonrpc": "2.0", // 协议版本
"id": 1, // 请求 ID,用于匹配响应
"method": "tools/call", // 方法名
"params": { ... } // 参数对象
}// 成功响应
{
"jsonrpc": "2.0",
"id": 1,
"result": { ... }
}
// 错误响应
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32600,
"message": "Invalid Request"
}
}id 用于匹配响应技术深究:两种传输方式
// 启动 MCP Server 作为子进程
npx @modelcontextprotocol/server-filesystem ./project
// 通过 stdio 通信
// stdin: 接收请求
// stdout: 发送响应// HTTP 传输(Server-Sent Events)
POST /mcp HTTP/1.1
Content-Type: application/json
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": { ... }
}
// SSE 长连接用于推送
GET /mcp/sse HTTP/1.1
// 持续接收服务器推送的更新技术深究:MCP 核心 API
2.5 Ẩn dụ: USB-C
MCP như USB-C:
- Trước: mỗi device 1 cổng (tròn, dẹt, magnetic...)
- Giờ: USB-C thống nhất sạc + transfer
- MCP: thống nhất cách AI connect mọi tool
Tool dev implement 1 lần MCP Server, mọi AI app support MCP (Claude, Cursor, Windsurf) đều dùng.
2.6 MCP scenario
| Scenario | Note | Vd |
|---|---|---|
| Local file | AI đọc/sửa local file | Đọc codebase, phân tích log |
| DB query | AI query DB trực tiếp | SQL, data analysis |
| API call | AI gọi 3rd party | GitHub API, Slack, mail |
| Dev tool | AI dùng dev tool | Git, terminal |
Case thực:
- Cursor/Windsurf: MCP connect FS, Git, terminal
- Claude Desktop: MCP connect note app, mail
- Auto script: AI execute auto task (backup, deploy, sync)
3. A2A (Agent-to-Agent Protocol)
3.1 Info
| Item | Content |
|---|---|
| Full | Agent-to-Agent Protocol |
| Initiator | |
| Release | 2025-04-09 |
| Docs | google.github.io/A2A |
| License | Apache 2.0 |
| GitHub | github.com/google/A2A |
Sao Google?
Google release A2A tại Cloud Next 2025, liên quan enterprise AI strategy.
Google nghĩ: tương lai enterprise AI không phải 1 super-Agent, mà nhiều specialized Agent collaborate — có cái data analysis, có cái code gen, có cái doc process.
Cần standard communicate.
3.2 Background
MCP giải "AI connect tool", còn: multi-Agent cộng tác thế nào?
Vd:
- Agent A: requirements analyst
- Agent B: code gen
- Agent C: test
User: "Build login function"
A phân tích req → assign B; B viết code → cho C test. Communicate thế nào?
Google:
"A2A is an open protocol that enables AI agents to communicate with each other, facilitating collaboration across different frameworks and vendors."
Mục tiêu: Agent khác vendor/framework seamless collaborate.
3.3 A2A là gì?
3 core concept:
| Concept | EN | Use | Ẩn dụ |
|---|---|---|---|
| Agent Card | — | Mô tả capability | Thẻ nhân viên |
| Task | — | Đơn vị công việc | Ticket |
| Message | — | Nội dung communicate | Chat log |
3.4 A2A internal
A2A 让多个 AI Agent 可以相互协作,不再是单打独斗。一个复杂任务可以分配给多个专业 Agent,每个 Agent 做自己擅长的事。
A2A 目前还在早期阶段,主要由 Google 推动。如果你想尝试 A2A,需要开发支持 A2A 协议的 Agent 服务。
/.well-known/agent.json,声明 Agent 的能力和版本 agents/get、tasks/send、tasks/get 等核心 API // Agent A 获取 Agent B 的 Agent Card
GET /.well-known/agent.json HTTP/1.1
Host: agent-b.company.com
// 响应
{
"name": "Code Agent",
"description": "专业代码生成 Agent",
"url": "https://agent-b.company.com",
"version": "1.0.0",
"capabilities": {
"streaming": true,
"pushNotifications": true
},
"skills": [
{"id": "code-gen", "name": "代码生成"},
{"id": "code-review", "name": "代码审查"}
]
}技术深究:Agent Card 名片格式
/.well-known/agent.json 路径 {
"name": "代码生成 Agent",
"description": "专业的前后端代码生成 Agent",
"url": "https://code-agent.company.com",
"version": "1.0.0",
"capabilities": {
"streaming": true,
"pushNotifications": true
},
"skills": [
{
"id": "frontend",
"name": "前端开发",
"description": "React/Vue/Angular"
},
{
"id": "backend",
"name": "后端开发",
"description": "Node/Python/Go"
}
],
"authentication": {
"schemes": ["Bearer", "OAuth2"]
}
}技术深究:HTTP + SSE 通信
POST /tasks/send HTTP/1.1
Host: agent-b.company.com
Content-Type: application/json
Authorization: Bearer {token}
{
"id": "task-001",
"message": {
"role": "user",
"parts": [{ "type": "text", "text": "写一个登录接口" }]
}
}GET /tasks/task-001/sse HTTP/1.1
Authorization: Bearer {token}
event: progress
data: {"status": "processing", "progress": 50}
event: completed
data: {"status": "completed", "result": {...}}技术深究:A2A 核心 API
技术深究:认证机制
Authorization: Bearer sk-xxxxx
# 或
Authorization: ApiKey sk-xxxxxAuthorization: Bearer {access_token}
# 支持刷新令牌
POST /oauth/token
{
"grant_type": "refresh_token",
"refresh_token": "xxx"
}3.5 Ẩn dụ: Slack/Teams nội bộ
A2A như Slack nội bộ:
- Agent Card: thẻ mỗi người, name + dept + role
- Send task: @ai đó, assign task
- Chat: trong task có thể chat
- Task tracking: thấy progress + status
Agent khác = đồng nghiệp khác, A2A cho collaborate project phức tạp.
3.6 A2A scenario
| Scenario | Note | Vd |
|---|---|---|
| Software dev | Multi-Agent dev | Req → code → test → deploy |
| Enterprise workflow | Dept khác Agent collaborate | HR + Finance + Legal Agent |
| Smart CS | Specialized Agent chia việc | Greet → answer → escalate → record |
| Data analysis | Multi-Agent | Collect → clean → analyze → visualize → report |
Case:
- Google Agent Space: multi-Agent xử doc, mail, calendar
- Dev team: Req → Code → Test → Deploy Agent
- CS system: Greet → Specialist → Human escalate Agent
4. MCP vs A2A
4.1 Khác biệt
| Dim | MCP | A2A |
|---|---|---|
| Initiator | Anthropic (2024.11) | Google (2025.04) |
| Position | AI ↔ Tool | Agent ↔ Agent |
| Scope | Client-Server | Peer-to-Peer |
| Format | JSON-RPC 2.0 | HTTP + JSON |
| Ẩn dụ | USB-C | Slack |
4.2 Quan hệ
MCP + A2A không phải cạnh tranh, mà bổ sung:
4.3 Chọn?
| Scenario | Chọn |
|---|---|
| AI gọi local function/tool | Function Call |
| Dùng 3rd party tool (DB, API, FS) | MCP |
| Build multi-Agent collaborative system | A2A |
| Cần cả tool integration + multi-Agent | MCP + A2A |
5. Tương lai
5.1 Ecosystem
MCP (đến 2026):
- Official Server: FS, SQLite, Git, PostgreSQL
- Community: Slack, Notion, Figma, Stripe
- App support: Claude Desktop, Cursor, Windsurf, Zed, Cline
A2A (mới ra):
- Google Agent products đầu tiên support
- Open source SDK đang phát triển
- Enterprise đang khám phá
5.2 Standardization
Hiện Agent protocol đang "thời chiến quốc":
- MCP + A2A mainstream
- Còn ANP, AGP và mới khác
- Tương lai có thể merge/unify
Như Internet:
- Sớm: nhiều LAN protocol
- Sau: TCP/IP standard
- Giờ: Agent protocol có thể đi tới unify
6. Tóm tắt
Key
| Protocol | 1 câu | Release | Initiator | Use |
|---|---|---|---|---|
| MCP | "USB-C" cho AI ↔ Tool | 2024.11 | Anthropic | Tool integration |
| A2A | "Slack" cho Agent collaboration | 2025.04 | Multi-Agent |
Insight:
- MCP giải "AI lấy external capability"
- A2A giải "Multi-AI collaborate"
- Bổ sung, tương lai có thể merge
- Chọn theo scenario, không có silver bullet
2026 cho VN dev
- MCP tăng tốc dữ dội: 500+ MCP server công khai trên GitHub
- Claude Code + MCP: pipeline AI dev hiện đại
- A2A vẫn nascent: chưa nhiều case thực, theo dõi Google
- Cursor + MCP: setup MCP server cho project = boost productivity
- Bài tập: viết 1 MCP server đơn giản đọc Notion DB
Tài liệu
- MCP: modelcontextprotocol.io
- MCP GitHub: github.com/modelcontextprotocol
- Anthropic blog: "Introducing MCP" (2024-11-25)
- A2A: google.github.io/A2A
- A2A GitHub: github.com/google/A2A
- Google Cloud blog: "Announcing A2A" (2025-04-09)