Python?MCPInspector調(diào)試思路詳解
Python-MCPInspector調(diào)試
使用FastMCP開發(fā)MCPServer,熟悉【McpServer編碼過程】+【MCPInspector調(diào)試方法】-> 可以這樣理解:只編寫一個McpServer,然后使用MCPInspector作為McpClient進(jìn)行McpServer的調(diào)試


1-核心知識點(diǎn)
- 1-熟悉【McpServer編碼過程】
- 2-熟悉【McpServer調(diào)試方法-MCP Inspector】
2-思路整理
1-核心思路
- 1-編寫傳統(tǒng)的Service業(yè)務(wù)代碼
- 2-在Service業(yè)務(wù)代碼頭上添加@tool裝飾器,即可實(shí)現(xiàn)FastMCP的Tool功能
- 3-在Service業(yè)務(wù)代碼頭上添加@mcp.tool()裝飾器,即可實(shí)現(xiàn)FastMCP的McpServer功能
- 4-主程序指定運(yùn)行方法-stdio進(jìn)程啟動(但是不要自己去啟動)
- 5-使用MCPInspector調(diào)試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業(yè)務(wù)代碼頭上添加@tool裝飾器,即可實(shí)現(xiàn)FastMCP的Tool功能
# 假設(shè) mcp 已經(jīng)正確導(dǎo)入
try:
from mcp import tool
except ImportError:
# 如果 mcp 未找到,模擬一個 tool 裝飾器
def tool(func):
return func
# 在 Service 業(yè)務(wù)代碼頭上添加 @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業(yè)務(wù)代碼頭上添加@mcp.tool()裝飾器,即可實(shí)現(xiàn)FastMCP的McpServer功能
from mcp.server.fastmcp import FastMCP
from city_01_service import CityDataServer
# 1-初始化 MCP 服務(wù)器
mcp = FastMCP("CityDataServer")
# 2-初始化城市信息服務(wù)器(業(yè)務(wù)代碼+@tool裝飾器)
city_server = CityDataServer()
# 3-在 Service 業(yè)務(wù)代碼頭上添加@mcp.tool()裝飾器
@mcp.tool()
# 獲取所有城市列表
async def get_city_list():
"""獲取所有城市列表。
返回:
str: 所有城市列表
"""
city_list = await city_server.get_city_list()
return city_list
# 4-主程序指定運(yùn)行方法-stdio進(jìn)程啟動
if __name__ == "__main__":
mcp.run(transport='stdio')3-參考網(wǎng)址
個人代碼實(shí)現(xiàn)倉庫:https://gitee.com/enzoism/python_mcp_01_inspector
4-上手實(shí)操
1-空工程初始化環(huán)境
mkdir my_project cd my_project python -m venv .venv
2-激活環(huán)境
# Windows source .venv/Scripts/activate # Mac source .venv/bin/activate
3-添加依賴
對應(yīng)的依賴是在激活的環(huán)境中
# uv用于后續(xù)MCP Inspector的連接 pip install uv httpx mcp
4-項目結(jié)構(gòu)
city_01_service.py:城市服務(wù)腳本city_02_mcp_server.py:MCP 服務(wù)器腳本
5-創(chuàng)建Python城市服務(wù)
city_01_service.py:城市服務(wù)腳本
import logging
# 假設(shè) mcp 已經(jīng)正確導(dǎo)入
try:
from mcp import tool
except ImportError:
# 如果 mcp 未找到,模擬一個 tool 裝飾器
def tool(func):
return func
# 配置日志打印級別
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
# 定義城市服務(wù)
class CityDataServer:
# 模擬城市的天氣數(shù)據(jù)
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},
}
@tool
async def get_city_weather(self, city: str) -> str:
"""獲取指定城市的天氣信息。
參數(shù):
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} 的天氣信息"
@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
@tool
async def get_city_detail(self, city: str) -> str:
"""獲取指定城市的信息。
參數(shù):
city (str): 城市名稱
返回:
str: 城市信息
"""
logging.info(f"獲取指定城市的信息: {city}")
if city in await self.get_city_list():
return f"{city} : 一個風(fēng)景秀麗的城市,你值得去玩一把"
else:
return f"抱歉,未找到 {city} 的城市信息"6-暴露Python城市MCPServer服務(wù)
city_02_mcp_server.py:MCP 服務(wù)器腳本
from mcp.server.fastmcp import FastMCP
from city_01_service import CityDataServer
# 初始化 MCP 服務(wù)器
mcp = FastMCP("CityDataServer")
# 初始化城市信息服務(wù)器
city_server = CityDataServer()
# 獲取天氣信息的工具
@mcp.tool()
async def get_city_weather(city: str) -> str:
"""獲取指定城市的天氣信息。
參數(shù):
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):
"""獲取指定城市的信息。
參數(shù):
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調(diào)試
1-安裝MCP Inspector
運(yùn)行機(jī)制:先運(yùn)行【MCPInspector】再運(yùn)行【uv run --with mcp mcp run city_02_mcp_server.py】
# 1-安裝MCP Inspector pip install mcp[cli]
2-運(yùn)行MCP Inspector服務(wù)
# 2-運(yùn)行MCP Inspector mcp dev city_02_mcp_server.py
3-訪問MCP Inspector網(wǎng)頁
再運(yùn)行【uv run --with mcp mcp run city_02_mcp_server.py】
http://127.0.0.1:6274

到此這篇關(guān)于Python MCPInspector調(diào)試的文章就介紹到這了,更多相關(guān)Python MCPInspector調(diào)試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python快速開發(fā)一個MCP服務(wù)器的實(shí)現(xiàn)示例
- 使用Python+Bright Data MCP實(shí)時抓取Google搜索結(jié)果完整教程
- 基于Python從零構(gòu)建一個MCP服務(wù)器
- 使用Python構(gòu)建MCP服務(wù)器的詳細(xì)配置步驟
- Python使用FastMCP實(shí)現(xiàn)Word文檔與JSON數(shù)據(jù)互轉(zhuǎn)
- Python?FastMCP構(gòu)建MCP服務(wù)端與客戶端的詳細(xì)步驟
- python開發(fā)Streamable?HTTP?MCP應(yīng)用小結(jié)
- 通過Python調(diào)用MCP的實(shí)現(xiàn)示例
相關(guān)文章
Python調(diào)用JavaScript代碼的幾種方法小結(jié)
日常Web端爬蟲過程中,經(jīng)常會遇到參數(shù)被加密的場景,因此,我們需要分析網(wǎng)頁源代碼通過調(diào)式,一層層剝離出關(guān)鍵的JS代碼,使用Python去執(zhí)行這段代碼,本文將聊聊利用 Python 調(diào)用 JS 的4種方式,需要的朋友可以參考下2024-12-12
Python使用FastMCP實(shí)現(xiàn)Word文檔與JSON數(shù)據(jù)互轉(zhuǎn)
這篇文章主要介紹了基于FastMCP框架實(shí)現(xiàn)的文檔處理服務(wù),可實(shí)現(xiàn)?Word?文檔(.docx)與?JSON?數(shù)據(jù)格式的雙向轉(zhuǎn)換,通過此服務(wù),開發(fā)者可以輕松實(shí)現(xiàn)文檔內(nèi)容提取、結(jié)構(gòu)化數(shù)據(jù)填充、樣式模板復(fù)用等功能,適用于自動化報告生成、數(shù)據(jù)導(dǎo)入導(dǎo)出等場景,需要的朋友可以參考下2025-06-06
Ubuntu 下 vim 搭建python 環(huán)境 配置
這篇文章主要介紹了Ubuntu 下 vim 搭建python環(huán)境配置,需要的朋友可以參考下2017-06-06
pytorch通過自己的數(shù)據(jù)集訓(xùn)練Unet網(wǎng)絡(luò)架構(gòu)
Unet是一個最近比較火的網(wǎng)絡(luò)結(jié)構(gòu)。它的理論已經(jīng)有很多大佬在討論了。本文主要從實(shí)際操作的層面,講解如何使用pytorch實(shí)現(xiàn)unet圖像分割2022-12-12
使用keras實(shí)現(xiàn)非線性回歸(兩種加激活函數(shù)的方式)
這篇文章主要介紹了使用keras實(shí)現(xiàn)非線性回歸(兩種加激活函數(shù)的方式),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Python多進(jìn)程multiprocessing用法實(shí)例分析
這篇文章主要介紹了Python多進(jìn)程multiprocessing用法,結(jié)合實(shí)例形式分析了Python多線程的概念以及進(jìn)程的創(chuàng)建、守護(hù)進(jìn)程、終止、退出進(jìn)程、進(jìn)程間消息傳遞等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

