Chrome DevTools for coding agents
refactor: bundle puppeteer-core (#417)
zyzyzyryxyb443033000e4
diff --git a/package.json b/package.jsonindex 10e165f8..b0f02da3 100644--- a/package.json+++ b/package.json@@ -40,7 +40,6 @@"dependencies": {"core-js": "3.46.0","debug": "4.4.3",- "puppeteer-core": "^24.24.1","yargs": "18.0.0"},"devDependencies": {diff --git a/rollup.config.mjs b/rollup.config.mjsindex b03ee276..9ca5ef30 100644--- a/rollup.config.mjs+++ b/rollup.config.mjs@@ -29,11 +29,31 @@ import license from 'rollup-plugin-license';const isProduction = process.env.NODE_ENV === 'production';-/** @type {import('rollup').RollupOptions} */-const sdk = {- input: './build/src/third_party/modelcontextprotocol-sdk/index.js',+const allowedLicenses = [+ 'MIT',+ 'Apache 2.0',+ 'Apache-2.0',+ 'BSD-3-Clause',+ 'BSD-2-Clause',+ 'ISC',+ '0BSD',+];++/**+ * @param {string} wrapperIndexPath+ * @param {import('rollup').OutputOptions} [extraOutputOptions={}]+ * @param {string[]} [external=[]]+ * @returns {import('rollup').RollupOptions}+ */+const bundleDependency = (+ wrapperIndexPath,+ extraOutputOptions = {},+ external = [],+) => ({+ input: wrapperIndexPath,output: {- file: './build/src/third_party/modelcontextprotocol-sdk/index.js',+ ...extraOutputOptions,+ file: wrapperIndexPath,sourcemap: !isProduction,format: 'esm',},@@ -48,18 +68,14 @@ const sdk = {thirdParty: {allow: {test: dependency => {- let allowed_licenses = ['MIT', 'Apache 2.0', 'BSD-2-Clause', 'ISC'];- return allowed_licenses.includes(dependency.license);+ return allowedLicenses.includes(dependency.license);},failOnUnlicensed: true,failOnViolation: true,},output: {file: path.join(- 'build',- 'src',- 'third_party',- 'modelcontextprotocol-sdk',+ path.dirname(wrapperIndexPath),'THIRD_PARTY_NOTICES',),template(dependencies) {@@ -90,6 +106,16 @@ const sdk = {json(),nodeResolve(),],-};+ external,+});-export default [sdk];+export default [+ bundleDependency('./build/src/third_party/modelcontextprotocol-sdk/index.js'),+ bundleDependency(+ './build/src/third_party/puppeteer-core/index.js',+ {+ inlineDynamicImports: true,+ },+ ['./bidi.js', '../bidi/bidi.js'],+ ),+];diff --git a/src/DevToolsConnectionAdapter.ts b/src/DevToolsConnectionAdapter.tsindex 814dc7f6..ec07d1a7 100644--- a/src/DevToolsConnectionAdapter.ts+++ b/src/DevToolsConnectionAdapter.ts@@ -4,10 +4,10 @@* SPDX-License-Identifier: Apache-2.0*/-import {type ConnectionTransport} from 'puppeteer-core';-import {Connection} from '../node_modules/chrome-devtools-frontend/front_end/core/protocol_client/InspectorBackend.js';+import {type ConnectionTransport} from './third_party/puppeteer-core/index.js';+/*** Allows a puppeteer {@link ConnectionTransport} to act like a DevTools {@link Connection}.*/diff --git a/src/McpContext.ts b/src/McpContext.tsindex 29170568..4f7641d5 100644--- a/src/McpContext.ts+++ b/src/McpContext.ts@@ -8,6 +8,10 @@ import os from 'node:os';import path from 'node:path';import type {Debugger} from 'debug';++import type {ListenerMap} from './PageCollector.js';+import {NetworkCollector, PageCollector} from './PageCollector.js';+import {Locator} from './third_party/puppeteer-core/index.js';import type {Browser,ConsoleMessage,@@ -17,10 +21,7 @@ import type {Page,SerializedAXNode,PredefinedNetworkConditions,-} from 'puppeteer-core';--import type {ListenerMap} from './PageCollector.js';-import {NetworkCollector, PageCollector} from './PageCollector.js';+} from './third_party/puppeteer-core/index.js';import {listPages} from './tools/pages.js';import {takeSnapshot} from './tools/snapshot.js';import {CLOSE_PAGE_ERROR} from './tools/ToolDefinition.js';@@ -91,9 +92,16 @@ export class McpContext implements Context {#nextSnapshotId = 1;#traceResults: TraceResult[] = [];- private constructor(browser: Browser, logger: Debugger) {+ #locatorClass: typeof Locator;++ private constructor(+ browser: Browser,+ logger: Debugger,+ locatorClass: typeof Locator,+ ) {this.browser = browser;this.logger = logger;+ this.#locatorClass = locatorClass;this.#networkCollector = new NetworkCollector(this.browser);@@ -122,8 +130,13 @@ export class McpContext implements Context {await this.#consoleCollector.init();}- static async from(browser: Browser, logger: Debugger) {- const context = new McpContext(browser, logger);+ static async from(+ browser: Browser,+ logger: Debugger,+ /* Let tests use unbundled Locator class to avoid overly strict checks within puppeteer that fail when mixing bundled and unbundled class instances */+ locatorClass: typeof Locator = Locator,+ ) {+ const context = new McpContext(browser, logger, locatorClass);await context.#init();return context;}@@ -428,4 +441,28 @@ export class McpContext implements Context {getNetworkRequestStableId(request: HTTPRequest): number {return this.#networkCollector.getIdForResource(request);}++ waitForTextOnPage({+ text,+ timeout,+ }: {+ text: string;+ timeout?: number | undefined;+ }): Promise<Element> {+ const page = this.getSelectedPage();+ const frames = page.frames();++ const locator = this.#locatorClass.race(+ frames.flatMap(frame => [+ frame.locator(`aria/${text}`),+ frame.locator(`text/${text}`),+ ]),+ );++ if (timeout) {+ locator.setTimeout(timeout);+ }++ return locator.wait();+ }}diff --git a/src/McpResponse.ts b/src/McpResponse.tsindex 8a1b6dc9..31bbdcc5 100644--- a/src/McpResponse.ts+++ b/src/McpResponse.ts@@ -3,8 +3,6 @@* Copyright 2025 Google LLC* SPDX-License-Identifier: Apache-2.0*/-import type {ConsoleMessage, ResourceType} from 'puppeteer-core';-import type {ConsoleMessageData} from './formatters/consoleFormatter.js';import {formatConsoleEventShort,@@ -23,6 +21,10 @@ import type {ImageContent,TextContent,} from './third_party/modelcontextprotocol-sdk/index.js';+import type {+ ConsoleMessage,+ ResourceType,+} from './third_party/puppeteer-core/index.js';import {handleDialog} from './tools/pages.js';import type {ImageContentData, Response} from './tools/ToolDefinition.js';import {paginate} from './utils/pagination.js';diff --git a/src/PageCollector.ts b/src/PageCollector.tsindex b6b3d679..44d1363f 100644--- a/src/PageCollector.ts+++ b/src/PageCollector.ts@@ -11,7 +11,7 @@ import {type HTTPRequest,type Page,type PageEvents,-} from 'puppeteer-core';+} from './third_party/puppeteer-core/index.js';export type ListenerMap<EventMap extends PageEvents = PageEvents> = {[K in keyof EventMap]?: (event: EventMap[K]) => void;diff --git a/src/WaitForHelper.ts b/src/WaitForHelper.tsindex 62cc83f0..a18f6448 100644--- a/src/WaitForHelper.ts+++ b/src/WaitForHelper.ts@@ -3,10 +3,10 @@* Copyright 2025 Google LLC* SPDX-License-Identifier: Apache-2.0*/-import type {Page, Protocol} from 'puppeteer-core';import type {CdpPage} from 'puppeteer-core/internal/cdp/Page.js';import {logger} from './logger.js';+import type {Page, Protocol} from './third_party/puppeteer-core/index.js';export class WaitForHelper {#abortController = new AbortController();diff --git a/src/browser.ts b/src/browser.tsindex d76d5a9f..47afe5a9 100644--- a/src/browser.ts+++ b/src/browser.ts@@ -13,8 +13,8 @@ import type {ChromeReleaseChannel,LaunchOptions,Target,-} from 'puppeteer-core';-import puppeteer from 'puppeteer-core';+} from './third_party/puppeteer-core/index.js';+import {puppeteer} from './third_party/puppeteer-core/index.js';let browser: Browser | undefined;diff --git a/src/formatters/networkFormatter.ts b/src/formatters/networkFormatter.tsindex 57279733..b3466e8c 100644--- a/src/formatters/networkFormatter.ts+++ b/src/formatters/networkFormatter.ts@@ -6,7 +6,10 @@import {isUtf8} from 'node:buffer';-import type {HTTPRequest, HTTPResponse} from 'puppeteer-core';+import type {+ HTTPRequest,+ HTTPResponse,+} from '../third_party/puppeteer-core/index.js';const BODY_CONTEXT_SIZE_LIMIT = 10000;diff --git a/src/third_party/puppeteer-core/index.ts b/src/third_party/puppeteer-core/index.tsnew file mode 100644index 00000000..92ae88ae--- /dev/null+++ b/src/third_party/puppeteer-core/index.ts@@ -0,0 +1,9 @@+/**+ * @license+ * Copyright 2025 Google LLC+ * SPDX-License-Identifier: Apache-2.0+ */++export {Locator, PredefinedNetworkConditions} from 'puppeteer-core';+export {default as puppeteer} from 'puppeteer-core';+export type * from 'puppeteer-core';diff --git a/src/tools/ToolDefinition.ts b/src/tools/ToolDefinition.tsindex e90264a7..19415dd2 100644--- a/src/tools/ToolDefinition.ts+++ b/src/tools/ToolDefinition.ts@@ -4,10 +4,13 @@* SPDX-License-Identifier: Apache-2.0*/-import type {Dialog, ElementHandle, Page} from 'puppeteer-core';-import type {TextSnapshotNode} from '../McpContext.js';import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';+import type {+ Dialog,+ ElementHandle,+ Page,+} from '../third_party/puppeteer-core/index.js';import type {TraceResult} from '../trace-processing/parse.js';import type {PaginationOptions} from '../utils/types.js';@@ -93,6 +96,10 @@ export type Context = Readonly<{filename: string,): Promise<{filename: string}>;waitForEventsAfterAction(action: () => Promise<unknown>): Promise<void>;+ waitForTextOnPage(params: {+ text: string;+ timeout?: number | undefined;+ }): Promise<Element>;}>;export function defineTool<Schema extends zod.ZodRawShape>(diff --git a/src/tools/console.ts b/src/tools/console.tsindex 3f8967eb..8be953c8 100644--- a/src/tools/console.ts+++ b/src/tools/console.ts@@ -4,9 +4,8 @@* SPDX-License-Identifier: Apache-2.0*/-import type {ConsoleMessageType} from 'puppeteer-core';-import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';+import type {ConsoleMessageType} from '../third_party/puppeteer-core/index.js';import {ToolCategories} from './categories.js';import {defineTool} from './ToolDefinition.js';diff --git a/src/tools/emulation.ts b/src/tools/emulation.tsindex d65ac5e8..9287c00d 100644--- a/src/tools/emulation.ts+++ b/src/tools/emulation.ts@@ -4,9 +4,8 @@* SPDX-License-Identifier: Apache-2.0*/-import {PredefinedNetworkConditions} from 'puppeteer-core';-import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';+import {PredefinedNetworkConditions} from '../third_party/puppeteer-core/index.js';import {ToolCategories} from './categories.js';import {defineTool} from './ToolDefinition.js';diff --git a/src/tools/input.ts b/src/tools/input.tsindex ffb1cbb7..973eb829 100644--- a/src/tools/input.ts+++ b/src/tools/input.ts@@ -4,10 +4,9 @@* SPDX-License-Identifier: Apache-2.0*/-import type {ElementHandle} from 'puppeteer-core';-import type {McpContext, TextSnapshotNode} from '../McpContext.js';import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';+import type {ElementHandle} from '../third_party/puppeteer-core/index.js';import {ToolCategories} from './categories.js';import {defineTool} from './ToolDefinition.js';diff --git a/src/tools/network.ts b/src/tools/network.tsindex 0dbc7f7e..d47fa996 100644--- a/src/tools/network.ts+++ b/src/tools/network.ts@@ -4,9 +4,8 @@* SPDX-License-Identifier: Apache-2.0*/-import type {ResourceType} from 'puppeteer-core';-import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';+import type {ResourceType} from '../third_party/puppeteer-core/index.js';import {ToolCategories} from './categories.js';import {defineTool} from './ToolDefinition.js';diff --git a/src/tools/performance.ts b/src/tools/performance.tsindex 6c172fbb..a76f30d5 100644--- a/src/tools/performance.ts+++ b/src/tools/performance.ts@@ -4,10 +4,9 @@* SPDX-License-Identifier: Apache-2.0*/-import type {Page} from 'puppeteer-core';-import {logger} from '../logger.js';import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';+import type {Page} from '../third_party/puppeteer-core/index.js';import type {InsightName} from '../trace-processing/parse.js';import {getInsightOutput,diff --git a/src/tools/screenshot.ts b/src/tools/screenshot.tsindex 991fbd2b..e053186f 100644--- a/src/tools/screenshot.ts+++ b/src/tools/screenshot.ts@@ -4,9 +4,8 @@* SPDX-License-Identifier: Apache-2.0*/-import type {ElementHandle, Page} from 'puppeteer-core';-import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';+import type {ElementHandle, Page} from '../third_party/puppeteer-core/index.js';import {ToolCategories} from './categories.js';import {defineTool} from './ToolDefinition.js';diff --git a/src/tools/script.ts b/src/tools/script.tsindex 128363bd..6f08af08 100644--- a/src/tools/script.ts+++ b/src/tools/script.ts@@ -3,9 +3,13 @@* Copyright 2025 Google LLC* SPDX-License-Identifier: Apache-2.0*/-import type {Frame, JSHandle, Page} from 'puppeteer-core';import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';+import type {+ Frame,+ JSHandle,+ Page,+} from '../third_party/puppeteer-core/index.js';import {ToolCategories} from './categories.js';import {defineTool} from './ToolDefinition.js';diff --git a/src/tools/snapshot.ts b/src/tools/snapshot.tsindex fb774f78..0cf548b4 100644--- a/src/tools/snapshot.ts+++ b/src/tools/snapshot.ts@@ -4,8 +4,6 @@* SPDX-License-Identifier: Apache-2.0*/-import {Locator} from 'puppeteer-core';-import {zod} from '../third_party/modelcontextprotocol-sdk/index.js';import {ToolCategories} from './categories.js';@@ -44,21 +42,7 @@ export const waitFor = defineTool({...timeoutSchema,},handler: async (request, response, context) => {- const page = context.getSelectedPage();- const frames = page.frames();-- const locator = Locator.race(- frames.flatMap(frame => [- frame.locator(`aria/${request.params.text}`),- frame.locator(`text/${request.params.text}`),- ]),- );-- if (request.params.timeout) {- locator.setTimeout(request.params.timeout);- }-- await locator.wait();+ await context.waitForTextOnPage(request.params);response.appendResponseLine(`Element with text "${request.params.text}" found.`,diff --git a/tests/utils.ts b/tests/utils.tsindex 6c8744fb..7adaaf53 100644--- a/tests/utils.ts+++ b/tests/utils.ts@@ -5,7 +5,7 @@*/import logger from 'debug';import type {Browser} from 'puppeteer';-import puppeteer from 'puppeteer';+import puppeteer, {Locator} from 'puppeteer';import type {Frame, HTTPRequest, HTTPResponse} from 'puppeteer-core';import {McpContext} from '../src/McpContext.js';@@ -36,7 +36,7 @@ export async function withBrowser(}),);const response = new McpResponse();- const context = await McpContext.from(browser, logger('test'));+ const context = await McpContext.from(browser, logger('test'), Locator);await cb(response, context);}