tools.headless_browser module

Headless Firefox browser automation via Playwright.

Provides navigation, interaction, content extraction, waiting, screenshots, JavaScript execution, cookie management, network interception, session persistence, download handling, and console/dialog tools – all exposed through the v3 multi-tool format.

class tools.headless_browser.HeadlessBrowserManager[source]

Bases: object

Process-wide owner of the shared headless Firefox instance.

Lazily launches a single Playwright Firefox browser and multiplexes it across named contexts (each with its own page, console-log buffer, response-pattern list, intercepted responses, downloads, and dialog handler), so every browser tool and other consumers share one warm browser rather than spawning their own. It tracks usage/error counts and self-heals: too many errors or a disconnected browser trigger a restart. A module-level singleton _bm is the instance everything uses, and it is imported directly by tools/web_scraper.py and bot/voice/puter.py to reuse the same browser.

default_context_id

The default context id.

is_initialized

The is initialized.

last_used

The last used.

usage_count

The usage count.

error_count

The error count.

max_errors_before_restart

The max errors before restart.

idle_timeout

The idle timeout.

_lock

The lock.

default_viewport

The default viewport.

default_user_agent

The default user agent.

__init__()[source]

Set up manager bookkeeping without launching a browser.

Initializes all per-instance state (context/page maps, console-log, interception, download, dialog, and persistent-session registries, the async lock, default viewport/user-agent, and error/usage counters) to empty defaults. The actual Playwright/Firefox launch is deferred to initialize so the module can be imported cheaply.

Called once at import time to build the module-level _bm singleton.

async initialize()[source]

Launch the Firefox browser and create the default context.

Starts Playwright and launches a headless Firefox instance (with autoplay and desktop-notification preferences disabled), then creates the default context so a page is ready to use. Guarded by the async lock and idempotent: if an initialized, live browser already exists it returns immediately. On failure it cleans up and reports False.

Called by ensure_ready and restart in this module, and by browser_create_persistent_context when no Playwright instance exists yet.

Returns:

True if the browser is ready, False if Playwright is missing or the launch failed.

Return type:

bool

async ensure_ready()[source]

Guarantee a live browser, restarting or initializing as needed.

The health gate every page/context accessor calls first. It restarts the browser if the error count has crossed max_errors_before_restart, initializes it if not yet started, and restarts it if the existing browser has lost its connection; on success it refreshes last_used.

Called by ensure_proxied_context, get_page, get_context, create_new_context, browser_emulate_device, and browser_set_geolocation in this module.

Returns:

True when a connected browser is available, False otherwise.

Return type:

bool

async restart()[source]

Tear down and relaunch the browser from scratch.

Recovery path used after repeated errors or a lost connection: it runs cleanup to release all pages/contexts/browser/Playwright resources, then initialize to launch a fresh browser and default context.

Called by ensure_ready in this module and exposed through the browser_restart tool handler.

Returns:

True if the relaunch succeeded, False otherwise.

Return type:

bool

async cleanup()[source]

Close all pages, contexts, the browser, and Playwright.

Best-effort teardown that swallows per-resource errors while closing every open page and context, the browser, and the Playwright driver, clearing the page/context maps and marking the manager uninitialized. Holds the async lock so it cannot race a concurrent initialize.

Called by restart in this module; safe to invoke during shutdown.

async resolve_context_id(context_id, proxy)[source]

Choose the context to use, preferring a proxy-derived one.

Resolves the effective browser context for a request: when proxy is set it routes to the stable SOCKS-backed context from ensure_proxied_context (the proxy deliberately overrides any explicit context_id); otherwise it falls back to context_id or the default context. This is why setting a proxy is enough to isolate a session.

Called by _ctx_or_err in this module (which all proxy-aware browser tools route through).

Parameters:
  • context_id (Optional[str]) – Explicit context id, used only when no proxy is supplied.

  • proxy (Optional[str]) – Optional SOCKS proxy URL; when present, wins.

Returns:

The resolved context id to operate on.

Return type:

str

async ensure_proxied_context(proxy_url)[source]

Return (creating if needed) the context bound to a SOCKS proxy.

Validates the proxy with assert_safe_socks_proxy_url, derives a stable context id via _proxy_derived_context_id, ensures the browser is ready, and lazily creates a proxied context the first time that proxy is seen so subsequent requests through the same proxy reuse one context.

Called by resolve_context_id in this module.

Parameters:

proxy_url (str) – The SOCKS proxy URL to route the context through.

Returns:

The context id bound to this proxy.

Return type:

str

Raises:
async get_page(context_id=None)[source]

Return the active page for a context, creating it if absent.

The workhorse accessor nearly every browser handler calls to obtain a page to drive. It ensures the browser is ready, creates the context (or just a new page within an existing context) when one does not yet exist, and bumps usage_count/last_used.

Called by most browser_* handlers in this module (navigation, interaction, content, waiting, screenshots, JS, etc.).

Parameters:

context_id (str) – Context to fetch the page from; defaults to the default context.

Returns:

The Playwright page for the context.

Raises:

RuntimeError – If the browser is not available.

async get_context(context_id=None)[source]

Return the browser context for an id, creating it if absent.

Ensures the browser is ready and lazily creates the named context when it does not already exist, then returns it. Used by the cookie tools, which operate at the context level rather than on a page.

Called by browser_get_cookies, browser_set_cookies, and browser_clear_cookies in this module.

Parameters:

context_id (str) – Context to fetch; defaults to the default context.

Returns:

The Playwright browser context.

Raises:

RuntimeError – If the browser is not available.

async create_new_context(context_id=None, **options)[source]

Create a brand-new isolated context and return its id.

Ensures the browser is ready, generates a random context_<hex> id when none is given, and delegates to _create_context with the supplied options (device, geolocation, proxy, etc.). Used to spin up incognito-like or specially-configured contexts.

Called by browser_emulate_device, browser_set_geolocation, and browser_new_context in this module.

Parameters:
  • context_id (str) – Desired context id; auto-generated when omitted.

  • **options – Context options forwarded to _create_context.

Returns:

The id of the newly created context.

Return type:

str

Raises:

RuntimeError – If the browser is not available.

async close_context(context_id)[source]

Close a context’s page and context, ignoring errors.

Best-effort teardown of a single context: closes its page and the context itself (swallowing close errors) and removes both from the manager’s maps, freeing the resources held by that browsing session.

Called by browser_close_context in this module.

Parameters:

context_id (str) – The context to close.

record_error()[source]

Increment the failure counter that drives auto-restart.

Bumps error_count; once it reaches max_errors_before_restart, the next ensure_ready call recycles the browser. Handlers call this on unexpected Playwright failures so a flaky browser self-heals.

Called from the exception paths of most browser_* handlers in this module.

async tools.headless_browser.browser_navigate(url, wait_until='load', timeout=30000, context_id=None, proxy=None, ctx=None)[source]

Navigate the headless browser to a URL.

Backs the browser_navigate tool. After requiring UNSANDBOXED_EXEC (_check_unsandboxed_exec) and validating the URL and wait state (_validate_url), it resolves the context (_ctx_or_err), grabs the page (_bm.get_page), and drives page.goto, returning the final URL, title, and HTTP status. Playwright errors are recorded via _bm.record_error so a flaky browser self-heals.

Dispatched by the tool runner in tools/__init__.py for the registered browser_navigate tool (tool_def.handler(**arguments, ctx=ctx)); also invoked directly in tests/test_unsandboxed_exec_gating.py.

Parameters:
  • url (str) – URL to navigate to.

  • wait_until (str) – When navigation is considered complete (load, domcontentloaded, networkidle, or commit).

  • timeout (int) – Navigation timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL; routes through a proxied context.

  • ctx (ToolContext) – Tool execution context; required for the privilege check.

Returns:

JSON envelope; on success carries the resulting url, title, status, ok flag, and context id; on failure an error message.

Return type:

str

async tools.headless_browser.browser_go_back(timeout=30000, context_id=None, proxy=None)[source]

Navigate back in the browser history.

Backs the browser_go_back tool. Resolves the context (_ctx_or_err), gets the page (_bm.get_page), and calls page.go_back, returning the resulting url, title, and status. Requires no extra privilege.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_go_back tool; there are no direct internal callers.

Parameters:
  • timeout (int) – Navigation timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the resulting url, title, and status, or an error.

Return type:

str

async tools.headless_browser.browser_go_forward(timeout=30000, context_id=None, proxy=None)[source]

Navigate forward in the browser history.

Backs the browser_go_forward tool. Resolves the context (_ctx_or_err), gets the page (_bm.get_page), and calls page.go_forward, returning the resulting url, title, and status. Requires no extra privilege.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_go_forward tool; there are no direct internal callers.

Parameters:
  • timeout (int) – Navigation timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the resulting url, title, and status, or an error.

Return type:

str

async tools.headless_browser.browser_reload(wait_until='load', timeout=30000, context_id=None, proxy=None, ctx=None)[source]

Reload the current page.

Backs the browser_reload tool. After requiring UNSANDBOXED_EXEC (_check_unsandboxed_exec), it resolves the context (_ctx_or_err), gets the page (_bm.get_page), and calls page.reload, returning the resulting url, title, and status.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments, ctx=ctx) for the registered browser_reload tool; there are no direct internal callers.

Parameters:
  • wait_until (str) – When the reload is considered complete.

  • timeout (int) – Timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

  • ctx (ToolContext) – Tool execution context; required for the privilege check.

Returns:

JSON envelope with the resulting url, title, and status, or an error.

Return type:

str

async tools.headless_browser.browser_close_context(context_id=None, proxy=None, ctx=None)[source]

Close a browser context (window), except the default one.

Backs the browser_close_context tool. After requiring UNSANDBOXED_EXEC (_check_unsandboxed_exec) and resolving the context (_ctx_or_err), it refuses to close the default context and otherwise delegates to _bm.close_context to free that browsing session’s page and context.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments, ctx=ctx) for the registered browser_close_context tool; there are no direct internal callers.

Parameters:
  • context_id (str) – Context to close (optional if proxy is set).

  • proxy (str) – Optional SOCKS proxy URL (same one used to create it).

  • ctx (ToolContext) – Tool execution context; required for the privilege check.

Returns:

JSON envelope confirming closure, or an error (including refusal to close the default context).

Return type:

str

async tools.headless_browser.browser_click(selector, button='left', click_count=1, timeout=30000, context_id=None, proxy=None, ctx=None)[source]

Click an element on the page.

Backs the browser_click tool. After requiring UNSANDBOXED_EXEC (_check_unsandboxed_exec) and validating the mouse button, it resolves the context (_ctx_or_err), gets the page (_bm.get_page), and calls page.click with the requested button and click count.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments, ctx=ctx) for the registered browser_click tool; there are no direct internal callers.

Parameters:
  • selector (str) – Element selector (CSS, XPath, text=...).

  • button (str) – Mouse button: left, right, or middle.

  • click_count (int) – Number of clicks (e.g. 2 for double-click).

  • timeout (int) – Element wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

  • ctx (ToolContext) – Tool execution context; required for the privilege check.

Returns:

JSON envelope confirming the click, or an error (e.g. element not found within the timeout).

Return type:

str

async tools.headless_browser.browser_type(selector, text, delay=0, clear_first=False, timeout=30000, context_id=None, proxy=None, ctx=None)[source]

Type text into an input element, simulating keystrokes.

Backs the browser_type tool. After requiring UNSANDBOXED_EXEC (_check_unsandboxed_exec), it resolves the context (_ctx_or_err), gets the page (_bm.get_page), optionally clears the field first, then drives page.type with the requested per-key delay so the input looks human-typed (useful for fields that react to keystrokes).

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments, ctx=ctx) for the registered browser_type tool; there are no direct internal callers.

Parameters:
  • selector (str) – Target input element selector.

  • text (str) – Text to type.

  • delay (int) – Delay between keystrokes in milliseconds.

  • clear_first (bool) – Whether to clear the field before typing.

  • timeout (int) – Element wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

  • ctx (ToolContext) – Tool execution context; required for the privilege check.

Returns:

JSON envelope confirming the typed text length, or an error.

Return type:

str

async tools.headless_browser.browser_fill(selector, value, timeout=30000, context_id=None, proxy=None, ctx=None)[source]

Fill an input field instantly (no per-key typing).

Backs the browser_fill tool. After requiring UNSANDBOXED_EXEC (_check_unsandboxed_exec), it resolves the context (_ctx_or_err), gets the page (_bm.get_page), and calls page.fill, which sets the value in one shot. Faster than browser_type when keystroke simulation is unnecessary.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments, ctx=ctx) for the registered browser_fill tool; there are no direct internal callers.

Parameters:
  • selector (str) – Target input element selector.

  • value (str) – Value to set.

  • timeout (int) – Element wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

  • ctx (ToolContext) – Tool execution context; required for the privilege check.

Returns:

JSON envelope confirming the filled value length, or an error.

Return type:

str

async tools.headless_browser.browser_fill_form(fields, timeout=30000, context_id=None, proxy=None)[source]

Fill several form fields in one call.

Backs the browser_fill_form tool. Parses fields (a JSON object mapping selectors to values), resolves the context (_ctx_or_err), gets the page (_bm.get_page), and calls page.fill for each entry, accumulating per-field successes and errors so a single bad selector does not abort the rest. Note this handler is not privilege-gated.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_fill_form tool; there are no direct internal callers.

Parameters:
  • fields (str) – JSON object mapping element selectors to values.

  • timeout (int) – Per-field wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope; success when all fields filled, partial with a filled/errors breakdown when some failed, or an error.

Return type:

str

async tools.headless_browser.browser_select_option(selector, value=None, label=None, index=None, timeout=30000, context_id=None, proxy=None)[source]

Select an option in a dropdown/<select> element.

Backs the browser_select_option tool. Requires at least one of value/label/index, resolves the context (_ctx_or_err), gets the page (_bm.get_page), and calls page.select_option with whichever matchers were supplied, returning the selected values.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_select_option tool; there are no direct internal callers.

Parameters:
  • selector (str) – The <select> element selector.

  • value (str) – Option value to select.

  • label (str) – Option visible label to select.

  • index (int) – Option index to select.

  • timeout (int) – Element wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the selected values, or an error.

Return type:

str

async tools.headless_browser.browser_press_key(key, selector=None, timeout=30000, context_id=None, proxy=None)[source]

Press a keyboard key, optionally focused on an element.

Backs the browser_press_key tool. Resolves the context (_ctx_or_err), gets the page (_bm.get_page), and either presses the key on a specific element (page.press) when a selector is given or on the page keyboard (page.keyboard.press) otherwise. Supports chords like Control+a.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_press_key tool; there are no direct internal callers.

Parameters:
  • key (str) – Key (or chord) to press, e.g. Enter, Tab, Control+a.

  • selector (str) – Optional element to focus before pressing.

  • timeout (int) – Element wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope confirming the key press, or an error.

Return type:

str

async tools.headless_browser.browser_hover(selector, timeout=30000, context_id=None, proxy=None)[source]

Hover the mouse over an element.

Backs the browser_hover tool. Resolves the context (_ctx_or_err), gets the page (_bm.get_page), and calls page.hover to move the pointer over the element, useful for triggering hover menus or tooltips.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_hover tool; there are no direct internal callers.

Parameters:
  • selector (str) – Element to hover over.

  • timeout (int) – Element wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope confirming the hover, or an error.

Return type:

str

async tools.headless_browser.browser_scroll(direction='down', amount=500, selector=None, context_id=None, proxy=None)[source]

Scroll the page or a specific element by a pixel amount.

Backs the browser_scroll tool. Validates the direction, resolves the context (_ctx_or_err), gets the page (_bm.get_page), translates the direction/amount into dx/dy, and runs a page.evaluate that scrolls either the matched element or the window.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_scroll tool; there are no direct internal callers.

Parameters:
  • direction (str) – One of up, down, left, right.

  • amount (int) – Pixels to scroll.

  • selector (str) – Optional element to scroll instead of the window.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope confirming the scroll, or an error.

Return type:

str

async tools.headless_browser.browser_get_content(format='text', selector=None, context_id=None, proxy=None)[source]

Extract page content as HTML, plain text, or markdown.

Backs the browser_get_content tool. Resolves the context (_ctx_or_err) and page (_bm.get_page), then reads either the whole page or a selected element. For markdown it converts the HTML through html2text (when available) and collapses blank runs; all output is bounded by _truncate_content so a huge page cannot overflow the tool result.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_content tool; there are no direct internal callers.

Parameters:
  • format (str) – Output format: html, text, or markdown.

  • selector (str) – Optional element to extract instead of the whole page.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the (possibly truncated) content, its length, the page url, and title, or an error.

Return type:

str

async tools.headless_browser.browser_get_text(selector, timeout=30000, context_id=None, proxy=None)[source]

Get the inner text of a single element.

Backs the browser_get_text tool. Resolves the context (_ctx_or_err) and page (_bm.get_page), then returns page.inner_text for the matched selector, waiting up to the timeout for it to appear.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_text tool; there are no direct internal callers.

Parameters:
  • selector (str) – Element whose text to read.

  • timeout (int) – Element wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the element text, or an error (e.g. not found).

Return type:

str

async tools.headless_browser.browser_get_attribute(selector, attribute, timeout=30000, context_id=None, proxy=None)[source]

Read a named attribute from an element.

Backs the browser_get_attribute tool. Resolves the context (_ctx_or_err) and page (_bm.get_page), then returns page.get_attribute for the selector/attribute pair (e.g. href, src, class).

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_attribute tool; there are no direct internal callers.

Parameters:
  • selector (str) – Element to inspect.

  • attribute (str) – Attribute name to read.

  • timeout (int) – Element wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the attribute value (None if absent), or an error.

Return type:

str

async tools.headless_browser.browser_get_page_info(context_id=None, proxy=None)[source]

Report the current page’s URL, title, and viewport.

Backs the browser_get_page_info tool. Resolves the context (_ctx_or_err) and page (_bm.get_page) and returns lightweight page metadata without extracting content.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_page_info tool; there are no direct internal callers.

Parameters:
  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the url, title, and viewport size, or an error.

Return type:

str

async tools.headless_browser.browser_query_selector_all(selector, attributes=None, timeout=30000, context_id=None, proxy=None)[source]

Find all elements matching a selector and return their data.

Backs the browser_query_selector_all tool. Optionally parses attributes (a JSON array of attribute names), resolves the context (_ctx_or_err) and page (_bm.get_page), runs page.query_selector_all, and for each match collects its inner text plus any requested attributes. Results are capped at 100 elements.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_query_selector_all tool; there are no direct internal callers.

Parameters:
  • selector (str) – Selector to match.

  • attributes (str) – Optional JSON array of attribute names to also collect.

  • timeout (int) – Reserved wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the match count and the per-element data (first 100), or an error.

Return type:

str

async tools.headless_browser.browser_wait_for_selector(selector, state='visible', timeout=30000, context_id=None, proxy=None)[source]

Wait for an element to reach a given state.

Backs the browser_wait_for_selector tool. Validates the requested state, resolves the context (_ctx_or_err) and page (_bm.get_page), and calls page.wait_for_selector to block until the element is visible, hidden, attached, or detached (or the timeout elapses).

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_wait_for_selector tool; there are no direct internal callers.

Parameters:
  • selector (str) – Element to wait on.

  • state (str) – Target state: visible, hidden, attached, or detached.

  • timeout (int) – Wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope confirming the state was reached, or a timeout/error.

Return type:

str

async tools.headless_browser.browser_wait_for_load_state(state='load', timeout=30000, context_id=None, proxy=None)[source]

Wait for the page to reach a load lifecycle state.

Backs the browser_wait_for_load_state tool. Validates the state, resolves the context (_ctx_or_err) and page (_bm.get_page), and calls page.wait_for_load_state to block until the page is loaded, DOM-content loaded, or network-idle.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_wait_for_load_state tool; there are no direct internal callers.

Parameters:
  • state (str) – load, domcontentloaded, or networkidle.

  • timeout (int) – Wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope confirming the load state and url, or a timeout/error.

Return type:

str

async tools.headless_browser.browser_wait_for_url(url_pattern, timeout=30000, context_id=None, proxy=None)[source]

Wait until the page URL matches a pattern.

Backs the browser_wait_for_url tool. Resolves the context (_ctx_or_err) and page (_bm.get_page) and calls page.wait_for_url; a pattern anchored with ^/$ is compiled as a regex, otherwise it is treated as a glob substring. Useful for awaiting redirects after a click or login.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_wait_for_url tool; there are no direct internal callers.

Parameters:
  • url_pattern (str) – Regex (when anchored) or glob substring to match.

  • timeout (int) – Wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the matched url, or a timeout/error.

Return type:

str

async tools.headless_browser.browser_wait(milliseconds, context_id=None, proxy=None)[source]

Sleep for a fixed number of milliseconds.

Backs the browser_wait tool. A simple asyncio.sleep (bounded to 0-60000 ms) for pacing automation between steps; it does not touch a page or context.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_wait tool; there are no direct internal callers.

Parameters:
  • milliseconds (int) – Time to wait, clamped to the 0-60000 range.

  • context_id (str) – Accepted for interface uniformity; unused.

  • proxy (str) – Accepted for interface uniformity; unused.

Returns:

JSON envelope confirming the wait, or an error if out of range.

Return type:

str

async tools.headless_browser.browser_screenshot(full_page=True, selector=None, filename=None, context_id=None, proxy=None)[source]

Capture a screenshot to a file under the output directory.

Backs the registered browser_screenshot tool (this definition shadows the earlier base64 variant). Resolves the context (_ctx_or_err) and page (_bm.get_page), then writes a PNG to OUTPUT_DIR (auto-named with a timestamp when no filename is given) for either a selected element or the full/visible page, returning the saved path.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_screenshot tool; there are no direct internal callers.

Parameters:
  • full_page (bool) – Whether to capture the full scrollable page.

  • selector (str) – Optional element to screenshot instead of the page.

  • filename (str) – Optional base filename (.png is appended).

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the saved file path and metadata, or an error.

Return type:

str

async tools.headless_browser.browser_pdf(filename=None, format='A4', landscape=False, print_background=True, context_id=None, proxy=None)[source]

Render the current page to a PDF file.

Backs the browser_pdf tool. Validates the paper format, resolves the context (_ctx_or_err) and page (_bm.get_page), then writes a PDF to OUTPUT_DIR (auto-named with a timestamp when no filename is given) via page.pdf, honoring orientation and background-printing options.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_pdf tool; there are no direct internal callers.

Parameters:
  • filename (str) – Optional base filename (.pdf is appended).

  • format (str) – Paper size: A4, Letter, Legal, Tabloid, A3, or A5.

  • landscape (bool) – Whether to use landscape orientation.

  • print_background (bool) – Whether to print background graphics.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the saved PDF path and metadata, or an error.

Return type:

str

async tools.headless_browser.browser_evaluate(expression, context_id=None, proxy=None)[source]

Evaluate a JavaScript expression and return its result.

Backs the browser_evaluate tool. Resolves the context (_ctx_or_err) and page (_bm.get_page), runs page.evaluate(expression), and serializes the return value (coercing non-JSON types via default=str), truncating the payload past MAX_CONTENT_LENGTH.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_evaluate tool; there are no direct internal callers.

Parameters:
  • expression (str) – JavaScript expression to evaluate in the page.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the evaluated result (and its type), truncated when too large, or an error.

Return type:

str

async tools.headless_browser.browser_execute(script, context_id=None, proxy=None)[source]

Execute a JavaScript snippet for its side effects.

Backs the browser_execute tool. Resolves the context (_ctx_or_err) and page (_bm.get_page) and runs page.evaluate(script) without returning the result, for scripts run purely for their effect on the page.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_execute tool; there are no direct internal callers.

Parameters:
  • script (str) – JavaScript code to run in the page.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope confirming execution (script length), or an error.

Return type:

str

async tools.headless_browser.browser_get_cookies(urls=None, context_id=None, proxy=None)[source]

List the context’s cookies with values redacted.

Backs the browser_get_cookies tool. Optionally parses urls (a JSON array to filter by), fetches cookies from the context (_bm.get_context, then context.cookies), and returns sanitized metadata (name, domain, path, flags, and value length) without exposing the secret cookie values.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_cookies tool; there are no direct internal callers.

Parameters:
  • urls (str) – Optional JSON array of URLs to filter cookies by.

  • context_id (str) – Browser context whose cookies to read.

  • proxy (str) – Accepted for interface uniformity; this handler reads the context directly.

Returns:

JSON envelope with the sanitized cookie list and count, or an error.

Return type:

str

async tools.headless_browser.browser_set_cookies(cookies, context_id=None, proxy=None)[source]

Add cookies to the browser context.

Backs the browser_set_cookies tool. Parses cookies (a JSON array of cookie objects), validates that each has a name/value and a url or domain, then adds them via _bm.get_context plus context.add_cookies. Lets automation pre-seed an authenticated session.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_set_cookies tool; there are no direct internal callers.

Parameters:
  • cookies (str) – JSON array of cookie objects to add.

  • context_id (str) – Browser context to add the cookies to.

  • proxy (str) – Accepted for interface uniformity; this handler uses the context directly.

Returns:

JSON envelope with the count and names set, or a validation error.

Return type:

str

async tools.headless_browser.browser_clear_cookies(context_id=None, proxy=None)[source]

Clear all cookies in the browser context.

Backs the browser_clear_cookies tool. Fetches the context (_bm.get_context) and calls context.clear_cookies to drop every cookie, effectively logging the session out.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_clear_cookies tool; there are no direct internal callers.

Parameters:
  • context_id (str) – Browser context whose cookies to clear.

  • proxy (str) – Accepted for interface uniformity; this handler uses the context directly.

Returns:

JSON envelope confirming cookies were cleared, or an error.

Return type:

str

async tools.headless_browser.browser_emulate_device(device_name, context_id=None, proxy=None)[source]

Create a context emulating a named mobile device or tablet.

Backs the browser_emulate_device tool. Ensures the browser is ready (_bm.ensure_ready), looks the device up in Playwright’s device registry, and spins up a new context configured for it via _bm.create_new_context (passing device), returning the new context id and the device viewport.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_emulate_device tool; there are no direct internal callers.

Parameters:
  • device_name (str) – Playwright device name (e.g. iPhone 12, Pixel 5).

  • context_id (str) – Optional id for the new context; auto-named otherwise.

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the new context id and device info, or an error (e.g. unknown device, with a sample of valid names).

Return type:

str

async tools.headless_browser.browser_set_geolocation(latitude, longitude, accuracy=100, context_id=None, proxy=None)[source]

Create a context with a spoofed geolocation.

Backs the browser_set_geolocation tool. Validates the latitude/longitude ranges, ensures the browser is ready (_bm.ensure_ready), and creates a new context with the geolocation (and the geolocation permission granted) via _bm.create_new_context, so pages that read navigator.geolocation see the supplied coordinates.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_set_geolocation tool; there are no direct internal callers.

Parameters:
  • latitude (float) – Latitude in -90..90.

  • longitude (float) – Longitude in -180..180.

  • accuracy (float) – Reported accuracy in meters.

  • context_id (str) – Optional id for the new context; auto-named otherwise.

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the new context id and coordinates, or an error.

Return type:

str

async tools.headless_browser.browser_set_viewport(width, height, context_id=None, proxy=None)[source]

Resize the page viewport.

Backs the browser_set_viewport tool. Validates the dimensions (100-4000 px each), resolves the context (_ctx_or_err) and page (_bm.get_page), and calls page.set_viewport_size to change the rendered window size.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_set_viewport tool; there are no direct internal callers.

Parameters:
  • width (int) – Viewport width in pixels (100-4000).

  • height (int) – Viewport height in pixels (100-4000).

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope confirming the new size, or an error.

Return type:

str

async tools.headless_browser.browser_block_resources(resource_types, context_id=None, proxy=None)[source]

Block selected resource types from loading.

Backs the browser_block_resources tool. Parses and validates resource_types (a JSON array), resolves the context (_ctx_or_err) and page (_bm.get_page), and installs a page.route handler that aborts requests of the blocked types and continues the rest, speeding up loads and saving bandwidth (e.g. blocking images and fonts).

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_block_resources tool; there are no direct internal callers.

Parameters:
  • resource_types (str) – JSON array of types to block, drawn from image, stylesheet, font, script, media, websocket.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope listing the blocked types, or a validation error.

Return type:

str

async tools.headless_browser.browser_get_status()[source]

Report the browser manager’s runtime status.

Backs the browser_get_status tool. Reads diagnostic fields off the shared _bm singleton (whether Playwright is available, init/connection state, usage and error counts, and the list of active contexts/pages) without touching any page.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_status tool; there are no direct internal callers.

Returns:

JSON envelope with the manager status snapshot.

Return type:

str

async tools.headless_browser.browser_restart()[source]

Restart the shared browser instance.

Backs the browser_restart tool. Delegates to _bm.restart to tear down and relaunch Firefox, used to recover from a wedged or misbehaving browser.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_restart tool; there are no direct internal callers.

Returns:

JSON envelope reporting success or failure of the restart.

Return type:

str

async tools.headless_browser.browser_new_context(context_id=None, incognito=True, proxy=None)[source]

Create a new isolated browser context (incognito-like window).

Backs the browser_new_context tool. Optionally validates a SOCKS proxy (assert_safe_socks_proxy_url) and creates a fresh context via _bm.create_new_context, returning its id. Each context has its own cookies, storage, and cache, so concurrent isolated sessions are possible.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_new_context tool; there are no direct internal callers.

Parameters:
  • context_id (str) – Optional id for the new context; auto-named otherwise.

  • incognito (bool) – Echoed back in the result; contexts are isolated by default.

  • proxy (str) – Optional SOCKS proxy URL to route the context through.

Returns:

JSON envelope with the new context id, or an error.

Return type:

str

async tools.headless_browser.browser_enable_response_interception(url_patterns, context_id=None, proxy=None)[source]

Start capturing network responses matching URL patterns.

Backs the browser_enable_response_interception tool. Parses url_patterns (a JSON array of globs), resolves the context (_ctx_or_err), ensures a page exists (_bm.get_page), and appends the patterns to this context’s _bm.response_patterns. The on_response handler installed by _setup_page_handlers then stores matching responses in _bm.intercepted_responses for later retrieval.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_enable_response_interception tool; there are no direct internal callers.

Parameters:
  • url_patterns (str) – JSON array of glob patterns to capture.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the active pattern list and context id, or an error.

Return type:

str

async tools.headless_browser.browser_wait_for_response(url_pattern, timeout=30000, context_id=None, proxy=None)[source]

Wait for one network response matching a pattern and return its body.

Backs the browser_wait_for_response tool. Resolves the context (_ctx_or_err) and page (_bm.get_page), then uses page.expect_response to block until a response URL matches the glob, decoding the body (JSON via _json_loads_network_body, text inline, large bodies summarized). Unlike interception, this captures a single awaited response inline.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_wait_for_response tool; there are no direct internal callers.

Parameters:
  • url_pattern (str) – Glob pattern the response URL must match.

  • timeout (int) – Wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the response url, status, headers, body, and method, or a timeout/error.

Return type:

str

async tools.headless_browser.browser_get_intercepted_responses(context_id=None, proxy=None, url_filter=None)[source]

List captured network responses as lightweight summaries.

Backs the browser_get_intercepted_responses tool. Reads from _bm.intercepted_responses, optionally narrowing to a context (_ctx_or_err) and/or a URL glob, and returns one summary per response (id, url, status, method, timestamp, and whether a body was captured) so the caller can then fetch a specific body via browser_get_response_body.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_intercepted_responses tool; there are no direct internal callers.

Parameters:
  • context_id (str) – Optional context to filter by.

  • proxy (str) – Optional SOCKS proxy URL used to derive the context filter.

  • url_filter (str) – Optional glob to filter response URLs.

Returns:

JSON envelope with the count and per-response summaries, or an error.

Return type:

str

async tools.headless_browser.browser_get_response_body(response_id)[source]

Return the full stored record for one intercepted response.

Backs the browser_get_response_body tool. Looks the id up in _bm.intercepted_responses and returns the complete captured record (headers and decoded body included), the companion to browser_get_intercepted_responses which only returns summaries.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_response_body tool; there are no direct internal callers.

Parameters:

response_id (str) – Id of a previously intercepted response.

Returns:

JSON envelope with the full response record, or an error if the id is unknown.

Return type:

str

async tools.headless_browser.browser_clear_intercepted_responses(context_id=None, proxy=None)[source]

Drop stored intercepted responses, optionally scoped to a context.

Backs the browser_clear_intercepted_responses tool. When a context (or proxy) is given it resolves it (_ctx_or_err) and removes only that context’s entries from _bm.intercepted_responses; otherwise it clears the whole capture buffer, freeing memory.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_clear_intercepted_responses tool; there are no direct internal callers.

Parameters:
  • context_id (str) – Optional context to scope the clear to.

  • proxy (str) – Optional SOCKS proxy URL used to derive the context.

Returns:

JSON envelope reporting how many responses were cleared, or an error.

Return type:

str

async tools.headless_browser.browser_create_persistent_context(session_name, context_id=None, proxy=None)[source]

Create a context whose cookies and storage persist on disk.

Backs the browser_create_persistent_context tool. Rejects a duplicate context id, ensures Playwright is initialized, optionally validates a SOCKS proxy, then calls _bm._create_persistent_context to launch a context rooted at a SESSIONS_DIR profile so a logged-in session can be reused across runs.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_create_persistent_context tool; there are no direct internal callers.

Parameters:
  • session_name (str) – Logical session name (also the on-disk profile name).

  • context_id (str) – Optional context id; defaults to session_name.

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the context id, sanitized session name, and session path, or an error.

Return type:

str

async tools.headless_browser.browser_list_sessions()[source]

List saved persistent browser sessions on disk.

Backs the browser_list_sessions tool. Walks SESSIONS_DIR for saved profile directories, computes each one’s on-disk size, and notes whether a live context (from _bm.persistent_contexts) is currently bound to it.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_list_sessions tool; there are no direct internal callers.

Returns:

JSON envelope listing each session’s name, path, size, and active context (if any), or an error.

Return type:

str

async tools.headless_browser.browser_delete_session(session_name)[source]

Delete a saved persistent session directory from disk.

Backs the browser_delete_session tool. Resolves the sanitized session path under SESSIONS_DIR, refuses to delete a session still bound to a live context (checked against _bm.persistent_contexts), and otherwise removes the directory tree off-thread via asyncio.to_thread(shutil.rmtree).

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_delete_session tool; there are no direct internal callers.

Parameters:

session_name (str) – Name of the saved session to delete.

Returns:

JSON envelope confirming deletion, or an error (missing session or session in use).

Return type:

str

async tools.headless_browser.browser_wait_for_download(timeout=30000, context_id=None, proxy=None)[source]

Wait for the next file download in a context to complete.

Backs the browser_wait_for_download tool. Resolves the context (_ctx_or_err), ensures a page exists (_bm.get_page), and registers a future in _bm.pending_downloads that the on_download handler resolves once the file is saved to DOWNLOADS_DIR; the future is awaited with a timeout.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_wait_for_download tool; there are no direct internal callers.

Parameters:
  • timeout (int) – Wait timeout in milliseconds.

  • context_id (str) – Browser context to use (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the completed download info, or a timeout/error.

Return type:

str

async tools.headless_browser.browser_get_downloads(context_id=None, proxy=None, state=None)[source]

List captured downloads, optionally filtered.

Backs the browser_get_downloads tool. Reads _bm.downloads, optionally narrowing to a context (_ctx_or_err) and/or a state, and returns the matching download records (id, url, filename, saved path, state, size).

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_downloads tool; there are no direct internal callers.

Parameters:
  • context_id (str) – Optional context to filter by.

  • proxy (str) – Optional SOCKS proxy URL used to derive the context filter.

  • state (str) – Optional state filter (completed, pending, failed).

Returns:

JSON envelope with the count and matching download records, or an error.

Return type:

str

async tools.headless_browser.browser_save_download(download_id, filename=None)[source]

Return a download’s info, optionally renaming the saved file.

Backs the browser_save_download tool. Looks the id up in _bm.downloads; when a filename is given it renames the file on disk (within DOWNLOADS_DIR, sanitizing the name) and updates the record. Returns the (possibly updated) download info.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_save_download tool; there are no direct internal callers.

Parameters:
  • download_id (str) – Id of a captured download.

  • filename (str) – Optional new filename to rename the saved file to.

Returns:

JSON envelope with the download info, or an error if the id is unknown.

Return type:

str

async tools.headless_browser.browser_get_console_logs(context_id=None, proxy=None, level=None, limit=100)[source]

Return captured browser console messages for a context.

Backs the browser_get_console_logs tool. Resolves the context (_ctx_or_err) and reads its buffer from _bm.console_logs (populated by the on_console page handler), optionally filtering by log level and capping to the most recent limit entries.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_console_logs tool; there are no direct internal callers.

Parameters:
  • context_id (str) – Browser context whose logs to read (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

  • level (str) – Optional level filter (log, warn, error, info, debug, warning).

  • limit (int) – Maximum number of most-recent entries to return.

Returns:

JSON envelope with the matching console logs and count, or an error.

Return type:

str

async tools.headless_browser.browser_clear_console_logs(context_id=None, proxy=None)[source]

Clear a context’s captured console logs.

Backs the browser_clear_console_logs tool. Resolves the context (_ctx_or_err) and empties its _bm.console_logs buffer, reporting how many entries were dropped.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_clear_console_logs tool; there are no direct internal callers.

Parameters:
  • context_id (str) – Browser context whose logs to clear (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope reporting the number of logs cleared, or an error.

Return type:

str

async tools.headless_browser.browser_handle_dialog(action, prompt_text=None, context_id=None, proxy=None)[source]

Set how JavaScript dialogs are auto-handled for a context.

Backs the browser_handle_dialog tool. Validates the action, resolves the context (_ctx_or_err), and stores the policy in _bm.dialog_handlers so the on_dialog page handler accepts, dismisses, or auto-accepts future alerts/confirms/prompts; it also echoes back the last seen dialog from _bm.pending_dialogs.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_handle_dialog tool; there are no direct internal callers.

Parameters:
  • action (str) – Policy: accept, dismiss, or manual.

  • prompt_text (str) – Optional text echoed back (for prompt dialogs).

  • context_id (str) – Browser context to configure (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope confirming the policy and the last dialog seen, or a validation error.

Return type:

str

async tools.headless_browser.browser_get_pending_dialog(context_id=None, proxy=None)[source]

Report the last JavaScript dialog seen in a context.

Backs the browser_get_pending_dialog tool. Resolves the context (_ctx_or_err) and returns its entry from _bm.pending_dialogs (recorded by the on_dialog page handler), so the caller can inspect the type, message, and how it was handled.

Dispatched by the tool runner in tools/__init__.py, which calls tool_def.handler(**arguments) for the registered browser_get_pending_dialog tool; there are no direct internal callers.

Parameters:
  • context_id (str) – Browser context to inspect (ignored when proxy is set).

  • proxy (str) – Optional SOCKS proxy URL.

Returns:

JSON envelope with the last dialog info, or a None data payload when no dialog has appeared, or an error.

Return type:

str