@@ -382,16 +382,24 @@ def create_app() -> FastAPI:
382382 pass
383383 _prev_path = _mcp_mod .mcp .settings .streamable_http_path
384384 _prev_security = _mcp_mod .mcp .settings .transport_security
385+ _prev_stateless = getattr (_mcp_mod .mcp .settings , "stateless_http" , False )
385386 try :
386387 _mcp_mod .mcp .settings .streamable_http_path = "/"
387388 _mcp_mod .mcp .settings .transport_security = _mcp_transport_security (_mcp_mod .mcp )
389+ # Hosted callers can bind a tenant service and principal around each HTTP
390+ # operation. Stateful Streamable HTTP runs tool callbacks in the persistent
391+ # initialization task, which would retain the first request's ContextVars
392+ # for later requests. Stateless mode keeps every callback in its request
393+ # context and is therefore required for request-scoped service isolation.
394+ _mcp_mod .mcp .settings .stateless_http = True
388395 _mcp_asgi = _mcp_mod .mcp .streamable_http_app ()
389396 finally :
390397 # streamable_http_app() captures these settings in its session manager. Restore
391398 # the global FastMCP instance so importing the dashboard cannot alter the
392399 # standalone MCP server in the same process.
393400 _mcp_mod .mcp .settings .streamable_http_path = _prev_path
394401 _mcp_mod .mcp .settings .transport_security = _prev_security
402+ _mcp_mod .mcp .settings .stateless_http = _prev_stateless
395403 _mcp_mgr = _mcp_mod .mcp .session_manager
396404 except (Exception , SystemExit ) as _exc : # noqa: BLE001 - MCP mount stays optional
397405 import logging as _logging
0 commit comments