豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 6c554ad

Browse files
committed
chore: configure cli args
1 parent dc53fa3 commit 6c554ad

File tree

7 files changed

+48
-13
lines changed

7 files changed

+48
-13
lines changed

docs/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The CLI acts as a client to a background `chrome-devtools-mcp` daemon (uses Unix
1717

1818
- **Automatic Start**: The first time you call a tool (e.g., `list_pages`), the CLI automatically starts the MCP server and the browser in the background if they aren't already running.
1919
- **Persistence**: The same background instance is reused for subsequent commands, preserving the browser state (open pages, cookies, etc.).
20-
- **Manual Control**: You can explicitly manage the background process using `start`, `stop`, and `status`. The `start` command forwards all subsequent arguments to the underlying MCP server (e.g., `--headless`, `--userDataDir`).
20+
- **Manual Control**: You can explicitly manage the background process using `start`, `stop`, and `status`. The `start` command forwards all subsequent arguments to the underlying MCP server (e.g., `--headless`, `--userDataDir`) but not all args are supported. Run `chrome-devtools start --help` for supported args. Headless and isolated are enabled by default.
2121

2222
```sh
2323
# Check if the daemon is running

src/bin/chrome-devtools.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@ async function start(args: string[]) {
3232

3333
const defaultArgs = ['--viaCli', '--experimentalStructuredContent'];
3434

35+
const startCliOptions = {
36+
...cliOptions,
37+
} as Partial<typeof cliOptions>;
38+
39+
// Not supported in CLI on purpose.
40+
delete startCliOptions.autoConnect;
41+
// Missing CLI serialization.
42+
delete startCliOptions.viewport;
43+
// CLI is generated based on the default tool definitions. To enable conditional
44+
// tools, they needs to be enabled during CLI generation.
45+
delete startCliOptions.experimentalPageIdRouting;
46+
delete startCliOptions.experimentalVision;
47+
delete startCliOptions.experimentalInteropTools;
48+
delete startCliOptions.experimentalScreencast;
49+
delete startCliOptions.categoryEmulation;
50+
delete startCliOptions.categoryPerformance;
51+
delete startCliOptions.categoryNetwork;
52+
delete startCliOptions.categoryExtensions;
53+
// Always on in CLI.
54+
delete startCliOptions.experimentalStructuredContent;
55+
// Change the defaults.
56+
if (!('default' in cliOptions.headless)) {
57+
throw new Error('headless cli option unexpectedly does not have a default');
58+
}
59+
if ('default' in cliOptions.isolated) {
60+
throw new Error('headless cli option unexpectedly does not have a default');
61+
}
62+
startCliOptions.headless!.default = true;
63+
3564
const y = yargs(hideBin(process.argv))
3665
.scriptName('chrome-devtools')
3766
.showHelpOnFail(true)
@@ -50,7 +79,7 @@ y.command(
5079
'Start or restart chrome-devtools-mcp',
5180
y =>
5281
y
53-
.options(cliOptions)
82+
.options(startCliOptions)
5483
.example(
5584
'$0 start --browserUrl http://localhost:9222',
5685
'Start the server connecting to an existing browser',
@@ -60,6 +89,13 @@ y.command(
6089
if (isDaemonRunning()) {
6190
await stopDaemon();
6291
}
92+
// Defaults but we do not want to affect the yargs conflict resolution.
93+
if (argv.isolated === undefined) {
94+
argv.isolated = true;
95+
}
96+
if (argv.headless === undefined) {
97+
argv.headless = true;
98+
}
6399
const args = serializeArgs(cliOptions, argv);
64100
await start(args);
65101
process.exit(0);

src/browser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ interface McpLaunchOptions {
149149
ignoreDefaultChromeArgs?: string[];
150150
devtools: boolean;
151151
enableExtensions?: boolean;
152+
viaCli?: boolean;
152153
}
153154

154155
export function detectDisplay(): void {
@@ -181,7 +182,7 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
181182
userDataDir = path.join(
182183
os.homedir(),
183184
'.cache',
184-
'chrome-devtools-mcp',
185+
options.viaCli ? 'chrome-devtools-mcp-cli' : 'chrome-devtools-mcp',
185186
profileDirName,
186187
);
187188
await fs.promises.mkdir(userDataDir, {

src/daemon/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export async function startDaemon(mcpArgs: string[] = []) {
7979
fs.unlinkSync(pidFilePath);
8080
}
8181

82-
logger('Starting daemon...');
82+
logger('Starting daemon...', ...mcpArgs);
8383
const child = spawn(process.execPath, [DAEMON_SCRIPT_PATH, ...mcpArgs], {
8484
detached: true,
8585
stdio: 'ignore',

src/daemon/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import os from 'node:os';
99
import path from 'node:path';
1010
import process from 'node:process';
1111

12-
import type {ParsedArguments} from '../cli.js';
1312
import {logger} from '../logger.js';
1413
import type {YargsOptions} from '../third_party/index.js';
1514

@@ -102,7 +101,7 @@ export function isDaemonRunning(pid = getDaemonPid()): pid is number {
102101

103102
export function serializeArgs(
104103
options: Record<string, YargsOptions>,
105-
argv: ParsedArguments,
104+
argv: Record<string, unknown>,
106105
): string[] {
107106
const args: string[] = [];
108107
for (const key of Object.keys(options)) {

src/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export async function createMcpServer(
9393
acceptInsecureCerts: serverArgs.acceptInsecureCerts,
9494
devtools,
9595
enableExtensions: serverArgs.categoryExtensions,
96+
viaCli: serverArgs.viaCli,
9697
});
9798

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

tests/e2e/chrome-devtools.test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {describe, it, afterEach, beforeEach} from 'node:test';
1212
const CLI_PATH = path.resolve('build/src/bin/chrome-devtools.js');
1313

1414
describe('chrome-devtools', () => {
15-
const START_ARGS = ['--headless', '--isolated'];
16-
1715
function assertDaemonIsNotRunning() {
1816
const result = spawnSync('node', [CLI_PATH, 'status']);
1917
assert.strictEqual(
@@ -45,7 +43,7 @@ describe('chrome-devtools', () => {
4543
it('reports daemon status correctly', () => {
4644
assertDaemonIsNotRunning();
4745

48-
const startResult = spawnSync('node', [CLI_PATH, 'start', ...START_ARGS]);
46+
const startResult = spawnSync('node', [CLI_PATH, 'start']);
4947
assert.strictEqual(
5048
startResult.status,
5149
0,
@@ -58,7 +56,7 @@ describe('chrome-devtools', () => {
5856
it('can start and stop the daemon', () => {
5957
assertDaemonIsNotRunning();
6058

61-
const startResult = spawnSync('node', [CLI_PATH, 'start', ...START_ARGS]);
59+
const startResult = spawnSync('node', [CLI_PATH, 'start']);
6260
assert.strictEqual(
6361
startResult.status,
6462
0,
@@ -80,7 +78,7 @@ describe('chrome-devtools', () => {
8078
it('can invoke list_pages', async () => {
8179
assertDaemonIsNotRunning();
8280

83-
const startResult = spawnSync('node', [CLI_PATH, 'start', ...START_ARGS]);
81+
const startResult = spawnSync('node', [CLI_PATH, 'start']);
8482
assert.strictEqual(
8583
startResult.status,
8684
0,
@@ -102,7 +100,7 @@ describe('chrome-devtools', () => {
102100
});
103101

104102
it('can take screenshot', async () => {
105-
const startResult = spawnSync('node', [CLI_PATH, 'start', ...START_ARGS]);
103+
const startResult = spawnSync('node', [CLI_PATH, 'start']);
106104
assert.strictEqual(
107105
startResult.status,
108106
0,
@@ -122,7 +120,7 @@ describe('chrome-devtools', () => {
122120
});
123121

124122
it('forwards disclaimers to stderr on start', () => {
125-
const result = spawnSync('node', [CLI_PATH, 'start', ...START_ARGS]);
123+
const result = spawnSync('node', [CLI_PATH, 'start']);
126124
assert.strictEqual(
127125
result.status,
128126
0,

0 commit comments

Comments
 (0)