From def1425b89b375b090c452f6008511b198abb725 Mon Sep 17 00:00:00 2001 From: street1983nk Date: Fri, 14 Aug 2026 20:46:04 +0200 Subject: [PATCH] fix(mcp): make stateless_http configurable and session-capable by default Clients built on the MCP SDK >= 1.28 keep the session that initialize creates. With stateless_http=True the fastmcp transport throws that session away per request, so the next call fails with "Session terminated" and both server-to-client channels are gone. Default to a session-capable transport, which is what those clients expect, and keep the previous behaviour available for deployments that want it by setting MCP_STATELESS_HTTP=1. Fixes #227 Signed-off-by: street1983nk --- ex_app/lib/main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ex_app/lib/main.py b/ex_app/lib/main.py index 591f1f2..d5e2b16 100644 --- a/ex_app/lib/main.py +++ b/ex_app/lib/main.py @@ -38,7 +38,10 @@ mcp = FastMCP(name="nextcloud") mcp.add_middleware(UserAuthMiddleware()) mcp.add_middleware(ToolListMiddleware(mcp)) -http_mcp_app = mcp.http_app("/", transport="http", stateless_http=True) +# Session-capable by default: SDK >= 1.28 clients keep the session after initialize +# and fail with "Session terminated" when the transport is stateless. See #227. +_stateless_http = os.getenv("MCP_STATELESS_HTTP", "0").lower() in ("1", "true", "yes") +http_mcp_app = mcp.http_app("/", transport="http", stateless_http=_stateless_http) fast_app = FastAPI(lifespan=http_mcp_app.lifespan)