fastmcp构建mcp server和client

文章目录

项目官网

fastmcp

Server

mcp_server.py

python 复制代码
# 新建实例,这个一定要是一个单独的文件
from fastmcp import FastMCP

mcp = FastMCP(name="MyAssistantServer")

weather_tools.py

python 复制代码
# 工具集需要导入之前的server实列
from typing import Annotated
from mcp_server import mcp
import datetime
from dataclasses import dataclass, field

@dataclass
class WeatherResult:
    date: datetime.date=field(metadata={"description": "日期"})
    temperature: float=field(metadata={"description": "温度,摄氏度"})
    weather:str=field(metadata={"description": "天气:小雨,晴,阴,雾等等"})

@dataclass
class CityWeahterResult:
    city: str=field(metadata={"description": "城市"})
    results: list[WeatherResult]=field(metadata={"description": "几天的天气集合"})

@mcp.tool(
        name="Get city weather",
        description="获取某个城市未来几天的天气",)
def get_weather(
    city: Annotated[str,"城市名"],
    days:Annotated[int,"想要预测的天数"]) -> CityWeahterResult:
    return CityWeahterResult()

run_server.py

python 复制代码
from mcp_server import mcp
import weather_tools #只要导入的工具集,就会自动注册

if __name__ == "__main__":
    mcp.run(transport="http",host="127.0.0.1", port=8765)

客户端

client.py

python 复制代码
import asyncio
from fastmcp import Client


client = Client("http://127.0.0.1:8765/mcp")

async def main():
    async with client:
        # Basic server interaction
        await client.ping()
        
        # List available operations
        tools = await client.list_tools()
        resources = await client.list_resources()
        prompts = await client.list_prompts()
        
        # Execute operations
        print(tools)

asyncio.run(main())

扫描到的工具集

javascript 复制代码
[
  Tool(
    (name = "Get city weather"),
    (title = None),
    (description = "获取某个城市未来几天的天气"),
    (inputSchema = {
      properties: {
        city: { description: "城市名", type: "string" },
        days: { description: "想要预测的天数", type: "integer" },
      },
      required: ["city", "days"],
      type: "object",
    }),
    (outputSchema = {
      $defs: {
        WeatherResult: {
          properties: {
            date: { description: "日期", format: "date", type: "string" },
            temperature: { description: "温度,摄氏度", type: "number" },
            weather: {
              description: "天气:小雨,晴,阴,雾等等",
              type: "string",
            },
          },
          required: ["date", "temperature", "weather"],
          type: "object",
        },
      },
      properties: {
        city: { description: "城市", type: "string" },
        results: {
          description: "几天的天气集合",
          items: { $ref: "#/$defs/WeatherResult" },
          type: "array",
        },
      },
      required: ["city", "results"],
      type: "object",
    }),
    (icons = None),
    (annotations = None),
    (meta = { _fastmcp: { tags: [] } }),
    (execution = None)
  ),
];
相关推荐
0思必得036 分钟前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
韩立学长43 分钟前
【开题答辩实录分享】以《基于Python的大学超市仓储信息管理系统的设计与实现》为例进行选题答辩实录分享
开发语言·python
qq_192779871 小时前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
u0109272711 小时前
使用Plotly创建交互式图表
jvm·数据库·python
爱学习的阿磊1 小时前
Python GUI开发:Tkinter入门教程
jvm·数据库·python
壮Sir不壮1 小时前
2026年奇点:Clawdbot引爆个人AI代理
人工智能·ai·大模型·claude·clawdbot·moltbot·openclaw
玉梅小洋1 小时前
Claude Code 从入门到精通(七):Sub Agent 与 Skill 终极PK
人工智能·ai·大模型·ai编程·claude·ai工具
-嘟囔着拯救世界-2 小时前
【保姆级教程】Win11 下从零部署 Claude Code:本地环境配置 + VSCode 可视化界面全流程指南
人工智能·vscode·ai·编辑器·html5·ai编程·claude code
Imm7772 小时前
中国知名的车膜品牌推荐几家
人工智能·python
x-cmd2 小时前
[x-cmd] x claude ds - 无需 Claude 账号!零门槛让 Claude Code 接入 DeepSeek AI 模型
ai·claude·deepseek·claude-code·写代码