MCP Protocol — Cầu Nối AI Với Mọi Hệ Thống

@Nguyễn Ngô Thượng//~6 phút đọc0
Chia sẻ:

import { Alert } from '@/components/mdx/alert';

MCP Protocol — Cầu Nối AI Với Mọi Hệ thống

TL;DR: MCP (Model Context Protocol) là chuẩn kết nối AI với external systems — giống như USB-C cho AI. 1 cổng duy nhất, kết nối mọi thứ: database, Lark, n8n, file system, API. Biết MCP = biết mở rộng khả năng AI vô hạn.


Vấn Đề Trước Khi Có MCP

Trước MCP, mỗi AI tool có cách kết nối riêng với hệ thống:

ChatGPT → cần plugin riêng → GPT plugin format
Claude → cần custom integration → Claude tool format  
Cursor → cần extension → VS Code extension format
GitHub Copilot → cần GitHub integration

→ Chaos. Developer phải build nhiều integrations cho nhiều AI tools.

ℹ️ Info: Tương tự: Trước USB-C, mỗi thiết bị có cổng riêng — Lightning, Micro USB, USB-A, Mini USB. Rồi USB-C ra đời → 1 cổng cho tất cả.


MCP Là Gì?

MCP = Model Context Protocol

chuẩn mở (open standard) do Anthropic phát triển năm 2024, cho phép AI kết nối với bất kỳ hệ thống nào qua 1 giao thức duy nhất.

AI (Host) ←→ MCP Client ←→ MCP Server ←→ External System (DB, API, File...)

Tại Sao MCP Quan Trọng?

Trước MCP Với MCP
Mỗi AI = 1 integration riêng 1 integration → dùng cho mọi AI
Developer phải code nhiều version Code 1 lần → works everywhere
Khó maintain Dễ maintain, chuẩn hóa
Limited tool ecosystem Open ecosystem, ai cũng build được

Success: MCP = USB-C của AI
1 cổng duy nhất → kết nối mọi hệ thống → mọi AI tool đều dùng được.


Kiến Trúc MCP

3 Thành Phần Chính

┌─────────────────────────────────────────────┐
│                 MCP Host                     │
│  (Ứng dụng chứa AI)                          │
│  Claude Desktop, VS Code, Cursor, ...        │
│                                              │
│  ┌──────────────┐                            │
│  │ MCP Client   │ ← Kết nối từ Host          │
│  └──────┬───────┘                            │
└─────────┼────────────────────────────────────┘
          │
          │ MCP Protocol
          │
┌─────────┼────────────────────────────────────┐
│         ▼                                     │
│  ┌──────────────┐                            │
│  │ MCP Server   │ ← Cung cấp tools/resources │
│  └──────┬───────┘                            │
│         │                                     │
│         ▼                                     │
│  External System (PostgreSQL, Lark, ...)      │
└──────────────────────────────────────────────┘

1. MCP Host

Ứng dụng chứa AI model.

Host Mô tả
Claude Desktop App desktop của Anthropic
VS Code (với MCP extension) IDE phổ biến nhất
Cursor AI-first code editor
Custom app App bạn tự build

2. MCP Client

Kết nối từ Host đến Server. Thường built-in trong Host.

3. MCP Server

Cung cấp toolsresources cho AI.

MCP Server Cung cấp
PostgreSQL Server Query database
Filesystem Server Đọc/ghi file
Lark Server CRUD Bitable, send message
n8n Server Trigger workflows
GitHub Server Read repos, create PRs
Slack Server Send messages, read channels

MCP Server Hoạt Động Như Thế Nào?

MCP Server expose 2 thứ:

1. Tools

Functions AI có thể gọi.

{
  "tools": [
    {
      "name": "query_postgres",
      "description": "Run SQL query on PostgreSQL database",
      "inputSchema": {
        "type": "object",
        "properties": {
          "sql": { "type": "string", "description": "SQL query to execute" }
        },
        "required": ["sql"]
      }
    }
  ]
}

2. Resources

Data AI có thể đọc.

{
  "resources": [
    {
      "uri": "file:///project/src/index.ts",
      "name": "index.ts",
      "description": "Main entry point",
      "mimeType": "text/typescript"
    }
  ]
}

Ví dụ Thực Tế: MCP với PostgreSQL

Step 1: Install MCP Server

npm install -D @modelcontextprotocol/server-postgres

Step 2: Configure MCP Host (Claude Desktop)

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://localhost:5432/mydb"
      ]
    }
  }
}

Step 3: AI Dùng MCP

User: "Có bao nhiêu đơn hàng trong tháng 3?"

AI → generate MCP tool call:
{
  "tool": "query_postgres",
  "args": {
    "sql": "SELECT COUNT(*) FROM orders WHERE created_at >= '2026-03-01' AND created_at < '2026-04-01'"
  }
}

MCP Server → execute query → return result: { count: 1250 }

AI → "Có 1,250 đơn hàng trong tháng 3."

⚠️ Warning: Bảo mật: MCP Server cho AI query database trực tiếp → rủi ro SQL injection, data leak. Nên:

  • Dùng read-only database user
  • Validate SQL trước khi execute
  • Limit accessible tables/views

MCP tại Diginno

Diginno dùng MCP để:

MCP Server Dùng cho Benefit
PostgreSQL MCP Query data khách hàng AI phân tích data real-time
Lark MCP CRUD Bitable, send messages AI sync data Lark tự động
n8n MCP Trigger workflows AI orchestrate automation
Filesystem MCP Read project files AI understand codebase context
Custom MCP Pancake POS, Sepay, Zalo AI tích hợp hệ thống VN

Cách Build MCP Server Đơn Giản

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";

const server = new McpServer({
  name: "diginno-tools",
  version: "1.0.0"
});

// Register tool
server.tool(
  "get_customer_orders",
  "Get orders for a customer by email",
  {
    email: z.string().email()
  },
  async ({ email }) => {
    // Query database
    const orders = await db.orders.findMany({
      where: { customer_email: email }
    });
    
    return {
      content: [{
        type: "text",
        text: JSON.stringify(orders, null, 2)
      }]
    };
  }
);

// Start server
const transport = new StdioServerTransport();
await server.connect(transport);

→ Giờ AI có thể gọi get_customer_orders để lấy đơn hàng theo email.


Bài Tập Thực Hành

Task 1: Setup MCP Server

  1. Install PostgreSQL MCP server (hoặc filesystem server nếu không có DB)
  2. Configure vào Claude Desktop hoặc Cursor
  3. Test: hỏi AI query data → check kết quả

Task 2: Design MCP Server

Design 1 MCP server cho use case: "Quản lý đơn hàng"

  • Tools cần có: ?
  • Resources cần có: ?
  • Input/output format: ?

Task 3: Multi-MCP Setup

Configure 2 MCP servers (filesystem + postgres) vào cùng 1 host. Test xem AI có thể dùng cả 2 cùng lúc không.


Tóm Tắt

Khái niệm Nội dung
MCP Model Context Protocol — chuẩn kết nối AI với hệ thống
Host Ứng dụng chứa AI (Claude Desktop, VS Code, Cursor)
Server Cung cấp tools + resources cho AI
Tool Function AI gọi được (query DB, send message...)
Resource Data AI đọc được (files, documents...)
Benefit 1 integration → works với mọi AI tool
Rủi ro Security — AI có thể query/delete data nếu không limit
Key takeaway MCP là "USB-C của AI" — 1 cổng kết nối mọi hệ thống

Bài Tiếp Theo

Bài 7: Agent — Khi AI Tự Làm Việc

Bài 5: Tool Use — Khi AI Biết "Dùng Tay"


Liên hệ tư vấn

Bài viết hữu ích?

Chia sẻ để nhiều người biết đến!

Chia sẻ:

>_ LLM-Friendly Copy

Copy as Markdown to use with ChatGPT, Claude, or other AI tools

1,224 words|7,730 characters

//Bình luận

Bài viết liên quan

Khám phá thêm những bài viết cùng chủ đề với MCP Protocol — Cầu Nối AI Với Mọi Hệ Thống

Bài viết hữu ích? Hãy kết nối với Diginno!

Chúng tôi giúp doanh nghiệp SME ứng dụng AI và automation vào quy trình làm việc - từ tư vấn chiến lược đến triển khai thực tế.