Skip to content

Auto-detect local Chrome CDP on port 9222 before falling to Playwright Chromium #6780

Description

@iRonin

Problem

When a user has Chrome running with --remote-debugging-port=9222, the agent's browser_navigate tool still falls through to launching a separate headless Chromium instance via Playwright (--session mode). This wastes time, resources, and ignores the user's live browser — which may have logged-in sessions, specific tabs open, etc.

The /browser connect command already knows how to detect and connect to Chrome on port 9222 (cli.py lines 5203-5213), but this logic lives exclusively in the CLI and is not available to the agent's browser tool.

Solution

Add _auto_detect_local_chrome() to tools/browser_tool.py that:

  1. Probes localhost:9222 for a listening socket (1s timeout)
  2. If open, fetches /json/version and resolves the WebSocket debugger URL
  3. Returns the ws://... URL, or empty string if Chrome isn't running

Wire this into _get_session_info() as a step between 'no cloud provider configured' and 'launch headless Chromium via Playwright'.

Priority chain

  1. BROWSER_CDP_URL env var (explicit, e.g. /browser connect)
  2. config.yamlbrowser.cdp_url (persistent fallback)
  3. Auto-detect Chrome on port 9222 (NEW)
  4. Cloud browser provider (Browserbase, Browser-Use)
  5. Local headless Chromium launch (--session)

Testing

  • Manual: python3 -c 'from tools.browser_tool import _auto_detect_local_chrome; print(_auto_detect_local_chrome())' returns ws://localhost:9222/devtools/browser/... when Chrome is running
  • Unit tests: mock socket probe, test resolution path
  • Integration: browser_navigate connects to live Chrome without needing /browser connect

Files changed

  • tools/browser_tool.py: new _auto_detect_local_chrome(), updated _get_session_info()
  • Also synced _get_cdp_override() config.yaml fallback (was in installed version, missing from upstream)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low — cosmetic, nice to havetool/browserBrowser automation (CDP, Playwright)type/featureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions