MCP(Model Context Protocol)
是讓 使用外部功能的。挑選並使用功能的智慧體是 MCP 。提供自身功能的程式是 。
什麼是 MCP
(Model Context Protocol)是把 應用連線到外部工具和的。沒有 MCP 的時候,每個 AI 應用都要為每個工具單獨做連線。有了 MCP,你只要做一次,任何支援 MCP 的應用——、、 或你自己寫的——都能使用它。
MCP 由開發 的公司 建立。
MCP 客戶端與 MCP 伺服器
MCP 由兩方對話。
-
你提出請求
-
挑選工具 MCP
-
伺服器執行功能
-
智慧體用結果回答
MCP 客戶端 = 智慧體
挑選並使用工具的一方。Claude Code、Codex 這樣的 AI 智慧體充當 MCP 客戶端,連線到伺服器。
MCP 伺服器 = 提供自身功能的一方
把自己能做的事(功能)透過 MCP 提供給智慧體使用的程式。
:規範裡的叫法,指裝著智慧體的 AI 應用。在這個應用裡,每連線一個伺服器,就有一個 MCP 客戶端連線。
例子:WebMCP
WebMCP 是網站像 MCP 那樣把自己提供出來的方法。它是網頁提供的 ,讓網站把自己的功能作為 AI 智慧體可以使用的工具提供出來。網站開發者在自己網站的里加入 registerTool(),註冊“搜尋商品”“加入購物車”這樣的功能後,智慧體就不必看著螢幕一個個點按鈕,而是直接這些工具。
- 智慧體(MCP 客戶端):被要求“在這家網店找一把傘並加入購物車”的 AI。
- 網站(MCP 伺服器角色):把自己的功能作為工具提供出來,智慧體呼叫時執行並返回結果。網頁瀏覽器是連線兩者的視窗。
也就是說,成為 MCP 伺服器就是“把自己提供給智慧體使用”。它和其他瀏覽器 MCP 的區別,在“使用 MCP”標籤頁裡看。
MCP 伺服器提供的三樣東西
伺服器可以提供三類東西:
工具(Tools)
AI 可以呼叫的函式,比如“搜尋筆記”或“新增一行”。
應用可以讀取的資料,比如一個檔案或一條記錄,每個都有自己的地址()。
(Prompts)
使用者可以挑選的現成訊息。
輕鬆理解
- 可以這樣想
- MCP 就像 AI 應用的 USB 介面。有 USB 之前,每臺印表機和鍵盤都需要自己的插頭;現在任何帶 USB 插頭的裝置都能插進任何電腦。MCP 伺服器是裝置,AI 應用是電腦,因為插頭形狀相同,所以你只要做一次伺服器,所有 MCP 應用都能用。
- 例子
- strange-add 伺服器提供一個工具:把兩個數相加,再加上一個 1 到 100 之間的隨機數。
- 你用一條命令把它連線到 Claude Code。
- 你說“用 strange-add 把 2 和 3 相加”:AI 讀工具描述,徵求你的批准,然後客戶端在伺服器上呼叫 strange-add。答案不是 5——這證明工具真的執行了。
- 常見錯誤
- 找到什麼 MCP 伺服器就裝什麼。伺服器會用你的在你的電腦上執行程式碼。先讀一讀,或者選一個可信的,並且只給它需要的資料夾或。
把 MCP 伺服器連線到智慧體
要連線到上才能用。連線命令( · codex add)、範圍和確認方法每個工具都不一樣,所以分別寫在各個工具頁面的 MCP 單元裡,各寫一次。
WebMCP vs Chrome DevTools MCP vs Playwright MCP
三者都讓智慧體在網頁裡工作。不同的是由誰來準備,以及智慧體怎麼工作。WebMCP 是網站一方宣告“我的網站可以使用這些功能”的瀏覽器 ;另外兩個是安裝在智慧體一方、在任何網站上都直接操作瀏覽器的 MCP 伺服器。
| 專案 | WebMCP | Chrome DevTools MCP | Playwright MCP |
|---|---|---|---|
| 是什麼 | 網站使用的瀏覽器 API——仍處於提案階段的網頁標準(Web 社群組草案) | 安裝在智慧體一方的 MCP 伺服器(Google 團隊) | 安裝在智慧體一方的 MCP 伺服器(Microsoft) |
| 由誰準備 | 網站開發者——在自己網站的里加入 registerTool() 來註冊工具 | 使用智慧體的人——把新增到自己的智慧體 | 使用智慧體的人——把伺服器新增到自己的智慧體 |
| 智慧體怎麼工作 | 按名字網站註冊的工具,不需要讀螢幕去猜。 | 透過 操作並檢視真正的 Chrome——點選、輸入、。 | 讀取頁面的無障礙樹(頁面結構)來點選和輸入——看的是結構,不是。 |
| 擅長的事 | 準確、快速地使用這個網站的功能,直接用我已登入的會話 | ——效能記錄、請求、控制檯錯誤 | 與測試——、、WebKit 多種瀏覽器 |
| 侷限 | 只能在網站註冊了工具的地方使用。它是新標準,支援的瀏覽器和網站還很少。 | 為 Chrome(和 Chrome for )打造。頁面一變,就要重新找。 | 頁面結構一變,就要重新找。它靠一個個按鈕工作,而不是靠網站定義的功能。 |
用哪個——一句話選出
- 網站提供了 WebMCP 工具 → 用 WebMCP 直接呼叫這些工具。
- 你在修改自己的網站,查詢慢的地方和錯誤 → Chrome DevTools MCP。
- 你要在多個瀏覽器裡重複同樣的步驟或執行測試 → Playwright MCP。
入門程式碼
// WebMCP: the website registers its own feature as a tool
// (first explainer / Chrome preview: navigator.modelContext,
// spec draft 2026-10-27: document.modelContext)
const modelContext = document.modelContext ?? navigator.modelContext;
modelContext.registerTool({
name: "search-products",
description: "Search this shop's products by keyword",
inputSchema: {
type: "object",
properties: { query: { type: "string" } },
required: ["query"],
},
async execute({ query }) {
const response = await fetch("/api/products?q=" + encodeURIComponent(query));
return await response.json(); // the agent gets this result
},
});
# Chrome DevTools MCP: add the server to your agent (Claude Code)
claude mcp add chrome-devtools -- npx -y chrome-devtools-mcp@latest
# Playwright MCP: add the server to your agent (Claude Code)
claude mcp add playwright -- npx @playwright/mcp@latest
WebMCP 寫在網站的 裡,另外兩個寫在你電腦的裡。WebMCP 最初的說明和 Chrome 預覽版用 navigator.modelContext,2026-10-27 的規範草案用 document.modelContext——規範還在變化中。
打個比方——掛出選單的店和跑腿機器人
WebMCP = 店家掛出的點單視窗和選單
- 店家(網站)事先在門口掛出寫著“請在這個視窗點單”的選單。
- 選單上寫著能點什麼(工具名)和怎麼填點單表(輸入 )。
- 機器人(智慧體)只要讀選單、把點單表遞進視窗就行——不用在店裡到處走。
- 不過只有設了視窗的店,才能這樣點單。
Chrome DevTools MCP · Playwright MCP = 顧客派出的跑腿機器人
- 顧客(使用智慧體的人)把跑腿機器人派進店裡。店傢什麼都不用準備。
- 機器人自己在店裡轉,讀貨架、拿商品、按收銀臺的按鈕。
- 哪家店都能進,但貨架一換就得重新找,所以可能很慢,還會迷路。
- Chrome DevTools MCP 機器人是連店裡的線路和電錶都要檢視的檢修師傅(效能、網路、控制檯)。
- Playwright MCP 機器人是在好幾家店(瀏覽器)裡一模一樣地重複規定購物路線的購物機器人。
總之,在有視窗的店裡,用視窗點單又快又準;在沒有視窗的店裡,機器人只能自己轉著找。所以三者不是互相替代,而是一起用。
MCP 安全守則
工具是在你的電腦上以你的執行的程式碼。規範要求主機在呼叫工具之前先徵得你的同意。
- 只安裝你親自讀過或信任的伺服器。如果不能信任一個伺服器,也不要信任它的工具描述。
- 只給伺服器它真正需要的夾和訪問許可權。
- 把放在環境變數裡,而不是程式碼裡。
- 要提防( injection):工具讀取的網頁或檔案裡可能藏著針對 的指令。
訊息與傳輸方式
和用 2.0 訊息對話——像 (“你能做什麼?”)和 (“用這些值做這件事”)這樣的小段 。訊息透過兩種標準之一來往:stdio 或 。
stdio
在同一臺電腦上啟動伺服器,透過標準對話。適合在你自己電腦上執行的伺服器。
Streamable HTTP
伺服器執行在一個網址上,很多使用者透過網際連線。適合很多人共用的服務。
親手做的伺服器和客戶端
1. 做伺服器——mcp-server.py
伺服器(mcp-server.py)只用 ,所以你能看到本身。它從標準輸入每行讀一條 JSON-RPC 訊息,並向標準輸出每行寫一條回覆:
- :客戶端帶著協議版本打招呼。伺服器用同一個回答,說明自己有工具,並報出自己的名字。
- tools/list:伺服器返回它唯一的工具 strange-add,附帶描述和輸入模式(a 和 b 是數字)。
- tools/call:伺服器執行 strange-add,並把結果作為文字返回。
- 沒有 id 的訊息是通知(比如 /initialized),所以伺服器不回覆。其他任何請求(比如 ping)都會得到一個空結果。
"""
mcp-server.py: the smallest MCP server.
One tool, plain Python (standard library only), no SDK.
Who is who
MCP server This program. It offers tools (functions the AI
may use).
Host The app the user talks to (Claude Code, Codex).
It runs one MCP client for each connected server.
MCP client The part of the host that talks to this server
with JSON-RPC messages (here over stdio: one JSON
message per line).
Model The LLM (Claude Opus, GPT, ...). It never talks to
the server directly. It only talks to the host.
Flow
1. Discover tools
The client asks for the tools (tools/list). The server replies
with each tool's name, description and input schema.
2. Offer tools
The host sends the user's message to the model, together
with the tool list.
3. Model decides
The model answers directly, or asks for a tool call
(tool name + arguments). It never runs the tool itself.
4. Call the tool
The client sends the request to the server (tools/call).
5. Run the tool
The server runs the function and returns the result.
6. Model continues
The host gives the result back to the model. The model may
call more tools (3-6 again) or write the final answer.
Model Host (MCP client) MCP server
| | 1. tools/list ------> |
| | <------ tool list |
| <-- 2. message + tool list |
| 3. tool call --> | |
| | 4. tools/call ------> |
| | <------ 5. result |
| <-- 6. result | |
Rule for stdio servers: stdout carries protocol messages only.
Never print() anything else there. Write logs to stderr.
"""
import json
import random
import sys
SERVER_INFO = {"name": "strange-add-mcp", "version": "1.0.0"}
# The tool. The model reads the description to decide when to use it.
STRANGE_ADD = {
"name": "strange-add",
"description": "Add two numbers with a strange number.",
"inputSchema": {
"type": "object",
"properties": {
"a": {"type": "number"},
"b": {"type": "number"},
},
"required": ["a", "b"],
},
}
def strange_add(a, b):
"""Add a + b and a random number from 1 to 100 (always > a + b).
The model cannot guess the answer, so a right answer proves
that the tool was really called.
"""
return a + b + random.randint(1, 100)
def handle(method, params):
"""Return the result for one request. Others (like ping) get {}."""
if method == "initialize":
# Use the client's protocol version and say "I have tools".
return {
"protocolVersion": params["protocolVersion"],
"capabilities": {"tools": {}},
"serverInfo": SERVER_INFO,
}
if method == "tools/list":
return {"tools": [STRANGE_ADD]}
if method == "tools/call":
args = params["arguments"]
total = strange_add(args["a"], args["b"])
return {"content": [{"type": "text", "text": str(total)}]}
return {}
def main():
# One JSON-RPC message per line comes in, one reply per line goes out.
for line in sys.stdin:
request = json.loads(line)
# No id means a notification: send no reply.
if "id" not in request:
continue
result = handle(request["method"], request.get("params", {}))
reply = {"jsonrpc": "2.0", "id": request["id"], "result": result}
# flush=True sends the reply at once.
print(json.dumps(reply), flush=True)
if __name__ == "__main__":
main()
strange-add 會在 a + b 上再加一個 1 到 100 之間的隨機數。自己算不出這個值,所以 2 + 3 的答案不是 5,就證明工具真的執行了。stdout 只協議訊息——混進一個多餘的 () 就會破壞對話,所以要寫到 stderr。print(..., =True) 會把每條回覆立刻發出去。
這個手寫的伺服器使用以 initialize 開始的方式(協議版本 2025-06-18), 等客戶端仍然接受這種方式。(2026-07-28)不再用 initialize ,而是在每個請求裡帶上版本。做真正的服務時,請使用官方 (Python MCPServer、),它們會替你遵循規範。
2. 用客戶端測試——mcp-client.sh
客戶端(mcp-client.sh)是一個不需要任何 就能扮演主機角色的 指令碼。它啟動伺服器,傳送整個會話(initialize、initialized 通知、tools/list、tools/call、ping),逐條檢查回覆,並列印 PASS 或 FAIL。退出碼 0 表示全部透過。在把伺服器連線到智慧體之前,先這樣測試它。在真正的客戶端裡,模型會加入這個迴圈:
- 把使用者的問題和工具列表發給模型;
- 當模型要求使用某個工具時,客戶端用 tools/call 在伺服器上它;
- 把結果發回去,模型寫出最終的回答。
#!/bin/sh
# mcp-client.sh: the simplest MCP client, a shell script.
# It starts mcp-server.py, talks to it over stdio (one JSON-RPC
# message per line) and checks every reply.
# Exit code 0 means every check passed.
SERVER="python3 $(dirname "$0")/mcp-server.py"
# 1. Send the whole session at once and keep the replies.
# id 1 initialize agree on a version
# (no id) notifications/initialized "ready" (no reply)
# id 2 tools/list which tools are there?
# id 3 tools/call run strange-add(2, 3)
# id 4 ping is the server alive?
replies=$(
$SERVER <<'EOF'
{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "shell-mcp-client", "version": "1.0.0"}}}
{"jsonrpc": "2.0", "method": "notifications/initialized"}
{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}
{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "strange-add", "arguments": {"a": 2, "b": 3}}}
{"jsonrpc": "2.0", "id": 4, "method": "ping"}
EOF
)
echo "--- Server replies ---"
echo "$replies"
echo "--- Checks ---"
failed=0
# 2. check <label> <reply id> <text the reply must contain>
check() {
reply=$(echo "$replies" | grep -F "\"id\": $2,")
if echo "$reply" | grep -qF "$3"; then
echo "PASS $1"
else
echo "FAIL $1 (expected: $3)"
failed=1
fi
}
check "initialize: protocol version" 1 '"protocolVersion": "2025-06-18"'
check "initialize: server info" 1 '"serverInfo": {'
check "initialize: has tools" 1 '"capabilities": {"tools": {}}'
check "tools/list: strange-add" 2 '"name": "strange-add"'
check "ping: empty result" 4 '"result": {}'
# 3. strange-add adds a random 0 to 100,
# so the answer only has to be >= 2 + 3.
total=$(echo "$replies" | grep -F '"id": 3,' |
sed 's/.*"text": "\([0-9]*\)".*/\1/')
if [ "$total" -ge 5 ] 2>/dev/null; then
echo "PASS tools/call: strange-add(2, 3) >= 5 (got $total)"
else
echo "FAIL tools/call: strange-add(2, 3) >= 5 (got $total)"
failed=1
fi
exit $failed
3. 連線到 Claude Code,再移除
# put the two files above into one folder
mkdir -p ~/tmp/mcp && cd ~/tmp/mcp
# mcp-server.py mcp-client.sh
# 1) test the server without any AI
sh mcp-client.sh # -> 6 x PASS, exit code 0
# 2) connect it to Claude Code (no --scope: the "local" scope)
claude mcp add strange-add -- python3 ~/tmp/mcp/mcp-server.py
claude mcp list # strange-add: ... - Connected
claude mcp get strange-add # Scope: Local config
# 3) where it was saved: NOT in this folder (no new file here)
# ~/.claude.json -> projects -> "<this folder>" -> mcpServers
jq '.projects["'"$PWD"'"].mcpServers' ~/.claude.json
# 4) use it: start Claude Code in THIS folder
claude # "Use strange-add to add 2 and 3"
# -> not 5 (e.g. 61): the tool ran
claude mcp remove strange-add -s local
這些命令在你自己的電腦或伺服器的裡執行,而不是在這個頁面上。執行前請逐行讀一遍。