
Model Context Protocol (MCP) là gì?
Model Context Protocol (MCP) là chuẩn mở cho phép AI agent giao tiếp với hệ thống bên ngoài một cách nhất quán — tương tự như USB-C cho AI, thay vì mỗi model cần riêng adapter cho từng công cụ. Ra mắt năm 2024 bởi Anthropic, MCP đang trở thành industry standard cho kết nối LLM với database, API, filesystem, và dịch vụ web.
Trước MCP, mỗi AI agent phải implement riêng connector cho từng nguồn dữ liệu: một bộ cho PostgreSQL, một bộ cho Slack, một bộ cho Google Drive — tạo ra integration hell. MCP giải quyết bằng cách định nghĩa single protocol, single schema, single transport layer cho tất cả kết nối.

Cách MCP hoạt động
MCP dùng kiến trúc client-server với 3 thành phần chính:
- Host: ứng dụng chạy LLM (Claude Desktop, IDE, CLI tool) — khởi tạo kết nối
- Client: bên trong host, quản lý 1 kết nối đến 1 server MCP
- Server: process cung cấp resources, tools, prompts — expose chức năng cho client
Client và server communicate qua JSON-RPC 2.0, hỗ trợ 2 transport:
- stdio: cho local server (command line tool, Python script chạy cùng máy)
- Streamable HTTP: cho remote server (điện toán đám mây, microservice)
Mỗi connection là stateful — client giữ session context với server, có thể gọi nhiều lần mà không cần reconnect. Server khởi tạo bằng handshake: client gửi initialize request → server trả về protocol version + capabilities → client gửi initialized notification.

3 primitive cốt lõi
MCP expose 3 loại primitive qua server capabilities:
- Resources: dữ liệu tĩnh hoặc động mà server expose — file content, database rows, API responses. Client đọc bằng
resources/readhoặc subscriberesources/templates/listđể discover. - Tools: hàm executable mà server expose —
search_database,send_email,run_migration. LLM gọi tool khi cần hành động; server thực thi và trả kết quả. Tool definition kèm JSON Schema cho input validation. - Prompts: template sẵn để LLM generate output chuẩn —
review_code,summarize_doc. Prompt có thể chứa placeholder cho resource/tool output.
Ngoài ra còn Sampling: server yêu cầu host LLM generate completion — dùng khi server cần AI reasoning mà không chạy LLM trực tiếp (ví dụ: oracle server yêu cầu host summarize dữ liệu trước khi trả về).
Ví dụ thực tế: MCP server cho database
Tạo MCP server Python đơn giản kết nối SQLite:
from mcp.server import Server
from mcp.types import Tool, TextContent
import sqlite3
app = Server("sqlite-mcp")
@app.list_tools()
async def list_tools():
return [
Tool(
name="query",
description="Run SQL query on SQLite database",
inputSchema={"type": "object", "properties": {"sql": {"type": "string"}}}
)
]
@app.call_tool()
async def call_tool(name: str, arguments: dict):
if name == "query":
conn = sqlite3.connect("app.db")
rows = conn.execute(arguments["sql"]).fetchall()
return [TextContent(type="text", text=str(rows))]
Client (Claude Desktop) cấu hình thêm server này vào claude_desktop_config.json:
{
"mcpServers": {
"sqlite": {
"command": "python",
"args": ["sqlite_mcp.py"]
}
}
}
Kết quả: Claude có thể gọi query tool tự nhiên — “hãy đếm xem có bao nhiêu user đăng ký tháng này” → Claude tự generate SQL → server thực thi → trả kết quả → Claude trả lời user.

So sánh MCP với các giải pháp khác
| Giải pháp | Đặc điểm | Nhược điểm |
|---|---|---|
| MCP | Chuẩn mở, JSON-RPC, nhiều transport, tool discovery | Cần server implementation riêng cho mỗi hệ thống |
| Function Calling | API gốc của OpenAI/Anthropic, built-in schema validation | Không chuẩn hóa giữa các provider, mỗi model tự định nghĩa format |
| Custom Plugin | Tích hợp sâu, kiểm soát hoàn toàn | Viết từ đầu, không tái sử dụng được giữa các model |
| RAG | Truy xuất dữ liệu ngữ cảnh, không thực thi | Chỉ đọc dữ liệu, không gọi tool hay update state |
Khi nào dùng MCP
MCP phù hợp khi bạn cần AI agent tương tác với hệ thống bên ngoài một cách có kiểm soát và nhất quán. Trường hợp phổ biến:
- IDE integration: Claude Code, Cursor, Windsurf dùng MCP để IDE expose filesystem, terminal, Git cho agent.
- Data analysis: MCP server expose database queries cho LLM — agent tự explore schema, chạy query, vẽ chart.
- Automation: Server expose API của ERP, CRM, ticketing system — agent tự đóng ticket, cập nhật order.
- Multi-tool orchestration: MCP quản lý nhiều server cùng lúc — 1 agent dùng PostgreSQL MCP + Slack MCP + GitHub MCP trong cùng workflow.
