MCP協議解析:Anthropic如何統一AI工具調用標準

MCP協議硬核解析:Anthropic如何用新標準統一Agent工具調用?
想讓AI查天氣、讀文件、調API,結果發現要為每個工具寫一堆適配代碼?不同工具的接口五花八門,LLM像個只會說普通話的外國人,每到一個地方都要配個翻譯。
這就是當前AI工具調用的碎片化痛點。MCP(Model Context Protocol)是Anthropic推出的“萬能轉接頭”標準——它想讓所有工具都說同一種“語言”,讓LLM能即插即用。
問題:工具調用的碎片化困境
想象一下:你開發了一個AI助手,需要它能:
- 讀取本地文件
- 查詢數據庫
- 調用天氣API
- 操作Git倉庫
傳統做法是為每個功能寫專門的適配層。文件系統有文件系統的接口,數據庫有數據庫的驅動,每個API都有自己的認證方式...你的代碼很快變成一鍋粥,而且換一個LLM可能就要重寫一遍。
更痛苦的是,當你想用別人開發的工具時,往往要花大量時間理解對方的代碼結構。工具之間無法互通,生態是割裂的。
方案:MCP作為“萬能轉接頭”
MCP的核心思想很簡單:定義一套標準協議,讓所有工具都通過這個協議與LLM對話。
就像USB-C接口一樣——不管你是充電、傳數據還是接顯示器,都用同一個接口。MCP就是AI世界的USB-C。
MCP的三層架構
MCP采用經典的客戶端-服務器架構,但加了一個關鍵角色:
- MCP Host:運行AI模型的環境(比如Claude Desktop、Cursor)
- MCP Client:Host內部的協議處理器,負責與Server通信
- MCP Server:提供具體工具的服務端,每個Server可以提供多個工具
你的AI應用 (Host)
↓ (MCP協議)
MCP Client
↓ (MCP協議)
MCP Server A (文件操作)
MCP Server B (數據庫查詢)
MCP Server C (API調用)關鍵設計:LLM不需要知道工具的具體實現細節,只需要通過標準協議說“我要讀文件”,MCP Client會自動找到對應的Server執行。
協議設計的硬核細節
MCP基于JSON-RPC 2.0,所有通信都是結構化的JSON消息。它定義了三種核心能力:
1. Tools(工具):可執行的函數
{
"name": "read_file",
"description": "讀取指定路徑的文件內容",
"inputSchema": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "文件路徑"}
},
"required": ["path"]
}
}2. Resources(資源):可讀取的數據源
{
"uri": "file:///home/user/document.txt",
"name": "document.txt",
"mimeType": "text/plain"
}3. Prompts(提示模板):預定義的對話模板
為什么這樣設計?因為不同工具需要不同交互方式。有些是“執行動作”(比如創建文件),有些是“獲取數據”(比如讀文件),有些是“提供上下文”(比如系統信息)。分開定義讓協議更清晰。
步驟:用Python SDK接入MCP服務
現在我們來動手實現。假設你有一個簡單的文件操作工具,想通過MCP提供給LLM使用。
第一步:安裝MCP Python SDK
pip install mcp第二步:創建MCP Server
# file_server.py
import os
from mcp.server import Server
from mcp.types import Tool, TextContent
import mcp.server.stdio
# 創建Server實例
server = Server("file-server")
# 定義工具
@server.list_tools()
async def list_tools():
return [
Tool(
name="read_file",
description="讀取本地文件內容",
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": "."
}
}
}
)
]
# 實現工具邏輯
@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()處理具體的工具調用請求- 使用標準輸入輸出通信,這是MCP推薦的簡單方式
- 每個工具都有清晰的
inputSchema,LLM知道需要提供什么參數
第三步:在Host中使用
以Claude Desktop為例,編輯配置文件:
{
"mcpServers": {
"file-server": {

"command": "python",
"args": ["/path/to/file_server.py"]
}
}
}重啟Claude Desktop后,你就可以直接說“幫我讀取/home/user/notes.txt”,LLM會自動調用你的read_file工具。
驗證:看看實際效果
創建測試文件:
echo "Hello MCP!" > test.txt在Claude中測試:
用戶:讀取test.txt文件的內容 Claude:[調用read_file工具] → 文件內容是:Hello MCP!測試錯誤處理:
用戶:讀取不存在的文件 Claude:[調用read_file工具] → 錯誤: [Errno 2] No such file or directory: '...'
為什么這很酷? 你不需要寫任何HTTP服務器代碼,不需要處理認證,不需要擔心不同LLM的接口差異。MCP幫你處理了所有底層通信。
實戰:將現有工具改造成MCP服務
假設你有一個現成的AI Agent平臺工具(假設是數據庫查詢工具),想讓它支持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="執行SQL查詢語句",
inputSchema={
"type": "object",
"properties": {
"sql": {
"type": "string",
"description": "要執行的SQL語句"
}
},
"required": ["sql"]
}
),
Tool(
name="list_tables",
description="列出數據庫中的所有表",
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執行錯誤: {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())改造要點:
- 保留原有業務邏輯,只添加MCP適配層
- 定義清晰的工具描述和輸入模式
- 統一錯誤處理格式
- 使用JSON序列化返回結果
現在你的數據庫工具可以同時被多個AI應用使用:Claude Desktop、Cursor、或者任何支持MCP的Host。
常見問題
Q:MCP和Function Calling有什么區別?
A:Function Calling是各家LLM廠商自己定義的接口,格式都不一樣。MCP是跨廠商的標準協議,寫一次到處用。
Q:MCP Server必須用Python寫嗎?
A:不是!官方支持TypeScript、Python、Java等多種語言。只要遵循協議規范,用什么語言都行。
Q:MCP安全嗎?
A:MCP設計時考慮了安全問題。Server運行在隔離環境中,Host可以控制權限。但要注意,不要運行來歷不明的Server。
Q:性能怎么樣?
A:對于大多數工具調用場景足夠。如果是高頻調用,可以考慮使用HTTP傳輸代替標準輸入輸出。
現成插件生態:別從零開始
MCP最實用的地方在于現成的插件生態。你不需要自己寫文件操作、Git操作、數據庫查詢等常見工具,直接用社區提供的Server就行:
# 安裝官方參考實現
npm install @modelcontextprotocol/server-filesystem
npm install @modelcontextprotocol/server-git
npm install @modelcontextprotocol/server-sqlite這些Server經過充分測試,比自己寫的更穩定。你的AI應用瞬間就能獲得幾十種工具能力。
下一步學習建議
- 動手實驗:先用現成的Server感受一下,比如filesystem和sqlite
- 閱讀規范:去modelcontextprotocol.io讀官方文檔
- 改造工具:把你手頭的一個小工具改造成MCP Server
- 參與生態:看看GitHub上有哪些有趣的MCP Server項目
MCP還在快速發展中,現在正是參與的好時機。想象一下,未來所有AI工具都通過MCP互聯,你的AI助手可以無縫調用任何服務——這不再是科幻場景,而是正在發生的技術演進。
相關資源:
- MCP官方文檔
- MCP Python SDK GitHub
- Awesome MCP Servers - 社區維護的Server列表
記住:好的標準需要社區共同建設。當你開發了一個有用的工具,考慮用MCP包裝一下,讓更多AI應用能受益。這才是開源生態的魅力所在。