/** * cdp-toolkit public surface. * * Two exports: * 1. `TOOLS` : the registry mapping every chrome-devtools-mcp tool name * (snake_case string, all 29 of them) plus the toolkit's own superset * tools, 48 entries in all, to its raw-CDP implementation function. The * CLI and any embedder dispatch through this single table. * 2. The client primitives, re-exported so consumers can build their own * flows on the same connection/timeout machinery the tools use. * * Every tool fn takes a single typed args object and returns a JSON-serializable * value (or throws). See CONTRACT.md for the design rules each module follows. */ // --- the 23 tools unified with Firefox onto one Driver-based implementation --- // (list_pages/new_page/close_page/select_page, navigate_page/wait_for, evaluate_script, // list_cookies/set_cookie/delete_cookies, // take_snapshot, click/hover/drag/fill/fill_form/type_text/press_key/upload_file, // take_screenshot/emulate/resize_page, handle_dialog). See shared-tools.ts's file header for // the byte-identical-Chrome-behavior contract this bind honors, and for the 7 tools that stay // two implementations (console/network/mock, immediately below, unchanged from before). import { createCdpDriver } from "./cdp/driver.ts"; import { LEASE_TOOLS } from "./leases-tools.ts"; import { SHARED_TOOLS } from "./shared-tools.ts"; // --- console + network (read the recorder buffer) --- import { listConsoleMessages, getConsoleMessage } from "./tools/console.ts"; import { listNetworkRequests, getNetworkRequest } from "./tools/network.ts"; // --- performance --- import { performanceStartTrace, performanceStopTrace, performanceAnalyzeInsight, performanceTrace, } from "./tools/performance.ts"; // --- screen recording (Page.startScreencast + ffmpeg) --- import { startScreenRecording, stopScreenRecording } from "./tools/screencast.ts"; // --- heap --- import { takeHeapsnapshot } from "./tools/heap.ts"; // --- lighthouse (the sole non-CDP tool) --- import { lighthouseAudit } from "./tools/lighthouse.ts"; // --- network mocking (Fetch domain) : toolkit addition beyond the 29 parity tools --- import { mockRequest, listMocks, clearMocks } from "./tools/network_mock.ts"; // --- raw mouse dispatch (Input.dispatchMouseEvent, one event per call) : chrome-only, capability "input.raw" --- import { dispatchMouse } from "./tools/dispatch-mouse.ts"; // --- focus emulation + focus-gated click (Emulation.setFocusEmulationEnabled) : chrome-only, capability "emulate.focus" --- import { focusEmulation, clickFocusGated } from "./tools/focus-emulation.ts"; // --- downloads + permissions (browser-endpoint state on a standing connection) : chrome-only, 1.8.0 Track P3 --- import { waitForDownload } from "./tools/downloads.ts"; import { grantPermissions } from "./tools/permissions.ts"; // --- tab groups (chrome.tabGroups through a helper extension) : chrome-only, capability "browser.tabGroups" --- import { listTabGroups, groupTabs, ungroupTabs } from "./tools/tab-groups.ts"; // --- structured extraction (cleaned page HTML -> OpenAI-compatible endpoint, both backends) --- import { extractPage } from "./tools/extract.ts"; /** A toolkit tool: a single typed-args function returning JSON-serializable data. */ export type ToolFn = (args: never) => Promise; // One stateless CDP driver, reused across calls (lifetime "per-call": every page() acquisition // still opens its own throwaway connection, same as the pre-migration withPage per-call model). // Constructing it does not dial a browser (capabilities.ts already relies on this). const cdpDriver = createCdpDriver(); /** Bind a shared-tools.ts function to the module-level CDP driver, args-typed for the registry. */ function onCdp(fn: (driver: ReturnType, args: A) => Promise): (args: A) => Promise { return (args: A) => fn(cdpDriver, args); } /** * The complete chrome-devtools-mcp parity surface: all 29 MCP tools keyed by * their canonical MCP (snake_case) name, plus one convenience superset tool * (`performance_trace`, a robust single-call trace : the start/stop pair cannot * span two stateless CLI processes), plus a 3-tool network-mocking group * (mock_request/list_mocks/clear_mocks : a persistent per-target fake backend), * plus a 3-tool cookie group (list_cookies/set_cookie/delete_cookies : the * cookie store including httpOnly cookies, which no page script can read or * write), plus a 3-tool lease group (claim_page/release_page/list_leases : * opt-in tab ownership so many agents can share one browser), plus a 2-tool * screen-recording pair (start_screen_recording/stop_screen_recording : a tab * captured to an H.265 MP4 with per-frame durations, so a variable-rate * repaint stream plays back at wallclock speed), plus a 2-tool input-parity * addition (1.8.0 Track P1: `scroll`, a wheel-event dispatch at an anchor * point sharing click/hover's element-locator and lease-gate contract; and * `dispatch_mouse`, the raw move/down/up mouse-event primitive composable * into anything a physical mouse can do, chrome-only per capability "input.raw"), plus a 2-tool * browser-state addition (1.8.0 Track P3: `wait_for_download`, which captures a file download to a * named path, and `grant_permissions`, which answers permission prompts for an origin; both * chrome-only and both driven from a standing browser-endpoint connection, see * tools/browser-session.ts). * 29 + 1 + 3 + 3 + 3 + 2 + 2 + 2 + 1 (extract_page) + 2 (focus pair) + 3 (tab groups, tools/tab-groups.ts) + 1 (list_contexts, #12) = 52 entries total. Listed explicitly so the mapping is auditable at a glance and * the CLI can `--list` it. * * 20 of the 29 MCP-parity tools (pages/navigation/evaluate/snapshot/interaction/ * screenshot+emulation/dialogs) now route through shared-tools.ts's single Driver-based * implementation via the CDP driver, instead of a Chrome-only copy in src/tools/*.ts. * See shared-tools.ts's file header for the shape/behavior contract this preserves. */ export const TOOLS = { // pages (4) list_pages: onCdp(SHARED_TOOLS.list_pages), new_page: onCdp(SHARED_TOOLS.new_page), close_page: onCdp(SHARED_TOOLS.close_page), select_page: onCdp(SHARED_TOOLS.select_page), // navigation (2) navigate_page: onCdp(SHARED_TOOLS.navigate_page), wait_for: onCdp(SHARED_TOOLS.wait_for), // evaluate (1) evaluate_script: onCdp(SHARED_TOOLS.evaluate_script), // cookies (3) : toolkit addition, reads and writes httpOnly cookies that document.cookie cannot touch list_cookies: onCdp(SHARED_TOOLS.list_cookies), set_cookie: onCdp(SHARED_TOOLS.set_cookie), delete_cookies: onCdp(SHARED_TOOLS.delete_cookies), // snapshot (1) take_snapshot: onCdp(SHARED_TOOLS.take_snapshot), // execution contexts (1) : #12, the realms inside one target (nested same-process frames, // late contexts). chrome-only (capability "runtime.contexts") list_contexts: onCdp(SHARED_TOOLS.list_contexts), // interaction (9) : scroll is 1.8.0 Track P1, a toolkit addition beyond the 29 parity tools click: onCdp(SHARED_TOOLS.click), hover: onCdp(SHARED_TOOLS.hover), drag: onCdp(SHARED_TOOLS.drag), scroll: onCdp(SHARED_TOOLS.scroll), fill: onCdp(SHARED_TOOLS.fill), fill_form: onCdp(SHARED_TOOLS.fill_form), type_text: onCdp(SHARED_TOOLS.type_text), press_key: onCdp(SHARED_TOOLS.press_key), upload_file: onCdp(SHARED_TOOLS.upload_file), // screenshot + emulation (3) take_screenshot: onCdp(SHARED_TOOLS.take_screenshot), emulate: onCdp(SHARED_TOOLS.emulate), resize_page: onCdp(SHARED_TOOLS.resize_page), // dialogs (1) handle_dialog: onCdp(SHARED_TOOLS.handle_dialog), // console + network (4) list_console_messages: listConsoleMessages, get_console_message: getConsoleMessage, list_network_requests: listNetworkRequests, get_network_request: getNetworkRequest, // performance (3 MCP + 1 convenience one-shot) performance_start_trace: performanceStartTrace, performance_stop_trace: performanceStopTrace, performance_analyze_insight: performanceAnalyzeInsight, performance_trace: performanceTrace, // screen recording (2) : toolkit addition, a tab-to-H.265-MP4 pair over Page.startScreencast start_screen_recording: startScreenRecording, stop_screen_recording: stopScreenRecording, // heap (1) take_heapsnapshot: takeHeapsnapshot, // lighthouse (1) : the only non-CDP tool lighthouse_audit: lighthouseAudit, // network mocking (3) : persistent per-target fake backend (toolkit addition, Fetch domain) mock_request: mockRequest, list_mocks: listMocks, clear_mocks: clearMocks, // raw mouse dispatch (1) : 1.8.0 Track P1 toolkit addition, chrome-only (capability "input.raw") dispatch_mouse: dispatchMouse, // focus emulation (2) : fake page focus without stealing OS focus; trusted click on a focus-gated // button (e.g. Claude Code OAuth Authorize). chrome-only (capability "emulate.focus") focus_emulation: focusEmulation, click_focus_gated: clickFocusGated, // downloads + permissions (2) : 1.8.0 Track P3 toolkit additions, chrome-only (capabilities // "browser.downloads" / "browser.permissions"). Both drive browser-endpoint state that Chrome // reverts when the issuing client disconnects, so both run on tools/browser-session.ts's standing // connection rather than a per-call socket : see that module's header for the measurements. wait_for_download: waitForDownload, grant_permissions: grantPermissions, // tab groups (3) : #17. CDP has no tab-group domain, so these evaluate chrome.tabGroups in a helper // extension's service worker (installed via Extensions.loadUnpacked). chrome-only (capability "browser.tabGroups") list_tab_groups: listTabGroups, group_tabs: groupTabs, ungroup_tabs: ungroupTabs, // structured extraction (1) : toolkit addition, both backends — a page's cleaned HTML // POSTed to an OpenAI-compatible /chat/completions endpoint (default: loopback // llm-ferry) with the caller's JSON Schema as a strict response_format, returning // schema-conformant JSON + token usage. API key is env-only (CDP_EXTRACT_API_KEY). extract_page: onCdp(extractPage), // tab leases (3) : opt-in ownership so many agents can share one browser claim_page: onCdp(LEASE_TOOLS.claim_page), release_page: onCdp(LEASE_TOOLS.release_page), list_leases: onCdp(LEASE_TOOLS.list_leases), } satisfies Record Promise>; /** Canonical MCP tool name accepted by the registry. */ export type ToolName = keyof typeof TOOLS; /** All tool names (handy for `--list` and validation). */ export const TOOL_NAMES = Object.keys(TOOLS) as ToolName[]; /* ----------------------------- client primitives ----------------------------- */ export { BASE, DEFAULT_TIMEOUT_MS, CdpError, CdpConnection, listTargets, browserWsUrl, resolveTarget, openBrowser, openPage, withPage, } from "./client.ts"; export type { Target, TargetSelector, Uid, CdpResponse, CdpEvent, ToolResult } from "./types.ts";