使用说明
mcp服务器
基于Python的MCP服务器示例
主要功能
- BMI计算:输入体重(kg)和身高(m)计算BMI(体质量指数)。
- 查询天气:输入城市名称以查看该城市的当前天气信息。
使用示例
1.BMI计算
@mcp.tool()
def calculate_bmi(weight_kg: float, height_m: float) -> float:
""" Calculate the BMI given weight in kg and height in meters"""
return weight_kg / (height_m ** 2)
2.查询天气
@mcp.tool()
async def fetch_weather(city: str) -> str:
""" Fetch current weather for a city"""
async with httpx.AsyncClient() as client:
response = await client.get(f"https://api.weather.com/{city}")
return response.text
执行方法
- 依赖性安装
pip install httpx fastmcp - 运行服务器
python server.py