blob: f7377182e7cca4c6a2048c77eabcb7fa032bffc9 [file] [view] [edit]
# Chrome DevTools CLI
The `chrome-devtools-mcp` package includes a CLI interface that allows you to interact with the browser directly from your terminal. This is particularly useful for debugging or when you want an agent to generate scripts that automate browser actions.
## Getting started
Install the package globally to make the `chrome-devtools` command available:
```sh
npm i chrome-devtools-mcp@latest -g
chrome-devtools status # check if install worked.
```
The CLI enables unrestricted filesystem access by default. Use `--workspace`
to limit file tools to specific directories. Repeat the flag for more than one
directory.
`--allow-unrestricted-paths` still works on its own for compatibility, but it
cannot be combined with `--workspace`.
```sh
chrome-devtools start --workspace=/path/to/project --workspace=/path/to/output
```
## Agent skill
The [`chrome-devtools-cli`](../skills/chrome-devtools-cli) skill teaches your agent how to use the CLI. Plugin installs include this skill. To install it manually, copy the skill folder into the skills directory of your agent:
```sh
# Example for Claude Code. Use the skills directory of your agent.
mkdir -p ~/.claude/skills
cp -R "$(npm root -g)/chrome-devtools-mcp/skills/chrome-devtools-cli" ~/.claude/skills/
```
See [Skills](../README.md#skills) for all skills and installation options.
> [!NOTE]
> Copy the skill again each time you update the CLI. See [Keep skills up to date](../README.md#keep-skills-up-to-date).
## How it works
The CLI acts as a client to a background `chrome-devtools-mcp` daemon (uses Unix sockets on Linux/Mac and named pipes on Windows).
- **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.
- **Persistence**: The same background instance is reused for subsequent commands, preserving the browser state (open pages, cookies, etc.).
- **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 is enabled by default. Isolated is enabled by default unless `--userDataDir` is provided.
- **Idle Timeout**: By default, the daemon runs until you call `stop`. Start it with `--idleTimeout` and a duration in seconds, minutes or hours (e.g., `chrome-devtools start --idleTimeout=30m`, or `90s`, `2h`) to close the browser it launched (along with all open pages) after that long without a tool call, or disconnect while leaving the browser open when using `--browserUrl`, `--wsEndpoint` or `--autoConnect`, and exit the daemon shortly afterwards (`status` does not count as a tool call). The next tool command starts a new daemon with only the default flags and the discovered config file: flags passed to `start` apply only to that daemon, so re-run `start` with your flags if you need them, or set `"idleTimeout": "30m"` in a discovered [configuration file](./configuration.md#configuration-file) to apply it to every daemon.
```sh
# Check if the daemon is running
chrome-devtools status
# Navigate page 1 to a URL
chrome-devtools navigate_page 1 --url "https://google.com"
# Take a screenshot of page 1 and save it to a file
chrome-devtools take_screenshot 1 --filePath screenshot.png
# Stop the background daemon when finished
chrome-devtools stop
```
## Command Usage
The CLI supports all tools available in the MCP server even if they require a flag to activate in the MCP server (see [Tool reference](./tool-reference.md)).
```sh
chrome-devtools <tool> [arguments] [flags]
```
- **Required Arguments**: Passed as positional arguments. Page-scoped tools require `<pageId>` as their first positional argument.
- **Optional Arguments**: Passed as flags (e.g., `--filePath`, `--fullPage`).
### Examples
**New Page and Navigation:**
```sh
chrome-devtools new_page "https://example.com"
chrome-devtools navigate_page 1 --url "https://web.dev"
```
**Interaction:**
```sh
# Click an element by its UID from a snapshot on page 1
chrome-devtools click 1 "element-uid-123"
# Fill a form field on page 1
chrome-devtools fill 1 "input-uid-456" "search query"
```
**Script Evaluation:**
- When `--categoryExtensions` and `--pageIdRouting` are enabled:
- Target a page using `--pageId <number>`: `chrome-devtools evaluate_script "() => document.title" --pageId 1`
- Target an extension service worker using `--serviceWorkerId <string>`: `chrome-devtools evaluate_script "() => self.registration.scope" --serviceWorkerId sw-1`
```sh
# Evaluate a JavaScript expression on page 1
chrome-devtools evaluate_script "() => document.title" --pageId 1
# Evaluate inside an extension service worker
chrome-devtools evaluate_script "() => self.registration.scope" --serviceWorkerId sw-1
```
**Analysis:**
```sh
# Run a Lighthouse audit on page 1 (defaults to navigation mode)
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:
```sh
chrome-devtools list_pages --output-format=json
```
## Timeouts
The CLI waits up to 180 seconds for a tool call. Pass `--rpc-timeout` with a duration in seconds, minutes or hours (e.g., `90s`, `5m`, `1h`) to wait for a different time, for example for a Lighthouse audit of a heavy page or a large heap snapshot:
```sh
chrome-devtools lighthouse_audit 1 --rpc-timeout=5m
```
A tool's own `--timeout` does not change how long the CLI waits. For `--timeout` values close to or above 180 seconds, also pass a longer `--rpc-timeout`.
When the time runs out, or when you stop the CLI (e.g., with Ctrl+C), the daemon cancels the tool call. A call that still waits for its turn or for the browser does not run. A tool that is already running is not interrupted: its result is discarded, and later calls that cannot run at the same time wait until it finishes. A cancelled call no longer counts as activity for `--idleTimeout`.
## Troubleshooting
If the CLI hangs or fails to connect, try stopping the background process:
```sh
chrome-devtools stop
```
For more verbose logs, set the `NODE_DEBUG` environment variable. The background
process does not write to the terminal, so restart it with `--logFile` to
capture its logs in a file:
```sh
NODE_DEBUG=* chrome-devtools start --logFile=/tmp/chrome-devtools.log
chrome-devtools list_pages
```
## CLI generation
Implemented in `scripts/generate-cli.ts`. Some commands are excluded from CLI
generation such as `wait_for` and `fill_form`.
`chrome-devtools-mcp` args are also filtered in `src/bin/chrome-devtools.ts`
because not all args make sense in a CLI interface.
## Sessions
By default, all commands share one daemon and browser. Pass `--sessionId=<id>`
or set `CHROME_DEVTOOLS_SESSION_ID` to scope `start`, `status`, `stop`, and
tool calls to an isolated daemon and browser. Session IDs use hex digits and
dashes (such as a UUID); `--sessionId` overrides `CHROME_DEVTOOLS_SESSION_ID`.
```sh
SESSION_ID=$(uuidgen)
chrome-devtools start --sessionId="$SESSION_ID"
chrome-devtools list_pages --sessionId="$SESSION_ID"
chrome-devtools stop --sessionId="$SESSION_ID"
```