Skip to content

Commit fcaf553

Browse files
authored
feat: support --user-data-dir (#622)
Allow configuring Chrome's `--user-data-dir` to allow changing the defaults to support discovery of running Chrome instances later on.
1 parent 589d994 commit fcaf553

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,12 @@ The Chrome DevTools MCP server supports the following configuration option:
343343
- **Type:** string
344344

345345
- **`--isolated`**
346-
If specified, creates a temporary user-data-dir that is automatically cleaned up after the browser is closed.
346+
If specified, creates a temporary user-data-dir that is automatically cleaned up after the browser is closed. Defaults to false.
347347
- **Type:** boolean
348-
- **Default:** `false`
348+
349+
- **`--userDataDir`**
350+
Path to the user data directory for Chrome. Default is $HOME/.cache/chrome-devtools-mcp/chrome-profile$CHANNEL_SUFFIX_IF_NON_STABLE
351+
- **Type:** string
349352

350353
- **`--channel`**
351354
Specify a different Chrome channel that should be used. The default is the stable channel version.

src/cli.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,13 @@ export const cliOptions = {
8888
isolated: {
8989
type: 'boolean',
9090
description:
91-
'If specified, creates a temporary user-data-dir that is automatically cleaned up after the browser is closed.',
92-
default: false,
91+
'If specified, creates a temporary user-data-dir that is automatically cleaned up after the browser is closed. Defaults to false.',
92+
},
93+
userDataDir: {
94+
type: 'string',
95+
description:
96+
'Path to the user data directory for Chrome. Default is $HOME/.cache/chrome-devtools-mcp/chrome-profile$CHANNEL_SUFFIX_IF_NON_STABLE',
97+
conflicts: ['browserUrl', 'wsEndpoint', 'isolated'],
9398
},
9499
channel: {
95100
type: 'string',
@@ -212,6 +217,10 @@ export function parseArguments(version: string, argv = process.argv) {
212217
'Disable tools in the performance category',
213218
],
214219
['$0 --no-category-network', 'Disable tools in the network category'],
220+
[
221+
'$0 --user-data-dir=/tmp/user-data-dir',
222+
'Use a custom user data directory',
223+
],
215224
]);
216225

217226
return yargsInstance

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ async function getContext(): Promise<McpContext> {
7474
headless: args.headless,
7575
executablePath: args.executablePath,
7676
channel: args.channel as Channel,
77-
isolated: args.isolated,
77+
isolated: args.isolated ?? false,
78+
userDataDir: args.userDataDir,
7879
logFile,
7980
viewport: args.viewport,
8081
args: extraArgs,

tests/cli.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ describe('cli args parsing', () => {
2525
...defaultArgs,
2626
_: [],
2727
headless: false,
28-
isolated: false,
2928
$0: 'npx chrome-devtools-mcp@latest',
3029
channel: 'stable',
3130
});
@@ -42,14 +41,31 @@ describe('cli args parsing', () => {
4241
...defaultArgs,
4342
_: [],
4443
headless: false,
45-
isolated: false,
4644
$0: 'npx chrome-devtools-mcp@latest',
4745
'browser-url': 'http://localhost:3000',
4846
browserUrl: 'http://localhost:3000',
4947
u: 'http://localhost:3000',
5048
});
5149
});
5250

51+
it('parses with user data dir', async () => {
52+
const args = parseArguments('1.0.0', [
53+
'node',
54+
'main.js',
55+
'--user-data-dir',
56+
'/tmp/chrome-profile',
57+
]);
58+
assert.deepStrictEqual(args, {
59+
...defaultArgs,
60+
_: [],
61+
headless: false,
62+
$0: 'npx chrome-devtools-mcp@latest',
63+
channel: 'stable',
64+
'user-data-dir': '/tmp/chrome-profile',
65+
userDataDir: '/tmp/chrome-profile',
66+
});
67+
});
68+
5369
it('parses an empty browser url', async () => {
5470
const args = parseArguments('1.0.0', [
5571
'node',
@@ -61,7 +77,6 @@ describe('cli args parsing', () => {
6177
...defaultArgs,
6278
_: [],
6379
headless: false,
64-
isolated: false,
6580
$0: 'npx chrome-devtools-mcp@latest',
6681
'browser-url': undefined,
6782
browserUrl: undefined,
@@ -81,7 +96,6 @@ describe('cli args parsing', () => {
8196
...defaultArgs,
8297
_: [],
8398
headless: false,
84-
isolated: false,
8599
$0: 'npx chrome-devtools-mcp@latest',
86100
'executable-path': '/tmp/test 123/chrome',
87101
e: '/tmp/test 123/chrome',
@@ -100,7 +114,6 @@ describe('cli args parsing', () => {
100114
...defaultArgs,
101115
_: [],
102116
headless: false,
103-
isolated: false,
104117
$0: 'npx chrome-devtools-mcp@latest',
105118
channel: 'stable',
106119
viewport: {
@@ -121,7 +134,6 @@ describe('cli args parsing', () => {
121134
...defaultArgs,
122135
_: [],
123136
headless: false,
124-
isolated: false,
125137
$0: 'npx chrome-devtools-mcp@latest',
126138
channel: 'stable',
127139
'chrome-arg': ['--no-sandbox', '--disable-setuid-sandbox'],
@@ -140,7 +152,6 @@ describe('cli args parsing', () => {
140152
...defaultArgs,
141153
_: [],
142154
headless: false,
143-
isolated: false,
144155
$0: 'npx chrome-devtools-mcp@latest',
145156
'ws-endpoint': 'ws://127.0.0.1:9222/devtools/browser/abc123',
146157
wsEndpoint: 'ws://127.0.0.1:9222/devtools/browser/abc123',
@@ -159,7 +170,6 @@ describe('cli args parsing', () => {
159170
...defaultArgs,
160171
_: [],
161172
headless: false,
162-
isolated: false,
163173
$0: 'npx chrome-devtools-mcp@latest',
164174
'ws-endpoint': 'wss://example.com:9222/devtools/browser/abc123',
165175
wsEndpoint: 'wss://example.com:9222/devtools/browser/abc123',
@@ -192,7 +202,6 @@ describe('cli args parsing', () => {
192202
...defaultArgs,
193203
_: [],
194204
headless: false,
195-
isolated: false,
196205
$0: 'npx chrome-devtools-mcp@latest',
197206
channel: 'stable',
198207
'category-emulation': false,

0 commit comments

Comments
 (0)