MCP協(xié)議解析:Anthropic如何統(tǒng)一AI工具調(diào)用標(biāo)準(zhǔn)

MCP協(xié)議硬核解析:Anthropic如何用新標(biāo)準(zhǔn)統(tǒng)一Agent工具調(diào)用?
想讓AI查天氣、讀文件、調(diào)API,結(jié)果發(fā)現(xiàn)要為每個工具寫一堆適配代碼?不同工具的接口五花八門,LLM像個只會說普通話的外國人,每到一個地方都要配個翻譯。
這就是當(dāng)前AI工具調(diào)用的碎片化痛點。MCP(Model Context Protocol)是Anthropic推出的“萬能轉(zhuǎn)接頭”標(biāo)準(zhǔn)——它想讓所有工具都說同一種“語言”,讓LLM能即插即用。
問題:工具調(diào)用的碎片化困境
想象一下:你開發(fā)了一個AI助手,需要它能:
- 讀取本地文件
- 查詢數(shù)據(jù)庫
- 調(diào)用天氣API
- 操作Git倉庫
傳統(tǒng)做法是為每個功能寫專門的適配層。文件系統(tǒng)有文件系統(tǒng)的接口,數(shù)據(jù)庫有數(shù)據(jù)庫的驅(qū)動,每個API都有自己的認(rèn)證方式...你的代碼很快變成一鍋粥,而且換一個LLM可能就要重寫一遍。
更痛苦的是,當(dāng)你想用別人開發(fā)的工具時,往往要花大量時間理解對方的代碼結(jié)構(gòu)。工具之間無法互通,生態(tài)是割裂的。
方案:MCP作為“萬能轉(zhuǎn)接頭”
MCP的核心思想很簡單:定義一套標(biāo)準(zhǔn)協(xié)議,讓所有工具都通過這個協(xié)議與LLM對話。
就像USB-C接口一樣——不管你是充電、傳數(shù)據(jù)還是接顯示器,都用同一個接口。MCP就是AI世界的USB-C。
MCP的三層架構(gòu)
MCP采用經(jīng)典的客戶端-服務(wù)器架構(gòu),但加了一個關(guān)鍵角色:
- MCP Host:運(yùn)行AI模型的環(huán)境(比如Claude Desktop、Cursor)
- MCP Client:Host內(nèi)部的協(xié)議處理器,負(fù)責(zé)與Server通信
- MCP Server:提供具體工具的服務(wù)端,每個Server可以提供多個工具
你的AI應(yīng)用 (Host)
↓ (MCP協(xié)議)
MCP Client
↓ (MCP協(xié)議)
MCP Server A (文件操作)
MCP Server B (數(shù)據(jù)庫查詢)
MCP Server C (API調(diào)用)關(guān)鍵設(shè)計:LLM不需要知道工具的具體實現(xiàn)細(xì)節(jié),只需要通過標(biāo)準(zhǔn)協(xié)議說“我要讀文件”,MCP Client會自動找到對應(yīng)的Server執(zhí)行。
協(xié)議設(shè)計的硬核細(xì)節(jié)
MCP基于JSON-RPC 2.0,所有通信都是結(jié)構(gòu)化的JSON消息。它定義了三種核心能力:
1. Tools(工具):可執(zhí)行的函數(shù)
{
"name": "read_file",
"description": "讀取指定路徑的文件內(nèi)容",
"inputSchema": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "文件路徑"}
},
"required": ["path"]
}
}2. Resources(資源):可讀取的數(shù)據(jù)源
{
"uri": "file:///home/user/document.txt",
"name": "document.txt",
"mimeType": "text/plain"
}3. Prompts(提示模板):預(yù)定義的對話模板
為什么這樣設(shè)計?因為不同工具需要不同交互方式。有些是“執(zhí)行動作”(比如創(chuàng)建文件),有些是“獲取數(shù)據(jù)”(比如讀文件),有些是“提供上下文”(比如系統(tǒng)信息)。分開定義讓協(xié)議更清晰。
步驟:用Python SDK接入MCP服務(wù)
現(xiàn)在我們來動手實現(xiàn)。假設(shè)你有一個簡單的文件操作工具,想通過MCP提供給LLM使用。
第一步:安裝MCP Python SDK
pip install mcp第二步:創(chuàng)建MCP Server
# file_server.py
import os
from mcp.server import Server
from mcp.types import Tool, TextContent
import mcp.server.stdio
# 創(chuàng)建Server實例
server = Server("file-server")
# 定義工具
@server.list_tools()
async def list_tools():
return [
Tool(
name="read_file",
description="讀取本地文件內(nèi)容",
inputSchema={
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "要讀取的文件路徑"
}
},
"required": ["path"]
}
),
Tool(
name="list_files",
description="列出目錄下的文件",
inputSchema={
"type": "object",
"properties": {
"dir_path": {
"type": "string",
"description": "目錄路徑",
"default": "."
}
}
}
)
]
# 實現(xiàn)工具邏輯
@server.call_tool()
async def call_tool(name: str, arguments: dict):
if name == "read_file":
path = arguments["path"]
try:
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
return [TextContent(type="text", text=content)]
except Exception as e:
return [TextContent(type="text", text=f"錯誤: {str(e)}")]
elif name == "list_files":
dir_path = arguments.get("dir_path", ".")
try:
files = os.listdir(dir_path)
return [TextContent(type="text", text="\n".join(files))]
except Exception as e:
return [TextContent(type="text", text=f"錯誤: {str(e)}")]
# 啟動Server
if __name__ == "__main__":
async def run():
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
await server.run(
read_stream,
write_stream,
server.create_initialization_options()
)
import asyncio
asyncio.run(run())為什么這樣寫?
@server.list_tools()告訴Client“我有哪些工具可用”@server.call_tool()處理具體的工具調(diào)用請求- 使用標(biāo)準(zhǔn)輸入輸出通信,這是MCP推薦的簡單方式
- 每個工具都有清晰的
inputSchema,LLM知道需要提供什么參數(shù)
第三步:在Host中使用
以Claude Desktop為例,編輯配置文件:
{
"mcpServers": {
"file-server": {

"command": "python",
"args": ["/path/to/file_server.py"]
}
}
}重啟Claude Desktop后,你就可以直接說“幫我讀取/home/user/notes.txt”,LLM會自動調(diào)用你的read_file工具。
驗證:看看實際效果
創(chuàng)建測試文件:
echo "Hello MCP!" > test.txt在Claude中測試:
用戶:讀取test.txt文件的內(nèi)容 Claude:[調(diào)用read_file工具] → 文件內(nèi)容是:Hello MCP!測試錯誤處理:
用戶:讀取不存在的文件 Claude:[調(diào)用read_file工具] → 錯誤: [Errno 2] No such file or directory: '...'
為什么這很酷? 你不需要寫任何HTTP服務(wù)器代碼,不需要處理認(rèn)證,不需要擔(dān)心不同LLM的接口差異。MCP幫你處理了所有底層通信。
實戰(zhàn):將現(xiàn)有工具改造成MCP服務(wù)
假設(shè)你有一個現(xiàn)成的OpenClaw工具(假設(shè)是數(shù)據(jù)庫查詢工具),想讓它支持MCP。
原始代碼(非MCP)
# legacy_db_tool.py
import sqlite3
class DatabaseTool:
def __init__(self, db_path):
self.db_path = db_path
def query(self, sql):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute(sql)
results = cursor.fetchall()
conn.close()
return results
def get_tables(self):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = [row[0] for row in cursor.fetchall()]
conn.close()
return tablesMCP改造版本
# db_mcp_server.py
from mcp.server import Server
from mcp.types import Tool, TextContent
import mcp.server.stdio
import json
from legacy_db_tool import DatabaseTool
server = Server("database-server")
db_tool = DatabaseTool("app.db")
@server.list_tools()
async def list_tools():
return [
Tool(
name="execute_sql",
description="執(zhí)行SQL查詢語句",
inputSchema={
"type": "object",
"properties": {
"sql": {
"type": "string",
"description": "要執(zhí)行的SQL語句"
}
},
"required": ["sql"]
}
),
Tool(
name="list_tables",
description="列出數(shù)據(jù)庫中的所有表",
inputSchema={
"type": "object",
"properties": {}
}
)
]
@server.call_tool()
async def call_tool(name: str, arguments: dict):
if name == "execute_sql":
sql = arguments["sql"]
try:
results = db_tool.query(sql)
return [TextContent(type="text", text=json.dumps(results, ensure_ascii=False))]
except Exception as e:
return [TextContent(type="text", text=f"SQL執(zhí)行錯誤: {str(e)}")]
elif name == "list_tables":
try:
tables = db_tool.get_tables()
return [TextContent(type="text", text=json.dumps(tables))]
except Exception as e:
return [TextContent(type="text", text=f"錯誤: {str(e)}")]
if __name__ == "__main__":
import asyncio
async def run():
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
await server.run(
read_stream,
write_stream,
server.create_initialization_options()
)
asyncio.run(run())改造要點:
- 保留原有業(yè)務(wù)邏輯,只添加MCP適配層
- 定義清晰的工具描述和輸入模式
- 統(tǒng)一錯誤處理格式
- 使用JSON序列化返回結(jié)果
現(xiàn)在你的數(shù)據(jù)庫工具可以同時被多個AI應(yīng)用使用:Claude Desktop、Cursor、或者任何支持MCP的Host。
常見問題
Q:MCP和Function Calling有什么區(qū)別?
A:Function Calling是各家LLM廠商自己定義的接口,格式都不一樣。MCP是跨廠商的標(biāo)準(zhǔn)協(xié)議,寫一次到處用。
Q:MCP Server必須用Python寫嗎?
A:不是!官方支持TypeScript、Python、Java等多種語言。只要遵循協(xié)議規(guī)范,用什么語言都行。
Q:MCP安全嗎?
A:MCP設(shè)計時考慮了安全問題。Server運(yùn)行在隔離環(huán)境中,Host可以控制權(quán)限。但要注意,不要運(yùn)行來歷不明的Server。
Q:性能怎么樣?
A:對于大多數(shù)工具調(diào)用場景足夠。如果是高頻調(diào)用,可以考慮使用HTTP傳輸代替標(biāo)準(zhǔn)輸入輸出。
現(xiàn)成插件生態(tài):別從零開始
MCP最實用的地方在于現(xiàn)成的插件生態(tài)。你不需要自己寫文件操作、Git操作、數(shù)據(jù)庫查詢等常見工具,直接用社區(qū)提供的Server就行:
# 安裝官方參考實現(xiàn)
npm install @modelcontextprotocol/server-filesystem
npm install @modelcontextprotocol/server-git
npm install @modelcontextprotocol/server-sqlite這些Server經(jīng)過充分測試,比自己寫的更穩(wěn)定。你的AI應(yīng)用瞬間就能獲得幾十種工具能力。
下一步學(xué)習(xí)建議
- 動手實驗:先用現(xiàn)成的Server感受一下,比如filesystem和sqlite
- 閱讀規(guī)范:去modelcontextprotocol.io讀官方文檔
- 改造工具:把你手頭的一個小工具改造成MCP Server
- 參與生態(tài):看看GitHub上有哪些有趣的MCP Server項目
MCP還在快速發(fā)展中,現(xiàn)在正是參與的好時機(jī)。想象一下,未來所有AI工具都通過MCP互聯(lián),你的AI助手可以無縫調(diào)用任何服務(wù)——這不再是科幻場景,而是正在發(fā)生的技術(shù)演進(jìn)。
相關(guān)資源:
- MCP官方文檔
- MCP Python SDK GitHub
- Awesome MCP Servers - 社區(qū)維護(hù)的Server列表
記?。汉玫臉?biāo)準(zhǔn)需要社區(qū)共同建設(shè)。當(dāng)你開發(fā)了一個有用的工具,考慮用MCP包裝一下,讓更多AI應(yīng)用能受益。這才是開源生態(tài)的魅力所在。