chrome-cdp
by ethandong16
README.md
# chrome-cdp
Supervise a local Chrome or Chromium process over the Chrome DevTools Protocol (CDP), with a built-in stdio MCP bridge. Playwright, Puppeteer, scripts, and MCP agents share one browser process on Linux and macOS.
```bash
chrome-cdp start
chrome-cdp status
```
`start` launches Chrome in the background and records its state. Concurrent `start` or `mcp` calls wait on an exclusive file lock and reuse the running browser. `stop` checks process ownership against the recorded profile before signaling the process group, so stale PIDs never touch unrelated processes and no child processes stay behind.
The package uses only the Python 3.10+ standard library. By default it binds to `127.0.0.1`, keeps the Chrome sandbox on, sets state directory permissions to `0700` and state files to `0600`, rotates `chrome.log` at 5 MiB, and disables `chrome-devtools-mcp` telemetry and CrUX requests.
## Installation
You need Python 3.10+ and a local Chrome, Chromium, Brave, or Edge binary. The `mcp` subcommand also needs Node.js (`npx`) or a local `chrome-devtools-mcp` binary.
With [`uv`](https://docs.astral.sh/uv/):
```bash
uv tool install chrome-cdp
```
From source:
```bash
git clone https://github.com/ethandong16/chrome-cdp.git
cd chrome-cdp
uv tool install --editable .
```
## Commands
```bash
chrome-cdp start # Start Chrome in the background (default 127.0.0.1:9222)
chrome-cdp start --console # Run in the foreground; Ctrl-C or SIGTERM stops cleanly
chrome-cdp stop # Stop Chrome and its entire process group
chrome-cdp restart # Stop (if running) and start with new options
chrome-cdp status # Show PID, browser version, endpoints, mode, profile, log
chrome-cdp status --json # Emit machine-readable JSON status
chrome-cdp url # Print the HTTP CDP endpoint (for shell composition)
chrome-cdp url --ws # Print the WebSocket debugger URL
chrome-cdp logs [-n 50] [-f] # Inspect or follow chrome.log
chrome-cdp doctor [--json] # Verify Chrome, state directory, port, and MCP tooling
chrome-cdp mcp # Ensure Chrome is running and exec chrome-devtools-mcp on stdio
```
### Run flags (`start`, `restart`, `mcp`)
| Flag | Environment variable | Description |
| --- | --- | --- |
| `--port N` | `CHROME_CDP_PORT` | CDP port (default `9222`; `0` picks any free port) |
| `--bind ADDR` | `CHROME_CDP_BIND` | Listen address (default `127.0.0.1`; supports IPv6) |
| `--headed` | — | Show the browser window |
| `--profile DIR` | `CHROME_CDP_PROFILE` | Chrome `--user-data-dir` to reuse |
| `--chrome PATH` | `CHROME_CDP_CHROME` / `CHROME_PATH` | Browser executable path |
| `--console` | — | Run in the foreground (`start` and `restart` only) |
| — | `CHROME_CDP_ARGS` | Extra Chrome flags (shell-split; use `--flag=value`) |
| — | `CHROME_CDP_HOME` | State directory (default `$XDG_STATE_HOME/chrome-cdp`) |
## Connecting clients
### Playwright (Python)
```python
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp("http://127.0.0.1:9222")
page = browser.contexts[0].new_page()
page.goto("https://example.com")
print(page.title())
page.close()
```
### MCP clients
Works with Claude, Codex, Cursor, OpenCode, and other stdio MCP clients:
```json
{
"mcpServers": {
"chrome-cdp": {
"command": "chrome-cdp",
"args": ["mcp"]
}
}
}
```
`chrome-cdp mcp` starts the browser if it is not running, then execs `chrome-devtools-mcp` against it with `--no-usage-statistics` and `--no-performance-crux`.
Additional MCP options:
- `--mcp-package SPEC` or `CHROME_CDP_MCP_PACKAGE` overrides the pinned npm package (default `chrome-devtools-mcp@1.15.0`).
- `CHROME_CDP_MCP_BIN` points to a local `chrome-devtools-mcp` binary so `mcp` runs offline without `npx`.
- `--usage-statistics` and `--performance-crux` turn telemetry or CrUX lookups back on.
- `-- <args...>` forwards extra arguments to `chrome-devtools-mcp`.
Async Playwright, Node.js, Puppeteer, SSH tunnels, systemd, macOS `launchd`, and multi-instance examples are in [docs/integration.md](docs/integration.md).
## State files
State lives in `~/.local/state/chrome-cdp/` (mode `0700`):
- `state.json` (`0600`): PID, port, bind address, WebSocket URL, profile, and log path.
- `chrome.log` (`0600`): browser stdout and stderr, rotated to `chrome.log.1` at 5 MiB.
- `chrome-profile/`: isolated Chrome user data directory.
Stop the server and delete this directory to reset everything.
## Security
CDP has no authentication. Any process that can reach the port can drive the browser and read cookies from the profile. Keep the default `127.0.0.1` bind and use an SSH tunnel for remote access. See [SECURITY.md](SECURITY.md).
## Development
```bash
uv sync
uv run pytest
uv run ruff check .
uv run mypy
```
## License
[MIT](LICENSE)
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues