mcp-browser-dev-tools 0.0.1-beta.9 → 0.0.2-beta.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Rindo Hinase <rindo.hinase@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -9,9 +9,11 @@ It is designed for a local trust boundary:
9
9
  ## What You Get
10
10
 
11
11
  - A stdio MCP server for local desktop and terminal clients
12
- - Browser discovery and attach/detach for Chromium-family browsers and Firefox
12
+ - Browser discovery and attach/detach for Chrome, Edge, other Chromium-family browsers, and Firefox
13
+ - Tab lifecycle tools to create and close browser tabs through MCP
13
14
  - Inspection tools for DOM lookup, richer element details, console messages, network requests, screenshots, tab listing, and buffered events
14
15
  - Page interaction tools for navigation, reload, click, hover, type, select, key presses, scroll, and viewport overrides
16
+ - Wait conditions for selector visibility, URL changes, and document ready state
15
17
  - Optional JavaScript evaluation behind an explicit environment flag
16
18
  - Helper commands to check browser connectivity, launch a debug-enabled browser, and relay CDP traffic across a local machine boundary
17
19
 
@@ -23,8 +25,6 @@ It is designed for a local trust boundary:
23
25
 
24
26
  ## Install
25
27
 
26
- If you are trying the prerelease from npm, replace `mcp-browser-dev-tools` below with `mcp-browser-dev-tools@beta`.
27
-
28
28
  The published package name stays `mcp-browser-dev-tools`. The preferred installed CLI command is `mbdt`.
29
29
 
30
30
  One-off execution with `npx`:
@@ -58,27 +58,38 @@ Quick Chromium flow:
58
58
  ```bash
59
59
  # Launch a local browser with remote debugging enabled.
60
60
  # open waits for the debug endpoint and prints the doctor summary automatically.
61
- npx -y mcp-browser-dev-tools@beta open about:blank --family chromium
61
+ npx -y mcp-browser-dev-tools open about:blank --family chromium
62
62
  ```
63
63
 
64
64
  If you want to verify a specific app URL too:
65
65
 
66
66
  ```bash
67
- npx -y mcp-browser-dev-tools@beta doctor --url http://127.0.0.1:3000
67
+ npx -y mcp-browser-dev-tools doctor --url http://127.0.0.1:3000
68
68
  ```
69
69
 
70
70
  Then start the MCP server in a dedicated terminal:
71
71
 
72
72
  ```bash
73
- npx -y mcp-browser-dev-tools@beta serve
73
+ npx -y mcp-browser-dev-tools serve
74
74
  ```
75
75
 
76
76
  `serve` stays attached to stdio because MCP clients talk to it over standard input and output. Run it in its own terminal or let your MCP client spawn it directly.
77
77
 
78
+ If you want one broker to handle Chromium and Firefox at the same time, run it in `auto` mode and point each adapter at its own endpoint:
79
+
80
+ ```bash
81
+ MCP_BROWSER_FAMILY=auto \
82
+ CDP_BASE_URL=http://127.0.0.1:9223 \
83
+ FIREFOX_BIDI_WS_URL=ws://127.0.0.1:9222 \
84
+ npx -y mcp-browser-dev-tools serve
85
+ ```
86
+
87
+ Use distinct ports in `auto` mode. A practical local split is Firefox on `9222` and Chrome or Edge on `9223`.
88
+
78
89
  If you already have a browser listening on `http://127.0.0.1:9222`, `doctor` is enough to confirm that the broker can reach it:
79
90
 
80
91
  ```bash
81
- CDP_BASE_URL=http://127.0.0.1:9222 npx -y mcp-browser-dev-tools@beta doctor
92
+ CDP_BASE_URL=http://127.0.0.1:9222 npx -y mcp-browser-dev-tools doctor
82
93
  ```
83
94
 
84
95
  If you need to bridge Windows Chrome into WSL without changing WSL networking mode, run the relay on Windows and point WSL at the relay port instead. The full procedure is in [docs/setup.md](docs/setup.md).
@@ -88,7 +99,7 @@ If you need to bridge Windows Chrome into WSL without changing WSL networking mo
88
99
  Use the same server command across MCP clients:
89
100
 
90
101
  ```bash
91
- npx -y mcp-browser-dev-tools@beta serve
102
+ npx -y mcp-browser-dev-tools serve
92
103
  ```
93
104
 
94
105
  If you installed the package already, the shorter equivalent is:
@@ -103,9 +114,10 @@ Add the server with the Codex CLI:
103
114
 
104
115
  ```bash
105
116
  codex mcp add browser-devtools \
106
- --env MCP_BROWSER_FAMILY=chromium \
107
- --env CDP_BASE_URL=http://127.0.0.1:9222 \
108
- -- npx -y mcp-browser-dev-tools@beta serve
117
+ --env MCP_BROWSER_FAMILY=auto \
118
+ --env CDP_BASE_URL=http://127.0.0.1:9223 \
119
+ --env FIREFOX_BIDI_WS_URL=ws://127.0.0.1:9222 \
120
+ -- npx -y mcp-browser-dev-tools serve
109
121
  ```
110
122
 
111
123
  Equivalent `~/.codex/config.toml` entry:
@@ -113,28 +125,52 @@ Equivalent `~/.codex/config.toml` entry:
113
125
  ```toml
114
126
  [mcp_servers.browser-devtools]
115
127
  command = "npx"
116
- args = ["-y", "mcp-browser-dev-tools@beta", "serve"]
128
+ args = ["-y", "mcp-browser-dev-tools", "serve"]
117
129
 
118
130
  [mcp_servers.browser-devtools.env]
119
- MCP_BROWSER_FAMILY = "chromium"
120
- CDP_BASE_URL = "http://127.0.0.1:9222"
131
+ MCP_BROWSER_FAMILY = "auto"
132
+ CDP_BASE_URL = "http://127.0.0.1:9223"
133
+ FIREFOX_BIDI_WS_URL = "ws://127.0.0.1:9222"
134
+ ```
135
+
136
+ If Codex runs in WSL but the browser runs on Windows, run the broker on Windows too so it can connect to Windows loopback directly. This avoids relay and WSL networking issues.
137
+
138
+ Example `~/.codex/config.toml` entry for Firefox on Windows:
139
+
140
+ ```toml
141
+ [mcp_servers.browser-devtools]
142
+ startup_timeout_sec = 30
143
+ command = "/mnt/c/nvm4w/nodejs/node.exe"
144
+ args = ["-e", "process.env.MCP_BROWSER_FAMILY='firefox';process.env.FIREFOX_BIDI_WS_URL='ws://127.0.0.1:9222';import('//wsl.localhost/<wsl-distro>/<repo-wsl-path>/src/cli.mjs').then(({ runCli }) => runCli(['serve'])).catch((error) => { console.error(error?.stack || String(error)); process.exit(1); });"]
145
+ ```
146
+
147
+ Example `auto` mode entry for Firefox plus Chrome or Edge on Windows:
148
+
149
+ ```toml
150
+ [mcp_servers.browser-devtools]
151
+ startup_timeout_sec = 30
152
+ command = "/mnt/c/nvm4w/nodejs/node.exe"
153
+ args = ["-e", "process.env.MCP_BROWSER_FAMILY='auto';process.env.CDP_BASE_URL='http://127.0.0.1:9223';process.env.FIREFOX_BIDI_WS_URL='ws://127.0.0.1:9222';import('//wsl.localhost/<wsl-distro>/<repo-wsl-path>/src/cli.mjs').then(({ runCli }) => runCli(['serve'])).catch((error) => { console.error(error?.stack || String(error)); process.exit(1); });"]
121
154
  ```
122
155
 
156
+ Replace `<wsl-distro>` and `<repo-wsl-path>` with your own WSL distro name and repository path.
157
+
123
158
  ### Claude Code
124
159
 
125
160
  Add the server with Claude Code:
126
161
 
127
162
  ```bash
128
163
  claude mcp add browser-devtools --scope user \
129
- --env MCP_BROWSER_FAMILY=chromium \
130
- --env CDP_BASE_URL=http://127.0.0.1:9222 \
131
- -- npx -y mcp-browser-dev-tools@beta serve
164
+ --env MCP_BROWSER_FAMILY=auto \
165
+ --env CDP_BASE_URL=http://127.0.0.1:9223 \
166
+ --env FIREFOX_BIDI_WS_URL=ws://127.0.0.1:9222 \
167
+ -- npx -y mcp-browser-dev-tools serve
132
168
  ```
133
169
 
134
170
  On native Windows, wrap `npx` with `cmd /c`:
135
171
 
136
172
  ```powershell
137
- claude mcp add browser-devtools --scope user --env MCP_BROWSER_FAMILY=chromium --env CDP_BASE_URL=http://127.0.0.1:9222 -- cmd /c npx -y mcp-browser-dev-tools@beta serve
173
+ claude mcp add browser-devtools --scope user --env MCP_BROWSER_FAMILY=auto --env CDP_BASE_URL=http://127.0.0.1:9223 --env FIREFOX_BIDI_WS_URL=ws://127.0.0.1:9222 -- cmd /c npx -y mcp-browser-dev-tools serve
138
174
  ```
139
175
 
140
176
  Equivalent `.mcp.json` shape:
@@ -144,10 +180,11 @@ Equivalent `.mcp.json` shape:
144
180
  "mcpServers": {
145
181
  "browser-devtools": {
146
182
  "command": "npx",
147
- "args": ["-y", "mcp-browser-dev-tools@beta", "serve"],
183
+ "args": ["-y", "mcp-browser-dev-tools", "serve"],
148
184
  "env": {
149
- "MCP_BROWSER_FAMILY": "chromium",
150
- "CDP_BASE_URL": "http://127.0.0.1:9222"
185
+ "MCP_BROWSER_FAMILY": "auto",
186
+ "CDP_BASE_URL": "http://127.0.0.1:9223",
187
+ "FIREFOX_BIDI_WS_URL": "ws://127.0.0.1:9222"
151
188
  }
152
189
  }
153
190
  }
@@ -165,8 +202,9 @@ Cursor reads MCP servers from `mcp.json`:
165
202
  "command": "npx",
166
203
  "args": ["-y", "mcp-browser-dev-tools", "serve"],
167
204
  "env": {
168
- "MCP_BROWSER_FAMILY": "chromium",
169
- "CDP_BASE_URL": "http://127.0.0.1:9222"
205
+ "MCP_BROWSER_FAMILY": "auto",
206
+ "CDP_BASE_URL": "http://127.0.0.1:9223",
207
+ "FIREFOX_BIDI_WS_URL": "ws://127.0.0.1:9222"
170
208
  }
171
209
  }
172
210
  }
@@ -175,6 +213,16 @@ Cursor reads MCP servers from `mcp.json`:
175
213
 
176
214
  ### Common Variants
177
215
 
216
+ Auto mode with both browsers attached to one MCP server:
217
+
218
+ ```json
219
+ {
220
+ "MCP_BROWSER_FAMILY": "auto",
221
+ "CDP_BASE_URL": "http://127.0.0.1:9223",
222
+ "FIREFOX_BIDI_WS_URL": "ws://127.0.0.1:9222"
223
+ }
224
+ ```
225
+
178
226
  Firefox:
179
227
 
180
228
  ```json
@@ -184,6 +232,15 @@ Firefox:
184
232
  }
185
233
  ```
186
234
 
235
+ Microsoft Edge:
236
+
237
+ ```json
238
+ {
239
+ "MCP_BROWSER_FAMILY": "edge",
240
+ "CDP_BASE_URL": "http://127.0.0.1:9222"
241
+ }
242
+ ```
243
+
187
244
  Windows browser bridged into WSL through the relay:
188
245
 
189
246
  ```json
@@ -194,6 +251,8 @@ Windows browser bridged into WSL through the relay:
194
251
  }
195
252
  ```
196
253
 
254
+ If you only want a single CDP browser, switch `MCP_BROWSER_FAMILY` back to `chromium` or `edge` and omit `FIREFOX_BIDI_WS_URL`.
255
+
197
256
  Enable `evaluate_js`:
198
257
 
199
258
  ```json
@@ -217,16 +276,20 @@ Examples:
217
276
  mbdt doctor
218
277
  mbdt doctor --url http://127.0.0.1:3000
219
278
  mbdt open http://127.0.0.1:3000 --family chromium
279
+ mbdt open http://127.0.0.1:3000 --family edge --port 9223 --user-data-dir /tmp/mbdt-edge
220
280
  mbdt open about:blank --family firefox --user-data-dir /tmp/mbdt-firefox
221
281
  mbdt relay --wsl
222
282
  ```
223
283
 
224
284
  ## Configuration
225
285
 
226
- - `MCP_BROWSER_FAMILY` defaults to `chromium`; set `firefox` for Firefox BiDi
286
+ - `MCP_BROWSER_FAMILY` defaults to `chromium`; set `edge` for Microsoft Edge, `firefox` for Firefox BiDi, or `auto` to multiplex both adapters
227
287
  - `CDP_BASE_URL` defaults to `http://127.0.0.1:9222`
228
- - `FIREFOX_BIDI_WS_URL` defaults to `ws://127.0.0.1:9222`; when pointed at the root Firefox remote debugging port, the broker first requests `webSocketUrl` from `POST /session` and then connects to the returned BiDi session socket
288
+ - `FIREFOX_BIDI_WS_URL` defaults to `ws://127.0.0.1:9222`; when pointed at the root Firefox remote debugging port, the broker connects to the `/session` websocket and creates a BiDi session there
289
+ - in `auto` mode, assign CDP and Firefox different ports so both browsers can run at once
229
290
  - `MCP_BROWSER_EVENT_BUFFER_SIZE` sets the per-session buffered event limit
291
+ - `MCP_BROWSER_LOG_LEVEL` controls diagnostic logging to `stderr`: `error`, `warn`, `info`, or `debug`
292
+ - `MCP_BROWSER_DEBUG_STDIO=1` emits raw MCP stdio transport diagnostics to `stderr`
230
293
  - `MCP_BROWSER_ENABLE_EVAL=1` enables `evaluate_js`
231
294
  - `MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1` allows non-loopback CDP or BiDi endpoints
232
295
  - `MCP_BROWSER_ALLOW_REMOTE_CDP=1` is still accepted as a legacy alias
@@ -238,12 +301,18 @@ The `relay` command defaults to `127.0.0.1:9223 -> 127.0.0.1:9222`. Non-loopback
238
301
 
239
302
  ## Exposed Tools
240
303
 
241
- - `browser_status`
304
+ - `browser_status` returns broker metadata too: `serverName` and `serverVersion`
305
+ In `auto` mode it also includes per-browser adapter status under `browsers`
242
306
  - `list_tabs`
307
+ In `auto` mode each `targetId` is namespaced as `chromium:<id>` or `firefox:<id>`
308
+ - `new_tab`
309
+ - `close_tab`
243
310
  - `list_sessions`
311
+ In `auto` mode each `sessionId` is namespaced the same way
244
312
  - `attach_tab`
245
313
  - `detach_tab`
246
314
  - `get_page_state`
315
+ - `wait_for`
247
316
  - `navigate`
248
317
  - `reload`
249
318
  - `click`
@@ -293,7 +362,7 @@ Playwright is still the stronger choice for deterministic browser automation and
293
362
  `mcp-browser-dev-tools` solves a different problem:
294
363
 
295
364
  - it is MCP-native, so AI clients call a bounded tool surface instead of generating and executing Playwright scripts
296
- - it attaches to an already-open browser tab and inspects the current session state, cookies, login state, extensions, console, and network history
365
+ - it can inspect an already-open browser tab or create a fresh one, then inspect the current session state, cookies, login state, extensions, console, and network history
297
366
  - it is designed for local AI debugging workflows, including loopback-only defaults, opt-in evaluation, and the Windows-to-WSL relay path
298
367
  - it presents one MCP interface across Chromium CDP and Firefox BiDi instead of requiring the AI client to know browser protocol details
299
368
 
@@ -304,6 +373,7 @@ Use Playwright when you want reproducible automation. Use this project when you
304
373
  - `attach_tab` now seeds network history from the Performance API so already-loaded pages still show useful requests
305
374
  - console buffers include source URL, line and column where the browser reports them, plus stack frames when available
306
375
  - `get_page_state` reports the current URL, title, viewport, and scroll position without enabling `evaluate_js`
376
+ - broker diagnostics write to `stderr` so `stdout` stays reserved for MCP protocol traffic
307
377
 
308
378
  ## Safety Defaults
309
379
 
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "mcp-browser-dev-tools",
3
- "version": "0.0.1-beta.9",
3
+ "version": "0.0.2-beta.0",
4
4
  "description": "Local MCP server for Chromium DevTools Protocol and Firefox WebDriver BiDi",
5
5
  "author": "Hinaser <rindo.hinase@gmail.com>",
6
+ "license": "MIT",
6
7
  "type": "module",
7
8
  "packageManager": "pnpm@10.29.3",
8
9
  "homepage": "https://github.com/Hinaser/mcp-browser-dev-tools#readme",
@@ -30,12 +31,14 @@
30
31
  "src/firefox-bidi-client.mjs",
31
32
  "src/index.mjs",
32
33
  "src/json-rpc-stdio.mjs",
34
+ "src/logger.mjs",
33
35
  "src/mcp-server.mjs",
34
36
  "src/page-context.mjs",
35
37
  "src/package-info.mjs",
36
38
  "src/session-events.mjs",
37
39
  "src/server.mjs",
38
40
  "src/tcp-relay.mjs",
41
+ "src/wait-for.mjs",
39
42
  "README.md"
40
43
  ],
41
44
  "bin": {
package/src/app.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createBrowserAdapter } from "./browser-adapter.mjs";
2
2
  import { loadConfig } from "./config.mjs";
3
+ import { createLogger } from "./logger.mjs";
3
4
  import { McpBrowserDevToolsServer } from "./mcp-server.mjs";
4
5
  import { PACKAGE_NAME } from "./package-info.mjs";
5
6
 
@@ -10,6 +11,11 @@ export function createBrowserDevToolsApp({
10
11
  errorOutput = process.stderr,
11
12
  } = {}) {
12
13
  const config = loadConfig(env);
14
+ const logger = createLogger({
15
+ level: config.logLevel,
16
+ output: errorOutput,
17
+ name: PACKAGE_NAME,
18
+ });
13
19
  const browserAdapter = createBrowserAdapter(config);
14
20
  const server = new McpBrowserDevToolsServer({
15
21
  config,
@@ -17,6 +23,7 @@ export function createBrowserDevToolsApp({
17
23
  input,
18
24
  output,
19
25
  errorOutput,
26
+ logger,
20
27
  });
21
28
 
22
29
  let closing = false;
@@ -27,7 +34,7 @@ export function createBrowserDevToolsApp({
27
34
  }
28
35
 
29
36
  closing = true;
30
- errorOutput.write(`[${PACKAGE_NAME}] shutting down after ${signal}\n`);
37
+ logger.info(`shutting down after ${signal}`);
31
38
  await browserAdapter.closeAll();
32
39
  }
33
40
 
@@ -47,6 +54,7 @@ export function createBrowserDevToolsApp({
47
54
 
48
55
  return {
49
56
  config,
57
+ logger,
50
58
  browserAdapter,
51
59
  server,
52
60
  start,
@@ -1,10 +1,315 @@
1
1
  import { CdpSessionManager } from "./cdp-client.mjs";
2
2
  import { FirefoxBidiSessionManager } from "./firefox-bidi-client.mjs";
3
3
 
4
+ const SUPPORTED_FAMILIES = ["chromium", "firefox"];
5
+
6
+ function prefixId(browserFamily, value) {
7
+ return `${browserFamily}:${value}`;
8
+ }
9
+
10
+ function resolvePrefixedId(value, kind) {
11
+ if (typeof value !== "string") {
12
+ throw new Error(`${kind} must be a string`);
13
+ }
14
+
15
+ for (const browserFamily of SUPPORTED_FAMILIES) {
16
+ const prefix = `${browserFamily}:`;
17
+ if (value.startsWith(prefix)) {
18
+ return {
19
+ browserFamily,
20
+ localId: value.slice(prefix.length),
21
+ };
22
+ }
23
+ }
24
+
25
+ throw new Error(
26
+ `${kind} must include a browser prefix (${SUPPORTED_FAMILIES.join(", ")})`,
27
+ );
28
+ }
29
+
30
+ function mapPrefixedFields(value, browserFamily) {
31
+ if (Array.isArray(value)) {
32
+ return value.map((entry) => mapPrefixedFields(entry, browserFamily));
33
+ }
34
+
35
+ if (!value || typeof value !== "object") {
36
+ return value;
37
+ }
38
+
39
+ return Object.fromEntries(
40
+ Object.entries(value).map(([key, entry]) => {
41
+ if (key === "sessionId" && typeof entry === "string") {
42
+ return [key, prefixId(browserFamily, entry)];
43
+ }
44
+
45
+ if (key === "targetId" && typeof entry === "string") {
46
+ return [key, prefixId(browserFamily, entry)];
47
+ }
48
+
49
+ return [key, mapPrefixedFields(entry, browserFamily)];
50
+ }),
51
+ );
52
+ }
53
+
54
+ function mergeStatusError(statuses) {
55
+ const failures = Object.entries(statuses)
56
+ .filter(([, status]) => !status.available)
57
+ .map(([browserFamily, status]) => `${browserFamily}: ${status.error}`);
58
+
59
+ return failures.length > 0 ? failures.join("; ") : null;
60
+ }
61
+
62
+ export class MultiBrowserAdapter {
63
+ constructor(adapters) {
64
+ this.adapters = adapters;
65
+ }
66
+
67
+ getAdapter(browserFamily) {
68
+ const adapter = this.adapters[browserFamily];
69
+ if (!adapter) {
70
+ throw new Error(`Unsupported browser family: ${browserFamily}`);
71
+ }
72
+
73
+ return adapter;
74
+ }
75
+
76
+ async getBrowserStatus() {
77
+ const entries = await Promise.all(
78
+ Object.entries(this.adapters).map(async ([browserFamily, adapter]) => [
79
+ browserFamily,
80
+ await adapter.getBrowserStatus(),
81
+ ]),
82
+ );
83
+ const browsers = Object.fromEntries(entries);
84
+ const availableBrowsers = Object.entries(browsers)
85
+ .filter(([, status]) => status.available)
86
+ .map(([browserFamily]) => browserFamily);
87
+
88
+ return {
89
+ available: availableBrowsers.length > 0,
90
+ browserFamily: "auto",
91
+ sessionCount: Object.values(browsers).reduce(
92
+ (total, status) => total + (status.sessionCount ?? 0),
93
+ 0,
94
+ ),
95
+ availableBrowsers,
96
+ browsers,
97
+ error:
98
+ availableBrowsers.length > 0
99
+ ? null
100
+ : (mergeStatusError(browsers) ?? "No browser endpoints available"),
101
+ };
102
+ }
103
+
104
+ async listTargets() {
105
+ const results = await Promise.all(
106
+ Object.entries(this.adapters).map(async ([browserFamily, adapter]) => {
107
+ try {
108
+ return (await adapter.listTargets()).map((target) => ({
109
+ ...target,
110
+ browserFamily,
111
+ targetId: prefixId(browserFamily, target.targetId),
112
+ }));
113
+ } catch {
114
+ return [];
115
+ }
116
+ }),
117
+ );
118
+
119
+ return results.flat();
120
+ }
121
+
122
+ listSessions() {
123
+ return Object.entries(this.adapters).flatMap(([browserFamily, adapter]) =>
124
+ adapter.listSessions().map((session) => ({
125
+ ...session,
126
+ browserFamily,
127
+ sessionId: prefixId(browserFamily, session.sessionId),
128
+ targetId:
129
+ typeof session.targetId === "string"
130
+ ? prefixId(browserFamily, session.targetId)
131
+ : session.targetId,
132
+ })),
133
+ );
134
+ }
135
+
136
+ async attachToTarget(targetId) {
137
+ const { browserFamily, localId } = resolvePrefixedId(targetId, "targetId");
138
+ const session =
139
+ await this.getAdapter(browserFamily).attachToTarget(localId);
140
+
141
+ return {
142
+ ...session,
143
+ browserFamily,
144
+ sessionId: prefixId(browserFamily, session.sessionId),
145
+ targetId:
146
+ typeof session.targetId === "string"
147
+ ? prefixId(browserFamily, session.targetId)
148
+ : targetId,
149
+ };
150
+ }
151
+
152
+ async detachSession(sessionId) {
153
+ const { browserFamily, localId } = resolvePrefixedId(
154
+ sessionId,
155
+ "sessionId",
156
+ );
157
+
158
+ return mapPrefixedFields(
159
+ await this.getAdapter(browserFamily).detachSession(localId),
160
+ browserFamily,
161
+ );
162
+ }
163
+
164
+ async createTab(url, options = {}) {
165
+ const browserFamily = options.browserFamily;
166
+ if (!browserFamily) {
167
+ throw new Error(
168
+ "browserFamily is required when multiple browsers are configured",
169
+ );
170
+ }
171
+
172
+ const result = await this.getAdapter(browserFamily).createTab(url);
173
+ return {
174
+ ...mapPrefixedFields(result, browserFamily),
175
+ browserFamily,
176
+ };
177
+ }
178
+
179
+ async closeTarget(targetId) {
180
+ const { browserFamily, localId } = resolvePrefixedId(targetId, "targetId");
181
+ return {
182
+ ...mapPrefixedFields(
183
+ await this.getAdapter(browserFamily).closeTarget(localId),
184
+ browserFamily,
185
+ ),
186
+ browserFamily,
187
+ };
188
+ }
189
+
190
+ async delegateSession(sessionId, methodName, args = []) {
191
+ const { browserFamily, localId } = resolvePrefixedId(
192
+ sessionId,
193
+ "sessionId",
194
+ );
195
+ const adapter = this.getAdapter(browserFamily);
196
+ const result = await adapter[methodName](localId, ...args);
197
+
198
+ if (result === undefined) {
199
+ return result;
200
+ }
201
+
202
+ if (methodName === "getConsoleMessages" || methodName === "getEvents") {
203
+ return mapPrefixedFields(result, browserFamily);
204
+ }
205
+
206
+ if (methodName === "getNetworkRequests") {
207
+ return mapPrefixedFields(result, browserFamily);
208
+ }
209
+
210
+ return mapPrefixedFields(result, browserFamily);
211
+ }
212
+
213
+ getPageState(sessionId) {
214
+ return this.delegateSession(sessionId, "getPageState");
215
+ }
216
+
217
+ waitFor(sessionId, options) {
218
+ return this.delegateSession(sessionId, "waitFor", [options]);
219
+ }
220
+
221
+ navigate(sessionId, url, options) {
222
+ return this.delegateSession(sessionId, "navigate", [url, options]);
223
+ }
224
+
225
+ reload(sessionId, options) {
226
+ return this.delegateSession(sessionId, "reload", [options]);
227
+ }
228
+
229
+ click(sessionId, selector) {
230
+ return this.delegateSession(sessionId, "click", [selector]);
231
+ }
232
+
233
+ hover(sessionId, selector) {
234
+ return this.delegateSession(sessionId, "hover", [selector]);
235
+ }
236
+
237
+ type(sessionId, selector, text, options) {
238
+ return this.delegateSession(sessionId, "type", [selector, text, options]);
239
+ }
240
+
241
+ select(sessionId, selector, options) {
242
+ return this.delegateSession(sessionId, "select", [selector, options]);
243
+ }
244
+
245
+ pressKey(sessionId, key, selector) {
246
+ return this.delegateSession(sessionId, "pressKey", [key, selector]);
247
+ }
248
+
249
+ scroll(sessionId, options) {
250
+ return this.delegateSession(sessionId, "scroll", [options]);
251
+ }
252
+
253
+ setViewport(sessionId, options) {
254
+ return this.delegateSession(sessionId, "setViewport", [options]);
255
+ }
256
+
257
+ evaluate(sessionId, expression) {
258
+ return this.delegateSession(sessionId, "evaluate", [expression]);
259
+ }
260
+
261
+ getDocument(sessionId, depth) {
262
+ return this.delegateSession(sessionId, "getDocument", [depth]);
263
+ }
264
+
265
+ getConsoleMessages(sessionId, limit) {
266
+ return this.delegateSession(sessionId, "getConsoleMessages", [limit]);
267
+ }
268
+
269
+ getNetworkRequests(sessionId, limit) {
270
+ return this.delegateSession(sessionId, "getNetworkRequests", [limit]);
271
+ }
272
+
273
+ inspectElement(sessionId, selector) {
274
+ return this.delegateSession(sessionId, "inspectElement", [selector]);
275
+ }
276
+
277
+ takeScreenshot(sessionId, format, options) {
278
+ return this.delegateSession(sessionId, "takeScreenshot", [format, options]);
279
+ }
280
+
281
+ getEvents(sessionId, limit) {
282
+ return this.delegateSession(sessionId, "getEvents", [limit]);
283
+ }
284
+
285
+ async closeAll() {
286
+ await Promise.all(
287
+ Object.values(this.adapters).map((adapter) => adapter.closeAll()),
288
+ );
289
+ }
290
+ }
291
+
4
292
  export function createBrowserAdapter(config) {
5
293
  if (config.browserFamily === "firefox") {
6
294
  return new FirefoxBidiSessionManager(config);
7
295
  }
8
296
 
297
+ if (config.browserFamily === "auto") {
298
+ return new MultiBrowserAdapter({
299
+ chromium: new CdpSessionManager({
300
+ ...config,
301
+ browserFamily: "chromium",
302
+ }),
303
+ firefox: new FirefoxBidiSessionManager({
304
+ ...config,
305
+ browserFamily: "firefox",
306
+ }),
307
+ });
308
+ }
309
+
310
+ if (config.browserFamily === "edge") {
311
+ return new CdpSessionManager(config);
312
+ }
313
+
9
314
  return new CdpSessionManager(config);
10
315
  }