MCP協(xié)議之MCP簡述 原創(chuàng)
背景
隨著AI Agent在2025年的火爆,與之相關(guān)的MCP協(xié)議也越來越受到開發(fā)者的重視,本文將結(jié)合示例深入了解MCP協(xié)議的原理架構(gòu)以及應(yīng)用方法。
(What)MCP協(xié)議是什么
MCP(Model Context Protocol)是一種專為AI Agent設(shè)計的標(biāo)準(zhǔn)化協(xié)議,旨在解決AI模型與外部數(shù)據(jù)、工具之間的集成難題。其核心定位是成為AI領(lǐng)域的“通用接口”,類似于物理世界中的USB-C標(biāo)準(zhǔn),為不同AI系統(tǒng)提供安全、無縫、可擴展的數(shù)據(jù)交換能力。
(Why)為什么要使用MCP協(xié)議
MCP的作用主要有三點:
- 消除對接碎片化。就像早期USB-C標(biāo)準(zhǔn)沒有誕生之前,我們的手機、電腦設(shè)備不得不面臨五花八門的插口問題,現(xiàn)在大模型在與各家服務(wù)商進行API調(diào)用的時候,如果沒有統(tǒng)一的協(xié)議,那么開發(fā)者和使用者不得不面臨類似的問題。通過MCP協(xié)議,開發(fā)者和AI模型可以輕松地集成,實現(xiàn)數(shù)據(jù)交換和交互。
 

- 在不同 LLM 提供商和供應(yīng)商之間切換的靈活性
 - 在您的基礎(chǔ)設(shè)施中保護數(shù)據(jù)的最佳實踐
 
備注:以上兩點在MCP官網(wǎng)有提到,但目前接觸不夠,暫未有深入體會。
(How)如何實現(xiàn)一個MCP協(xié)議

MCP協(xié)議的架構(gòu)構(gòu)成主要主要由5部分組成:
- MCP主機(MCP Hosts)如Claude Desktop、IDE 或 AI 工具等想要通過 MCP 訪問數(shù)據(jù)的程序
 - MCP客戶端(MCP Clients)與服務(wù)器保持 1:1 連接的協(xié)議客戶端
 - MCP服務(wù)器(MCP Servers)通過標(biāo)準(zhǔn)化的模型上下文協(xié)議暴露特定功能的輕量級程序
 - 本地數(shù)據(jù)源(Local Data Sources)MCP 服務(wù)器可以安全訪問的計算機文件、數(shù)據(jù)庫和服務(wù)
 - 遠程服務(wù)(Remote Services)MCP 服務(wù)器可以連接的通過互聯(lián)網(wǎng)提供的外部系統(tǒng)(例如通過 API)
 
接下來,我們實現(xiàn)一個??MCPServer???,這個??Server???可以通過??Web API??訪問遠程的服務(wù)器以獲取天氣信息。
1. 準(zhǔn)備環(huán)境:安裝??uv??
# Mac下使用curl命令安裝
curl -LsSf https://astral.sh/uv/install.sh | sh
# Window下使用PowerShell命令安裝
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"運行結(jié)果:

備注:如果提示 ?
?url: (7) Failed to connect to github.com port 443 after 93 ms: Couldn't connect to server?? 可能需要科學(xué)上網(wǎng)或者過一段時間再試。
2. 創(chuàng)建項目
# 為我們的項目創(chuàng)建一個新目錄
uv init weather
cd weather
# 創(chuàng)建虛擬環(huán)境并激活它
uv venv
source .venv/bin/activate
# 安裝依賴
uv add "mcp[cli]" httpx
# 創(chuàng)建我們的服務(wù)器文件
touch weather.py運行結(jié)果:

3. 實現(xiàn)weather.py的代碼
3.1 導(dǎo)入包并設(shè)置實例
from typing import Any, Dict
import httpx
from mcp.server.fastmcp import FastMCP
# 初始化FastMCP服務(wù)器
mcp = FastMCP("weather")
# 常量
BAIDU_API_BASE = "https://api.map.baidu.com/weather/v1/"
BAIDU_API_KEY = "8HkEwz5h********"3.2 實現(xiàn)輔助函數(shù)
# 城市與行政區(qū)ID映射表
WEATHER_DISTRICT_ID = {
    "北京": "110100",
    "上海": "310000",
    "廣州": "440100",
    "深圳": "440300",
    # 可以根據(jù)需要添加更多城市
}
asyncdefmake_baidu_request(district_id: str) -> Dict[str, Any] | None:
    """向百度天氣API發(fā)出GET請求,處理錯誤并返回JSON響應(yīng)"""
    params = {
        "district_id": district_id,
        "data_type": "now",
        "ak": BAIDU_API_KEY
    }
    asyncwith httpx.AsyncClient() as client:
        try:
            response = await client.get(BAIDU_API_BASE, params=params, timeout=30.0)
            response.raise_for_status()
            return response.json()
        except Exception:
            returnNone
defformat_weather(data: Dict) -> str:
    """將天氣數(shù)據(jù)格式化為可讀字符串"""
    location = data["result"]["location"]
    now = data["result"]["now"]
    returnf"""
城市: {location['city']}
天氣狀況: {now['text']}
溫度: {now['temp']}°C
體感溫度: {now['feels_like']}°C
濕度: {now['rh']}%
風(fēng)力: {now['wind_class']}
風(fēng)向: {now['wind_dir']}
更新時間: {now['uptime']}
"""
defget_district_id(city: str) -> str | None:
    """根據(jù)城市名稱獲取對應(yīng)的行政區(qū)ID"""
    return WEATHER_DISTRICT_ID.get(city)3.3 實現(xiàn)工具執(zhí)行
@mcp.tool()
asyncdefget_weather(city: str) -> str:
    """獲取指定城市的當(dāng)前天氣
    
    Args:
        city: 城市名稱
    """
    district_id = get_district_id(city)
    ifnot district_id:
        returnf"未找到{city}對應(yīng)的行政區(qū)ID。"
    
    data = await make_baidu_request(district_id)
    
    ifnot data or data.get("status") != 0:
        return"無法獲取天氣信息。"
    
    return format_weather(data)3.4 實現(xiàn)入口函數(shù)
if __name__ == "__main__":
    # 初始化并運行服務(wù)器
    mcp.run(transport='stdio')完整代碼
from typing importAny, Dict
import httpx
from mcp.server.fastmcp import FastMCP
# 初始化FastMCP服務(wù)器
mcp = FastMCP("weather")
# 常量
BAIDU_API_BASE = "https://api.map.baidu.com/weather/v1/"
BAIDU_API_KEY = "8HkEwz5h********"
# 城市與行政區(qū)ID映射表
WEATHER_DISTRICT_ID = {
    "北京": "110100",
    "上海": "310000",
    "廣州": "440100",
    "深圳": "440300",
    # 可以根據(jù)需要添加更多城市
}
asyncdefmake_baidu_request(district_id: str) -> Dict[str, Any] | None:
    """向百度天氣API發(fā)出GET請求,處理錯誤并返回JSON響應(yīng)"""
    params = {
        "district_id": district_id,
        "data_type": "now",
        "ak": BAIDU_API_KEY
    }
    asyncwith httpx.AsyncClient() as client:
        try:
            response = await client.get(BAIDU_API_BASE, params=params, timeout=30.0)
            response.raise_for_status()
            return response.json()
        except Exception:
            returnNone
defformat_weather(data: Dict) -> str:
    """將天氣數(shù)據(jù)格式化為可讀字符串"""
    location = data["result"]["location"]
    now = data["result"]["now"]
    returnf"""
城市: {location['city']}
天氣狀況: {now['text']}
溫度: {now['temp']}°C
體感溫度: {now['feels_like']}°C
濕度: {now['rh']}%
風(fēng)力: {now['wind_class']}
風(fēng)向: {now['wind_dir']}
更新時間: {now['uptime']}
"""
defget_district_id(city: str) -> str | None:
    """根據(jù)城市名稱獲取對應(yīng)的行政區(qū)ID"""
    return WEATHER_DISTRICT_ID.get(city)
@mcp.tool()
asyncdefget_weather(city: str) -> str:
    """獲取指定城市的當(dāng)前天氣
    
    Args:
        city: 城市名稱
    """
    district_id = get_district_id(city)
    ifnot district_id:
        returnf"未找到{city}對應(yīng)的行政區(qū)ID。"
    
    data = await make_baidu_request(district_id)
    
    ifnot data or data.get("status") != 0:
        return"無法獲取天氣信息。"
    
    return format_weather(data)
if __name__ == "__main__":
    # 初始化并運行服務(wù)器
    mcp.run(transport='stdio')?
?BAIDU_API_KEY??? 需要訪問 ??http://lbsyun.baidu.com?? 注冊獲取。
4. 啟動服務(wù)器
通過??uv run weather.py??啟動服務(wù)。

5. 測試服務(wù)
命令行下運行如下命令安裝inspector。
npx @modelcontextprotocol/inspector運行結(jié)果:

6. 調(diào)試服務(wù)
- 安裝完畢后,在瀏覽器中打開?
?http://localhost:5173/??。 - 頁面輸入調(diào)試命令:
 
- command: uv
 - Arguments: --directory /Users/deadwalk/Code/ai_proj_agent/weather run weather.py
 
備注:
- ?
 ?/Users/deadwalk/Code/ai_proj_agent/weather??對應(yīng)創(chuàng)建的工程目錄,請根據(jù)自己的情況進行修改。
3. 點擊Connect按鈕,確認服務(wù)可以正常連接;
4. 在右側(cè)Tools點擊List Tools->Weather->輸入?yún)?shù)"北京"->Run Tool,可以看到正常獲得北京的天氣情況,此時代表mcp-server-weather可以正常運行了。
運行結(jié)果:

7. 集成到其他應(yīng)用中
7.1 在cherry-studio中集成服務(wù)
- 打開cherry-studio的設(shè)置->MCP服務(wù)器->編輯JSON,添加如下mcp服務(wù)設(shè)置:
 
{
    "mcpServers":{
        "weather":{
            "command":"/Users/deadwalk/.local/bin/uv",
            "args":[
                "--directory",
                "/Users/deadwalk/Code/ai_proj_agent/weather",
                "run",
                "weather.py"
            ]
        }
    }
}備注:
- ?
 ?/Users/deadwalk/.local/bin/uv??? 對應(yīng)??uv??可執(zhí)行文件的完整路徑,可以通過MacOS/Linux上運行which uv或在Windows上運行where uv來獲取此路徑。
2.添加成功之后,啟用??weather???服務(wù)和??deepseek-chat??模型,提問大模型:北京今天的天氣是多少?運行結(jié)果:

從圖中可以看到,大模型調(diào)用了我們之前封裝的mcp-server-weather服務(wù),并成功獲取了北京的天氣。
7.2 在cursor中集成服務(wù)
- 我們也可以在cursor中添加對應(yīng)的服務(wù)

 
- 配置完畢MCPserver之后,啟用weather服務(wù)(weather左側(cè)的綠點點亮),然后在對話框中詢問北京的天氣情況即可獲得查詢結(jié)果。
 
運行結(jié)果:

總結(jié)
- MCP是一套服務(wù)間通信協(xié)議,它通過統(tǒng)一的協(xié)議,解決了大模型與工具間繁瑣的適配通信問題。
 - MCP的構(gòu)成包括:MCP Host、MCP Client、MCP Server、Local Data Source、Remote Data Source。
 - 封裝MCP-server-weather服務(wù)時,需要在工具函數(shù)上添加@mcp.tool()裝飾器。
 - 通過MCP協(xié)議,我們可以封裝各種服務(wù)提供給Cursor、Cherry-Studio、甚至我們自己開發(fā)的Agent使用,從而使得LLM+Agent的功能更加強大。
 
本文轉(zhuǎn)載自公眾號一起AI技術(shù) 作者:熱情的Dongming


















