Skip to content

Commit a32e8f2

Browse files
committed
feat: support --auto-connect
1 parent 4cd294f commit a32e8f2

5 files changed

Lines changed: 32 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ The Chrome DevTools MCP server supports the following configuration option:
350350

351351
<!-- BEGIN AUTO GENERATED OPTIONS -->
352352

353+
- **`--autoConnect`**
354+
If specified, automatically connects to a server running in the user data directory identified by the channel param
355+
- **Type:** boolean
356+
353357
- **`--browserUrl`, `-u`**
354358
Connect to a running, debuggable Chrome instance (e.g. `http://127.0.0.1:9222`). For more details see: https://github.com/ChromeDevTools/chrome-devtools-mcp#connecting-to-a-running-chrome-instance.
355359
- **Type:** string

src/browser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export async function ensureBrowserConnected(options: {
4848
wsEndpoint?: string;
4949
wsHeaders?: Record<string, string>;
5050
devtools: boolean;
51+
channel?: Channel;
5152
}) {
53+
const {channel} = options;
5254
if (browser?.connected) {
5355
return browser;
5456
}
@@ -66,6 +68,12 @@ export async function ensureBrowserConnected(options: {
6668
}
6769
} else if (options.browserURL) {
6870
connectOptions.browserURL = options.browserURL;
71+
} else if (channel) {
72+
const puppeteerChannel =
73+
channel && channel !== 'stable'
74+
? (`chrome-${channel}` as ChromeReleaseChannel)
75+
: 'chrome';
76+
connectOptions.channel = puppeteerChannel;
6977
} else {
7078
throw new Error('Either browserURL or wsEndpoint must be provided');
7179
}

src/cli.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import type {YargsOptions} from './third_party/index.js';
88
import {yargs, hideBin} from './third_party/index.js';
99

1010
export const cliOptions = {
11+
autoConnect: {
12+
type: 'boolean',
13+
description:
14+
'If specified, automatically connects to a server running in the user data directory identified by the channel param',
15+
conflicts: ['isolated', 'executablePath', 'userDataDir'],
16+
},
1117
browserUrl: {
1218
type: 'string',
1319
description:

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ async function getContext(): Promise<McpContext> {
5454
}
5555
const devtools = args.experimentalDevtools ?? false;
5656
const browser =
57-
args.browserUrl || args.wsEndpoint
57+
args.browserUrl || args.wsEndpoint || args.autoConnect
5858
? await ensureBrowserConnected({
5959
browserURL: args.browserUrl,
6060
wsEndpoint: args.wsEndpoint,
6161
wsHeaders: args.wsHeaders,
62+
channel: args.autoConnect ? (args.channel as Channel) : undefined,
6263
devtools,
6364
})
6465
: await ensureBrowserLaunched({

tests/cli.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,16 @@ describe('cli args parsing', () => {
208208
categoryEmulation: false,
209209
});
210210
});
211+
it('parses auto-connect', async () => {
212+
const args = parseArguments('1.0.0', ['node', 'main.js', '--auto-connect']);
213+
assert.deepStrictEqual(args, {
214+
...defaultArgs,
215+
_: [],
216+
headless: false,
217+
$0: 'npx chrome-devtools-mcp@latest',
218+
channel: 'stable',
219+
'auto-connect': true,
220+
autoConnect: true,
221+
});
222+
});
211223
});

0 commit comments

Comments
 (0)