Agentic Protocols (MCP, A2A)
Model Context Protocol, Agent-to-Agent và NLWeb — chuẩn giao tiếp cho AI agents
Tổng Quan
AI Agents cần giao tiếp — với tools, với nhau, và với web. Ba protocols chính:
| Protocol | Mục đích | Tương tự |
|---|---|---|
| MCP | Agent ↔ Tools/Data | USB-C cho AI |
| A2A | Agent ↔ Agent | HTTP cho agents |
| NLWeb | Agent ↔ Web | Natural language web queries |
Model Context Protocol (MCP)
MCP Là Gì?
MCP là chuẩn mở cho phép AI agents kết nối với external tools và data sources. Thay vì mỗi tool cần custom integration, MCP cung cấp interface thống nhất.
┌──────────┐ MCP ┌──────────────┐
│ AI Agent │ ◄────────► │ MCP Server │
│ (Client) │ │ (Database) │
└──────────┘ └──────────────┘
│ MCP ┌──────────────┐
└─────────────────►│ MCP Server │
│ (GitHub) │
└──────────────┘
Core Concepts
- MCP Host: Application chứa agent (IDE, chatbot)
- MCP Client: Kết nối đến MCP servers
- MCP Server: Expose tools, resources, prompts qua protocol chuẩn
Ví Dụ: Tạo MCP Server
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "weather-server",
version: "1.0.0"
});
// Đăng ký tool
server.tool(
"get-weather",
"Lấy thông tin thời tiết hiện tại",
{
city: z.string().describe("Tên thành phố")
},
async ({ city }) => {
// Fetch weather data
const weather = await fetchWeather(city);
return {
content: [
{ type: "text", text: JSON.stringify(weather) }
]
};
}
);
// Đăng ký resource (data source)
server.resource(
"config",
"config://app",
async () => ({
contents: [
{
uri: "config://app",
text: JSON.stringify({ version: "1.0", region: "asia" })
}
]
})
);
// Start server
const transport = new StdioServerTransport();
await server.connect(transport);
MCP Client (Agent Side)
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
const client = new Client({
name: "my-agent",
version: "1.0.0"
});
const transport = new StdioClientTransport({
command: "node",
args: ["weather-server.js"]
});
await client.connect(transport);
// Discover tools
const tools = await client.listTools();
console.log("Available tools:", tools);
// Call tool
const result = await client.callTool({
name: "get-weather",
arguments: { city: "Ho Chi Minh City" }
});
Agent-to-Agent Protocol (A2A)
A2A Là Gì?
A2A cho phép agents từ các hệ thống khác nhau giao tiếp với nhau. Mỗi agent có “Agent Card” mô tả khả năng.
Core Components
// Agent Card — "CV" của agent
interface AgentCard {
name: string;
description: string;
capabilities: string[];
endpoints: {
send: string; // URL để gửi task
status: string; // URL để check status
};
authentication: {
type: "api_key" | "oauth2";
};
}
// Task — đơn vị công việc giữa agents
interface A2ATask {
id: string;
from: string; // Agent gửi
to: string; // Agent nhận
description: string;
status: "submitted" | "working" | "completed" | "failed";
result?: any;
}
Ví Dụ: Multi-Agent Travel System
// Travel Orchestrator Agent
async function travelOrchestrator(request: string) {
// Discover available agents
const flightAgent = await discoverAgent("flight-booking");
const hotelAgent = await discoverAgent("hotel-booking");
// Send tasks to specialized agents
const [flightTask, hotelTask] = await Promise.all([
sendTask(flightAgent, {
description: "Tìm vé máy bay đi Tokyo ngày 15/3"
}),
sendTask(hotelAgent, {
description: "Tìm khách sạn ở Shinjuku 5 đêm"
})
]);
// Wait for results
const [flights, hotels] = await Promise.all([
waitForCompletion(flightTask.id),
waitForCompletion(hotelTask.id)
]);
return { flights: flights.result, hotels: hotels.result };
}
NLWeb
NLWeb cho phép truy vấn bất kỳ website nào bằng natural language, thay vì phải parse HTML.
// Thay vì crawl và parse
const response = await fetch("https://example.com/nlweb", {
method: "POST",
body: JSON.stringify({
query: "What are the best hotels near Shibuya under $200?",
format: "json"
})
});
const results = await response.json();
// Trả về structured data từ website
So Sánh Protocols
| Feature | MCP | A2A | NLWeb |
|---|---|---|---|
| Mục đích | Agent ↔ Tools | Agent ↔ Agent | Agent ↔ Web |
| Use case | Mở rộng capabilities | Collaboration | Data retrieval |
| Transport | stdio, HTTP/SSE | HTTP | HTTP |
| Maturity | Cao | Trung bình | Thấp |
| Adoption | Phổ biến | Đang phát triển | Mới |
Khi Nào Dùng Protocol Nào?
Agent cần gọi database, APIs → MCP
Agent cần delegate task cho agent khác → A2A
Agent cần query websites → NLWeb
Xây internal tools → MCP
Xây agent marketplace → A2A
Tổng Kết
- MCP: Chuẩn kết nối agent với tools — bạn đã dùng Supabase MCP trong Antigravity!
- A2A: Agent-to-Agent communication cho hệ thống phân tán
- NLWeb: Natural language queries cho web content
- MCP đang là protocol phổ biến nhất — focus vào đây trước