Bỏ qua để đến nội dung

MCP Clients

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.


┌─────────────────────────────────────┐
│ HOST │
│ │
│ ┌─────────────────────────────┐ │
│ │ CLIENT │ │
│ │ │ │
│ │ - Session management │ │
│ │ - Request/Response queue │ │
│ │ - Error handling │ │
│ │ - Reconnection logic │ │
│ │ │ │
│ └─────────────┬───────────────┘ │
│ │ │
└────────────────┼────────────────────┘
┌────────┴────────┐
│ TRANSPORT │
│ (stdio / SSE) │
└────────┬────────┘
┌─────────────┐
│ SERVER │
└─────────────┘

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

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

Mọi message đều theo format JSON-RPC:

{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "read_file",
"arguments": {
"path": "/Users/me/document.txt"
}
}
}
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"content": [
{
"type": "text",
"text": "File content here..."
}
]
}
}
{
"jsonrpc": "2.0",
"method": "notifications/progress",
"params": {
"progressToken": "task-1",
"progress": 50,
"total": 100
}
}

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

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.


Hiểu cách Client giao tiếp bằng cách xem logs.

  1. Chạy MCP server với debug mode:
Terminal window
npx -y @modelcontextprotocol/server-filesystem /tmp --debug
  1. Quan sát stdin/stdout trong terminal

  2. Hoặc sử dụng MCP Inspector:

Terminal window
npx @modelcontextprotocol/inspector
  1. Kết nối server và gửi request thủ công
  • Request tools/list trả về những tools nào?
  • Thử gọi tools/call với tool read_file

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.