1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use anyhow::Result;
use browser_control::cli::{
agent_instructions, cookies, eval, fetch, list, set, show, storage, targets as cli_targets,
wait, wait_for_cookie,
};
use clap::{Parser, Subcommand};
#[derive(Parser, Debug)]
#[command(name = "browser-control", version, about, long_about = None, arg_required_else_help = true)]
struct Cli {
/// Print canonical instructions for agents using browser-control.
#[arg(long)]
agent_instructions: bool,
#[command(subcommand)]
command: Option<Command>,
}
#[derive(Subcommand, Debug)]
enum Command {
/// List browsers installed on this machine.
ListInstalled {
#[arg(long)]
json: bool,
},
/// List browsers currently registered and alive.
ListRunning {
#[arg(long)]
json: bool,
},
/// Start a browser and register it.
Start {
/// Browser kind (chrome, edge, chromium, brave, firefox) or friendly name.
browser: Option<String>,
#[arg(long)]
headless: bool,
/// Do not wait for the browser's debugging endpoint to be reachable.
#[arg(long)]
no_wait: bool,
/// Seconds to wait for the endpoint when not using --no-wait.
#[arg(long, default_value_t = 30)]
wait_timeout: u64,
#[arg(long)]
json: bool,
},
/// Explicitly reveal the selected browser for login or debugging.
Show {
/// Browser to reveal. Defaults to the configured default browser.
#[arg(long, short = 'b', env = "BROWSER_CONTROL")]
browser: Option<String>,
#[arg(long)]
json: bool,
},
/// Start the MCP server on stdio.
Mcp {
/// Browser to target. Overrides $BROWSER_CONTROL.
#[arg(long, short = 'b', env = "BROWSER_CONTROL")]
browser: Option<String>,
/// Override the `playwright-core` version used by the internal
/// Playwright sidecar (defaults to the pinned version in the
/// bundled `package.json`). Useful for picking up an upstream
/// API change without waiting for a browser-control release.
#[arg(long)]
playwright_version: Option<String>,
},
/// List page targets in the active browser.
Targets {
#[arg(long, short = 'b', env = "BROWSER_CONTROL")]
browser: Option<String>,
/// Filter pages whose URL matches this regex.
#[arg(long)]
url: Option<String>,
#[arg(long)]
json: bool,
},
/// Export cookies from the active browser.
Cookies {
#[arg(long, short = 'b', env = "BROWSER_CONTROL")]
browser: Option<String>,
/// Filter by cookie domain regex.
#[arg(long)]
domain: Option<String>,
/// Filter by cookie name regex.
#[arg(long)]
name: Option<String>,
/// Output format.
#[arg(long, default_value = "json")]
format: String,
/// Write to FILE (chmod 0600) instead of stdout.
#[arg(long, short = 'o')]
output: Option<std::path::PathBuf>,
/// Print cookie values in human output (default: redacted).
#[arg(long)]
reveal: bool,
#[arg(long)]
json: bool,
},
/// Run an HTTP request from the browser page context.
Fetch {
#[arg(long, short = 'b', env = "BROWSER_CONTROL")]
browser: Option<String>,
url: String,
#[arg(long, short = 'X', default_value = "GET")]
method: String,
/// Repeat -H to add multiple headers; format `Key: Value`.
#[arg(long = "header", short = 'H')]
headers: Vec<String>,
#[arg(long, short = 'd')]
data: Option<String>,
/// Select a page target by URL regex (default: first page).
#[arg(long)]
target: Option<String>,
/// Prepend HTTP status + headers to stdout, like `curl -i`.
#[arg(long, short = 'i')]
include: bool,
#[arg(long, short = 'o')]
output: Option<std::path::PathBuf>,
/// Per-call timeout in milliseconds for the in-page fetch. Default
/// 60 s is generous for slow networks; lower bounds catch wedged
/// renderers faster.
#[arg(long, default_value_t = 60_000)]
timeout_ms: u64,
/// Reload the page first if its document is older than this duration.
#[arg(long, default_value = browser_control::session::freshness::DEFAULT_MAX_AGE_STR)]
max_age: String,
},
/// Read or write localStorage / sessionStorage.
Storage {
#[command(subcommand)]
action: storage::StorageCmd,
},
/// Evaluate a JavaScript expression in the active page.
Eval {
#[arg(long, short = 'b', env = "BROWSER_CONTROL")]
browser: Option<String>,
/// JavaScript expression.
expression: String,
#[arg(long)]
target: Option<String>,
/// Print the full Runtime.evaluate envelope.
#[arg(long)]
json: bool,
/// Treat the expression as a Promise and await it.
#[arg(long, default_value_t = true)]
await_promise: bool,
/// Per-call timeout in milliseconds. The default catches wedged
/// renderers (service-worker-paused tabs, busy event loops, embedded
/// admin UIs that ignore Runtime.evaluate) so the CLI fails fast
/// instead of dragging through the upstream 30 s protocol timeout.
#[arg(long, default_value_t = 10_000)]
timeout_ms: u64,
/// Reload the page first if its document is older than this duration.
#[arg(long, default_value = browser_control::session::freshness::DEFAULT_MAX_AGE_STR)]
max_age: String,
},
/// Wait until the browser endpoint is reachable.
Wait {
#[arg(long, short = 'b', env = "BROWSER_CONTROL")]
browser: Option<String>,
/// Deprecated no-op kept for backward compatibility. Readiness is now
/// the default (and only) mode.
#[arg(long, hide = true)]
ready: bool,
#[arg(long, default_value_t = 30)]
timeout: u64,
},
/// Wait until a cookie matching the filter appears.
WaitForCookie {
#[arg(long, short = 'b', env = "BROWSER_CONTROL")]
browser: Option<String>,
#[arg(long)]
domain: String,
#[arg(long)]
name: String,
#[arg(long, default_value_t = 120)]
timeout: u64,
#[arg(long, default_value_t = 1)]
poll_interval: u64,
/// After the cookie appears, GET this URL through the page and require 2xx.
#[arg(long)]
validate_url: Option<String>,
/// Reload the validation page first if its document is older than this duration.
#[arg(long, default_value = browser_control::session::freshness::DEFAULT_MAX_AGE_STR)]
max_age: String,
},
/// Set a persistent setting (e.g. `set default firefox`).
Set {
#[arg(value_enum)]
key: set::Key,
/// Value to assign. Omit and use `unset` instead to clear.
value: Option<String>,
#[arg(long)]
json: bool,
},
/// Print a persistent setting.
Get {
#[arg(value_enum)]
key: set::Key,
#[arg(long)]
json: bool,
},
/// Clear a persistent setting.
Unset {
#[arg(value_enum)]
key: set::Key,
#[arg(long)]
json: bool,
},
/// Open or navigate tabs: `tab open <browser>/<name> <url>` (re-running
/// with a new url navigates the existing tab), `tab list`, `tab adopt`.
/// This is the way to go to a URL — do not navigate by evaling
/// `location.href`. Addressable as `--browser <browser>/<name>` in
/// page-context commands.
Tab {
#[command(subcommand)]
cmd: browser_control::cli::tab::TabCmd,
},
}
fn init_tracing() {
let filter = tracing_subscriber::EnvFilter::try_from_env("BROWSER_CONTROL_LOG")
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
tracing_subscriber::fmt()
.with_env_filter(filter)
.with_writer(std::io::stderr)
.init();
}
#[tokio::main]
async fn main() -> Result<()> {
init_tracing();
let cli = Cli::parse();
if cli.agent_instructions {
agent_instructions::print();
return Ok(());
}
let Some(command) = cli.command else {
return Ok(());
};
match command {
Command::ListInstalled { json } => list::run_list_installed(json),
Command::ListRunning { json } => list::run_list_running(json).await,
Command::Start {
browser,
headless,
no_wait,
wait_timeout,
json,
} => browser_control::cli::start::run(browser, headless, no_wait, wait_timeout, json).await,
Command::Show { browser, json } => show::run(browser, json).await,
Command::Mcp {
browser,
playwright_version,
} => browser_control::cli::mcp::run_cli(browser, playwright_version).await,
Command::Set { key, value, json } => set::run_set(key, value, json),
Command::Get { key, json } => set::run_get(key, json),
Command::Unset { key, json } => set::run_unset(key, json),
Command::Targets { browser, url, json } => cli_targets::run(browser, url, json).await,
Command::Cookies {
browser,
domain,
name,
format,
output,
reveal,
json,
} => cookies::run(browser, domain, name, format, output, reveal, json).await,
Command::Fetch {
browser,
url,
method,
headers,
data,
target,
include,
output,
timeout_ms,
max_age,
} => {
fetch::run(
browser, url, method, headers, data, target, include, output, timeout_ms, max_age,
)
.await
}
Command::Storage { action } => storage::run(action).await,
Command::Eval {
browser,
expression,
target,
json,
await_promise,
timeout_ms,
max_age,
} => {
eval::run(
browser,
expression,
target,
json,
await_promise,
timeout_ms,
max_age,
)
.await
}
Command::Wait {
browser,
ready,
timeout,
} => wait::run(browser, ready, timeout).await,
Command::WaitForCookie {
browser,
domain,
name,
timeout,
poll_interval,
validate_url,
max_age,
} => {
wait_for_cookie::run(
browser,
domain,
name,
timeout,
poll_interval,
validate_url,
max_age,
)
.await
}
Command::Tab { cmd } => browser_control::cli::tab::run(cmd).await,
}
}