Python-MCPInspector調試
使用FastMCP開發MCPServer,熟悉【McpServer編碼過程】+【MCPInspector調試方法】-> 可以這樣理解:只編寫一個McpServer,然后使用MCPInspector作為McpClient進行McpServer的調試
1-核心知識點
- 1-熟悉【McpServer編碼過程】
- 2-熟悉【McpServer調試方法-MCP Inspector】
2-思路整理
1-核心思路
- 1-編寫傳統的Service業務代碼
- 2-在Service業務代碼頭上添加@tool裝飾器,即可實現FastMCP的Tool功能
- 3-在Service業務代碼頭上添加@mcp.tool()裝飾器,即可實現FastMCP的McpServer功能
- 4-主程序指定運行方法-stdio進程啟動(但是不要自己去啟動)
- 5-使用MCPInspector調試McpServer(2個步驟)
- 【mcp dev city_02_mcp_server.py】是啟動mcpInspector并指定mcpServer的路徑,
- 然后在Inspector中啟動city_02_mcp_server.py->【uv run --with mcp mcp run city_02_mcp_server.py】
2-核心代碼
- 1-在Service業務代碼頭上添加@tool裝飾器,即可實現FastMCP的Tool功能
# 假設 mcp 已經正確導入
try:from mcp import tool
except ImportError:# 如果 mcp 未找到,模擬一個 tool 裝飾器def tool(func):return func# 在 Service 業務代碼頭上添加 @tool 裝飾器
@tool
async def get_city_list(self) -> list:"""獲取所有的城市信息。返回:str: 所有的城市信息列表"""logging.info(f"獲取所有的城市信息")city_list = []for city in self.CITY_WEATHER_DATA:city_list.append(city)return city_list
- 2-在Service業務代碼頭上添加@mcp.tool()裝飾器,即可實現FastMCP的McpServer功能
from mcp.server.fastmcp import FastMCPfrom city_01_service import CityDataServer# 1-初始化 MCP 服務器
mcp = FastMCP("CityDataServer")# 2-初始化城市信息服務器(業務代碼+@tool裝飾器)
city_server = CityDataServer()# 3-在 Service 業務代碼頭上添加@mcp.tool()裝飾器
@mcp.tool()
# 獲取所有城市列表
async def get_city_list():"""獲取所有城市列表。返回:str: 所有城市列表"""city_list = await city_server.get_city_list()return city_list# 4-主程序指定運行方法-stdio進程啟動
if __name__ == "__main__":mcp.run(transport='stdio')
3-參考網址
- 個人代碼實現倉庫:https://gitee.com/enzoism/python_mcp_01_inspector
4-上手實操
1-空工程初始化環境
mkdir my_project
cd my_project
python -m venv .venv
2-激活環境
# Windows
source .venv/Scripts/activate# Mac
source .venv/bin/activate
3-添加依賴
對應的依賴是在激活的環境中
# uv用于后續MCP Inspector的連接
pip install uv httpx mcp
4-項目結構
city_01_service.py
:城市服務腳本city_02_mcp_server.py
:MCP 服務器腳本
5-創建Python城市服務
city_01_service.py
:城市服務腳本
import logging# 假設 mcp 已經正確導入
try:from mcp import tool
except ImportError:# 如果 mcp 未找到,模擬一個 tool 裝飾器def tool(func):return func# 配置日志打印級別
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)# 定義城市服務
class CityDataServer:# 模擬城市的天氣數據CITY_WEATHER_DATA = {"北京": {"condition": "晴", "temperature": 25, "humidity": 40},"上海": {"condition": "多云", "temperature": 27, "humidity": 60},"廣州": {"condition": "雨", "temperature": 30, "humidity": 80},"深圳": {"condition": "多云", "temperature": 29, "humidity": 70},"杭州": {"condition": "晴", "temperature": 26, "humidity": 50},}@toolasync def get_city_weather(self, city: str) -> str:"""獲取指定城市的天氣信息。參數:city (str): 城市名稱返回:str: 天氣信息描述"""logging.info(f"獲取天氣信息: {city}")if city in self.CITY_WEATHER_DATA:weather = self.CITY_WEATHER_DATA[city]return f"{city} : {weather['condition']} , {weather['temperature']} °C,濕度 {weather['humidity']} %"else:return f"抱歉,未找到 {city} 的天氣信息"@toolasync def get_city_list(self) -> list:"""獲取所有的城市信息。返回:str: 所有的城市信息列表"""logging.info(f"獲取所有的城市信息")city_list = []for city in self.CITY_WEATHER_DATA:city_list.append(city)return city_list@toolasync def get_city_detail(self, city: str) -> str:"""獲取指定城市的信息。參數:city (str): 城市名稱返回:str: 城市信息"""logging.info(f"獲取指定城市的信息: {city}")if city in await self.get_city_list():return f"{city} : 一個風景秀麗的城市,你值得去玩一把"else:return f"抱歉,未找到 {city} 的城市信息"
6-暴露Python城市MCPServer服務
city_02_mcp_server.py
:MCP 服務器腳本
from mcp.server.fastmcp import FastMCPfrom city_01_service import CityDataServer# 初始化 MCP 服務器
mcp = FastMCP("CityDataServer")
# 初始化城市信息服務器
city_server = CityDataServer()# 獲取天氣信息的工具
@mcp.tool()
async def get_city_weather(city: str) -> str:"""獲取指定城市的天氣信息。參數:city (str): 城市名稱返回:str: 天氣信息描述"""city_weather_info = await city_server.get_city_weather(city)return city_weather_info@mcp.tool()
# 獲取所有城市列表
async def get_city_list():"""獲取所有城市列表。返回:str: 所有城市列表"""city_list = await city_server.get_city_list()return city_list@mcp.tool()
# 獲取指定城市的信息
async def get_city_detail(city: str):"""獲取指定城市的信息。參數:city (str): 城市名稱返回:str: 指定城市的信息"""city_info = await city_server.get_city_detail(city)return city_info# 主程序
if __name__ == "__main__":mcp.run(transport='stdio')
7-MCP Inspector調試
1-安裝MCP Inspector
運行機制:先運行【MCPInspector】再運行【uv run --with mcp mcp run city_02_mcp_server.py】
# 1-安裝MCP Inspector
pip install mcp[cli]
2-運行MCP Inspector服務
# 2-運行MCP Inspector
mcp dev city_02_mcp_server.py
3-訪問MCP Inspector網頁
再運行【uv run --with mcp mcp run city_02_mcp_server.py】
- http://127.0.0.1:6274