LESSON 1 / 78免費試讀
(點選上方圖片觀看本課程影片)
生成式 AI 應用是一個重要的進步,因為它們通常允許使用者使用自然語言提示與應用互動。然而,隨著在這些應用上投入的時間和資源增加,你需要確保能輕鬆整合功能和資源,使得擴充變得容易,應用能夠支援多個模型的使用,並處理各種模型細節。簡而言之,建置生成式 AI 應用開始時很簡單,但隨著應用的發展和複雜化,你需要開始定義架構,並可能需要依賴某個標準來確保應用以一致的方式建置。這就是 MCP 出現的地方,用於組織事務並提供標準。
模型上下文協議(MCP) 是一個開放的、標準化的介面,允許大型語言模型(LLM)無縫地與外部工具、API 和資料來源互動。它提供了一種一致的架構,增強了 AI 模型的功能,超越了它們的訓練資料,實現更智慧、可擴充和響應更快的 AI 系統。
隨著生成式 AI 應用變得更加複雜,採用標準以確保可擴充性、可延展性、可維護性和避免供應商鎖定變得至關重要。MCP 透過以下方式滿足這些需求:
**注意:**雖然 MCP 自稱為開放標準,但目前沒有計劃透過任何現有標準機構如 IEEE、IETF、W3C、ISO 或其他標準機構來標準化 MCP。
在閱讀本文後,你將能夠:
在 MCP 之前,整合模型與工具需要:
| 好處 | 描述 |
|---|---|
| 互操作性 | 大型語言模型可無縫協作不同供應商的工具 |
| 一致性 | 跨平臺和工具的統一行為 |
| 可重用性 | 建置一次的工具可在多個專案和系統中使用 |
| 加速開發 | 透過使用標準化、即插即用的介面減少開發時間 |
MCP 遵循客戶端-伺服器模型,其中:
2026-07-28釋出候選版本中已棄用)2026-07-28釋出候選版本中已棄用)MCP 採用兩層架構:
MCP 伺服器的操作方式如下:
---
title: MCP Architecture and Component Interactions
description: A diagram showing the flows of the components in MCP.
---
graph TD
Client[MCP Client/Application] -->|Sends Request| H[MCP Host]
H -->|Invokes| A[AI Model]
A -->|Tool Call Request| H
H -->|MCP Protocol| T1[MCP Server Tool 01: Web Search]
H -->|MCP Protocol| T2[MCP Server Tool 02: Calculator tool]
H -->|MCP Protocol| T3[MCP Server Tool 03: Database Access tool]
H -->|MCP Protocol| T4[MCP Server Tool 04: File System tool]
H -->|Sends Response| Client
subgraph "MCP Host Components"
H
G[Tool Registry]
I[Authentication]
J[Request Handler]
K[Response Formatter]
end
H <--> G
H <--> I
H <--> J
H <--> K
style A fill:#f9d5e5,stroke:#333,stroke-width:2px
style H fill:#eeeeee,stroke:#333,stroke-width:2px
style Client fill:#d5e8f9,stroke:#333,stroke-width:2px
style G fill:#fffbe6,stroke:#333,stroke-width:1px
style I fill:#fffbe6,stroke:#333,stroke-width:1px
style J fill:#fffbe6,stroke:#333,stroke-width:1px
style K fill:#fffbe6,stroke:#333,stroke-width:1px
style T1 fill:#c2f0c2,stroke:#333,stroke-width:1px
style T2 fill:#c2f0c2,stroke:#333,stroke-width:1px
style T3 fill:#c2f0c2,stroke:#333,stroke-width:1px
style T4 fill:#c2f0c2,stroke:#333,stroke-width:1px
MCP 伺服器允許你透過提供資料和功能擴充 LLM 能力。
準備好試一試了嗎?以下是不同語言和/或技術棧的 SDK,附有建立簡單 MCP 伺服器的示例:
Python SDK: https://github.com/modelcontextprotocol/python-sdk
TypeScript SDK: https://github.com/modelcontextprotocol/typescript-sdk
C#/.NET SDK: https://github.com/modelcontextprotocol/csharp-sdk
MCP 透過擴充 AI 能力支援廣泛的應用:
| 應用 | 描述 |
|---|---|
| 企業資料整合 | 連線 LLM 與資料庫、CRM 或內部工具 |
| 代理式 AI 系統 | 實現具備工具訪問和決策工作流的自治代理 |
| 多模態應用 | 在單一統一 AI 應用中結合文字、影象和音訊工具 |
| 即時資料整合 | 將即時資料引入 AI 互動,實現更準確、及時的輸出 |
模型上下文協議(MCP)充當了 AI 互動的通用標準,就像 USB-C 統一了裝置的物理連線方式一樣。在 AI 領域,MCP 提供了一致的介面,使模型(客戶端)能無縫整合外部工具和資料提供方(伺服器)。這消除了為每個 API 或資料來源開發不同定製協議的需求。
在 MCP 下,相容 MCP 的工具(稱為 MCP 伺服器)遵循統一標準。這些伺服器能列出它們提供的工具或操作,並在 AI 代理請求時執行這些操作。支援 MCP 的 AI 代理平臺能發現伺服器上的可用工具,並透過該標準協議呼叫它們。
除了提供工具外,MCP 還促進了知識訪問。它使應用可以透過連線各種資料來源為大型語言模型(LLM)提供上下文。例如,一個 MCP 伺服器可能代表公司的檔案庫,讓代理按需檢索相關資訊。另一個伺服器可以處理傳送郵件或更新記錄等特定操作。從代理的角度看,這些都是它可以使用的工具——一些工具返回資料(知識上下文),另一些執行操作。MCP 高效地管理兩者。
連線到 MCP 伺服器的代理能夠透過標準格式自動了解伺服器的可用功能和可訪問資料。這種標準化實現了動態工具可用性。例如,向代理系統新增新 MCP 伺服器,其功能立即可用,無需進一步定製代理指令。
這種簡化的整合符合下圖所示的流程,其中伺服器提供工具和知識,確保系統間無縫協作。
---
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 伺服器能夠相互通訊和共享功能,允許 ServerA 委託任務給 ServerB 或訪問其工具和知識。這在伺服器間實現工具和資料的聯合,支援可擴充和模組化的代理架構。由於 MCP 標準化了工具暴露,代理能動態發現並在伺服器間路由請求,無需硬編碼整合。
工具和知識聯合:工具和資料可跨伺服器訪問,實現更可擴充和模組化的代理架構。
除了基本的 MCP 架構,還有一些高階場景,其中客戶端和伺服器都包含 LLM,實現更複雜的互動。下圖中,客戶端應用可能是帶有多個 MCP 工具的 IDE,由 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 的主要收穫:
思考一個你感興趣建置的 AI 應用。
下一課:MCP 核心概念
內容來源:本頁取自 MCP for Beginners,
原文連結:https://github.com/microsoft/mcp-for-beginners/blob/main/00-Introduction/README.mdMIT License, Copyright (c) Microsoft Corporation。
這門課上游沒有 zh-TW 官方翻譯,只有translations/zh-CN(由
Co-op Translator 機器翻譯產生的簡體中文)。
中文譯文以這份簡體譯稿為底稿,先做簡轉繁字形轉換,再校訂用詞(陸式用語換成台式用語)、
標點與連結,並抽查修正機器翻譯的錯誤,最後補上重點整理;少數上游簡體譯稿缺漏的段落
則直接依英文原文補譯。
非官方版本,內容如與英文原文有出入,以原文為準。程式碼範例請直接使用原始 repo。