Python?MCPInspector調(diào)試思路詳解
Python-MCPInspector調(diào)試
使用FastMCP開發(fā)MCPServer,熟悉【McpServer編碼過程】+【MCPInspector調(diào)試方法】-> 可以這樣理解:只編寫一個McpServer,然后使用MCPInspector作為McpClient進行McpServer的調(diào)試
1-核心知識點
- 1-熟悉【McpServer編碼過程】
- 2-熟悉【McpServer調(diào)試方法-MCP Inspector】
2-思路整理
1-核心思路
- 1-編寫傳統(tǒng)的Service業(yè)務(wù)代碼
- 2-在Service業(yè)務(wù)代碼頭上添加@tool裝飾器,即可實現(xiàn)FastMCP的Tool功能
- 3-在Service業(yè)務(wù)代碼頭上添加@mcp.tool()裝飾器,即可實現(xiàn)FastMCP的McpServer功能
- 4-主程序指定運行方法-stdio進程啟動(但是不要自己去啟動)
- 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裝飾器,即可實現(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()裝飾器,即可實現(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-主程序指定運行方法-stdio進程啟動 if __name__ == "__main__": mcp.run(transport='stdio')
3-參考網(wǎng)址
個人代碼實現(xiàn)倉庫:https://gitee.com/enzoism/python_mcp_01_inspector
4-上手實操
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
運行機制:先運行【MCPInspector】再運行【uv run --with mcp mcp run city_02_mcp_server.py】
# 1-安裝MCP Inspector pip install mcp[cli]
2-運行MCP Inspector服務(wù)
# 2-運行MCP Inspector mcp dev city_02_mcp_server.py
3-訪問MCP Inspector網(wǎng)頁
再運行【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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python編程實戰(zhàn)之Oracle數(shù)據(jù)庫操作示例
這篇文章主要介紹了Python編程實戰(zhàn)之Oracle數(shù)據(jù)庫操作,結(jié)合具體實例形式分析了Python的Oracle數(shù)據(jù)庫模塊cx_Oracle包安裝、Oracle連接及操作技巧,需要的朋友可以參考下2017-06-06Python3 ID3決策樹判斷申請貸款是否成功的實現(xiàn)代碼
這篇文章主要介紹了Python3 ID3決策樹判斷申請貸款是否成功的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05Python3編程實現(xiàn)獲取阿里云ECS實例及監(jiān)控的方法
這篇文章主要介紹了Python3編程實現(xiàn)獲取阿里云ECS實例及監(jiān)控的方法,涉及Python URL登陸及請求處理相關(guān)操作技巧,需要的朋友可以參考下2017-08-08python筆記_將循環(huán)內(nèi)容在一行輸出的方法
今天小編就為大家分享一篇python筆記_將循環(huán)內(nèi)容在一行輸出的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08詳解pandas.DataFrame.plot() 畫圖函數(shù)
這篇文章主要介紹了詳解pandas.DataFrame.plot()畫圖函數(shù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06