Model Context Protocol (MCP) 的運作原理

隨著 Large Language Model (LLM) Agent 的發展,如何讓 AI 存取外部資料與執行工具(tools)成為關鍵問題。

Model Context Protocol (MCP) 提供了一個標準化方式,讓 LLM application 可以透過統一介面連接各種外部能力。

MCP 的核心架構可以簡化成三個角色:
1. MCP Host
2. MCP Client
3. MCP Server

1. MCP Host:Agent 的入口

MCP Host 是使用 MCP 的主要 application,例如:
* Claude Desktop
* Claude Code
* 自建的 Agent Platform

它負責管理 LLM,以及決定需要連接哪些 MCP Server。

當 Host 需要使用某個 MCP Server 時,會建立一個對應的 MCP Client。

2. MCP Client:Protocol Adapter

MCP Client 是 Host 內部的一個 component。

它的責任是:
* 建立與 MCP Server 的 connection
* 交換 MCP protocol messages
* 取得 server 提供的 tools
* 呼叫指定的 tool

一個 MCP Host 可以同時建立多個 MCP Client:
MCP Host
 |
 +-- MCP Client 1 ---> MCP Server A
 |
 +-- MCP Client 2 ---> MCP Server B
 |
 +-- MCP Client 3 ---> MCP Server C
每個 MCP Client 對應一個 MCP Server。

3. MCP Server:能力提供者

MCP Server 是實際提供能力的 component。

它主要提供兩類功能:

(1) List Tools

Client 可以詢問:
你有哪些 tools 可以使用?
Server 回傳 tool description,例如:
{
  "name": "fetch",
  "description": "Fetch webpage content",
  "parameters": {
      "url": "string"
  }
}
LLM 可以根據這些 metadata 理解:
* tool 的用途
* input parameters
* 如何呼叫

(2) Call Tool

當 LLM 決定使用某個 tool 時:
MCP Client
    |
    | call_tool(fetch, url)
    |
MCP Server
MCP Server 執行實際工作,並回傳結果。

MCP Server 本質上是 API Wrapper

很多 MCP Server 本身並不提供資料,而是包裝既有 API。

例如:
LLM
 |
 | Natural Language
 |
MCP Server
 |
 | API Call
 |
External Service

MCP Server 的價值在於:

把原本:
REST API
GraphQL API
Database Query
Cloud Service
轉換成 LLM 可以理解與使用的 tool interface。

例如:
原本需要:
GET /weather?city=taipei
透過 MCP 後:
get_weather("Taipei")
LLM 可以直接理解。

MCP Server 通常跑在哪裡?

一個常見誤解是:
MCP Server 是遠端 Internet Server。
實際上,大部分 MCP Server 是跑在本機環境。

例如:
User Computer

+----------------------+
| Claude Desktop       |
|                      |
| MCP Client           |
|        |             |
|        | STDIO       |
|        v             |
| MCP Server           |
|                      |
+----------------------+
MCP Server 可能:
1. 本機執行
2. 再去呼叫遠端 API

例如:
Local MCP Server
        |
        |
        v
Google API
GitHub API
Database
Garmin API

因此 MCP 更像是一個:
Local Agent Capability Adapter

MCP Transport Mechanism

MCP Client 與 MCP Server 之間需要 communication channel。

目前主要有兩種:

1. STDIO (Standard Input / Output)

這是最常見的 local MCP Server 方式。

概念非常簡單:
MCP Client
     |
     | stdin
     |
MCP Server Process
     |
     | stdout
     |
MCP Client

Client 啟動一個 process:
例如:
Python MCP Server:
uvx some-mcp-server
Node.js MCP Server:
npx some-mcp-server

Client 透過:
* stdin 傳送 request
* stdout 接收 response
與 server 溝通。

2. Streamable HTTP

用於 remote MCP Server。
架構:
MCP Client
      |
      |
 HTTP Connection
      |
      v
Remote MCP Server
例如:
https://example.com/mcp
當 MCP Server 位於 cloud environment 時,就使用 Streamable HTTP。

MCP Server 如何被啟動?

對於 local MCP Server,configuration 通常描述:
「如何啟動這個 server」。
例如:
Python:
{
  "command": "uvx",
  "args": [
      "fetch-mcp-server"
  ]
}
意思是:
啟動:
uvx fetch-mcp-server
Node.js:
{
  "command": "npx",
  "args": [
      "playwright-mcp"
  ]
}
意思是:
啟動:
npx playwright-mcp
也可以:
docker run xxx-mcp-server
啟動 Docker container。

MCP 的核心價值

傳統 LLM:
User
 |
LLM
 |
Text Response
能力有限。

加入 MCP:
User
 |
LLM Agent
 |
MCP Client
 |
MCP Server
 |
+----------------+
| API            |
| Database       |
| File System    |
| Device Data    |
+----------------+

LLM 不需要知道每個 service 的 API 細節。
它只需要知道:
* 有哪些 tools
* tool 的用途
* input/output schema

MCP 負責把 AI 與外部世界連接起來。

總結

MCP 可以理解為:
A standard protocol that allows LLM applications to discover and use external capabilities through a unified tool interface.

它的核心架構:
MCP Host
    |
    |
MCP Client
    |
    |
MCP Server
    |
    |
External Tools / APIs

其中:
* Host:管理 Agent application
* Client:負責 protocol communication
* Server:提供 tools capability

MCP 的重要性在於,它將「LLM 的語言理解能力」與「外部世界的 software capability」連接起來,成為 Agentic AI 架構中的重要基礎 layer。

參考資料:

Master AI Agents in 30 days: build 8 real-world projects with OpenAI Agents SDK, CrewAI, LangGraph, AutoGen and MCP., Ed Donner