Skip to Content

MCP Clients

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

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

Transport Types

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ểmNhượ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 setupServer phải restart với Host

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ểmNhược điểm
Remote connectionsCần network setup
ScalableSecurity concerns
Shareable across usersLatency overhead

JSON-RPC 2.0 Protocol

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

Request (Client → Server)

{ "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "name": "read_file", "arguments": { "path": "/Users/me/document.txt" } } }

Response (Server → Client)

{ "jsonrpc": "2.0", "id": 1, "result": { "content": [ { "type": "text", "text": "File content here..." } ] } }

Notification (No response expected)

{ "jsonrpc": "2.0", "method": "notifications/progress", "params": { "progressToken": "task-1", "progress": 50, "total": 100 } }

Common Methods

MethodDirectionDescription
initializeClient → ServerHandshake, exchange capabilities
tools/listClient → ServerGet available tools
tools/callClient → ServerExecute a tool
resources/listClient → ServerGet available resources
resources/readClient → ServerRead a resource
prompts/listClient → ServerGet available prompts
prompts/getClient → ServerGet prompt template

So sánh Stdio vs SSE

AspectStdioSSE
SetupConfig command pathConfig URL + auth
NetworkNone (local process)HTTP/HTTPS
SecurityInherently sandboxedNeed TLS + auth
Use caseLocal dev toolsEnterprise, shared
LatencyMicrosecondsMilliseconds
Scale1 user per processMany 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

Mục tiêu

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

Bước thực hiện

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

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

npx @modelcontextprotocol/inspector
  1. Kết nối server và gửi request thủ công

Câu hỏi

  • Request tools/list trả về những tools nào?
  • Thử gọi tools/call với tool read_file

Tóm tắt

Khái niệmÝ nghĩa
ClientProtocol layer trong Host
StdioTransport qua stdin/stdout
SSETransport qua HTTP
JSON-RPCMessage format chuẩn

Bài tiếp theo: MCP Servers - Xây dựng nguồn dữ liệu.

Last updated on