Skip to content
Merged
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
10 changes: 10 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ chrome-devtools evaluate_script "() => self.registration.scope" --serviceWorkerI
chrome-devtools lighthouse_audit 1 --mode snapshot
```

**Logpoints:**

```sh
# Set a logpoint on page 1
chrome-devtools set_logpoints 1 "src/app.ts:42 'total', cart.total"

# Remove all logpoints on page 1
chrome-devtools set_logpoints 1
```

## Output format

By default, the CLI outputs a human-readable summary of the tool's result. For programmatic use, you can request raw JSON:
Expand Down
5 changes: 5 additions & 0 deletions scripts/generate-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface CliOption {
description: string;
required: boolean;
positional: boolean;
minItems?: number;
default?: unknown;
enum?: unknown[];
}
Expand All @@ -48,6 +49,7 @@ interface JsonSchema {
description?: string;
properties?: Record<string, JsonSchema>;
required?: string[];
minItems?: number;
default?: unknown;
enum?: unknown[];
}
Expand All @@ -73,6 +75,7 @@ function schemaToCLIOptions(toolName: string, schema: JsonSchema): CliOption[] {
required: isRequired,
positional:
isRequired || OPTIONAL_POSITIONAL_ARGS.has(`${toolName}:${name}`),
minItems: prop.minItems,
default: prop.default,
enum: prop.enum,
};
Expand Down Expand Up @@ -189,6 +192,8 @@ export interface ArgDef {
required: boolean;
/** Whether the arg is a positional on the command line, otherwise a flag. */
positional: boolean;
/** Minimum number of items for array args, from JSON Schema minItems. */
minItems?: number;
default?: string | number | boolean;
enum?: ReadonlyArray<string | number>;
}
Expand Down
2 changes: 2 additions & 0 deletions skills/chrome-devtools-cli/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ chrome-devtools list_console_messages 1 # List all console messages
chrome-devtools list_console_messages 1 --pageSize 20 --pageIdx 1 # List console messages with pagination
chrome-devtools list_console_messages 1 --types error --types info # Filter console messages by type
chrome-devtools list_console_messages 1 --includePreservedMessages true # Include preserved messages
chrome-devtools set_logpoints 1 "src/app.ts:42 'total', cart.total" # Set logpoints on page 1
chrome-devtools set_logpoints 1 # Remove all logpoints on page 1
chrome-devtools take_screenshot 1 # Take a screenshot of the page viewport
chrome-devtools take_screenshot 1 --fullPage true --format "jpeg" --quality 80 # Take a full page screenshot as JPEG with quality
chrome-devtools take_screenshot 1 --uid "id" --filePath "s.png" # Take a screenshot of an element
Expand Down
42 changes: 2 additions & 40 deletions src/bin/chrome-devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import process from 'node:process';

import type {Options, PositionalOptions} from 'yargs';

import {
startDaemon,
stopDaemon,
Expand All @@ -33,6 +31,7 @@ import {VERSION} from '../version.js';
import {
buildCommand,
getCommandArgSpec,
registerCommandArgs,
shieldLiteralArgs,
} from '../config/cli-commands.js';
import {commands} from '../config/cli-options.js';
Expand Down Expand Up @@ -216,44 +215,7 @@ for (const [commandName, commandDef] of Object.entries(commands)) {
choices: ['md', 'json'],
default: 'md',
});
for (const [argName, opt] of Object.entries(args)) {
const type =
opt.type === 'integer' || opt.type === 'number'
? 'number'
: opt.type === 'boolean'
? 'boolean'
: opt.type === 'array'
? 'array'
: 'string';

if (opt.positional) {
const options: PositionalOptions = {
describe: opt.description,
type: type as PositionalOptions['type'],
};
if (opt.default !== undefined) {
options.default = opt.default;
}
if (opt.enum) {
options.choices = opt.enum as Array<string | number>;
}
y.positional(argName, options);
} else {
const options: Options = {
describe: opt.description,
type: type as Options['type'],
};
if (opt.default !== undefined) {
// Document the default in --help without populating argv, so the
// daemon applies its current tool schema and config defaults.
options.defaultDescription = JSON.stringify(opt.default);
}
if (opt.enum) {
options.choices = opt.enum as Array<string | number>;
}
y.option(argName, options);
}
}
registerCommandArgs(y, args);
},
async argv => {
const sessionId = argv.sessionId as string;
Expand Down
63 changes: 61 additions & 2 deletions src/config/cli-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

import type {Argv, Options, PositionalOptions} from 'yargs';

import type {ArgDef} from './cli-options.js';

/**
Expand All @@ -13,6 +15,9 @@ import type {ArgDef} from './cli-options.js';
* part of the command string yargs parses each one as a trailing positional,
* and a variadic positional has to be the last one. Which args are positional
* is decided at build time by `scripts/generate-cli.ts`.
*
* A required positional array without `minItems > 0` is rendered as `[name..]`
* so omitting it passes an empty array instead of failing yargs validation.
*/
export function buildCommand(
commandName: string,
Expand All @@ -23,16 +28,70 @@ export function buildCommand(
for (const [name, arg] of Object.entries(args)) {
if (!arg.positional) {
flags += ` [--${name}]`;
} else if (arg.type === 'array') {
const requiresItem = arg.required && (arg.minItems ?? 0) > 0;
command += requiresItem ? ` <${name}..>` : ` [${name}..]`;
} else if (arg.required) {
command += arg.type === 'array' ? ` <${name}..>` : ` <${name}>`;
command += ` <${name}>`;
} else {
command += arg.type === 'array' ? ` [${name}..]` : ` [${name}]`;
command += ` [${name}]`;
}
}

return {command, usage: `$0 ${command}${flags}`};
}

/** Maps a non-array JSON Schema type to the yargs type that parses it. */
function toYargsScalarType(type: string): 'number' | 'boolean' | 'string' {
if (type === 'integer' || type === 'number') {
return 'number';
}
return type === 'boolean' ? 'boolean' : 'string';
}

/**
* Registers positional and flag definitions for a tool command on a yargs
* builder instance.
*/
export function registerCommandArgs(
y: Argv,
args: Record<string, ArgDef>,
): void {
for (const [argName, opt] of Object.entries(args)) {
if (opt.positional) {
const options: PositionalOptions = {
describe: opt.description,
};
if (opt.type === 'array') {
options.array = true;
} else {
options.type = toYargsScalarType(opt.type);
}
if (opt.default !== undefined) {
options.default = opt.default;
}
if (opt.enum) {
options.choices = opt.enum;
}
y.positional(argName, options);
} else {
const options: Options = {
describe: opt.description,
type: opt.type === 'array' ? 'array' : toYargsScalarType(opt.type),
};
if (opt.default !== undefined) {
// Document the default in --help without populating argv, so the
// daemon applies its current tool schema and config defaults.
options.defaultDescription = JSON.stringify(opt.default);
}
if (opt.enum) {
options.choices = opt.enum;
}
y.option(argName, options);
}
}
}

/**
* How an option takes its value. `text` is a free-form string, which can start
* with `-`. `value` is any other single value: a number or one of a fixed set
Expand Down
3 changes: 3 additions & 0 deletions src/config/cli-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface ArgDef {
required: boolean;
/** Whether the arg is a positional on the command line, otherwise a flag. */
positional: boolean;
/** Minimum number of items for array args, from JSON Schema minItems. */
minItems?: number;
default?: string | number | boolean;
enum?: ReadonlyArray<string | number>;
}
Expand Down Expand Up @@ -2297,6 +2299,7 @@ export const commands: Commands = {
'One or more files paths to upload. File paths have to be local to the browser instance (not the MCP).',
required: true,
positional: true,
minItems: 1,
},
includeSnapshot: {
name: 'includeSnapshot',
Expand Down
Loading