MCP (Model Context Protocol) — Giải thích đơn giản
MCP là gì?
Model Context Protocol (MCP) là một open standard do Anthropic phát triển, được thiết kế để giải quyết một vấn đề căn bản: làm thế nào để AI models kết nối một cách nhất quán với external tools, data sources, và services.
Trước khi có MCP, mỗi AI application phải tự xây dựng integration riêng với từng tool — một codebase phức tạp, không tái sử dụng được, và khó maintain. MCP tạo ra một "ngôn ngữ chung" để mọi AI client có thể nói chuyện với mọi tool server theo cùng một cách.
MCP giống như USB-C trong thế giới AI integration: thay vì mỗi thiết bị cần một loại cáp riêng, tất cả dùng chung một chuẩn kết nối.
Tại sao MCP quan trọng?
Vấn đề trước MCP
Hãy tưởng tượng bạn muốn xây dựng một AI assistant có thể:
- Đọc file từ máy tính local
- Query database
- Gọi GitHub API
- Gửi message Slack
Trước MCP, bạn phải viết custom integration code cho từng usecase, với từng AI model. Khi đổi từ Claude sang model khác, toàn bộ integration phải viết lại.
Với MCP
Mỗi service (GitHub, Slack, filesystem...) xây dựng một MCP Server theo chuẩn chung. Mọi AI client hỗ trợ MCP (Claude Code, Claude Desktop, và các tools khác) có thể kết nối với bất kỳ MCP Server nào — không cần custom code thêm.
Đây là interoperability thực sự: build once, work everywhere trong hệ sinh thái MCP.
Kiến trúc MCP — Client-Server Model
Các thành phần chính
MCP hoạt động theo mô hình client-server với ba loại thành phần:
- MCP Host: Ứng dụng AI bạn đang dùng (Claude Desktop, Claude Code, IDE plugins...)
- MCP Client: Component bên trong Host, quản lý kết nối với servers
- MCP Server: Process độc lập cung cấp capabilities (tools, resources, prompts)
Luồng hoạt động điển hình:
- User yêu cầu Claude làm gì đó liên quan đến external data
- Claude (qua MCP Client) gọi MCP Server tương ứng
- MCP Server thực hiện action/query và trả kết quả
- Claude nhận kết quả và đưa vào context để trả lời user
Ba khái niệm cốt lõi của MCP
1. Resources
Resources là dữ liệu mà server expose để AI có thể đọc — giống như file system hoặc database records. Resources có URI dạng protocol://path/to/resource và có thể là text hoặc binary.
Ví dụ:
-
file:///home/user/project/README.md— một file cụ thể -
github://repos/anthropic/mcp/issues— list GitHub issues -
postgres://db/customers/recent— query database
2. Tools
Tools là các hàm mà AI có thể gọi để thực hiện actions — tạo file, gửi message, search web, chạy query. Mỗi tool có schema định nghĩa input parameters và output format.
Ví dụ tool schema:
{
"name": "create_github_issue",
"description": "Tạo một issue mới trên GitHub repository",
"inputSchema": {
"type": "object",
"properties": {
"repo": {
"type": "string",
"description": "Repository name (owner/repo)"
},
"title": {
"type": "string",
"description": "Tiêu đề issue"
},
"body": {
"type": "string",
"description": "Nội dung issue"
}
},
"required": ["repo", "title"]
}
}
3. Prompts
Prompts là các template prompt được pre-defined trong server — reusable workflows mà user có thể invoke. Ví dụ: "Analyze this codebase for security issues" là một prompt template mà filesystem server có thể cung cấp.
MCP với Claude Code
Claude Code là MCP Client
Claude Code có built-in MCP support. Khi bạn install một MCP server, Claude Code có thể sử dụng tất cả tools và resources mà server đó cung cấp.
Cài đặt MCP server trong Claude Code:
# Thêm MCP server (ví dụ: filesystem server)
claude mcp add filesystem npx @modelcontextprotocol/server-filesystem /path/to/allowed/dir
# Xem danh sách servers đã cài
claude mcp list
# Xóa server
claude mcp remove filesystem
Sau khi add server, Claude Code có thể trực tiếp đọc file, tạo file, hay search trong thư mục được cho phép — không cần bạn copy-paste nội dung thủ công.
CLAUDE.md và MCP context
Trong file CLAUDE.md của project, bạn có thể định nghĩa MCP servers nào nên được active và cách Claude nên sử dụng chúng, tạo ra workflow nhất quán cho toàn team.
MCP với Claude Desktop
Cấu hình claude_desktop_config.json
Claude Desktop đọc MCP server configuration từ file:
-
macOS:
~/Library/Application Support/Claude/claude_desktop_config.json -
Windows:
%APPDATA%Claudeclaude_desktop_config.json
Format cấu hình:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/username/Documents",
"/Users/username/Projects"
]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_your_token_here"
}
},
"slack": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-slack"],
"env": {
"SLACK_BOT_TOKEN": "xoxb-your-token"
}
}
}
}
Sau khi lưu file và restart Claude Desktop, bạn sẽ thấy biểu tượng hammer trong chat interface — nhấn vào để xem danh sách tools khả dụng.
Popular MCP Servers
Ecosystem hiện tại
Cộng đồng đã xây dựng nhiều MCP servers cho các use cases phổ biến:
| Server | Package | Chức năng |
|---|---|---|
| Filesystem | @modelcontextprotocol/server-filesystem |
Đọc/ghi file local |
| GitHub | @modelcontextprotocol/server-github |
Issues, PRs, repos |
| Slack | @modelcontextprotocol/server-slack |
Channels, messages |
| PostgreSQL | @modelcontextprotocol/server-postgres |
Query database |
| SQLite | @modelcontextprotocol/server-sqlite |
Local SQLite DB |
| Brave Search | @modelcontextprotocol/server-brave-search |
Web search |
| Google Maps | @modelcontextprotocol/server-google-maps |
Maps, geocoding |
| Puppeteer | @modelcontextprotocol/server-puppeteer |
Browser automation |
Toàn bộ danh sách servers tham khảo tại: github.com/modelcontextprotocol/servers
Community servers
Ngoài các server chính thức, cộng đồng đã tạo ra hàng trăm MCP servers cho các services khác: Notion, Jira, Figma, AWS, Google Workspace, Shopify, và nhiều hơn nữa. Tìm kiếm trên npm với keyword "mcp-server" để khám phá.
MCP vs Traditional API Integration
So sánh hai cách tiếp cận
| Tiêu chí | Traditional API | MCP |
|---|---|---|
| Reusability | Mỗi app tự build | Build once, dùng nhiều nơi |
| AI model switching | Phải rewrite integration | Server tương thích mọi MCP client |
| Discovery | Hardcoded trong app | Dynamic — AI thấy tools qua schema |
| Security | Tùy app implement | Chuẩn hóa qua protocol |
| Complexity | Cao khi nhiều integrations | Giảm nhờ chuẩn hóa |
Khi nào dùng MCP, khi nào dùng traditional API?
Dùng MCP khi:
- Muốn Claude trực tiếp access và thao tác với external services
- Xây dựng agent workflows cần nhiều tools
- Muốn reuse integration cho nhiều AI clients
Dùng traditional API call khi:
- Ứng dụng của bạn gọi API và truyền kết quả vào Claude (không cần Claude chủ động gọi)
- Integration đơn giản, một chiều
- Không cần AI model chủ động quyết định khi nào gọi tool
Security và Permissions
Nguyên tắc least privilege
Khi cấu hình MCP servers, chỉ cấp quyền tối thiểu cần thiết:
- Filesystem server: chỉ expose những thư mục cần thiết, không expose
/hay~ - GitHub server: dùng fine-grained tokens với chỉ những repo permissions cần thiết
- Database servers: dùng read-only user nếu Claude chỉ cần đọc data
User consent
MCP được thiết kế để user luôn biết Claude đang làm gì. Trong Claude Desktop và Claude Code, bạn có thể thấy và approve các tool calls trước khi chúng được thực thi (tùy cấu hình).
Debugging và Troubleshooting MCP
MCP server không hiện trong Claude Desktop
Các nguyên nhân phổ biến:
-
JSON syntax error: Dùng jsonlint.com để validate
claude_desktop_config.json -
Node.js chưa install: Kiểm tra bằng
node --versiontrong terminal - Chưa restart app: Quit hoàn toàn (không chỉ đóng cửa sổ) và mở lại
- Permission denied: npx cần quyền download package lần đầu
Xem MCP logs
Claude Desktop ghi logs của MCP servers tại:
-
macOS:
~/Library/Logs/Claude/mcp-server-[name].log -
Windows:
%APPDATA%Claudelogsmcp-server-[name].log
Xem logs để debug lỗi kết nối hoặc error trong server code.
MCP Protocol — Cơ chế hoạt động kỹ thuật
Transport layer
MCP hỗ trợ hai transport mechanisms:
- stdio (Standard I/O): Dùng cho local servers — Claude Desktop fork server process và giao tiếp qua stdin/stdout. Đây là transport mặc định và đơn giản nhất.
- HTTP with SSE: Dùng cho remote servers — server chạy tại một URL, Claude connect qua HTTP. Phù hợp khi server cần chạy ở xa (cloud, team server).
Message format
MCP dùng JSON-RPC 2.0 format cho tất cả communication:
// Request từ client đến server
{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "read_file",
"arguments": { "path": "/home/user/notes.txt" }
}
}
// Response từ server
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"content": [{ "type": "text", "text": "Nội dung file..." }]
}
}
Lifecycle của một MCP session
- Initialize: Client gửi capabilities của mình, server trả về capabilities của server
- Discovery: Client hỏi list tools, resources, prompts server cung cấp
- Operation: Client gọi tools, đọc resources theo nhu cầu
- Shutdown: Session kết thúc khi app close hoặc server disconnect
MCP trong Claude Code — Workflow developer
CLAUDE.md và MCP
Trong project có file CLAUDE.md, bạn có thể định nghĩa MCP context guidelines:
# CLAUDE.md
## MCP Tools Available
- filesystem: có thể đọc/ghi toàn bộ thư mục project
- github: access repo anthropic/myproject
## MCP Usage Guidelines
- Trước khi sửa file, đọc file đó trước để hiểu context
- Khi tạo file mới, kiểm tra naming conventions từ file hiện có
- Commit message format: "type(scope): description"
Agentic workflows với MCP
MCP là foundation cho các agentic tasks trong Claude Code. Ví dụ workflow "Refactor module X":
- Claude đọc tất cả file trong module (filesystem tool)
- Phân tích dependencies (đọc import statements)
- Thực hiện refactoring theo plan
- Check GitHub issues để xem có liên quan không (GitHub tool)
- Tạo summary commit message
Tất cả các bước trên xảy ra tự động khi bạn chỉ cần ra lệnh một lần — MCP là cơ sở hạ tầng cho phép điều này.
Roadmap và tương lai
MCP được phát hành open source và đang được cộng đồng phát triển mạnh mẽ. Anthropic đã công bố MCP như là foundation cho agentic AI — tức là các AI agents có thể tự chủ thực hiện complex tasks bằng cách kết hợp nhiều MCP tools.
Nhiều IDE, code editors, và productivity tools đang thêm MCP support. Đây đang trở thành "USB standard" thực sự của AI tool integration.
Xây dựng MCP Server của riêng bạn
Khi nào nên build custom server
Bạn nên build MCP server riêng khi:
- Cần tích hợp với internal tools hoặc proprietary systems (CRM nội bộ, database riêng)
- Muốn expose business logic cụ thể của công ty dưới dạng Claude tools
- Community server chưa tồn tại cho service bạn dùng
- Cần customization mà ready-made server không hỗ trợ
Stack để build MCP Server
MCP SDK hiện có official support cho:
-
TypeScript/Node.js:
@modelcontextprotocol/sdk— stable, đầy đủ examples -
Python:
mcppackage — cũng stable, tốt cho data science workflows
Community đã xây dựng thêm SDK cho Go, Rust, Java, và nhiều ngôn ngữ khác.
Pattern cơ bản của một MCP Server
// TypeScript — cấu trúc tối giản của một MCP Server
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { ListToolsRequestSchema, CallToolRequestSchema } from "@modelcontextprotocol/sdk/types.js";
const server = new Server(
{ name: "my-server", version: "1.0.0" },
{ capabilities: { tools: {} } }
);
// 1. Khai báo tools
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [{ name: "my_tool", description: "...", inputSchema: { type: "object", properties: {} } }]
}));
// 2. Xử lý tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "my_tool") {
// Thực hiện logic
return { content: [{ type: "text", text: "Kết quả" }] };
}
throw new Error("Tool không tồn tại");
});
// 3. Kết nối transport và start
const transport = new StdioServerTransport();
await server.connect(transport);
MCP trong tương lai của Agentic AI
MCP và multi-agent systems
Một trong những ứng dụng quan trọng nhất của MCP là trong multi-agent architectures. Khi nhiều AI agents cần collaborate — một agent orchestrate, một agent execute code, một agent search web — MCP cung cấp standard interface để chúng communicate và chia sẻ tools.
Thay vì hardcode "agent A gọi agent B theo cách X", MCP cho phép dynamic discovery: agent có thể hỏi "tools nào đang khả dụng?" và quyết định theo context.
Long-running tools
MCP đang phát triển support cho long-running tools — những operations mất nhiều phút thay vì giây. Điều này mở ra khả năng cho các workflows phức tạp như:
- Running CI/CD pipeline và chờ kết quả
- Executing large data processing jobs
- Multi-step automation với human-in-the-loop checkpoints
Kết luận
MCP giải quyết một vấn đề thực sự trong AI development: fragmentation của integrations. Thay vì mỗi team, mỗi app phải reinvent the wheel, MCP tạo ra ecosystem nơi servers được build một lần và work với mọi MCP-compatible client.
Nếu bạn đang dùng Claude Code hoặc Claude Desktop, hãy bắt đầu với filesystem MCP server — nó ngay lập tức giúp Claude làm việc với codebase local của bạn mà không cần copy-paste file. Đó là điểm khởi đầu tốt nhất để hiểu MCP trong thực tế.
Bai viet co huu ich khong?
Bản quyền thuộc về tác giả. Vui lòng dẫn nguồn khi chia sẻ.






