bidi-browser-mcp 0.1.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +398 -343
  3. package/dist/bin.js +3 -2
  4. package/dist/config.js +4 -0
  5. package/dist/connection/bidi-client.d.ts +13 -1
  6. package/dist/connection/bidi-client.js +59 -8
  7. package/dist/connection/browser-discovery.d.ts +20 -0
  8. package/dist/connection/browser-discovery.js +76 -12
  9. package/dist/connection/chromium-mapper.d.ts +9 -0
  10. package/dist/connection/chromium-mapper.js +24 -0
  11. package/dist/connection/event-pipeline.d.ts +15 -2
  12. package/dist/connection/event-pipeline.js +45 -3
  13. package/dist/connection/protocol-probe.d.ts +43 -0
  14. package/dist/connection/protocol-probe.js +112 -0
  15. package/dist/index.js +6 -0
  16. package/dist/session/session-manager.d.ts +26 -1
  17. package/dist/session/session-manager.js +262 -6
  18. package/dist/snapshot/cache.d.ts +4 -0
  19. package/dist/snapshot/cache.js +8 -0
  20. package/dist/snapshot/dom-snapshot.js +190 -181
  21. package/dist/snapshot/element-script.d.ts +23 -0
  22. package/dist/snapshot/element-script.js +70 -0
  23. package/dist/snapshot/token-optimizer.js +26 -8
  24. package/dist/snapshot/uid-verify.d.ts +43 -0
  25. package/dist/snapshot/uid-verify.js +154 -0
  26. package/dist/tools/browser.js +21 -6
  27. package/dist/tools/context.js +4 -6
  28. package/dist/tools/interact.d.ts +1 -0
  29. package/dist/tools/interact.js +593 -0
  30. package/dist/tools/network.d.ts +1 -0
  31. package/dist/tools/network.js +166 -0
  32. package/dist/tools/page.js +53 -2
  33. package/dist/tools/ping.js +5 -1
  34. package/dist/tools/script.d.ts +1 -0
  35. package/dist/tools/script.js +166 -0
  36. package/dist/types.d.ts +50 -1
  37. package/dist/utils/bidi-value.d.ts +10 -0
  38. package/dist/utils/bidi-value.js +56 -0
  39. package/dist/utils/errors.d.ts +3 -1
  40. package/dist/utils/errors.js +13 -7
  41. package/dist/utils/redact.d.ts +16 -0
  42. package/dist/utils/redact.js +47 -0
  43. package/package.json +86 -86
package/README.md CHANGED
@@ -1,343 +1,398 @@
1
- <p align="center">
2
- <h1 align="center">BiDi Browser MCP Server</h1>
3
- <p align="center">
4
- <strong>Control your real Firefox over the W3C standard protocol.</strong><br>
5
- Keep your cookies. Keep your sessions. Keep your containers.
6
- </p>
7
- </p>
8
-
9
- <p align="center">
10
- <img src="https://img.shields.io/badge/version-0.1.7-blue" alt="Version">
11
- <img src="https://img.shields.io/badge/status-connect-orange" alt="Status">
12
- <img src="https://img.shields.io/badge/node-%3E%3D20-brightgreen" alt="Node">
13
- <img src="https://img.shields.io/badge/license-MIT-yellow" alt="License">
14
- </p>
15
-
16
- ---
17
-
18
- ## Status
19
-
20
- v0.1.9 — first public npm release. The stdio MCP server exposes `ping`,
21
- `browser`, `tabs`, `page` (snapshot/content), `console`, `observe`, `context`
22
- (containers), `spaces` (Zen), and `cleanup`. Socket drops clear session state
23
- honestly and the next connect recovers. Chromium browsers are supported without
24
- chromedriver: the `chromium-bidi` mapper runs inside this server and translates
25
- BiDi to CDP, with Firefox-only tools gated by a clear `UNSUPPORTED` error.
26
-
27
- v1 attaches to a Firefox you start; it does not launch, profile-manage, or close
28
- the browser. Launch/private/incognito control is future work.
29
-
30
- The full design and protocol research ship on the `dev` branch. `main` carries
31
- product code and release artifacts only.
32
-
33
- ## Why BiDi
34
-
35
- Firefox removed CDP in Firefox 141. WebDriver BiDi is the W3C standard that
36
- replaced it, and it is native in Firefox 129+ — no driver binary, no
37
- Puppeteer/Playwright, no browser download.
38
-
39
- Firefox-family browsers such as Zen, LibreWolf, Waterfox, and Floorp should work
40
- once their Remote Agent is enabled; profile discovery still needs testing per
41
- browser.
42
-
43
- This server connects to an **already-running Firefox** over its native BiDi
44
- WebSocket. Your logins, cookies, extensions, and containers stay intact.
45
-
46
- ## Tool surface
47
-
48
- Nine tools ship today (`core`): `ping` · `browser` · `tabs` · `page` ·
49
- `console` · `observe` · `context` · `spaces` · `cleanup`.
50
-
51
- Next (0.2.0) — the automation loop:
52
-
53
- - `interact` — click, type, clear, press, select, check, upload, scroll
54
- - `page.navigate` with `wait: none | domContentLoaded | load`
55
- - `network` (opt-in, read-only): wait for request/response metadata
56
- - gated `script.evaluate`
57
-
58
- Later — state and environment:
59
-
60
- - `storage` — cookies first, then web storage
61
- - `emulate` — locale, timezone, geolocation (tested null-reset only)
62
- - network intercepts with scoped, auto-removed handlers
63
- - `extensions` — Firefox, install gated by `BIDI_ALLOW_EXTENSION_INSTALL`
64
- - opt-in `devtools` — `moz:debugging` (Firefox 153+) and `moz:profiler`
65
- (Firefox 154+), screencast (Firefox 154+), preload scripts and realms
66
- - `BIDI_OUTPUT_MODE=file`, health monitor
67
-
68
- Firefox-specific capabilities:
69
-
70
- - **User contexts** - isolated containers / storage (not the same as a private
71
- window)
72
- - **Web extension install/list/uninstall** (install is opt-in and path-gated)
73
- - **Preload scripts** and script realms
74
- - **Debugger and profiler** - `moz:debugging` (Firefox 153+) and
75
- `moz:profiler` (Firefox 154+), opt-in `devtools` toolset
76
- - **Screencast** - Firefox 154+
77
-
78
- Not available in BiDi (compared with CDP): full accessibility tree, CSS domain,
79
- heap snapshots, network/CPU throttling, touch emulation. Debugger and profiler
80
- are Firefox vendor modules, not part of the core W3C command set, and do not
81
- exist in every browser.
82
-
83
- ## Cross-browser roadmap
84
-
85
- Firefox-family browsers expose a native BiDi endpoint; this server attaches to
86
- it directly. Chromium browsers (Chrome, Brave, Edge, Vivaldi, Chromium) expose
87
- CDP only, so this server hosts the
88
- [chromium-bidi mapper](https://github.com/GoogleChromeLabs/chromium-bidi)
89
- in-process — no chromedriver and no driver binary. Safari is experimental and
90
- partial, and is not supported yet.
91
-
92
- ## Requirements
93
-
94
- - Node.js 20.19+
95
- - Firefox 129+ for the core surface; 153 ESR recommended; 158+ for the full
96
- surface (debugger, profiler, screencast, newer emulation)
97
- - Two runtime dependencies: `ws`, `@modelcontextprotocol/sdk`
98
- - Optional: `chromium-bidi` for Chromium support. Its own `engines` range is
99
- Node 20.19+, 21.x, or 22.12+ (22.0–22.11 are excluded); Firefox works on the
100
- wider Node range either way.
101
-
102
- ## Chromium browsers (Chrome, Brave, Edge, …)
103
-
104
- Chromium does not speak WebDriver BiDi natively: `--remote-debugging-port`
105
- exposes CDP, a different protocol. When this server discovers a Chromium
106
- endpoint it starts the **chromium-bidi mapper inside its own process**, hands
107
- it a CDP connection, and keeps speaking BiDi to the tool layer. No chromedriver,
108
- no extension, no browser restart.
109
-
110
- 1. Launch Chromium with `--remote-debugging-port=<port>`.
111
- 2. Approve the debugging permission dialog in the browser window when the
112
- server connects (Chromium asks once per connection).
113
- 3. The optional mapper is installed together with this package. If it is
114
- missing — an install that omitted optional dependencies, or Node outside
115
- the mapper's range — Chromium sessions fail with `MISSING_DEPENDENCY` and
116
- the fix names the exact command. npm users can install it beside the
117
- server:
118
-
119
- ```bash
120
- npm install -g chromium-bidi@158.0.8123-0 # global npm server
121
- npm install chromium-bidi@158.0.8123-0 # project npm server
122
- ```
123
-
124
- pnpm isolates global packages, so a separately installed global
125
- `chromium-bidi` is not visible to the server; reinstall the server itself
126
- (`pnpm add -g bidi-browser-mcp`) or run it through `pnpm dlx` instead.
127
- The version is pinned because the mapper's deep import is a build artifact.
128
- It also pulls `devtools-protocol` (about 30 MB on disk).
129
-
130
- Discovery finds a running Chromium browser two ways: it reads
131
- `DevToolsActivePort` from the standard Chrome, Brave, Edge, Chromium, and
132
- Vivaldi user-data directories, and it resolves the `--user-data-dir` of a
133
- running Chromium process — so portable installs work with no configuration.
134
- `BIDI_CHROMIUM_DATA_DIR` adds directories explicitly (`;`-separated), and
135
- `BIDI_WS_URL` can name the browser's
136
- `ws://127.0.0.1:<port>/devtools/browser/<id>` URL. Chromium ports are never
137
- probed as BiDi `/session` endpoints (they always answer 403).
138
-
139
- `ping`, `browser`, `tabs`, `page`, `console`, `observe`, and `cleanup` work
140
- through the mapper. Firefox-family features (`context`, `spaces`) refuse with
141
- a clear `UNSUPPORTED` error on non-Firefox sessions. Firefox endpoints are
142
- preferred when both browser families are running; set `BIDI_WS_URL` to force a
143
- specific browser.
144
-
145
- ## Start Firefox with BiDi
146
-
147
- ```powershell
148
- # Windows
149
- & "C:\Program Files\Mozilla Firefox\firefox.exe" --remote-debugging-port=9222
150
- ```
151
-
152
- ```bash
153
- # macOS / Linux
154
- firefox --remote-debugging-port=9222
155
- ```
156
-
157
- BiDi endpoint: `ws://127.0.0.1:9222/session`
158
-
159
- Firefox sets `navigator.webdriver === true` whenever the Remote Agent is
160
- enabled. This is not a stealth or anti-detection tool; some sites may challenge
161
- the session. There is no spoofing layer.
162
-
163
- ## Test it now
164
-
165
- For a quick endpoint check, use [`TESTING.md`](./TESTING.md). Short version:
166
-
167
- ```powershell
168
- & "C:\Program Files\Mozilla Firefox\firefox.exe" --remote-debugging-port=9223
169
- pnpm build
170
- pnpm smoke
171
- ```
172
-
173
- The smoke script calls `session.status` only; it does not take over the session.
174
- `about:debugging` is not used by this MCP server.
175
-
176
- To see whether a running Firefox-family process has the Remote Agent on:
177
-
178
- ```powershell
179
- pnpm detect
180
- ```
181
-
182
- ## Install
183
-
184
- v0.1.9 is the first public release. Install globally with npm or pnpm:
185
-
186
- ```bash
187
- npm install -g bidi-browser-mcp
188
- pnpm add -g bidi-browser-mcp
189
- ```
190
-
191
- A global install is the simplest MCP setup. For Chromium, avoid
192
- `--omit=optional` (see Chromium browsers above).
193
-
194
- Or reference it from an MCP client without a global install:
195
-
196
- ```json
197
- {
198
- "mcpServers": {
199
- "bidi-browser": {
200
- "command": "npx",
201
- "args": ["-y", "bidi-browser-mcp@latest"]
202
- }
203
- }
204
- }
205
- ```
206
-
207
- The pnpm equivalent:
208
-
209
- ```json
210
- {
211
- "mcpServers": {
212
- "bidi-browser": {
213
- "command": "pnpm",
214
- "args": ["dlx", "bidi-browser-mcp@latest"]
215
- }
216
- }
217
- }
218
- ```
219
-
220
- The published package contains only compiled `dist/` plus `README`, `LICENSE`,
221
- and `package.json`. Repository docs, tests, and build scripts are not installed.
222
-
223
- ## Setup
224
-
225
- One-shot install/build/test/verify:
226
-
227
- ```bash
228
- pnpm setup
229
- ```
230
-
231
- If Firefox or Zen is running with `--remote-debugging-port`, this also probes the
232
- BiDi endpoint automatically. No env vars are required for the common case.
233
-
234
- Process-based discovery finds a running Firefox or Zen without env vars. There
235
- is no supported in-browser toggle to start the Remote Agent.
236
-
237
- ## Build from source
238
-
239
- ```bash
240
- pnpm install
241
- pnpm build
242
- pnpm typecheck
243
- pnpm test
244
- ```
245
-
246
- Point your MCP client at `dist/bin.js` while developing. Use an absolute path in
247
- your local client config; do not commit it:
248
-
249
- ```json
250
- {
251
- "mcpServers": {
252
- "bidi-browser": {
253
- "command": "node",
254
- "args": ["<absolute-path-to-repo>/dist/bin.js"]
255
- }
256
- }
257
- }
258
- ```
259
-
260
- After `pnpm build`, restart the MCP server so it loads the new `dist/`. Once the
261
- package is published, replace the dev entry with
262
- `pnpm dlx bidi-browser-mcp@latest` (or a global install).
263
-
264
- ## Troubleshooting
265
-
266
- - **`MISSING_DEPENDENCY` on Chromium** — the optional `chromium-bidi` mapper is
267
- not resolvable from the server. npm: install the exact pin beside the server
268
- (`npm install -g chromium-bidi@158.0.8123-0`) or reinstall without
269
- `--omit=optional`. pnpm: reinstall the server itself (`pnpm add -g
270
- bidi-browser-mcp`) or use `pnpm dlx`; pnpm's global installs are isolated.
271
- - **Chromium connect times out / hangs** — approve the in-browser debugging
272
- permission dialog; Chromium stalls the connection until it is allowed.
273
- - **`SESSION_BUSY` on Firefox/Zen** — Firefox allows one BiDi session. Close the
274
- other client (another MCP server, a test script, DevTools) and retry; the
275
- session is not released until the browser or the client ends it.
276
- - **No Firefox endpoint found** — start the browser with
277
- `--remote-debugging-port=9222` (Firefox/Zen) or set `BIDI_WS_URL`.
278
-
279
- ## Configuration
280
-
281
- `BIDI_OUTPUT_MODE=file` is not implemented yet and is rejected at startup; the
282
- allow flags are reserved for later waves.
283
-
284
- | Variable | Default | Description |
285
- |---|---|---|
286
- | `BIDI_HOST` | `127.0.0.1` | BiDi WebSocket host |
287
- | `BIDI_PORT` | `9222` | BiDi WebSocket port |
288
- | `BIDI_WS_URL` | auto | Override full WebSocket URL |
289
- | `BIDI_PROFILE_DIR` | auto | Firefox profile directory for discovery |
290
- | `BIDI_APP_DATA_ROOT` | `%APPDATA%` | App-data root used for profile discovery |
291
- | `BIDI_LOCAL_APP_DATA_ROOT` | `%LOCALAPPDATA%` | Local app-data root used to find Chromium user-data dirs |
292
- | `BIDI_CHROMIUM_DATA_DIR` | auto | Extra Chromium user-data dirs (one or more, `;`-separated) for `DevToolsActivePort` discovery |
293
- | `BIDI_TIMEOUT` | `30000` | Command timeout (ms) |
294
- | `BIDI_TOOLSET` | `core` | `core`, `network`, or `devtools` |
295
- | `BIDI_OUTPUT_MODE` | `full` | `full` (no inline cut; snapshots capped at 1 MiB) or `inline` (fit `BIDI_MAX_INLINE_LEN`); `file` is rejected |
296
- | `BIDI_MAX_INLINE_LEN` | `60000` | Max inline response chars |
297
- | `BIDI_TEMP_DIR` | `.temp` | Temp file directory — parsed and validated, but no temp files are written yet (reserved for later waves) |
298
- | `BIDI_ALLOW_EXTENSION_INSTALL` | `false` | Gate extension install |
299
- | `BIDI_ALLOW_BROWSER_CLOSE` | `false` | Gate `browser.close` |
300
-
301
- ## Safety limits
302
-
303
- - Snapshots never read live field values. Passwords, tokens, and typed text
304
- stay out of the snapshot, the agent context, and client logs.
305
- - `tabs.new` opens `http:`, `https:`, and `about:blank` only. `file:`, `data:`,
306
- `javascript:`, and `blob:` are rejected — they would turn the browser into a
307
- local-file or script reader.
308
- - `context.remove` needs `confirm: true`; it deletes the container with its
309
- tabs, cookies, and storage.
310
- - Non-loopback endpoints need `BIDI_ALLOW_REMOTE=1` — the gate covers every
311
- discovered candidate, including profile files.
312
-
313
- ## Architecture
314
-
315
- ```
316
- src/
317
- ├── bin.ts # CLI entry (shebang) → dist/bin.js
318
- ├── index.ts # public API: createServer(), VERSION
319
- ├── config.ts # env-based config (only env reader)
320
- ├── types.ts # shared contracts
321
- ├── connection/ # planned: BiDi client, discovery, health, event pipeline
322
- ├── session/ # planned: single-session manager, context manager
323
- ├── snapshot/ # planned: JS DOM snapshots, cache, refs, token optimizer
324
- ├── tools/ # defineTool, registry, ping, browser, tabs, page, console, observe, cleanup
325
- ├── utils/ # errors, result envelope, argument guards
326
- └── __tests__/ # hermetic unit tests + MockBiDiServer
327
- ```
328
-
329
- Firefox allows only one BiDi session at a time, so the server reuses a single
330
- session instead of copying CDP's multi-session model. A second client gets
331
- `BIDI_SESSION_BUSY`, not a retry loop. All tools in **one server process** share
332
- the session; separate MCP server processes need the future broker. BiDi has no
333
- full accessibility-tree API, so snapshots are built with `script.evaluate` plus
334
- DOM traversal.
335
-
336
- ## Contributing
337
-
338
- See [CONTRIBUTING.md](./CONTRIBUTING.md) and [AGENTS.md](./AGENTS.md). Work
339
- happens on `dev`; PRs target `dev`, and releases are cut from `main`.
340
-
341
- ## License
342
-
343
- MIT © 2026 Tushar Agarwal
1
+ <p align="center">
2
+ <h1 align="center">BiDi Browser MCP Server</h1>
3
+ <p align="center">
4
+ <strong>Control your real Firefox over the W3C standard protocol.</strong><br>
5
+ Keep your cookies. Keep your sessions. Keep your containers.
6
+ </p>
7
+ </p>
8
+
9
+ <p align="center">
10
+ <img src="https://img.shields.io/badge/version-0.1.7-blue" alt="Version">
11
+ <img src="https://img.shields.io/badge/status-connect-orange" alt="Status">
12
+ <img src="https://img.shields.io/badge/node-%3E%3D20-brightgreen" alt="Node">
13
+ <img src="https://img.shields.io/badge/license-MIT-yellow" alt="License">
14
+ </p>
15
+
16
+ ---
17
+
18
+ ## Status
19
+
20
+ v0.1.9 — first public npm release. The stdio MCP server exposes `ping`,
21
+ `browser`, `tabs`, `page` (snapshot/content), `console`, `observe`, `context`
22
+ (containers), `spaces` (Zen), and `cleanup`. Socket drops clear session state
23
+ honestly and the next connect recovers. Chromium browsers are supported without
24
+ chromedriver: the `chromium-bidi` mapper runs inside this server and translates
25
+ BiDi to CDP, with Firefox-only tools gated by a clear `UNSUPPORTED` error.
26
+
27
+ v1 attaches to a Firefox you start; it does not launch, profile-manage, or close
28
+ the browser. Launch/private/incognito control is future work.
29
+
30
+ The full design and protocol research ship on the `dev` branch. `main` carries
31
+ product code and release artifacts only.
32
+
33
+ ## Why BiDi
34
+
35
+ Firefox removed CDP in Firefox 141. WebDriver BiDi is the W3C standard that
36
+ replaced it, and it is native in Firefox 129+ — no driver binary, no
37
+ Puppeteer/Playwright, no browser download.
38
+
39
+ Firefox-family browsers such as Zen, LibreWolf, Waterfox, and Floorp should work
40
+ once their Remote Agent is enabled; profile discovery still needs testing per
41
+ browser.
42
+
43
+ This server connects to an **already-running Firefox** over its native BiDi
44
+ WebSocket. Your logins, cookies, extensions, and containers stay intact.
45
+
46
+ ## Tool surface
47
+
48
+ Nine tools in `core`, plus `network` (visible with `BIDI_TOOLSET=network` or
49
+ `devtools`): `ping` · `browser` · `tabs` · `page` · `interact` · `console` ·
50
+ `observe` · `context` · `spaces` · `cleanup` · `script` · `network`.
51
+
52
+ - `page` — snapshot (uid element map), readable text, and `navigate`
53
+ (`wait: none | domContentLoaded | load`; failures map to
54
+ `NAVIGATION_FAILED`).
55
+ - `interact` — act on snapshot uids: `click` `type` `clear` `press` `check`
56
+ `select` `scroll` `upload`. Every action re-verifies the uid against the
57
+ live page first (`ELEMENT_NOT_FOUND` / `STALE_UID` when the page moved on);
58
+ clicks hit-test and refuse covered targets with `NOT_INTERACTABLE` (iframe
59
+ and shadow crossings get boundary-specific fix text); input state is always
60
+ released, and the snapshot cache is dropped after every dispatched action.
61
+ Read-write: `readOnlyHint: false`, `destructiveHint: true`.
62
+ - `script` — `waitFor` (safe condition polling, 250 ms interval, 120 s cap)
63
+ and `evaluate` (credential-unsafe; **gated by `BIDI_ALLOW_SCRIPT_EVAL=1`** —
64
+ the tool stays visible and the refusal names the flag).
65
+ - `network` — read-only waits for request/response metadata
66
+ (`until: request|response`, `method`, `urlPattern` filter; no bodies, no
67
+ interception). Requires **`BIDI_ENABLE_NETWORK=1`**; the first subscription
68
+ starts at session connect, so trigger-then-wait flows still match.
69
+ `readOnlyHint: true`.
70
+
71
+ Later — state and environment:
72
+
73
+ - `storage` — cookies first, then web storage
74
+ - `emulate` — locale, timezone, geolocation (tested null-reset only)
75
+ - network intercepts with scoped, auto-removed handlers
76
+ - `extensions` — Firefox, install gated by `BIDI_ALLOW_EXTENSION_INSTALL`
77
+ - opt-in `devtools` — `moz:debugging` (Firefox 153+) and `moz:profiler`
78
+ (Firefox 154+), screencast (Firefox 154+), preload scripts and realms
79
+ - `BIDI_OUTPUT_MODE=file`, health monitor
80
+
81
+ Firefox-specific capabilities:
82
+
83
+ - **User contexts** - isolated containers / storage (not the same as a private
84
+ window)
85
+ - **Web extension install/list/uninstall** (install is opt-in and path-gated)
86
+ - **Preload scripts** and script realms
87
+ - **Debugger and profiler** - `moz:debugging` (Firefox 153+) and
88
+ `moz:profiler` (Firefox 154+), opt-in `devtools` toolset
89
+ - **Screencast** - Firefox 154+
90
+
91
+ Not available in BiDi (compared with CDP): full accessibility tree, CSS domain,
92
+ heap snapshots, network/CPU throttling, touch emulation. Debugger and profiler
93
+ are Firefox vendor modules, not part of the core W3C command set, and do not
94
+ exist in every browser.
95
+
96
+ ## Cross-browser roadmap
97
+
98
+ Firefox-family browsers expose a native BiDi endpoint; this server attaches to
99
+ it directly. Chromium browsers (Chrome, Brave, Edge, Vivaldi, Chromium) expose
100
+ CDP only, so this server hosts the
101
+ [chromium-bidi mapper](https://github.com/GoogleChromeLabs/chromium-bidi)
102
+ in-process — no chromedriver and no driver binary. Safari is experimental and
103
+ partial, and is not supported yet.
104
+
105
+ ## Requirements
106
+
107
+ - Node.js 20.19+
108
+ - Firefox 129+ for the core surface; 153 ESR recommended; 158+ for the full
109
+ surface (debugger, profiler, screencast, newer emulation)
110
+ - Two runtime dependencies: `ws`, `@modelcontextprotocol/sdk`
111
+ - Optional: `chromium-bidi` for Chromium support. Its own `engines` range is
112
+ Node 20.19+, 21.x, or 22.12+ (22.0–22.11 are excluded); Firefox works on the
113
+ wider Node range either way.
114
+
115
+ ## Chromium browsers (Chrome, Brave, Edge, …)
116
+
117
+ Chromium does not speak WebDriver BiDi natively: `--remote-debugging-port`
118
+ exposes CDP, a different protocol. When this server discovers a Chromium
119
+ endpoint it starts the **chromium-bidi mapper inside its own process**, hands
120
+ it a CDP connection, and keeps speaking BiDi to the tool layer. No chromedriver,
121
+ no extension, no browser restart.
122
+
123
+ 1. Launch Chromium with `--remote-debugging-port=<port>`.
124
+ 2. Approve the debugging permission dialog in the browser window when the
125
+ server connects (Chromium asks once per connection).
126
+ 3. The optional mapper is installed together with this package. If it is
127
+ missing — an install that omitted optional dependencies, or Node outside
128
+ the mapper's range — Chromium sessions fail with `MISSING_DEPENDENCY` and
129
+ the fix names the exact command. npm users can install it beside the
130
+ server:
131
+
132
+ ```bash
133
+ npm install -g chromium-bidi@158.0.8123-0 # global npm server
134
+ npm install chromium-bidi@158.0.8123-0 # project npm server
135
+ ```
136
+
137
+ pnpm isolates global packages, so a separately installed global
138
+ `chromium-bidi` is not visible to the server; reinstall the server itself
139
+ (`pnpm add -g bidi-browser-mcp`) or run it through `pnpm dlx` instead.
140
+ The version is pinned because the mapper's deep import is a build artifact.
141
+ It also pulls `devtools-protocol` (about 30 MB on disk).
142
+
143
+ Discovery finds a running Chromium browser two ways: it reads
144
+ `DevToolsActivePort` from the standard Chrome, Brave, Edge, Chromium, and
145
+ Vivaldi user-data directories, and it resolves the `--user-data-dir` of a
146
+ running Chromium process — so portable installs work with no configuration.
147
+ `BIDI_CHROMIUM_DATA_DIR` adds directories explicitly (`;`-separated), and
148
+ `BIDI_WS_URL` can name the browser's
149
+ `ws://127.0.0.1:<port>/devtools/browser/<id>` URL. Chromium ports are never
150
+ probed as BiDi `/session` endpoints (they always answer 403).
151
+
152
+ `ping`, `browser`, `tabs`, `page`, `console`, `observe`, and `cleanup` work
153
+ through the mapper. Firefox-family features (`context`, `spaces`) refuse with
154
+ a clear `UNSUPPORTED` error on non-Firefox sessions. Firefox endpoints are
155
+ preferred when both browser families are running; set `BIDI_WS_URL` to force a
156
+ specific browser.
157
+
158
+ ## Discovery and connect
159
+
160
+ `browser.profiles` lists every browser the server can find — Firefox-family
161
+ endpoints from profile discovery files, process command lines, and port probes;
162
+ Chromium endpoints from `DevToolsActivePort` records and running processes. Each
163
+ candidate carries:
164
+
165
+ | Field | Meaning |
166
+ | --- | --- |
167
+ | `id` | Stable handle for `browser.connect` — `f-<port>` (Firefox/BiDi), `c-<port>` (Chromium/CDP), `x-<port>` (unidentified port) |
168
+ | `endpoint` | The WebSocket the server would attach to |
169
+ | `family` / `port` | `firefox` or `chromium`, plus the endpoint's port |
170
+ | `reachable` | TCP: something is listening on the port |
171
+ | `connectable` | Protocol verdict — a BiDi endpoint answered `session.status`; a Chromium DevTools endpoint is recorded and its port answers |
172
+ | `reason` | Why the endpoint is **not** connectable (no listener, not BiDi, stale record) |
173
+ | `live` | This candidate is the currently attached session |
174
+
175
+ Probing stays read-only and side-effect-free: the BiDi check is a real
176
+ `session.status` handshake, which never creates a session, so listing cannot
177
+ take Firefox's single-session slot. Chromium is **never** WebSocket-probed by
178
+ the listing — attaching may require approving the browser's debugging prompt,
179
+ and that handshake blocks until granted; the connect attempt is where that
180
+ error surfaces, with the approval step in its fix text.
181
+
182
+ `browser.connect` takes any of: a candidate `id` (`c-9222`), a port (`9222`),
183
+ an exact `ws://` endpoint, or a profile/user-data path (the redacted `~\…`
184
+ form shown in the listing works). Omit the target for ordered auto-connect,
185
+ which skips endpoints the listing already proved dead or non-BiDi.
186
+
187
+ ```json
188
+ {
189
+ "candidates": [
190
+ { "id": "f-9223", "endpoint": "ws://127.0.0.1:9223/session", "source": "profiles-ini",
191
+ "browserFamily": "firefox", "port": 9223, "reachable": true, "connectable": true },
192
+ { "id": "c-9222", "endpoint": "ws://127.0.0.1:9222/devtools/browser/<id>", "source": "chromium-devtools-port",
193
+ "browserFamily": "chromium", "port": 9222, "reachable": true, "connectable": true }
194
+ ]
195
+ }
196
+ ```
197
+
198
+ ## Start Firefox with BiDi
199
+
200
+ ```powershell
201
+ # Windows
202
+ & "C:\Program Files\Mozilla Firefox\firefox.exe" --remote-debugging-port=9222
203
+ ```
204
+
205
+ ```bash
206
+ # macOS / Linux
207
+ firefox --remote-debugging-port=9222
208
+ ```
209
+
210
+ BiDi endpoint: `ws://127.0.0.1:9222/session`
211
+
212
+ Firefox sets `navigator.webdriver === true` whenever the Remote Agent is
213
+ enabled. This is not a stealth or anti-detection tool; some sites may challenge
214
+ the session. There is no spoofing layer.
215
+
216
+ ## Test it now
217
+
218
+ For a quick endpoint check, use [`TESTING.md`](./TESTING.md). Short version:
219
+
220
+ ```powershell
221
+ & "C:\Program Files\Mozilla Firefox\firefox.exe" --remote-debugging-port=9223
222
+ pnpm build
223
+ pnpm smoke
224
+ ```
225
+
226
+ The smoke script calls `session.status` only; it does not take over the session.
227
+ `about:debugging` is not used by this MCP server.
228
+
229
+ To see whether a running Firefox-family process has the Remote Agent on:
230
+
231
+ ```powershell
232
+ pnpm detect
233
+ ```
234
+
235
+ ## Install
236
+
237
+ v0.1.9 is the first public release. Install globally with npm or pnpm:
238
+
239
+ ```bash
240
+ npm install -g bidi-browser-mcp
241
+ pnpm add -g bidi-browser-mcp
242
+ ```
243
+
244
+ A global install is the simplest MCP setup. For Chromium, avoid
245
+ `--omit=optional` (see Chromium browsers above).
246
+
247
+ Or reference it from an MCP client without a global install:
248
+
249
+ ```json
250
+ {
251
+ "mcpServers": {
252
+ "bidi-browser": {
253
+ "command": "npx",
254
+ "args": ["-y", "bidi-browser-mcp@latest"]
255
+ }
256
+ }
257
+ }
258
+ ```
259
+
260
+ The pnpm equivalent:
261
+
262
+ ```json
263
+ {
264
+ "mcpServers": {
265
+ "bidi-browser": {
266
+ "command": "pnpm",
267
+ "args": ["dlx", "bidi-browser-mcp@latest"]
268
+ }
269
+ }
270
+ }
271
+ ```
272
+
273
+ The published package contains only compiled `dist/` plus `README`, `LICENSE`,
274
+ and `package.json`. Repository docs, tests, and build scripts are not installed.
275
+
276
+ ## Setup
277
+
278
+ One-shot install/build/test/verify:
279
+
280
+ ```bash
281
+ pnpm setup
282
+ ```
283
+
284
+ If Firefox or Zen is running with `--remote-debugging-port`, this also probes the
285
+ BiDi endpoint automatically. No env vars are required for the common case.
286
+
287
+ Process-based discovery finds a running Firefox or Zen without env vars. There
288
+ is no supported in-browser toggle to start the Remote Agent.
289
+
290
+ ## Build from source
291
+
292
+ ```bash
293
+ pnpm install
294
+ pnpm build
295
+ pnpm typecheck
296
+ pnpm test
297
+ ```
298
+
299
+ Point your MCP client at `dist/bin.js` while developing. Use an absolute path in
300
+ your local client config; do not commit it:
301
+
302
+ ```json
303
+ {
304
+ "mcpServers": {
305
+ "bidi-browser": {
306
+ "command": "node",
307
+ "args": ["<absolute-path-to-repo>/dist/bin.js"]
308
+ }
309
+ }
310
+ }
311
+ ```
312
+
313
+ After `pnpm build`, restart the MCP server so it loads the new `dist/`. Once the
314
+ package is published, replace the dev entry with
315
+ `pnpm dlx bidi-browser-mcp@latest` (or a global install).
316
+
317
+ ## Troubleshooting
318
+
319
+ - **`MISSING_DEPENDENCY` on Chromium** — the optional `chromium-bidi` mapper is
320
+ not resolvable from the server. npm: install the exact pin beside the server
321
+ (`npm install -g chromium-bidi@158.0.8123-0`) or reinstall without
322
+ `--omit=optional`. pnpm: reinstall the server itself (`pnpm add -g
323
+ bidi-browser-mcp`) or use `pnpm dlx`; pnpm's global installs are isolated.
324
+ - **Chromium connect times out / hangs** — approve the in-browser debugging
325
+ permission dialog; Chromium stalls the connection until it is allowed.
326
+ - **`SESSION_BUSY` on Firefox/Zen** — Firefox allows one BiDi session. Close the
327
+ other client (another MCP server, a test script, DevTools) and retry; the
328
+ session is not released until the browser or the client ends it.
329
+ - **No Firefox endpoint found** — start the browser with
330
+ `--remote-debugging-port=9222` (Firefox/Zen) or set `BIDI_WS_URL`.
331
+
332
+ ## Configuration
333
+
334
+ `BIDI_OUTPUT_MODE=file` is not implemented yet and is rejected at startup; the
335
+ allow flags are reserved for later waves.
336
+
337
+ | Variable | Default | Description |
338
+ |---|---|---|
339
+ | `BIDI_HOST` | `127.0.0.1` | BiDi WebSocket host |
340
+ | `BIDI_PORT` | `9222` | BiDi WebSocket port |
341
+ | `BIDI_WS_URL` | auto | Override full WebSocket URL |
342
+ | `BIDI_PROFILE_DIR` | auto | Firefox profile directory for discovery |
343
+ | `BIDI_APP_DATA_ROOT` | `%APPDATA%` | App-data root used for profile discovery |
344
+ | `BIDI_LOCAL_APP_DATA_ROOT` | `%LOCALAPPDATA%` | Local app-data root used to find Chromium user-data dirs |
345
+ | `BIDI_CHROMIUM_DATA_DIR` | auto | Extra Chromium user-data dirs (one or more, `;`-separated) for `DevToolsActivePort` discovery |
346
+ | `BIDI_TIMEOUT` | `30000` | Command timeout (ms) |
347
+ | `BIDI_TOOLSET` | `core` | `core`, `network`, or `devtools` |
348
+ | `BIDI_OUTPUT_MODE` | `full` | `full` (no inline cut; snapshots capped at 1 MiB) or `inline` (fit `BIDI_MAX_INLINE_LEN`); `file` is rejected |
349
+ | `BIDI_MAX_INLINE_LEN` | `60000` | Max inline response chars |
350
+ | `BIDI_TEMP_DIR` | `.temp` | Temp file directory — parsed and validated, but no temp files are written yet (reserved for later waves) |
351
+ | `BIDI_ALLOW_EXTENSION_INSTALL` | `false` | Gate extension install |
352
+ | `BIDI_ALLOW_BROWSER_CLOSE` | `false` | Gate `browser.close` |
353
+ | `BIDI_ENABLE_NETWORK` | `false` | Opt-in for `network.wait` (read-only request/response metadata) |
354
+ | `BIDI_ALLOW_SCRIPT_EVAL` | `false` | Opt-in for `script.evaluate` (reads credentials; `script.waitFor` needs no flag) |
355
+
356
+ ## Safety limits
357
+
358
+ - Snapshots never read live field values. Passwords, tokens, and typed text
359
+ stay out of the snapshot, the agent context, and client logs.
360
+ - `tabs.new` opens `http:`, `https:`, and `about:blank` only. `file:`, `data:`,
361
+ `javascript:`, and `blob:` are rejected — they would turn the browser into a
362
+ local-file or script reader.
363
+ - `context.remove` needs `confirm: true`; it deletes the container with its
364
+ tabs, cookies, and storage.
365
+ - Non-loopback endpoints need `BIDI_ALLOW_REMOTE=1` — the gate covers every
366
+ discovered candidate, including profile files.
367
+
368
+ ## Architecture
369
+
370
+ ```
371
+ src/
372
+ ├── bin.ts # CLI entry (shebang) → dist/bin.js
373
+ ├── index.ts # public API: createServer(), VERSION
374
+ ├── config.ts # env-based config (only env reader)
375
+ ├── types.ts # shared contracts
376
+ ├── connection/ # planned: BiDi client, discovery, health, event pipeline
377
+ ├── session/ # planned: single-session manager, context manager
378
+ ├── snapshot/ # planned: JS DOM snapshots, cache, refs, token optimizer
379
+ ├── tools/ # defineTool, registry, ping, browser, tabs, page, console, observe, cleanup
380
+ ├── utils/ # errors, result envelope, argument guards
381
+ └── __tests__/ # hermetic unit tests + MockBiDiServer
382
+ ```
383
+
384
+ Firefox allows only one BiDi session at a time, so the server reuses a single
385
+ session instead of copying CDP's multi-session model. A second client gets
386
+ `BIDI_SESSION_BUSY`, not a retry loop. All tools in **one server process** share
387
+ the session; separate MCP server processes need the future broker. BiDi has no
388
+ full accessibility-tree API, so snapshots are built with `script.evaluate` plus
389
+ DOM traversal.
390
+
391
+ ## Contributing
392
+
393
+ See [CONTRIBUTING.md](./CONTRIBUTING.md) and [AGENTS.md](./AGENTS.md). Work
394
+ happens on `dev`; PRs target `dev`, and releases are cut from `main`.
395
+
396
+ ## License
397
+
398
+ MIT © 2026 Tushar Agarwal