微信扫码
添加专属顾问
我要投稿
揭开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,
json_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 (read, write, get_session_id):
async with ClientSession(read, write) as session:
print(f"连接成功!")
# 初始化会话
await session.initialize()
print("会话初始化完成")
# 获取会话ID
session_id = get_session_id()
print(f"会话ID: {session_id}")
......
02
深入两个核心参数
"Accept": "application/json, text/event-stream"
03
认识session-id
04
样例实测与验证
@app.tool(name='hello')
async def hello(ctx: Context, name: str) -> str:
steps = 10
await ctx.report_progress(0.0, steps, 'MCP Server正在处理请求...')
# 模拟计算过程的多个步骤
for step in range(1, steps + 1):
await asyncio.sleep(1)
logger.info(f"正在处理第{step}步,发送进度通知...")
await ctx.report_progress(float(step), float(steps),f'正在处理第{step}步...')
await ctx.report_progress(steps, steps, '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,
json_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_app, host="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,
json_response=False
)
app2 = FastMCP(
name="SimpleServer2",...
stateless_http=False,
json_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(server, host="0.0.0.0", port=5050)
end
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-05-21
CursorRules:AI编程助手规则调优实战
2025-05-21
可以将任何符合OpenAPI规范的接口转 MCP Server吗?
2025-05-21
AI驱动的软件:为何强大的CI/CD基础至关重要
2025-05-21
Embedding模型微调:基于已有数据快速构建训练与评估数据集
2025-05-20
RAG与微调,大语言模型的“大脑升级”,该选哪条路?(小白科普)
2025-05-20
两万字记录微软Build2025主题演讲谈了什么:萨提亚展现好人缘,奥特曼、马斯克、黄仁勋轮番出镜,软件工程的本质在于好工具
2025-05-20
百度自研Agent专用版模型上线千帆!已针对企业级大模型应用进行指令调优
2025-05-19
大模型微调
2025-02-04
2025-02-04
2024-09-18
2024-07-11
2024-07-09
2024-07-11
2024-07-26
2025-02-05
2025-01-27
2025-02-01