安装fastmcp
pip install fastmcp
编写mcp服务端代码
from fastmcp import FastMCP
mcp= FastMCP('weather')@mcp.tool()
def get_weather(city: str):'''获取对应城市的天气:param city: 目标城市:return: 该城市的天气'''return f"{city}天气晴朗,温度60度!"
@mcp.tool()
def get_price(meat: str):'''获取肉类价格:param meat: 肉类名称:return: 肉类对应的价格'''return f"{meat}价格35元每斤!"if __name__ == '__main__':mcp.run(transport='stdio')
编写mcp客户端代码
from fastmcp import Client
import asyncioasync def run():# 客户端实例化!传入服务端的代码脚本client = Client('server.py')# 异步调用调用clientasync with client:# 获取服务端工具列表!tools = await client.list_tools()# 调用服务端工具response = await client.call_tool('{}'.format(tools[1].name),{'meat':'猪肉'})print(response)if __name__ == '__main__':asyncio.run(run())