GradioUI + Smolagents + MCP "Event loop is closed"

I have an existing MCP Server that runs well on the command line and is instantiated like this

if __name__ == "__main__":

    # Log server startup
    logger.info("Starting MCP Server...")

    # Initialize and run the server
    mcp.run(transport='stdio')

    # This line won't be reached during normal operation
    logger.info("Server stopped")

However, when I want to use GradioUI with that MCP Server

# Define server parameters for MCP tool
server_parameters = StdioServerParameters(
    command="uv",
    args=["run", "./finMCP_server.py"],
    env={**os.environ},
)

And then launch the GradioUI like this

# Gradio UI via smolagent
GradioUI(agent).launch()

The code execution fails due to: RuntimeError: Event loop is closed

I have been searching for a while now to no conclusion.
I know that this is a sync/async problem.
Where can I tell Gradio to wait until the MCP server has responded?

1 Like

There are Gradio developers on Discord, so it might be quicker to ask them there…

https://stackoverflow.com/questions/45600579/asyncio-event-loop-is-closed-when-getting-loop