Skip to main content

Module capture

Module capture 

Source
Expand description

Passive console and network capture for the MCP server.

The MCP server keeps one CDP WebSocket open for its lifetime, and CDP pushes Runtime.* / Log.* / Network.* events to any attached session that has those domains enabled. This module keeps one long-lived flat session per touched tab (any tab a tool call has routed to), enables the domains once, and routes the pushed events into bounded per-tab ring buffers that browser_console_messages / browser_network_requests read on demand.

This is the sanctioned exception to the “no idle work” rule in docs/specs/boundaries.md: nothing here polls or wakes on a timer. The only background task blocks on the event channel and does a mutex push per event; when the browser is quiet, it is parked.

Lifecycle: state for a tab is dropped when the tab is closed through the MCP server (forget), when the browser detaches the session (Target.detachedFromTarget), when the renderer crashes (Inspector.targetCrashed), and wholesale on browser_select (reset) or when the socket closes. Nothing in here ever returns TabHung / TabCrashed: attach failures are swallowed and retried on the next touch, so the recover-once flows elsewhere are unaffected.

Firefox (WebDriver BiDi) uses the same hub with a different ingress: one global session.subscribe per backend for log.entryAdded, network.beforeRequestSent / responseCompleted / fetchError, and browsingContext.navigationStarted / contextDestroyed, routed by the browsing-context id, which is the target id. Response bodies are not available on BiDi (no getResponseBody equivalent without browser-side retention), so browser_network_body stays Chromium-only.

Opt-out: BROWSER_CONTROL_CAPTURE=0 (or false) disables attachment entirely on both engines. Runtime.enable is observable by some anti-bot scripts, and a user logging into such a site through browser_show may prefer the server not to touch their tabs.

Structs§

BodyResult
Result of a body fetch.
CaptureHub
The capture hub. Cheap to clone via Arc on ServerState.
ConsoleEntry
One captured console line.
ConsoleQuery
Filters for read_console.
ConsoleReport
Result of a console read.
NetworkEntry
One captured network request.
NetworkQuery
Filters for read_network.
NetworkReport
Result of a network read.

Enums§

Level
NetState
StatusFilter

Constants§

BIDI_NO_BODIES_HINT
Agent-facing explanation for browser_network_body on Firefox.
BODY_DEFAULT_MAX
Default cap for browser_network_body.
BODY_HARD_MAX
Hard cap for browser_network_body, aligned with browser_curl.
CONSOLE_CAP
Console entries kept per tab.
CONSOLE_TEXT_CAP
Bytes of rendered text kept per console entry.
NETWORK_CAP
Network entries kept per tab.
TOUCH_WAIT
How long a reader (or browser_navigate) waits for an in-flight attach.
URL_CAP
Bytes of URL kept per entry.

Functions§

fmt_iso_ms
Epoch milliseconds → YYYY-MM-DDTHH:MM:SS.mmmZ.
format_console_line
One console entry as a single text line (no page separator).
format_console_text
Render a console report as text with -- page: <url> -- separators.
format_network_line
One network entry as a single text line.
format_network_text
Render a network report as text.
render_bidi_remote_value
Render a BiDi script.RemoteValue for console output.
render_remote_object
Render a Runtime.RemoteObject the way DevTools’ console preview does, without any round trip: primitives verbatim, objects from their preview, everything else by description.