最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)試方法】-> 可以這樣理解:只編寫(xiě)一個(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-編寫(xiě)傳統(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-添加依賴(lài)

對(duì)應(yīng)的依賴(lài)是在激活的環(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): 城市名稱(chēng)
        返回:
        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): 城市名稱(chēng)
        返回:
        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): 城市名稱(chēng)
    返回:
    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): 城市名稱(chēng)
    返回:
    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調(diào)用JavaScript代碼的幾種方法小結(jié)

    Python調(diào)用JavaScript代碼的幾種方法小結(jié)

    日常Web端爬蟲(chóng)過(guò)程中,經(jīng)常會(huì)遇到參數(shù)被加密的場(chǎng)景,因此,我們需要分析網(wǎng)頁(yè)源代碼通過(guò)調(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)

    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)換,通過(guò)此服務(wù),開(kāi)發(fā)者可以輕松實(shí)現(xiàn)文檔內(nèi)容提取、結(jié)構(gòu)化數(shù)據(jù)填充、樣式模板復(fù)用等功能,適用于自動(dòng)化報(bào)告生成、數(shù)據(jù)導(dǎo)入導(dǎo)出等場(chǎng)景,需要的朋友可以參考下
    2025-06-06
  • Python第三方庫(kù)OS庫(kù)方法實(shí)操

    Python第三方庫(kù)OS庫(kù)方法實(shí)操

    這篇文章主要給大家介紹了關(guān)于Python第三方庫(kù)OS庫(kù)的相關(guān)資料,os庫(kù)主要是對(duì)文件和文件夾進(jìn)行操作,在Python中對(duì)?件和?件夾的操作要借助os模塊??的相關(guān)功能,需要的朋友可以參考下
    2024-06-06
  • Ubuntu 下 vim 搭建python 環(huán)境 配置

    Ubuntu 下 vim 搭建python 環(huán)境 配置

    這篇文章主要介紹了Ubuntu 下 vim 搭建python環(huán)境配置,需要的朋友可以參考下
    2017-06-06
  • 使用Python自定義創(chuàng)建的Log日志模塊

    使用Python自定義創(chuàng)建的Log日志模塊

    這篇文章主要介紹了使用Python自定義創(chuàng)建的Log日志模塊,日志文件是用于記錄系統(tǒng)操作事件的文件集合,可分為事件日志和消息日志。具有處理歷史數(shù)據(jù)、診斷問(wèn)題的追蹤以及理解系統(tǒng)的活動(dòng)等重要作用,需要的朋友可以參考下
    2023-07-07
  • pytorch通過(guò)自己的數(shù)據(jù)集訓(xùn)練Unet網(wǎng)絡(luò)架構(gòu)

    pytorch通過(guò)自己的數(shù)據(jù)集訓(xùn)練Unet網(wǎng)絡(luò)架構(gòu)

    Unet是一個(gè)最近比較火的網(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ù)的方式)

    這篇文章主要介紹了使用keras實(shí)現(xiàn)非線性回歸(兩種加激活函數(shù)的方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Python實(shí)現(xiàn)端口檢測(cè)的方法

    Python實(shí)現(xiàn)端口檢測(cè)的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)端口檢測(cè)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • python實(shí)現(xiàn)矩陣打印

    python實(shí)現(xiàn)矩陣打印

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)矩陣打印的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • Python多進(jìn)程multiprocessing用法實(shí)例分析

    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

最新評(píng)論

探索| 万载县| 龙口市| 蓝田县| 云龙县| 壤塘县| 张家界市| 巴塘县| 琼海市| 界首市| 岗巴县| 东兰县| 铜山县| 宝兴县| 泾阳县| 武乡县| 康定县| 伊吾县| 龙岩市| 志丹县| 舟山市| 都江堰市| 溧水县| 华容县| 澄江县| 清河县| 信丰县| 惠东县| 安西县| 故城县| 石家庄市| 土默特右旗| 丹阳市| 海盐县| 广昌县| 绍兴市| 逊克县| 家居| 拜城县| 蓝田县| 永州市|