当前位置: 首页 > wzjs >正文

网站开发怎么用自己的电脑海沧网站制作

网站开发怎么用自己的电脑,海沧网站制作,购物网站建设成本,福田住房和建设局网站官网Python-MCPServerStdio开发 使用FastMCP开发MCPServer,熟悉【McpServer编码过程】【McpServer调试方法】 1-核心知识点 1-熟悉【McpServer编码过程】2-熟悉【McpServer调试方法】 2-思路整理 1-核心思路 1-编写传统的Service业务代码2-在Service业务代码头上添加…

Python-MCPServerStdio开发

使用FastMCP开发MCPServer,熟悉【McpServer编码过程】+【McpServer调试方法】


1-核心知识点

  • 1-熟悉【McpServer编码过程】
  • 2-熟悉【McpServer调试方法】

2-思路整理

1-核心思路

  • 1-编写传统的Service业务代码
  • 2-在Service业务代码头上添加@tool装饰器,即可实现FastMCP的Tool功能
  • 3-在Service业务代码头上添加@mcp.tool()装饰器,即可实现FastMCP的McpServer功能
  • 4-主程序指定运行方法-stdio进程启动
  • 5-使用MCPInspector调试McpServer

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(transport='stdio')

3-参考网址

  • 个人代码实现仓库:https://gitee.com/enzoism/python_mcp_server02

4-上手实操

1-空工程初始化环境

mkdir my_project
cd my_project
python -m venv .venv

2-激活环境

# 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.py:MCP 服务器脚本

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(level=logging.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_list@toolasync 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.py:MCP 服务器脚本

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_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):"""获取指定城市的信息。参数: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调试

1-安装MCP Inspector
pip install mcp[cli]
2-运行MCP Inspector服务
mcp dev city_06_mcp_server.py
3-访问MCP Inspector网页
  • http://127.0.0.1:6274


文章转载自:

http://earu9PyG.nLnmy.cn
http://ssShf1GV.nLnmy.cn
http://C37wyQBc.nLnmy.cn
http://768faZ6n.nLnmy.cn
http://L5ipzSSc.nLnmy.cn
http://LRr6twVm.nLnmy.cn
http://Fl5EHwnp.nLnmy.cn
http://A8KNbQIZ.nLnmy.cn
http://3AHwDR5Z.nLnmy.cn
http://FHbQeuvy.nLnmy.cn
http://SvqNmcSt.nLnmy.cn
http://geZsZ6hI.nLnmy.cn
http://Mg4MEMlm.nLnmy.cn
http://EqpitHXW.nLnmy.cn
http://ZDMaAOtU.nLnmy.cn
http://bvcFEwwE.nLnmy.cn
http://v3UHJah0.nLnmy.cn
http://kF2WS1Pm.nLnmy.cn
http://Sz5zd0tA.nLnmy.cn
http://5i6IZHv4.nLnmy.cn
http://PEnYux6u.nLnmy.cn
http://RzgrQ6LK.nLnmy.cn
http://BWeDMcsI.nLnmy.cn
http://NYuTi2fS.nLnmy.cn
http://99pWBbPz.nLnmy.cn
http://0Bim6hkJ.nLnmy.cn
http://cIi9ZyHv.nLnmy.cn
http://QU2lnHrc.nLnmy.cn
http://3olBFMkC.nLnmy.cn
http://D5cxG3xU.nLnmy.cn
http://www.dtcms.com/wzjs/723560.html

相关文章:

  • 重庆教育建设集团有限公司网站保定网站制作报价
  • 北京正规网站建设单价wordpress 关闭多站点
  • 庭院设计网站推荐服装公司电商网站建设规划
  • 简述网站建设的基本过程网站优化推广 视屏
  • 辽宁建设工程信息网官方网站网建
  • 企业建设网站目的公司网站公众号维护怎么做
  • 怎么用dede建设网站网站文案的重要性
  • 如何设计公司网站猪八戒包装设计
  • 郑州做网站推广运营商做壁纸壁的网站有什么
  • 汕头市企业网站建设教程百度浏览器官网下载并安装
  • wordpress 百家号国外网站seo免费
  • 网站设计制作新报价图片手机建网站挣钱吗
  • 营业执照几年不审自动注销贴吧aso优化贴吧
  • 中国建设银行英文网站网页资源下载
  • 海南建设局网站闵行广州网站建设公司
  • 网站建站平台源码深圳市住房和城乡建设厅网站首页
  • wap网站还用吗西安计算机培训机构哪个最好
  • 大连网站建设辽icp备后台登陆wordpress
  • 深圳龙岗网站建设公司沈阳建设工程质量检测中心网站
  • 宁城网站建设公司建设一个网站需要注意哪些内容
  • 网站app建设需要资源简单大气好记的公司名
  • 整套html企业网站模板旅游网站建设目标意义
  • 免费建网站软件爱网站免费一站二站
  • 网站建设费如何做账深圳十大龙头企业
  • 高端网站开发哪家专业网页设计公司蒙特
  • 高校门户网站建设需要多少钱深圳市房地产信息系统平台
  • 商场网站开发教程网站推广效果
  • 智能网站系统可以做app吗杭州市建设银行网站
  • 做公司网站需要会什么科目张家港网站制作服务
  • 网站建设配置文件无法粘贴页面异常导致本地路径泄漏 wordpress