欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?MCPInspector調(diào)試思路詳解

 更新時(shí)間:2025年05月12日 10:03:18   作者:坐吃山豬  
這篇文章主要介紹了Python?MCPInspector調(diào)試思路詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

Python-MCPInspector調(diào)試

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

1-核心知識(shí)點(diǎn)

  • 1-熟悉【McpServer編碼過(guò)程】
  • 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)程啟動(dòng)(但是不要自己去啟動(dòng))
  • 5-使用MCPInspector調(diào)試McpServer(2個(gè)步驟)
    • 【mcp dev city_02_mcp_server.py】是啟動(dòng)mcpInspector并指定mcpServer的路徑,
    • 然后在Inspector中啟動(dòng)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 未找到,模擬一個(gè) 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)程啟動(dòng)
if __name__ == "__main__":
    mcp.run(transport='stdio')

3-參考網(wǎng)址

個(gè)人代碼實(shí)現(xiàn)倉(cāng)庫(kù):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-添加依賴

對(duì)應(yīng)的依賴是在激活的環(huán)境中

# uv用于后續(xù)MCP Inspector的連接
pip install uv httpx mcp

4-項(xiàng)目結(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 未找到,模擬一個(gè) tool 裝飾器
    def tool(func):
        return func
# 配置日志打印級(jí)別
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} : 一個(gè)風(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-訪問(wèn)MCP Inspector網(wǎng)頁(yè)

再運(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python編程實(shí)戰(zhàn)之Oracle數(shù)據(jù)庫(kù)操作示例

    Python編程實(shí)戰(zhàn)之Oracle數(shù)據(jù)庫(kù)操作示例

    這篇文章主要介紹了Python編程實(shí)戰(zhàn)之Oracle數(shù)據(jù)庫(kù)操作,結(jié)合具體實(shí)例形式分析了Python的Oracle數(shù)據(jù)庫(kù)模塊cx_Oracle包安裝、Oracle連接及操作技巧,需要的朋友可以參考下
    2017-06-06
  • Python3 ID3決策樹(shù)判斷申請(qǐng)貸款是否成功的實(shí)現(xiàn)代碼

    Python3 ID3決策樹(shù)判斷申請(qǐng)貸款是否成功的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Python3 ID3決策樹(shù)判斷申請(qǐng)貸款是否成功的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Python3編程實(shí)現(xiàn)獲取阿里云ECS實(shí)例及監(jiān)控的方法

    Python3編程實(shí)現(xiàn)獲取阿里云ECS實(shí)例及監(jiān)控的方法

    這篇文章主要介紹了Python3編程實(shí)現(xiàn)獲取阿里云ECS實(shí)例及監(jiān)控的方法,涉及Python URL登陸及請(qǐng)求處理相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • 使用Python自動(dòng)生成HTML的方法示例

    使用Python自動(dòng)生成HTML的方法示例

    這篇文章主要介紹了使用Python自動(dòng)生成HTML的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 詳解Python中通用工具類與異常處理

    詳解Python中通用工具類與異常處理

    在Python開(kāi)發(fā)中,編寫可重用的工具類和通用的異常處理機(jī)制是提高代碼質(zhì)量和開(kāi)發(fā)效率的關(guān)鍵,本文將介紹如何將特定的異常類改寫為更通用的ValidationException,并創(chuàng)建一個(gè)通用的工具類Utils,需要的可以參考下
    2024-12-12
  • python筆記_將循環(huán)內(nèi)容在一行輸出的方法

    python筆記_將循環(huán)內(nèi)容在一行輸出的方法

    今天小編就為大家分享一篇python筆記_將循環(huán)內(nèi)容在一行輸出的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • 詳解pandas.DataFrame.plot() 畫圖函數(shù)

    詳解pandas.DataFrame.plot() 畫圖函數(shù)

    這篇文章主要介紹了詳解pandas.DataFrame.plot()畫圖函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Python基礎(chǔ)實(shí)戰(zhàn)總結(jié)

    Python基礎(chǔ)實(shí)戰(zhàn)總結(jié)

    今天要給大家介紹的是Python基礎(chǔ)實(shí)戰(zhàn),本文主要以舉例說(shuō)明講解:?jiǎn)栴}的關(guān)鍵點(diǎn)就是在于構(gòu)造姓名,學(xué)號(hào)和成績(jī),之后以字典的形式進(jìn)行寫入文件。這里準(zhǔn)備兩個(gè)列表,一個(gè)姓,一個(gè)名,之后使用random庫(kù)進(jìn)行隨機(jī)字符串拼接,得到姓名,需要的朋友可以參考一下
    2021-10-10
  • Python線程下使用鎖的技巧分享

    Python線程下使用鎖的技巧分享

    本篇文章給大家分享了Python線程下使用鎖需要注意的地方,有興趣的朋友們可以學(xué)習(xí)參考下。
    2018-09-09
  • Python實(shí)現(xiàn)的爬取百度貼吧圖片功能完整示例

    Python實(shí)現(xiàn)的爬取百度貼吧圖片功能完整示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的爬取百度貼吧圖片功能,結(jié)合完整實(shí)例形式分析了Python實(shí)現(xiàn)的百度貼吧圖片爬蟲(chóng)相關(guān)操作技巧,需要的朋友可以參考下
    2019-05-05

最新評(píng)論