生成式 AI 應用是一大進步,因為它們常常讓使用者可以用自然語言提示與應用互動。然而,隨著投入更多時間和資源在這類應用上,你會希望能輕鬆整合各種功能和資源,讓系統易於擴充,能同時支援多個模型並處理各種模型的細節。簡言之,建立生成式 AI 應用一開始很簡單,但隨著規模擴大和複雜度增加,你需要開始定義架構,並可能需要依賴標準來確保應用以一致的方式建置。這正是 MCP 出場的地方,它幫助組織架構並提供標準。
Model Context Protocol (MCP) 是一個開放且標準化的介面,讓大型語言模型(LLMs)能夠無縫與外部工具、API 及資料來源互動。它提供一致的架構,擴展 AI 模型功能超越訓練資料,讓 AI 系統更聰明、可擴展且反應更靈敏。
隨著生成式 AI 應用變得越來越複雜,採用標準以確保可擴展性、可延展性與可維護性變得至關重要。MCP 透過以下方式滿足這些需求:
- 統一模型與工具的整合
- 減少脆弱且一次性的客製解決方案
- 允許多個模型共存於同一生態系統中
閱讀本文後,你將能夠:
- 定義 Model Context Protocol (MCP) 及其應用場景
- 理解 MCP 如何標準化模型與工具的溝通
- 識別 MCP 架構的核心元件
- 探索 MCP 在企業及開發環境中的實際應用
在 MCP 出現之前,模型與工具的整合需要:
- 為每對工具與模型撰寫客製化程式碼
- 每個廠商使用非標準 API
- 更新時常常導致中斷
- 工具數量增加時擴展性差
| 好處 | 說明 |
|---|---|
| 互通性 | LLM 能與不同廠商的工具無縫合作 |
| 一致性 | 在各平台與工具間行為一致 |
| 可重用性 | 工具建置一次即可跨專案與系統使用 |
| 加速開發 | 利用標準化、即插即用介面減少開發時間 |
MCP 採用客戶端-伺服器模型,其中:
- MCP Hosts 執行 AI 模型
- MCP Clients 發起請求
- MCP Servers 提供上下文、工具與功能
- Resources – 靜態或動態的模型資料
- Prompts – 預設的工作流程以引導生成
- Tools – 可執行的功能,如搜尋、計算
- Sampling – 透過遞迴互動實現代理行為
MCP 伺服器的運作方式如下:
-
請求流程:
- MCP Client 向運行在 MCP Host 上的 AI 模型發送請求。
- AI 模型判斷何時需要外部工具或資料。
- 模型透過標準化協議與 MCP Server 通訊。
-
MCP Server 功能:
- 工具註冊表:維護可用工具及其功能目錄。
- 身份驗證:確認工具存取權限。
- 請求處理器:處理模型送來的工具請求。
- 回應格式化器:將工具輸出整理成模型可理解的格式。
-
工具執行:
- 伺服器將請求導向適當的外部工具。
- 工具執行其專門功能(搜尋、計算、資料庫查詢等)。
- 結果以一致格式回傳給模型。
-
回應完成:
- AI 模型將工具輸出整合進回應中。
- 最終回應回傳給客戶端應用。
---
title: MCP Server Architecture and Component Interactions
description: A diagram showing how AI models interact with MCP servers and various tools, depicting the request flow and server components including Tool Registry, Authentication, Request Handler, and Response Formatter
---
graph TD
A[AI Model in MCP Host] <-->|MCP Protocol| B[MCP Server]
B <-->|Tool Interface| C[Tool 1: Web Search]
B <-->|Tool Interface| D[Tool 2: Calculator]
B <-->|Tool Interface| E[Tool 3: Database Access]
B <-->|Tool Interface| F[Tool 4: File System]
Client[MCP Client/Application] -->|Sends Request| A
A -->|Returns Response| Client
subgraph "MCP Server Components"
B
G[Tool Registry]
H[Authentication]
I[Request Handler]
J[Response Formatter]
end
B <--> G
B <--> H
B <--> I
B <--> J
style A fill:#f9d5e5,stroke:#333,stroke-width:2px
style B fill:#eeeeee,stroke:#333,stroke-width:2px
style Client fill:#d5e8f9,stroke:#333,stroke-width:2px
style C fill:#c2f0c2,stroke:#333,stroke-width:1px
style D fill:#c2f0c2,stroke:#333,stroke-width:1px
style E fill:#c2f0c2,stroke:#333,stroke-width:1px
style F fill:#c2f0c2,stroke:#333,stroke-width:1px
MCP 伺服器讓你能擴充 LLM 功能,提供資料與功能。
準備試試看嗎?以下是用不同語言建立簡單 MCP 伺服器的範例:
-
Python 範例:https://github.com/modelcontextprotocol/python-sdk
-
TypeScript 範例:https://github.com/modelcontextprotocol/typescript-sdk
-
C#/.NET 範例:https://github.com/modelcontextprotocol/csharp-sdk
MCP 擴展 AI 功能,支持多種應用:
| 應用 | 說明 |
|---|---|
| 企業資料整合 | 連接 LLM 與資料庫、CRM 或內部工具 |
| 代理 AI 系統 | 讓自主代理可使用工具並執行決策流程 |
| 多模態應用 | 在單一統一 AI 應用中結合文字、影像與音訊工具 |
| 即時資料整合 | 將即時資料引入 AI 互動,提供更準確且即時的結果 |
Model Context Protocol (MCP) 就像 USB-C 統一了裝置間的實體連接一樣,是 AI 互動的通用標準。在 AI 領域,MCP 提供一致介面,讓模型(客戶端)能無縫整合外部工具與資料提供者(伺服器),避免為每個 API 或資料來源設計多樣化、客製化的協議。
符合 MCP 標準的工具(稱為 MCP 伺服器)遵循統一標準,能列出提供的工具或動作,並在 AI 代理請求時執行。支援 MCP 的 AI 代理平台能從伺服器發現可用工具,並透過此標準協議呼叫它們。
除了提供工具,MCP 也促進知識存取。它讓應用能將上下文提供給大型語言模型(LLMs),連結到各種資料來源。例如,某個 MCP 伺服器可能代表公司的文件庫,讓代理能按需檢索相關資訊。另一個伺服器則可處理特定動作,如寄送電子郵件或更新紀錄。對代理而言,這些都只是可用工具——有些工具回傳資料(知識上下文),有些則執行動作。MCP 高效管理這兩種情況。
代理連接 MCP 伺服器時,會自動透過標準格式學習該伺服器的可用功能與資料。這種標準化讓工具可動態新增。舉例來說,將新的 MCP 伺服器加入代理系統後,其功能即刻可用,無需額外修改代理指令。
這種精簡的整合流程如同 mermaid 圖示所示,伺服器同時提供工具與知識,確保系統間的無縫協作。
---
title: Scalable Agent Solution with MCP
description: A diagram illustrating how a user interacts with an LLM that connects to multiple MCP servers, with each server providing both knowledge and tools, creating a scalable AI system architecture
---
graph TD
User -->|Prompt| LLM
LLM -->|Response| User
LLM -->|MCP| ServerA
LLM -->|MCP| ServerB
ServerA -->|Universal connector| ServerB
ServerA --> KnowledgeA
ServerA --> ToolsA
ServerB --> KnowledgeB
ServerB --> ToolsB
subgraph Server A
KnowledgeA[Knowledge]
ToolsA[Tools]
end
subgraph Server B
KnowledgeB[Knowledge]
ToolsB[Tools]
end
除了基本 MCP 架構,還有進階場景中,客戶端與伺服器雙方都包含 LLM,實現更複雜的互動:
---
title: Advanced MCP Scenarios with Client-Server LLM Integration
description: A sequence diagram showing the detailed interaction flow between user, client application, client LLM, multiple MCP servers, and server LLM, illustrating tool discovery, user interaction, direct tool calling, and feature negotiation phases
---
sequenceDiagram
autonumber
actor User as 👤 User
participant ClientApp as 🖥️ Client App
participant ClientLLM as 🧠 Client LLM
participant Server1 as 🔧 MCP Server 1
participant Server2 as 📚 MCP Server 2
participant ServerLLM as 🤖 Server LLM
%% Discovery Phase
rect rgb(220, 240, 255)
Note over ClientApp, Server2: TOOL DISCOVERY PHASE
ClientApp->>+Server1: Request available tools/resources
Server1-->>-ClientApp: Return tool list (JSON)
ClientApp->>+Server2: Request available tools/resources
Server2-->>-ClientApp: Return tool list (JSON)
Note right of ClientApp: Store combined tool<br/>catalog locally
end
%% User Interaction
rect rgb(255, 240, 220)
Note over User, ClientLLM: USER INTERACTION PHASE
User->>+ClientApp: Enter natural language prompt
ClientApp->>+ClientLLM: Forward prompt + tool catalog
ClientLLM->>-ClientLLM: Analyze prompt & select tools
end
%% Scenario A: Direct Tool Calling
alt Direct Tool Calling
rect rgb(220, 255, 220)
Note over ClientApp, Server1: SCENARIO A: DIRECT TOOL CALLING
ClientLLM->>+ClientApp: Request tool execution
ClientApp->>+Server1: Execute specific tool
Server1-->>-ClientApp: Return results
ClientApp->>+ClientLLM: Process results
ClientLLM-->>-ClientApp: Generate response
ClientApp-->>-User: Display final answer
end
%% Scenario B: Feature Negotiation (VS Code style)
else Feature Negotiation (VS Code style)
rect rgb(255, 220, 220)
Note over ClientApp, ServerLLM: SCENARIO B: FEATURE NEGOTIATION
ClientLLM->>+ClientApp: Identify needed capabilities
ClientApp->>+Server2: Negotiate features/capabilities
Server2->>+ServerLLM: Request additional context
ServerLLM-->>-Server2: Provide context
Server2-->>-ClientApp: Return available features
ClientApp->>+Server2: Call negotiated tools
Server2-->>-ClientApp: Return results
ClientApp->>+ClientLLM: Process results
ClientLLM-->>-ClientApp: Generate response
ClientApp-->>-User: Display final answer
end
end
使用 MCP 的實際好處包括:
- 資訊新鮮度:模型能存取超越訓練資料的即時資訊
- 能力擴充:模型可利用專門工具完成未訓練的任務
- 減少幻覺:外部資料來源提供事實依據
- 隱私保護:敏感資料可保留在安全環境,不需嵌入提示中
MCP 使用的重點包括:
- MCP 標準化 AI 模型與工具及資料的互動方式
- 促進 可擴展性、一致性與互通性
- MCP 有助於 縮短開發時間、提升可靠性並擴展模型能力
- 客戶端-伺服器架構支持靈活且可延展的 AI 應用
思考你有興趣開發的 AI 應用:
- 哪些外部工具或資料能提升它的功能?
- MCP 如何讓整合變得更簡單且更可靠?
免責聲明:
本文件係使用 AI 翻譯服務 Co-op Translator 進行翻譯。雖然我們致力於準確性,但請注意,自動翻譯可能包含錯誤或不準確之處。原始文件的母語版本應視為權威來源。對於重要資訊,建議採用專業人工翻譯。我們不對因使用本翻譯所引起之任何誤解或誤譯負責。