MCP Clients
Khái niệm cơ bản
Phần tiêu đề “Khái niệm cơ bản”Tưởng tượng Client như “adapter” sạc điện thoại…
- Ổ cắm (Server) cho ra điện 220V
- Adapter (Client) chuyển đổi thành 5V USB
- Điện thoại (LLM) nhận điện an toàn
Client = Protocol layer nằm trong Host, dịch ngôn ngữ giữa Host và Server.
Kiến trúc Client
Phần tiêu đề “Kiến trúc Client”┌─────────────────────────────────────┐│ HOST ││ ││ ┌─────────────────────────────┐ ││ │ CLIENT │ ││ │ │ ││ │ - Session management │ ││ │ - Request/Response queue │ ││ │ - Error handling │ ││ │ - Reconnection logic │ ││ │ │ ││ └─────────────┬───────────────┘ ││ │ │└────────────────┼────────────────────┘ │ ┌────────┴────────┐ │ TRANSPORT │ │ (stdio / SSE) │ └────────┬────────┘ │ ▼ ┌─────────────┐ │ SERVER │ └─────────────┘Transport Types
Phần tiêu đề “Transport Types”1. Stdio (Standard Input/Output)
Phần tiêu đề “1. Stdio (Standard Input/Output)”Server chạy như subprocess, giao tiếp qua stdin/stdout.
HOST SERVER │ │ │ ──────stdin────────────▶ │ │ {"jsonrpc":"2.0",...} │ │ │ │ ◀─────stdout─────────── │ │ {"result":{...}} │ │ │Config ví dụ:
{ "filesystem": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path"] }}| Ưu điểm | Nhược điểm |
|---|---|
| Nhanh (no network overhead) | Chỉ chạy local |
| Bảo mật (no ports exposed) | Không share được |
| Đơn giản setup | Server phải restart với Host |
2. SSE (Server-Sent Events)
Phần tiêu đề “2. SSE (Server-Sent Events)”Server chạy như HTTP server, client kết nối qua network.
HOST (Browser/App) SERVER (HTTP) │ │ │ ──POST /message────────▶ │ │ {"jsonrpc":"2.0",...} │ │ │ │ ◀─────SSE stream────── │ │ event: message │ │ data: {"result":{...}} │ │ │Config ví dụ:
{ "remote-server": { "url": "https://mcp.example.com", "transport": "sse", "headers": { "Authorization": "Bearer token123" } }}| Ưu điểm | Nhược điểm |
|---|---|
| Remote connections | Cần network setup |
| Scalable | Security concerns |
| Shareable across users | Latency overhead |
JSON-RPC 2.0 Protocol
Phần tiêu đề “JSON-RPC 2.0 Protocol”Mọi message đều theo format JSON-RPC:
Request (Client → Server)
Phần tiêu đề “Request (Client → Server)”{ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "read_file", "arguments": { "path": "/Users/me/document.txt" } }}Response (Server → Client)
Phần tiêu đề “Response (Server → Client)”{ "jsonrpc": "2.0", "id": 1, "result": { "content": [ { "type": "text", "text": "File content here..." } ] }}Notification (No response expected)
Phần tiêu đề “Notification (No response expected)”{ "jsonrpc": "2.0", "method": "notifications/progress", "params": { "progressToken": "task-1", "progress": 50, "total": 100 }}Common Methods
Phần tiêu đề “Common Methods”| Method | Direction | Description |
|---|---|---|
initialize |
Client → Server | Handshake, exchange capabilities |
tools/list |
Client → Server | Get available tools |
tools/call |
Client → Server | Execute a tool |
resources/list |
Client → Server | Get available resources |
resources/read |
Client → Server | Read a resource |
prompts/list |
Client → Server | Get available prompts |
prompts/get |
Client → Server | Get prompt template |
So sánh Stdio vs SSE
Phần tiêu đề “So sánh Stdio vs SSE”| Aspect | Stdio | SSE |
|---|---|---|
| Setup | Config command path | Config URL + auth |
| Network | None (local process) | HTTP/HTTPS |
| Security | Inherently sandboxed | Need TLS + auth |
| Use case | Local dev tools | Enterprise, shared |
| Latency | Microseconds | Milliseconds |
| Scale | 1 user per process | Many users |
💡 Khuyến nghị: Bắt đầu với Stdio cho development, chuyển sang SSE khi cần deploy.
Bài tập thực hành
Phần tiêu đề “Bài tập thực hành”Mục tiêu
Phần tiêu đề “Mục tiêu”Hiểu cách Client giao tiếp bằng cách xem logs.
Bước thực hiện
Phần tiêu đề “Bước thực hiện”- Chạy MCP server với debug mode:
npx -y @modelcontextprotocol/server-filesystem /tmp --debug-
Quan sát stdin/stdout trong terminal
-
Hoặc sử dụng MCP Inspector:
npx @modelcontextprotocol/inspector- Kết nối server và gửi request thủ công
Câu hỏi
Phần tiêu đề “Câu hỏi”- Request
tools/listtrả về những tools nào? - Thử gọi
tools/callvới toolread_file
Tóm tắt
Phần tiêu đề “Tóm tắt”| Khái niệm | Ý nghĩa |
|---|---|
| Client | Protocol layer trong Host |
| Stdio | Transport qua stdin/stdout |
| SSE | Transport qua HTTP |
| JSON-RPC | Message format chuẩn |
Bài tiếp theo: MCP Servers - Xây dựng nguồn dữ liệu.