Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- END AUTO GENERATED OPTIONS -->

Pass them via the `args` property in the JSON configuration. For example:
Expand Down
23 changes: 15 additions & 8 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -70,6 +75,7 @@ interface McpLaunchOptions {
height: number;
};
args?: string[];
protocolTimeout?: number;
}

export async function launch(options: McpLaunchOptions): Promise<Browser> {
Expand Down Expand Up @@ -112,7 +118,7 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {

try {
const browser = await puppeteer.launch({
...connectOptions,
...getConnectOptions(options.protocolTimeout ?? 10_000),
channel: puppeteerChannel,
executablePath,
defaultViewport: null,
Expand Down Expand Up @@ -162,6 +168,7 @@ export async function ensureBrowserLaunched(
return browser;
}
browser = await launch(options);

return browser;
}

Expand Down
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, YargsOptions>;

export function parseArguments(version: string, argv = process.argv) {
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async function getContext(): Promise<McpContext> {
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,
Expand All @@ -85,6 +85,7 @@ async function getContext(): Promise<McpContext> {
viewport: args.viewport,
args: extraArgs,
acceptInsecureCerts: args.acceptInsecureCerts,
protocolTimeout: args.protocolTimeout,
});

if (context?.browser !== browser) {
Expand Down