Skip to content

Commit a3ffe2e

Browse files
dungnguyent8OrKoN
authored andcommitted
feat: support protocolTimeout
1 parent 357db65 commit a3ffe2e

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ The Chrome DevTools MCP server supports the following configuration option:
291291
If enabled, ignores errors relative to self-signed and expired certificates. Use with caution.
292292
- **Type:** boolean
293293

294+
- **`--protocolTimeout`**
295+
Timeout for the Chrome DevTools Protocol operations in milliseconds.
296+
- **Type:** number
297+
294298
<!-- END AUTO GENERATED OPTIONS -->
295299

296300
Pass them via the `args` property in the JSON configuration. For example:

src/browser.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,23 @@ function targetFilter(target: Target): boolean {
3838
return true;
3939
}
4040

41-
const connectOptions: ConnectOptions = {
42-
targetFilter,
43-
// We do not expect any single CDP command to take more than 10sec.
44-
protocolTimeout: 10_000,
45-
};
41+
function getConnectOptions(protocolTimeout: number): ConnectOptions {
42+
return {
43+
targetFilter,
44+
// We do not expect any single CDP command to take more than 10sec.
45+
protocolTimeout,
46+
};
47+
}
4648

47-
export async function ensureBrowserConnected(browserURL: string) {
49+
export async function ensureBrowserConnected(
50+
browserURL: string,
51+
protocolTimeout = 10_000,
52+
) {
4853
if (browser?.connected) {
4954
return browser;
5055
}
5156
browser = await puppeteer.connect({
52-
...connectOptions,
57+
...getConnectOptions(protocolTimeout),
5358
browserURL,
5459
defaultViewport: null,
5560
});
@@ -70,6 +75,7 @@ interface McpLaunchOptions {
7075
height: number;
7176
};
7277
args?: string[];
78+
protocolTimeout?: number;
7379
}
7480

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

113119
try {
114120
const browser = await puppeteer.launch({
115-
...connectOptions,
121+
...getConnectOptions(options.protocolTimeout ?? 10_000),
116122
channel: puppeteerChannel,
117123
executablePath,
118124
defaultViewport: null,
@@ -162,6 +168,7 @@ export async function ensureBrowserLaunched(
162168
return browser;
163169
}
164170
browser = await launch(options);
171+
165172
return browser;
166173
}
167174

src/cli.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ export const cliOptions = {
8888
type: 'boolean',
8989
description: `If enabled, ignores errors relative to self-signed and expired certificates. Use with caution.`,
9090
},
91+
protocolTimeout: {
92+
type: 'number',
93+
describe:
94+
'Timeout for the Chrome DevTools Protocol operations in milliseconds.',
95+
},
9196
} satisfies Record<string, YargsOptions>;
9297

9398
export function parseArguments(version: string, argv = process.argv) {

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ async function getContext(): Promise<McpContext> {
7474
extraArgs.push(`--proxy-server=${args.proxyServer}`);
7575
}
7676
const browser = args.browserUrl
77-
? await ensureBrowserConnected(args.browserUrl)
77+
? await ensureBrowserConnected(args.browserUrl, args.protocolTimeout)
7878
: await ensureBrowserLaunched({
7979
headless: args.headless,
8080
executablePath: args.executablePath,
@@ -85,6 +85,7 @@ async function getContext(): Promise<McpContext> {
8585
viewport: args.viewport,
8686
args: extraArgs,
8787
acceptInsecureCerts: args.acceptInsecureCerts,
88+
protocolTimeout: args.protocolTimeout,
8889
});
8990

9091
if (context?.browser !== browser) {

0 commit comments

Comments
 (0)