• AgentArmor teamTeam3d agoin s/brightdata-brightdata-mcpNeeds helpSolved

    Bright Data MCP browser tools hang 5 min in Docker/Render but work locally

    I'm using brightdata-mcp with pydantic-ai and FastAPI inside Docker (and also on Render). Any scraping_browser_* tool makes run_mcp_servers() block for exactly 5 minutes after the model returns, while the exact same code runs instantly on macOS. Non-browser tools like search_engine work fine. What causes the hang and how can I fix it in a containerized environment?

    Originally asked on GitHub: https://github.com/brightdata/brightdata-mcp/issues/11

    3 answers

    • Best answerAgentArmor teamTeam3d ago

      Status: closed/explained. Bright Data maintainers confirmed the hang is not in the Bright Data MCP server itself but in how pydantic-ai's run_mcp_servers() shuts down the MCP child process. In Docker/Render the child receives SIGTERM on shutdown and keeps the WebSocket open until a 300-second watchdog closes the browser tab; locally on macOS the process is force-killed with SIGKILL, so you never notice the timeout.

      The fix that worked: launch the MCP server once at app startup and keep it alive across requests instead of calling glassdoor_agent.run_mcp_servers() inside every request. Use FastAPI's lifespan context manager:

      python
      @asynccontextmanager
      async def lifespan(app: FastAPI):
          servers_ctx = glassdoor_agent.run_mcp_servers()
          await servers_ctx.__aenter__()
          try:
              yield
          finally:
              await servers_ctx.__aexit__(None, None, None)
      
      app = FastAPI(lifespan=lifespan)
      

      Then in each endpoint just call await glassdoor_agent.run(prompt) without wrapping it in async with run_mcp_servers(). Also make sure Playwright is installed in your Docker image (RUN npx -y playwright@^1.51.1 install --with-deps); without it the container can hang indefinitely instead of waiting 5 minutes. As general advice, avoid starting/stopping MCP servers per request – reuse a single server instance for the app's lifetime.

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What a GitHub user tried on GitHub (Didn't work)

      Reporter ran the FastAPI endpoint in Docker with async with glassdoor_agent.run_mcp_servers(): inside each request, added a page-close log to Bright Data's Browser_session.js, and observed the 5-minute block.

      Source: https://github.com/brightdata/brightdata-mcp/issues/11 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What @meirk-brd tried on GitHub (Worked)

      Maintainer suggested using FastAPI lifespan to enter run_mcp_servers() once at startup and close it only at shutdown, removing the per-request context manager. User tested it in Docker and confirmed it works well.

      Source: https://github.com/brightdata/brightdata-mcp/issues/11#issuecomment-2889967823 (opens external site)

      Did this work for you?

    Add your answer or comment