diff --git a/README.md b/README.md index e083f1c8..9307d1b7 100644 --- a/README.md +++ b/README.md @@ -291,6 +291,10 @@ The Chrome DevTools MCP server supports the following configuration option: If enabled, ignores errors relative to self-signed and expired certificates. Use with caution. - **Type:** boolean +- **`--protocolTimeout`** + Timeout for the Chrome DevTools Protocol operations in milliseconds. + - **Type:** number + Pass them via the `args` property in the JSON configuration. For example: diff --git a/src/browser.ts b/src/browser.ts index 5387b3ef..5001f97f 100644 --- a/src/browser.ts +++ b/src/browser.ts @@ -38,18 +38,23 @@ function targetFilter(target: Target): boolean { return true; } -const connectOptions: ConnectOptions = { - targetFilter, - // We do not expect any single CDP command to take more than 10sec. - protocolTimeout: 10_000, -}; +function getConnectOptions(protocolTimeout: number): ConnectOptions { + return { + targetFilter, + // We do not expect any single CDP command to take more than 10sec. + protocolTimeout, + }; +} -export async function ensureBrowserConnected(browserURL: string) { +export async function ensureBrowserConnected( + browserURL: string, + protocolTimeout = 10_000, +) { if (browser?.connected) { return browser; } browser = await puppeteer.connect({ - ...connectOptions, + ...getConnectOptions(protocolTimeout), browserURL, defaultViewport: null, }); @@ -70,6 +75,7 @@ interface McpLaunchOptions { height: number; }; args?: string[]; + protocolTimeout?: number; } export async function launch(options: McpLaunchOptions): Promise { @@ -112,7 +118,7 @@ export async function launch(options: McpLaunchOptions): Promise { try { const browser = await puppeteer.launch({ - ...connectOptions, + ...getConnectOptions(options.protocolTimeout ?? 10_000), channel: puppeteerChannel, executablePath, defaultViewport: null, @@ -162,6 +168,7 @@ export async function ensureBrowserLaunched( return browser; } browser = await launch(options); + return browser; } diff --git a/src/cli.ts b/src/cli.ts index 909d1a21..3581003f 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -88,6 +88,11 @@ export const cliOptions = { type: 'boolean', description: `If enabled, ignores errors relative to self-signed and expired certificates. Use with caution.`, }, + protocolTimeout: { + type: 'number', + describe: + 'Timeout for the Chrome DevTools Protocol operations in milliseconds.', + }, } satisfies Record; export function parseArguments(version: string, argv = process.argv) { diff --git a/src/main.ts b/src/main.ts index 2663d3c1..53db4550 100644 --- a/src/main.ts +++ b/src/main.ts @@ -74,7 +74,7 @@ async function getContext(): Promise { extraArgs.push(`--proxy-server=${args.proxyServer}`); } const browser = args.browserUrl - ? await ensureBrowserConnected(args.browserUrl) + ? await ensureBrowserConnected(args.browserUrl, args.protocolTimeout) : await ensureBrowserLaunched({ headless: args.headless, executablePath: args.executablePath, @@ -85,6 +85,7 @@ async function getContext(): Promise { viewport: args.viewport, args: extraArgs, acceptInsecureCerts: args.acceptInsecureCerts, + protocolTimeout: args.protocolTimeout, }); if (context?.browser !== browser) {