Repository: gradio-app/gradio
File: gradio/blocks.py (Python)

Code before:
                    server_port=node_port,
                    node_path=self.node_path,
                )
            )
        else:
            self.node_server_name = self.node_port = self.node_process = None
        
        self.i18n_instance = i18n

        if app_kwargs is None:
            app_kwargs = {}

        mcp_subpath = API_PREFIX + "/mcp"
        if mcp_server is None:
            mcp_server = os.environ.get("GRADIO_MCP_SERVER", "False").lower() == "true"
        if mcp_server:
            try:
                import gradio.mcp
            except ImportError as e:
                raise ImportError(
                    "In order to use `mcp_server=True`, you must install gradio with the `mcp` extra. Please install it with `pip install gradio[mcp]`"
                ) from e
            try:
                self.mcp_server_obj = gradio.mcp.GradioMCPServer(self, self.root_path)
                self.mcp_server = True
                user_lifespan = None
                if "lifespan" in app_kwargs:
                    user_lifespan = app_kwargs["lifespan"]

                @contextlib.asynccontextmanager
                async def _lifespan(app: App):
                    async with contextlib.AsyncExitStack() as stack:
                        if self.mcp_server_obj:
                            await stack.enter_async_context(
                                self.mcp_server_obj.lifespan(app)
                            )
                        if user_lifespan is not None:
                            await stack.enter_async_context(user_lifespan(app))
                        yield

                app_kwargs["lifespan"] = _lifespan
            except Exception as e:
                self.mcp_server = False
                if not quiet:
                    print(f"Error launching MCP server: {e}")

        self.server_app = self.app = App.create_app(
            self,
            auth_dependency=auth_dependency,
            app_kwargs=app_kwargs,
            strict_cors=strict_cors,

Review comment (refactor):
The StreamableHTTPSessionManager's run method must be called when the app [starts](https://github.com/modelcontextprotocol/python-sdk/blob/05b7156ea8a34d8476a7cfbef5f754e22ab6c697/examples/servers/simple-streamablehttp/mcp_simple_streamablehttp/server.py#L149)

FastAPI will not run the lifespan of mounted subapplications so we need to pass the mcp lifespan all the way back up to `App.create_app`. 

This refactor switches the order of creating the app and the mcp server so that the mcp server and lifespan are defined before the gradio server.

There is also some logic here to ensure the mcp server lifespan works well with a user defined lifespan.

Code after:
                    server_port=node_port,
                    node_path=self.node_path,
                )
            )
        else:
            self.node_server_name = self.node_port = self.node_process = None

        self.i18n_instance = i18n

        if app_kwargs is None:
            app_kwargs = {}

        mcp_subpath = API_PREFIX + "/mcp"
        if mcp_server is None:
            mcp_server = os.environ.get("GRADIO_MCP_SERVER", "False").lower() == "true"
        if mcp_server:
            try:
                import gradio.mcp
            except ImportError as e:
                raise ImportError(
                    "In order to use `mcp_server=True`, you must install gradio with the `mcp` extra. Please install it with `pip install gradio[mcp]`"
                ) from e
            try:
                self.mcp_server_obj = gradio.mcp.GradioMCPServer(self, self.root_path)
                self.mcp_server = True
                user_lifespan = None
                if "lifespan" in app_kwargs:
                    user_lifespan = app_kwargs["lifespan"]

                @contextlib.asynccontextmanager
                async def _lifespan(app: App):
                    async with contextlib.AsyncExitStack() as stack:
                        if self.mcp_server_obj:
                            await stack.enter_async_context(
                                self.mcp_server_obj.lifespan(app)
                            )
                        if user_lifespan is not None:
                            await stack.enter_async_context(user_lifespan(app))
                        yield

                app_kwargs["lifespan"] = _lifespan
            except Exception as e:
                self.mcp_server = False
                if not quiet:
                    print(f"Error launching MCP server: {e}")

        self.server_app = self.app = App.create_app(
            self,
            auth_dependency=auth_dependency,
            app_kwargs=app_kwargs,
            strict_cors=strict_cors,