微信扫码
添加专属顾问
×
注意!页面内容来自https://www.53ai.com/news/finetuning/2025052142596.html,本站不储存任何内容,为了更好的阅读体验进行在线解析,若有广告出现,请及时反馈。若您觉得侵犯了您的利益,请通知我们进行删除,然后访问 原网页
我要投稿
揭开MCP SDK最新版streamable HTTP模式的神秘面纱,深入解析与实操指南。
核心内容:
1. MCP SDK v1.9.0版本更新亮点:streamable HTTP模式
2. 快速开启streamable HTTP模式的步骤与核心参数解析
3. 客户端和服务端代码示例,以及session-id的理解和应用
01
快速上手:开启streamable HTTP
# 创建FastMCP实例
app = FastMCP(
name="SimpleServer",
port=5050,
stateless_http=False,
on_response=False,
streamable_http_path="/mcp"
)
....工具....
if __name__ == '__main__':
app.run(transport='streamable-http')http://localhost:5050/mcp
try:
async with streamablehttp_client(url=server_url) as (readwriteget_session_id):
async with ClientSession(readwrite) as session:
print(f"连接成功!")
# 初始化会话
await session.initialize()
print("会话初始化完成")
# 获取会话ID
session_id = get_session_id()
print(f"会话ID: {session_id}")
......02
深入两个核心参数
"Accept": "application/ontext/event-stream"
03
认识session-id
04
样例实测与验证
@app.tool(name='hello')
async def hello(ctx: Contextname: str) -> str:
steps = 10
await ctx.report_progress(0.0steps, 'MCP Server正在处理请求...')
# 模拟计算过程的多个步骤
for step in range(1steps + 1):
await asyncio.sleep(1)
logger.info(f"正在处理第{step}步,发送进度通知...")
await ctx.report_progress(float(step)float(steps),f'正在处理第{step}步...')
await ctx.report_progress(stepssteps, 'MCP Server请求处理完成!')
return f'Hello,{name}'05
完整的通信流程
06
Low-level API开发服务端
...
mcp_server = Server(name="example")
...call_tool等实现...
try:
# 创建会话管理器
session_manager = StreamableHTTPSessionManager(
app=mcp_server,
on_response=True,
stateless=False
)
starlette_app = Starlette(
debug=True,
routes=[
Mount("/mcp"app=session_manager.handle_request),
],
lifespan=lambda app: session_manager.run(),
)
config = uvicorn.Config(starlette_apphost="127.0.0.1"port=5050)
server = uvicorn.Server(config)
await server.serve()
logger.info("MCP server is running on http://127.0.0.1:5050")
......07
解读多应用实例模式
......
app = FastMCP(
name="SimpleServer",...
stateless_http=True,
on_response=False
)
app2 = FastMCP(
name="SimpleServer2",...
stateless_http=False,
on_response=False
)
if __name__ == '__main__':
......
@asynccontextmanager
async def lifespan(server):
asyncwith contextlib.AsyncExitStack() as stack:
await stack.enter_async_context(app.session_manager.run())
await stack.enter_async_context(app2.session_manager.run())
yield
server = FastAPI(lifespan=lifespan)
server.mount("/server1"app.streamable_http_app())
server.mount("/server2"app2.streamable_http_app())
print("Starting FastAPI server on http://localhost:5050")
print("- App1 available at: http://localhost:5050/server1")
print("- App2 available at: http://localhost:5050/server2")
uvicorn.run(serverhost="0.0.0.0"port=5050)end
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-01-10
前Mata GenAI研究员田渊栋的年终总结:关于未来AI的思考
2026-01-07
智元发布SOP:让机器人在真实世界规模化部署与智能化运行
2026-01-04
英伟达4B小模型:合成数据+测试时微调+优化集成
2026-01-04
2026年 LLM 微调全指南
2026-01-03
本地跑小模型带来5倍性能且成本极低!斯坦福从信息论视角重构智能体设计
2026-01-02
DeepSeek 发布新论文,提出全新 MHC 架构,有何创新与应用前景?
2026-01-01
刚刚,梁文锋署名,DeepSeek元旦新论文要开启架构新篇章
2025-12-30
数据蒸馏技术探索
2025-10-21
2025-11-21
2025-11-05
2025-11-05
2025-12-04
2025-11-22
2025-11-20
2025-11-19
2026-01-01
2026-01-02
2026-01-02
2025-11-19
2025-09-25
2025-06-20
2025-06-17
2025-05-21
2025-05-17
2025-05-14