You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ju...@apache.org on 2026/08/06 04:31:30 UTC

(superset) branch master updated: fix(mcp): make streamable-http session mode configurable via MCP_STATELESS_HTTP (#42814)

This is an automated email from the ASF dual-hosted git repository.

justinpark pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new d594a4d1578 fix(mcp): make streamable-http session mode configurable via MCP_STATELESS_HTTP (#42814)
d594a4d1578 is described below

commit d594a4d1578d506597cc902490e1139794acef8a
Author: JUST.in DO IT <ju...@airbnb.com>
AuthorDate: Wed Aug 5 21:31:22 2026 -0700

    fix(mcp): make streamable-http session mode configurable via MCP_STATELESS_HTTP (#42814)
    
    Co-authored-by: Claude Sonnet 5 <no...@anthropic.com>
---
 superset/mcp_service/__main__.py                   | 17 +++-
 superset/mcp_service/mcp_config.py                 | 19 +++++
 superset/mcp_service/server.py                     | 21 ++++-
 tests/unit_tests/mcp_service/test_mcp_config.py    | 21 +++++
 tests/unit_tests/mcp_service/test_mcp_e2e_smoke.py | 12 ++-
 tests/unit_tests/mcp_service/test_mcp_server.py    | 93 +++++++++++++++++++++-
 6 files changed, 174 insertions(+), 9 deletions(-)

diff --git a/superset/mcp_service/__main__.py b/superset/mcp_service/__main__.py
index f477c9eaf01..474d5bcc3f3 100644
--- a/superset/mcp_service/__main__.py
+++ b/superset/mcp_service/__main__.py
@@ -165,7 +165,22 @@ def main() -> None:
         if transport == "streamable-http":
             host = os.environ.get("FASTMCP_HOST", "127.0.0.1")
             port = int(os.environ.get("FASTMCP_PORT", "5008"))
-            mcp.run(transport=transport, host=host, port=port, stateless_http=True)
+
+            # See MCP_STATELESS_HTTP's docstring in mcp_config.py -- stateless
+            # mode races a tool's progress notifications against the
+            # transport teardown that follows its HTTP request.
+            from superset.mcp_service.flask_singleton import get_flask_app
+            from superset.mcp_service.mcp_config import MCP_STATELESS_HTTP
+
+            stateless_http = get_flask_app().config.get(
+                "MCP_STATELESS_HTTP", MCP_STATELESS_HTTP
+            )
+            mcp.run(
+                transport=transport,
+                host=host,
+                port=port,
+                stateless_http=stateless_http,
+            )
         else:
             mcp.run(transport=transport)
 
diff --git a/superset/mcp_service/mcp_config.py b/superset/mcp_service/mcp_config.py
index 95190e921f7..612448b98ca 100644
--- a/superset/mcp_service/mcp_config.py
+++ b/superset/mcp_service/mcp_config.py
@@ -66,6 +66,24 @@ MCP_BUG_REPORT_CONTACT: str | None = None
 # MCP Debug mode - shows suppressed initialization output in stdio mode
 MCP_DEBUG = False
 
+# Streamable-HTTP session mode used by run_server() (superset/mcp_service/server.py)
+# and the CLI entrypoint (superset/mcp_service/__main__.py).
+#
+# True (default): each HTTP request gets a fresh, ephemeral transport that is
+# torn down as soon as that single request/response completes, while the
+# tool call it started keeps running as a background task. If a client gives
+# up on a still-running call (its own timeout, a reconnect, etc.), the next
+# progress notification that tool sends hits the now-closed transport and
+# raises anyio.ClosedResourceError/BrokenResourceError -- crashing that
+# session and disconnecting other concurrent clients on the same worker.
+#
+# False: sessions are tracked by Mcp-Session-Id and the transport stays alive
+# for the session's lifetime, so a client disconnecting mid-call no longer
+# crashes the tool. This requires session-affinity routing on Mcp-Session-Id
+# at the mesh/ingress layer for multi-pod deployments -- a client's follow-up
+# requests must land on the pod that created its session.
+MCP_STATELESS_HTTP = True
+
 # MCP RBAC - when True, tools with class_permission_name are checked
 # against the FAB security_manager before execution.
 MCP_RBAC_ENABLED = True
@@ -716,6 +734,7 @@ def get_mcp_config(app_config: dict[str, Any] | None = None) -> dict[str, Any]:
         "WEBDRIVER_BASEURL": WEBDRIVER_BASEURL,
         "WEBDRIVER_BASEURL_USER_FRIENDLY": WEBDRIVER_BASEURL_USER_FRIENDLY,
         "MCP_DEBUG": MCP_DEBUG,
+        "MCP_STATELESS_HTTP": MCP_STATELESS_HTTP,
         "MCP_RBAC_ENABLED": MCP_RBAC_ENABLED,
         "MCP_DISABLED_TOOLS": set(MCP_DISABLED_TOOLS),
         "MCP_DISABLED_CHART_PLUGINS": MCP_DISABLED_CHART_PLUGINS,
diff --git a/superset/mcp_service/server.py b/superset/mcp_service/server.py
index 630141d712a..81540c6380a 100644
--- a/superset/mcp_service/server.py
+++ b/superset/mcp_service/server.py
@@ -36,6 +36,7 @@ from superset.mcp_service.app import create_mcp_app, init_fastmcp_server
 from superset.mcp_service.jwt_verifier import BrowserHelloMiddleware
 from superset.mcp_service.mcp_config import (
     get_mcp_factory_config,
+    MCP_STATELESS_HTTP,
     MCP_STORE_CONFIG,
     MCP_TOOL_SEARCH_CONFIG,
 )
@@ -944,7 +945,11 @@ def run_server(
     Uses streamable-http transport for HTTP server mode.
 
     For multi-pod deployments, configure MCP_EVENT_STORE_CONFIG with Redis URL
-    to share session state across pods.
+    to share session state across pods. If MCP_STATELESS_HTTP is also set to
+    False (see its docstring in mcp_config.py), sessions are stateful and
+    multi-pod additionally requires session-affinity routing on
+    Mcp-Session-Id at the mesh/ingress layer -- otherwise a session's
+    follow-up requests can land on a pod that never created it.
 
     Args:
         host: Host to bind to
@@ -1025,13 +1030,23 @@ def run_server(
         try:
             logging.info("Starting FastMCP on %s:%s", host, port)
 
+            # See MCP_STATELESS_HTTP's docstring in mcp_config.py: stateless
+            # mode races a tool's progress notifications against the
+            # transport teardown that follows its HTTP request, crashing the
+            # session if a client disconnects mid-call.
+            stateless_http = (
+                flask_app.config.get("MCP_STATELESS_HTTP", MCP_STATELESS_HTTP)
+                if flask_app is not None
+                else MCP_STATELESS_HTTP
+            )
+
             if event_store is not None:
                 # Multi-pod: Use http_app with Redis EventStore, run with uvicorn
                 logging.info("Running in multi-pod mode with Redis EventStore")
                 app = mcp_instance.http_app(
                     transport="streamable-http",
                     event_store=event_store,
-                    stateless_http=True,
+                    stateless_http=stateless_http,
                     middleware=starlette_middleware,
                 )
                 uvicorn.run(app, host=host, port=port)
@@ -1042,7 +1057,7 @@ def run_server(
                     transport="streamable-http",
                     host=host,
                     port=port,
-                    stateless_http=True,
+                    stateless_http=stateless_http,
                     middleware=starlette_middleware,
                 )
         except Exception as e:
diff --git a/tests/unit_tests/mcp_service/test_mcp_config.py b/tests/unit_tests/mcp_service/test_mcp_config.py
index 99602aa9f33..fc69afb327e 100644
--- a/tests/unit_tests/mcp_service/test_mcp_config.py
+++ b/tests/unit_tests/mcp_service/test_mcp_config.py
@@ -228,6 +228,27 @@ def test_get_mcp_config_respects_app_config_override() -> None:
     assert config["MCP_DISABLED_TOOLS"] == custom
 
 
+def test_get_mcp_config_includes_mcp_stateless_http_key() -> None:
+    """get_mcp_config must include MCP_STATELESS_HTTP in its defaults dict, like
+    MCP_DEBUG and MCP_RBAC_ENABLED, so an operator override in superset_config.py
+    is actually read back out via flask_app.config (see run_server() and
+    __main__.main() in the mcp_service package, which read this key)."""
+    from superset.mcp_service.mcp_config import get_mcp_config, MCP_STATELESS_HTTP
+
+    config = get_mcp_config()
+    assert "MCP_STATELESS_HTTP" in config
+    assert config["MCP_STATELESS_HTTP"] is MCP_STATELESS_HTTP is True
+
+
+def test_get_mcp_config_respects_mcp_stateless_http_override() -> None:
+    """An operator's MCP_STATELESS_HTTP=False in superset_config.py must take
+    precedence over the module-level True default."""
+    from superset.mcp_service.mcp_config import get_mcp_config
+
+    config = get_mcp_config({"MCP_STATELESS_HTTP": False})
+    assert config["MCP_STATELESS_HTTP"] is False
+
+
 def test_build_composite_verifier_string_prefix():
     """A plain-string FAB_API_KEY_PREFIXES is wrapped into a single-element list."""
     from superset.mcp_service.mcp_config import _build_composite_verifier
diff --git a/tests/unit_tests/mcp_service/test_mcp_e2e_smoke.py b/tests/unit_tests/mcp_service/test_mcp_e2e_smoke.py
index bad7db9a32b..a2930ed4032 100644
--- a/tests/unit_tests/mcp_service/test_mcp_e2e_smoke.py
+++ b/tests/unit_tests/mcp_service/test_mcp_e2e_smoke.py
@@ -30,8 +30,12 @@ CI.
 This module closes that gap with a single smoke test file that:
 
 1. Builds the *real* ASGI app the way ``run_server()`` does --
-   ``mcp.http_app(transport="streamable-http", stateless_http=True)`` --
+   ``mcp.http_app(transport="streamable-http", stateless_http=...)`` --
    with the production FastMCP-level middleware list attached.
+   ``stateless_http`` defaults to True (``MCP_STATELESS_HTTP`` in
+   ``mcp_config.py``); this suite pins it to False, the value deployments
+   override to in order to avoid the crash documented on that config's
+   docstring.
 2. Serves it in-process over real MCP streamable-HTTP JSON-RPC using
    ``httpx.ASGITransport`` (no real TCP socket, no real network).
 3. Drives it with FastMCP's own high-level ``Client``, proving the full
@@ -143,7 +147,9 @@ async def _real_asgi_client() -> AsyncIterator[Client]:
     Builds the app the way ``run_server()`` does for the multi-pod/http_app
     path (``server.py:938``): FastMCP-level middleware from
     ``build_middleware_list()`` attached to the shared ``mcp`` instance, then
-    ``mcp.http_app(transport="streamable-http", stateless_http=True)``.
+    ``mcp.http_app(transport="streamable-http", stateless_http=False)`` --
+    pinned to False rather than reading ``MCP_STATELESS_HTTP``'s True default,
+    since False is what deployments actually run (see module docstring).
 
     The request/response cycle is driven over ``httpx.ASGITransport`` (no
     real socket) using FastMCP's own ``StreamableHttpTransport`` so the
@@ -165,7 +171,7 @@ async def _real_asgi_client() -> AsyncIterator[Client]:
         mcp.add_middleware(middleware)
 
     try:
-        asgi_app = mcp.http_app(transport="streamable-http", stateless_http=True)
+        asgi_app = mcp.http_app(transport="streamable-http", stateless_http=False)
 
         def httpx_client_factory(**kwargs: Any) -> httpx.AsyncClient:
             return httpx.AsyncClient(
diff --git a/tests/unit_tests/mcp_service/test_mcp_server.py b/tests/unit_tests/mcp_service/test_mcp_server.py
index 4ab76afecaa..db701189b6e 100644
--- a/tests/unit_tests/mcp_service/test_mcp_server.py
+++ b/tests/unit_tests/mcp_service/test_mcp_server.py
@@ -17,8 +17,10 @@
 
 """Tests for MCP server EventStore creation."""
 
-from collections.abc import Awaitable, Callable
-from typing import cast
+import contextlib
+import os
+from collections.abc import Awaitable, Callable, Iterator
+from typing import Any, cast
 from unittest.mock import MagicMock, patch
 
 import pytest
@@ -316,3 +318,90 @@ def test_create_auth_provider_fails_closed_on_insecure_guest_secret() -> None:
     ):
         with pytest.raises(MCPAuthConfigError):
             _create_auth_provider(flask_app)
+
+
+@contextlib.contextmanager
+def _run_server_dependencies(
+    flask_config: dict[str, Any],
+) -> Iterator[MagicMock]:
+    """Patch every ``run_server()`` collaborator except stateless_http resolution.
+
+    Returns the ``mcp_instance`` mock so callers can assert on the kwargs its
+    ``run()`` was called with -- everything else (auth, middleware, event
+    store, health endpoint) is stubbed out since this is only exercising the
+    ``flask_app.config.get("MCP_STATELESS_HTTP", ...)`` wiring, not those
+    other startup steps.
+    """
+    from superset.mcp_service import server
+
+    flask_app = MagicMock()
+    flask_app.config = flask_config
+    mcp_instance = MagicMock()
+
+    with (
+        patch.object(server, "configure_logging"),
+        patch.object(server, "_suppress_third_party_warnings"),
+        patch(
+            "superset.mcp_service.flask_singleton.get_flask_app",
+            return_value=flask_app,
+        ),
+        patch.object(server, "_create_auth_provider", return_value=None),
+        patch.object(server, "build_middleware_list", return_value=[]),
+        patch.object(
+            server, "create_response_size_guard_middleware", return_value=None
+        ),
+        patch(
+            "superset.mcp_service.caching.create_response_caching_middleware",
+            return_value=None,
+        ),
+        patch.object(server, "init_fastmcp_server", return_value=mcp_instance),
+        patch.object(server, "_register_health_endpoint"),
+        patch.object(server, "create_event_store", return_value=None),
+        patch.object(server, "_build_starlette_middleware", return_value=[]),
+    ):
+        yield mcp_instance
+
+
+def test_run_server_defaults_stateless_http_to_true_when_unset() -> None:
+    """run_server() must fall back to MCP_STATELESS_HTTP's True default when the
+    operator's Flask config has no override.
+
+    This pins the production wiring added to fix mid-workflow disconnects: if
+    the ``flask_app.config.get("MCP_STATELESS_HTTP", MCP_STATELESS_HTTP)`` call
+    in ``run_server()`` were reverted to a hardcoded ``True``, or the default
+    were flipped, this test would still pass -- so it's the ``is True`` on the
+    *resolved* value, not just the module constant, that catches a broken
+    resolution.
+    """
+    from superset.mcp_service.server import run_server
+
+    port = 59901
+    os.environ.pop(f"FASTMCP_RUNNING_{port}", None)
+    try:
+        with _run_server_dependencies(flask_config={}) as mcp_instance:
+            run_server(host="127.0.0.1", port=port)
+
+        mcp_instance.run.assert_called_once()
+        assert mcp_instance.run.call_args.kwargs["stateless_http"] is True
+    finally:
+        os.environ.pop(f"FASTMCP_RUNNING_{port}", None)
+
+
+def test_run_server_respects_mcp_stateless_http_false_override() -> None:
+    """An operator's MCP_STATELESS_HTTP=False (the value deployments actually run,
+    per the docstring in mcp_config.py) must reach ``mcp_instance.run()`` rather
+    than the module's True default."""
+    from superset.mcp_service.server import run_server
+
+    port = 59902
+    os.environ.pop(f"FASTMCP_RUNNING_{port}", None)
+    try:
+        with _run_server_dependencies(
+            flask_config={"MCP_STATELESS_HTTP": False}
+        ) as mcp_instance:
+            run_server(host="127.0.0.1", port=port)
+
+        mcp_instance.run.assert_called_once()
+        assert mcp_instance.run.call_args.kwargs["stateless_http"] is False
+    finally:
+        os.environ.pop(f"FASTMCP_RUNNING_{port}", None)