FastMcp 案例四(Streamable-http)
1、首先安装python环境
conda create -n mcp python=3.10
2、安装依赖
pip install fastmcp
3、编写一个server-sh.py
# -*- coding: utf-8 -*- # @Time : 2025/7/28 17:10 # @Author : yangwenjie # @Email : 邮箱 # @File : server-sh.py # @Project : fastmcp from fastmcp import FastMCP from datetime import datetime mcp = FastMCP() @mcp.tool def get_current_time(): """Get current time""" return datetime.now() @mcp.resource("resource://cities") def get_cities(): """返回支持查询天气的城市列表""" cities = ["北京", "上海", "广州", "深圳"] return f"Cities: {', '.join(cities)}" @mcp.resource("resource://{city}/weather") def get_city_weather(city: str) -> str: return f"Weather for {city}" @mcp.tool() def greet(name: str) -> str: """向用户打招呼""" return f"Hello, {name}!" @mcp.prompt def ask_about_topic(topic: str) -> str: """Generates a user message asking for an explanation of a topic.""" return f"Can you please explain the concept of '{topic}'?" if __name__ == "__main__": mcp.run(transport="streamable-http", host="0.0.0.0", port=3003, path="/mcp")
4、运行
python server-sh.py
╭─ FastMCP 2.0 ──────────────────────────────────────────────────────────────╮ │ │ │ _ __ ___ ______ __ __ _____________ ____ ____ │ │ _ __ ___ / ____/___ ______/ /_/ |/ / ____/ __ \ |___ \ / __ \ │ │ _ __ ___ / /_ / __ `/ ___/ __/ /|_/ / / / /_/ / ___/ / / / / / │ │ _ __ ___ / __/ / /_/ (__ ) /_/ / / / /___/ ____/ / __/_/ /_/ / │ │ _ __ ___ /_/ \__,_/____/\__/_/ /_/\____/_/ /_____(_)____/ │ │ │ │ │ │ │ │ 🖥️ Server name: FastMCP │ │ 📦 Transport: Streamable-HTTP │ │ 🔗 Server URL: http://0.0.0.0:3003/mcp │ │ │ │ 📚 Docs: https://gofastmcp.com │ │ 🚀 Deploy: https://fastmcp.cloud │ │ │ │ 🏎️ FastMCP version: 2.10.6 │ │ 🤝 MCP version: 1.12.2 │ │ │ ╰────────────────────────────────────────────────────────────────────────────╯ [07/29/25 10:14:24] INFO Starting MCP server 'FastMCP' with transport 'streamable-http' on http://0.0.0.0:3003/mcp server.py:1448 INFO: Started server process [1545763] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:3003 (Press CTRL+C to quit)
5、修改端口为3002 ,运行文件
fastmcp dev server-sh.py
Starting MCP inspector... ⚙️ Proxy server listening on localhost:6277 🔑 Session token: 1e1e09283bb43d35e11c95e03669efc9e296cf9197620c59d2119db5bd4f2c74 Use this token to authenticate requests or set DANGEROUSLY_OMIT_AUTH=true to disable auth 🚀 MCP Inspector is up and running at: http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=1e1e09283bb43d35e11c95e03669efc9e296cf9197620c59d2119db5bd4f2c74 🌐 Opening browser... New StreamableHttp connection request Query parameters: {"url":"http://0.0.0.0:3003/mcp","transportType":"streamable-http"} Created StreamableHttp server transport Created StreamableHttp client transport Client <-> Proxy sessionId: 3c5d3289-e1b1-45f2-a19f-1bb0728beaf7 Proxy <-> Server sessionId: 9781d4c316c24277b635ce8e6f6484a7 Received POST message for sessionId 3c5d3289-e1b1-45f2-a19f-1bb0728beaf7 Received GET message for sessionId 3c5d3289-e1b1-45f2-a19f-1bb0728beaf7
6、测试验证

本文来自博客园,作者:小白啊小白,Fighting,转载请注明原文链接:https://www.cnblogs.com/ywjfx/p/19010302

浙公网安备 33010602011771号