电子商务网站建设课程设计报告,网站建设分为哪几种类型,各大网站发布信息,价格低怎么说好听Python-MCPInspector调试 使用FastMCP开发MCPServer#xff0c;熟悉【McpServer编码过程】【MCPInspector调试方法】- 可以这样理解#xff1a;只编写一个McpServer#xff0c;然后使用MCPInspector作为McpClient进行McpServer的调试 1-核心知识点
1-熟悉【McpServer编…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调试McpServer2个步骤 【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(transportstdio) 3-参考网址
个人代码实现仓库https://gitee.com/enzoism/python_mcp_01_inspector 4-上手实操
1-空工程初始化环境
mkdir my_project
cd my_project
python -m venv .venv2-激活环境
# 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.pyMCP 服务器脚本
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(levellogging.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_listtoolasync 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.pyMCP 服务器脚本 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_infomcp.tool()
# 获取所有城市列表
async def get_city_list():获取所有城市列表。返回:str: 所有城市列表city_list await city_server.get_city_list()return city_listmcp.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(transportstdio) 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.py3-访问MCP Inspector网页 再运行【uv run --with mcp mcp run city_02_mcp_server.py】 http://127.0.0.1:6274