mcp-browser-dev-tools 0.0.1-beta.9 → 0.0.1
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/README.md +88 -26
- package/package.json +1 -1
- package/src/browser-adapter.mjs +275 -0
- package/src/browser-launcher.mjs +32 -0
- package/src/cdp-client.mjs +15 -9
- package/src/cli.mjs +3 -2
- package/src/config.mjs +24 -9
- package/src/doctor.mjs +25 -5
- package/src/index.mjs +4 -1
- package/src/mcp-server.mjs +5 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ 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
13
|
- Inspection tools for DOM lookup, richer element details, console messages, network requests, screenshots, tab listing, and buffered events
|
|
14
14
|
- Page interaction tools for navigation, reload, click, hover, type, select, key presses, scroll, and viewport overrides
|
|
15
15
|
- Optional JavaScript evaluation behind an explicit environment flag
|
|
@@ -23,8 +23,6 @@ It is designed for a local trust boundary:
|
|
|
23
23
|
|
|
24
24
|
## Install
|
|
25
25
|
|
|
26
|
-
If you are trying the prerelease from npm, replace `mcp-browser-dev-tools` below with `mcp-browser-dev-tools@beta`.
|
|
27
|
-
|
|
28
26
|
The published package name stays `mcp-browser-dev-tools`. The preferred installed CLI command is `mbdt`.
|
|
29
27
|
|
|
30
28
|
One-off execution with `npx`:
|
|
@@ -58,27 +56,38 @@ Quick Chromium flow:
|
|
|
58
56
|
```bash
|
|
59
57
|
# Launch a local browser with remote debugging enabled.
|
|
60
58
|
# open waits for the debug endpoint and prints the doctor summary automatically.
|
|
61
|
-
npx -y mcp-browser-dev-tools
|
|
59
|
+
npx -y mcp-browser-dev-tools open about:blank --family chromium
|
|
62
60
|
```
|
|
63
61
|
|
|
64
62
|
If you want to verify a specific app URL too:
|
|
65
63
|
|
|
66
64
|
```bash
|
|
67
|
-
npx -y mcp-browser-dev-tools
|
|
65
|
+
npx -y mcp-browser-dev-tools doctor --url http://127.0.0.1:3000
|
|
68
66
|
```
|
|
69
67
|
|
|
70
68
|
Then start the MCP server in a dedicated terminal:
|
|
71
69
|
|
|
72
70
|
```bash
|
|
73
|
-
npx -y mcp-browser-dev-tools
|
|
71
|
+
npx -y mcp-browser-dev-tools serve
|
|
74
72
|
```
|
|
75
73
|
|
|
76
74
|
`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
75
|
|
|
76
|
+
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:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
MCP_BROWSER_FAMILY=auto \
|
|
80
|
+
CDP_BASE_URL=http://127.0.0.1:9223 \
|
|
81
|
+
FIREFOX_BIDI_WS_URL=ws://127.0.0.1:9222 \
|
|
82
|
+
npx -y mcp-browser-dev-tools serve
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Use distinct ports in `auto` mode. A practical local split is Firefox on `9222` and Chrome or Edge on `9223`.
|
|
86
|
+
|
|
78
87
|
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
88
|
|
|
80
89
|
```bash
|
|
81
|
-
CDP_BASE_URL=http://127.0.0.1:9222 npx -y mcp-browser-dev-tools
|
|
90
|
+
CDP_BASE_URL=http://127.0.0.1:9222 npx -y mcp-browser-dev-tools doctor
|
|
82
91
|
```
|
|
83
92
|
|
|
84
93
|
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 +97,7 @@ If you need to bridge Windows Chrome into WSL without changing WSL networking mo
|
|
|
88
97
|
Use the same server command across MCP clients:
|
|
89
98
|
|
|
90
99
|
```bash
|
|
91
|
-
npx -y mcp-browser-dev-tools
|
|
100
|
+
npx -y mcp-browser-dev-tools serve
|
|
92
101
|
```
|
|
93
102
|
|
|
94
103
|
If you installed the package already, the shorter equivalent is:
|
|
@@ -103,9 +112,10 @@ Add the server with the Codex CLI:
|
|
|
103
112
|
|
|
104
113
|
```bash
|
|
105
114
|
codex mcp add browser-devtools \
|
|
106
|
-
--env MCP_BROWSER_FAMILY=
|
|
107
|
-
--env CDP_BASE_URL=http://127.0.0.1:
|
|
108
|
-
--
|
|
115
|
+
--env MCP_BROWSER_FAMILY=auto \
|
|
116
|
+
--env CDP_BASE_URL=http://127.0.0.1:9223 \
|
|
117
|
+
--env FIREFOX_BIDI_WS_URL=ws://127.0.0.1:9222 \
|
|
118
|
+
-- npx -y mcp-browser-dev-tools serve
|
|
109
119
|
```
|
|
110
120
|
|
|
111
121
|
Equivalent `~/.codex/config.toml` entry:
|
|
@@ -113,28 +123,52 @@ Equivalent `~/.codex/config.toml` entry:
|
|
|
113
123
|
```toml
|
|
114
124
|
[mcp_servers.browser-devtools]
|
|
115
125
|
command = "npx"
|
|
116
|
-
args = ["-y", "mcp-browser-dev-tools
|
|
126
|
+
args = ["-y", "mcp-browser-dev-tools", "serve"]
|
|
117
127
|
|
|
118
128
|
[mcp_servers.browser-devtools.env]
|
|
119
|
-
MCP_BROWSER_FAMILY = "
|
|
120
|
-
CDP_BASE_URL = "http://127.0.0.1:
|
|
129
|
+
MCP_BROWSER_FAMILY = "auto"
|
|
130
|
+
CDP_BASE_URL = "http://127.0.0.1:9223"
|
|
131
|
+
FIREFOX_BIDI_WS_URL = "ws://127.0.0.1:9222"
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
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.
|
|
135
|
+
|
|
136
|
+
Example `~/.codex/config.toml` entry for Firefox on Windows:
|
|
137
|
+
|
|
138
|
+
```toml
|
|
139
|
+
[mcp_servers.browser-devtools]
|
|
140
|
+
startup_timeout_sec = 30
|
|
141
|
+
command = "/mnt/c/nvm4w/nodejs/node.exe"
|
|
142
|
+
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); });"]
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Example `auto` mode entry for Firefox plus Chrome or Edge on Windows:
|
|
146
|
+
|
|
147
|
+
```toml
|
|
148
|
+
[mcp_servers.browser-devtools]
|
|
149
|
+
startup_timeout_sec = 30
|
|
150
|
+
command = "/mnt/c/nvm4w/nodejs/node.exe"
|
|
151
|
+
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
152
|
```
|
|
122
153
|
|
|
154
|
+
Replace `<wsl-distro>` and `<repo-wsl-path>` with your own WSL distro name and repository path.
|
|
155
|
+
|
|
123
156
|
### Claude Code
|
|
124
157
|
|
|
125
158
|
Add the server with Claude Code:
|
|
126
159
|
|
|
127
160
|
```bash
|
|
128
161
|
claude mcp add browser-devtools --scope user \
|
|
129
|
-
--env MCP_BROWSER_FAMILY=
|
|
130
|
-
--env CDP_BASE_URL=http://127.0.0.1:
|
|
131
|
-
--
|
|
162
|
+
--env MCP_BROWSER_FAMILY=auto \
|
|
163
|
+
--env CDP_BASE_URL=http://127.0.0.1:9223 \
|
|
164
|
+
--env FIREFOX_BIDI_WS_URL=ws://127.0.0.1:9222 \
|
|
165
|
+
-- npx -y mcp-browser-dev-tools serve
|
|
132
166
|
```
|
|
133
167
|
|
|
134
168
|
On native Windows, wrap `npx` with `cmd /c`:
|
|
135
169
|
|
|
136
170
|
```powershell
|
|
137
|
-
claude mcp add browser-devtools --scope user --env MCP_BROWSER_FAMILY=
|
|
171
|
+
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
172
|
```
|
|
139
173
|
|
|
140
174
|
Equivalent `.mcp.json` shape:
|
|
@@ -144,10 +178,11 @@ Equivalent `.mcp.json` shape:
|
|
|
144
178
|
"mcpServers": {
|
|
145
179
|
"browser-devtools": {
|
|
146
180
|
"command": "npx",
|
|
147
|
-
"args": ["-y", "mcp-browser-dev-tools
|
|
181
|
+
"args": ["-y", "mcp-browser-dev-tools", "serve"],
|
|
148
182
|
"env": {
|
|
149
|
-
"MCP_BROWSER_FAMILY": "
|
|
150
|
-
"CDP_BASE_URL": "http://127.0.0.1:
|
|
183
|
+
"MCP_BROWSER_FAMILY": "auto",
|
|
184
|
+
"CDP_BASE_URL": "http://127.0.0.1:9223",
|
|
185
|
+
"FIREFOX_BIDI_WS_URL": "ws://127.0.0.1:9222"
|
|
151
186
|
}
|
|
152
187
|
}
|
|
153
188
|
}
|
|
@@ -165,8 +200,9 @@ Cursor reads MCP servers from `mcp.json`:
|
|
|
165
200
|
"command": "npx",
|
|
166
201
|
"args": ["-y", "mcp-browser-dev-tools", "serve"],
|
|
167
202
|
"env": {
|
|
168
|
-
"MCP_BROWSER_FAMILY": "
|
|
169
|
-
"CDP_BASE_URL": "http://127.0.0.1:
|
|
203
|
+
"MCP_BROWSER_FAMILY": "auto",
|
|
204
|
+
"CDP_BASE_URL": "http://127.0.0.1:9223",
|
|
205
|
+
"FIREFOX_BIDI_WS_URL": "ws://127.0.0.1:9222"
|
|
170
206
|
}
|
|
171
207
|
}
|
|
172
208
|
}
|
|
@@ -175,6 +211,16 @@ Cursor reads MCP servers from `mcp.json`:
|
|
|
175
211
|
|
|
176
212
|
### Common Variants
|
|
177
213
|
|
|
214
|
+
Auto mode with both browsers attached to one MCP server:
|
|
215
|
+
|
|
216
|
+
```json
|
|
217
|
+
{
|
|
218
|
+
"MCP_BROWSER_FAMILY": "auto",
|
|
219
|
+
"CDP_BASE_URL": "http://127.0.0.1:9223",
|
|
220
|
+
"FIREFOX_BIDI_WS_URL": "ws://127.0.0.1:9222"
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
178
224
|
Firefox:
|
|
179
225
|
|
|
180
226
|
```json
|
|
@@ -184,6 +230,15 @@ Firefox:
|
|
|
184
230
|
}
|
|
185
231
|
```
|
|
186
232
|
|
|
233
|
+
Microsoft Edge:
|
|
234
|
+
|
|
235
|
+
```json
|
|
236
|
+
{
|
|
237
|
+
"MCP_BROWSER_FAMILY": "edge",
|
|
238
|
+
"CDP_BASE_URL": "http://127.0.0.1:9222"
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
187
242
|
Windows browser bridged into WSL through the relay:
|
|
188
243
|
|
|
189
244
|
```json
|
|
@@ -194,6 +249,8 @@ Windows browser bridged into WSL through the relay:
|
|
|
194
249
|
}
|
|
195
250
|
```
|
|
196
251
|
|
|
252
|
+
If you only want a single CDP browser, switch `MCP_BROWSER_FAMILY` back to `chromium` or `edge` and omit `FIREFOX_BIDI_WS_URL`.
|
|
253
|
+
|
|
197
254
|
Enable `evaluate_js`:
|
|
198
255
|
|
|
199
256
|
```json
|
|
@@ -217,15 +274,17 @@ Examples:
|
|
|
217
274
|
mbdt doctor
|
|
218
275
|
mbdt doctor --url http://127.0.0.1:3000
|
|
219
276
|
mbdt open http://127.0.0.1:3000 --family chromium
|
|
277
|
+
mbdt open http://127.0.0.1:3000 --family edge --port 9223 --user-data-dir /tmp/mbdt-edge
|
|
220
278
|
mbdt open about:blank --family firefox --user-data-dir /tmp/mbdt-firefox
|
|
221
279
|
mbdt relay --wsl
|
|
222
280
|
```
|
|
223
281
|
|
|
224
282
|
## Configuration
|
|
225
283
|
|
|
226
|
-
- `MCP_BROWSER_FAMILY` defaults to `chromium`; set `firefox` for Firefox BiDi
|
|
284
|
+
- `MCP_BROWSER_FAMILY` defaults to `chromium`; set `edge` for Microsoft Edge, `firefox` for Firefox BiDi, or `auto` to multiplex both adapters
|
|
227
285
|
- `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
|
|
286
|
+
- `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
|
|
287
|
+
- in `auto` mode, assign CDP and Firefox different ports so both browsers can run at once
|
|
229
288
|
- `MCP_BROWSER_EVENT_BUFFER_SIZE` sets the per-session buffered event limit
|
|
230
289
|
- `MCP_BROWSER_ENABLE_EVAL=1` enables `evaluate_js`
|
|
231
290
|
- `MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1` allows non-loopback CDP or BiDi endpoints
|
|
@@ -238,9 +297,12 @@ The `relay` command defaults to `127.0.0.1:9223 -> 127.0.0.1:9222`. Non-loopback
|
|
|
238
297
|
|
|
239
298
|
## Exposed Tools
|
|
240
299
|
|
|
241
|
-
- `browser_status`
|
|
300
|
+
- `browser_status` returns broker metadata too: `serverName` and `serverVersion`
|
|
301
|
+
In `auto` mode it also includes per-browser adapter status under `browsers`
|
|
242
302
|
- `list_tabs`
|
|
303
|
+
In `auto` mode each `targetId` is namespaced as `chromium:<id>` or `firefox:<id>`
|
|
243
304
|
- `list_sessions`
|
|
305
|
+
In `auto` mode each `sessionId` is namespaced the same way
|
|
244
306
|
- `attach_tab`
|
|
245
307
|
- `detach_tab`
|
|
246
308
|
- `get_page_state`
|
package/package.json
CHANGED
package/src/browser-adapter.mjs
CHANGED
|
@@ -1,10 +1,285 @@
|
|
|
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 delegateSession(sessionId, methodName, args = []) {
|
|
165
|
+
const { browserFamily, localId } = resolvePrefixedId(
|
|
166
|
+
sessionId,
|
|
167
|
+
"sessionId",
|
|
168
|
+
);
|
|
169
|
+
const adapter = this.getAdapter(browserFamily);
|
|
170
|
+
const result = await adapter[methodName](localId, ...args);
|
|
171
|
+
|
|
172
|
+
if (result === undefined) {
|
|
173
|
+
return result;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (methodName === "getConsoleMessages" || methodName === "getEvents") {
|
|
177
|
+
return mapPrefixedFields(result, browserFamily);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (methodName === "getNetworkRequests") {
|
|
181
|
+
return mapPrefixedFields(result, browserFamily);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return mapPrefixedFields(result, browserFamily);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
getPageState(sessionId) {
|
|
188
|
+
return this.delegateSession(sessionId, "getPageState");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
navigate(sessionId, url, options) {
|
|
192
|
+
return this.delegateSession(sessionId, "navigate", [url, options]);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
reload(sessionId, options) {
|
|
196
|
+
return this.delegateSession(sessionId, "reload", [options]);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
click(sessionId, selector) {
|
|
200
|
+
return this.delegateSession(sessionId, "click", [selector]);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
hover(sessionId, selector) {
|
|
204
|
+
return this.delegateSession(sessionId, "hover", [selector]);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
type(sessionId, selector, text, options) {
|
|
208
|
+
return this.delegateSession(sessionId, "type", [selector, text, options]);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
select(sessionId, selector, options) {
|
|
212
|
+
return this.delegateSession(sessionId, "select", [selector, options]);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
pressKey(sessionId, key, selector) {
|
|
216
|
+
return this.delegateSession(sessionId, "pressKey", [key, selector]);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
scroll(sessionId, options) {
|
|
220
|
+
return this.delegateSession(sessionId, "scroll", [options]);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
setViewport(sessionId, options) {
|
|
224
|
+
return this.delegateSession(sessionId, "setViewport", [options]);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
evaluate(sessionId, expression) {
|
|
228
|
+
return this.delegateSession(sessionId, "evaluate", [expression]);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
getDocument(sessionId, depth) {
|
|
232
|
+
return this.delegateSession(sessionId, "getDocument", [depth]);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
getConsoleMessages(sessionId, limit) {
|
|
236
|
+
return this.delegateSession(sessionId, "getConsoleMessages", [limit]);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
getNetworkRequests(sessionId, limit) {
|
|
240
|
+
return this.delegateSession(sessionId, "getNetworkRequests", [limit]);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
inspectElement(sessionId, selector) {
|
|
244
|
+
return this.delegateSession(sessionId, "inspectElement", [selector]);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
takeScreenshot(sessionId, format, options) {
|
|
248
|
+
return this.delegateSession(sessionId, "takeScreenshot", [format, options]);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
getEvents(sessionId, limit) {
|
|
252
|
+
return this.delegateSession(sessionId, "getEvents", [limit]);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
async closeAll() {
|
|
256
|
+
await Promise.all(
|
|
257
|
+
Object.values(this.adapters).map((adapter) => adapter.closeAll()),
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
4
262
|
export function createBrowserAdapter(config) {
|
|
5
263
|
if (config.browserFamily === "firefox") {
|
|
6
264
|
return new FirefoxBidiSessionManager(config);
|
|
7
265
|
}
|
|
8
266
|
|
|
267
|
+
if (config.browserFamily === "auto") {
|
|
268
|
+
return new MultiBrowserAdapter({
|
|
269
|
+
chromium: new CdpSessionManager({
|
|
270
|
+
...config,
|
|
271
|
+
browserFamily: "chromium",
|
|
272
|
+
}),
|
|
273
|
+
firefox: new FirefoxBidiSessionManager({
|
|
274
|
+
...config,
|
|
275
|
+
browserFamily: "firefox",
|
|
276
|
+
}),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (config.browserFamily === "edge") {
|
|
281
|
+
return new CdpSessionManager(config);
|
|
282
|
+
}
|
|
283
|
+
|
|
9
284
|
return new CdpSessionManager(config);
|
|
10
285
|
}
|
package/src/browser-launcher.mjs
CHANGED
|
@@ -37,6 +37,36 @@ export function getBrowserCandidates(
|
|
|
37
37
|
env = process.env,
|
|
38
38
|
platform = detectPlatform(env),
|
|
39
39
|
) {
|
|
40
|
+
if (family === "edge") {
|
|
41
|
+
if (platform === "darwin") {
|
|
42
|
+
return [
|
|
43
|
+
"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
|
44
|
+
"microsoft-edge",
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (platform === "wsl") {
|
|
49
|
+
return [
|
|
50
|
+
"microsoft-edge",
|
|
51
|
+
"microsoft-edge-stable",
|
|
52
|
+
"/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe",
|
|
53
|
+
"/mnt/c/Program Files/Microsoft/Edge/Application/msedge.exe",
|
|
54
|
+
"msedge.exe",
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (platform === "win32") {
|
|
59
|
+
return [
|
|
60
|
+
"C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe",
|
|
61
|
+
"C:\\Program Files\\Microsoft\\Edge\\Application\\msedge.exe",
|
|
62
|
+
"msedge.exe",
|
|
63
|
+
"microsoft-edge",
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return ["microsoft-edge", "microsoft-edge-stable", "msedge"];
|
|
68
|
+
}
|
|
69
|
+
|
|
40
70
|
if (family === "firefox") {
|
|
41
71
|
if (platform === "darwin") {
|
|
42
72
|
return ["/Applications/Firefox.app/Contents/MacOS/firefox", "firefox"];
|
|
@@ -128,10 +158,12 @@ export async function findBrowserExecutable(family, env = process.env) {
|
|
|
128
158
|
|
|
129
159
|
export async function detectInstalledBrowsers(env = process.env) {
|
|
130
160
|
const chromium = await findBrowserExecutable("chromium", env);
|
|
161
|
+
const edge = await findBrowserExecutable("edge", env);
|
|
131
162
|
const firefox = await findBrowserExecutable("firefox", env);
|
|
132
163
|
|
|
133
164
|
return {
|
|
134
165
|
chromium,
|
|
166
|
+
edge,
|
|
135
167
|
firefox,
|
|
136
168
|
};
|
|
137
169
|
}
|
package/src/cdp-client.mjs
CHANGED
|
@@ -19,6 +19,10 @@ function normalizeTarget(target) {
|
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
function cdpBrowserFamily(config) {
|
|
23
|
+
return config.browserFamily === "edge" ? "edge" : "chromium";
|
|
24
|
+
}
|
|
25
|
+
|
|
22
26
|
function summarizeRemoteObject(object) {
|
|
23
27
|
if (!object) {
|
|
24
28
|
return null;
|
|
@@ -294,6 +298,7 @@ async function readWebSocketData(data) {
|
|
|
294
298
|
export class CdpSession {
|
|
295
299
|
constructor(target, options = {}) {
|
|
296
300
|
this.id = crypto.randomUUID();
|
|
301
|
+
this.config = options.config ?? { browserFamily: "chromium" };
|
|
297
302
|
this.target = target;
|
|
298
303
|
this.eventBufferSize = options.eventBufferSize ?? 200;
|
|
299
304
|
this.onClosed = options.onClosed ?? null;
|
|
@@ -562,7 +567,7 @@ export class CdpSession {
|
|
|
562
567
|
const result = await this.evaluateRuntime(
|
|
563
568
|
buildPageContextExpression(
|
|
564
569
|
{
|
|
565
|
-
browserFamily:
|
|
570
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
566
571
|
...payload,
|
|
567
572
|
},
|
|
568
573
|
{ serialize: true },
|
|
@@ -588,7 +593,7 @@ export class CdpSession {
|
|
|
588
593
|
});
|
|
589
594
|
|
|
590
595
|
return {
|
|
591
|
-
browserFamily:
|
|
596
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
592
597
|
...result.page,
|
|
593
598
|
lastNavigationAt: this.lastNavigationAt,
|
|
594
599
|
lastReloadAt: this.lastReloadAt,
|
|
@@ -606,7 +611,7 @@ export class CdpSession {
|
|
|
606
611
|
|
|
607
612
|
if (!inspected.found) {
|
|
608
613
|
return {
|
|
609
|
-
browserFamily:
|
|
614
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
610
615
|
format,
|
|
611
616
|
scope: "element",
|
|
612
617
|
selector: options.selector,
|
|
@@ -628,7 +633,7 @@ export class CdpSession {
|
|
|
628
633
|
});
|
|
629
634
|
|
|
630
635
|
return formatScreenshotResult(screenshot.data, format, {
|
|
631
|
-
browserFamily:
|
|
636
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
632
637
|
scope: "element",
|
|
633
638
|
selector: options.selector,
|
|
634
639
|
clip,
|
|
@@ -637,7 +642,7 @@ export class CdpSession {
|
|
|
637
642
|
|
|
638
643
|
const screenshot = await this.send("Page.captureScreenshot", { format });
|
|
639
644
|
return formatScreenshotResult(screenshot.data, format, {
|
|
640
|
-
browserFamily:
|
|
645
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
641
646
|
scope: "page",
|
|
642
647
|
});
|
|
643
648
|
}
|
|
@@ -746,7 +751,7 @@ export class CdpSession {
|
|
|
746
751
|
|
|
747
752
|
const page = lifecycleWaiter ? await this.getPageState() : null;
|
|
748
753
|
return {
|
|
749
|
-
browserFamily:
|
|
754
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
750
755
|
url,
|
|
751
756
|
frameId: result.frameId ?? null,
|
|
752
757
|
loaderId: result.loaderId ?? null,
|
|
@@ -784,7 +789,7 @@ export class CdpSession {
|
|
|
784
789
|
this.lastReloadAt = reloadedAt;
|
|
785
790
|
const page = lifecycleWaiter ? await this.getPageState() : null;
|
|
786
791
|
return {
|
|
787
|
-
browserFamily:
|
|
792
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
788
793
|
url: this.target.url,
|
|
789
794
|
waitUntil,
|
|
790
795
|
ignoreCache: options.ignoreCache ?? false,
|
|
@@ -812,7 +817,7 @@ export class CdpSession {
|
|
|
812
817
|
};
|
|
813
818
|
|
|
814
819
|
return {
|
|
815
|
-
browserFamily:
|
|
820
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
816
821
|
applied: true,
|
|
817
822
|
viewport: this.viewportOverride,
|
|
818
823
|
page: await this.getPageState(),
|
|
@@ -855,7 +860,7 @@ export class CdpSession {
|
|
|
855
860
|
targetId: this.target.targetId,
|
|
856
861
|
title: this.target.title,
|
|
857
862
|
url: this.target.url,
|
|
858
|
-
browserFamily:
|
|
863
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
859
864
|
connectedAt: this.connectedAt,
|
|
860
865
|
bufferedEvents: this.bufferedEvents.length,
|
|
861
866
|
lastNavigationAt: this.lastNavigationAt,
|
|
@@ -928,6 +933,7 @@ export class CdpSessionManager {
|
|
|
928
933
|
}
|
|
929
934
|
|
|
930
935
|
const session = new CdpSession(target, {
|
|
936
|
+
config: this.config,
|
|
931
937
|
eventBufferSize: this.config.eventBufferSize,
|
|
932
938
|
onClosed: (closedSession) => {
|
|
933
939
|
this.sessions.delete(closedSession.id);
|
package/src/cli.mjs
CHANGED
|
@@ -67,7 +67,7 @@ function printUsage() {
|
|
|
67
67
|
" --version Print the package version",
|
|
68
68
|
"",
|
|
69
69
|
"Open options:",
|
|
70
|
-
" --family <name> chromium or firefox",
|
|
70
|
+
" --family <name> chromium, edge, or firefox",
|
|
71
71
|
" --port <port> remote debugging port",
|
|
72
72
|
" --address <host> remote debugging address for Chromium",
|
|
73
73
|
" --user-data-dir <p> optional Chromium user data dir or Firefox profile dir",
|
|
@@ -217,7 +217,8 @@ async function runOpen(positional, options) {
|
|
|
217
217
|
const family =
|
|
218
218
|
typeof options.family === "string" ? options.family : config.browserFamily;
|
|
219
219
|
const requestedAddress =
|
|
220
|
-
family === "chromium"
|
|
220
|
+
(family === "chromium" || family === "edge") &&
|
|
221
|
+
typeof options.address === "string"
|
|
221
222
|
? options.address.trim()
|
|
222
223
|
: null;
|
|
223
224
|
|
package/src/config.mjs
CHANGED
|
@@ -3,6 +3,7 @@ export const DEFAULT_FIREFOX_BIDI_WS_URL = "ws://127.0.0.1:9222";
|
|
|
3
3
|
export const DEFAULT_EVENT_BUFFER_SIZE = 200;
|
|
4
4
|
export const DEFAULT_PROTOCOL_VERSION = "2024-11-05";
|
|
5
5
|
export const DEFAULT_BROWSER_FAMILY = "chromium";
|
|
6
|
+
export const CDP_BROWSER_FAMILIES = ["chromium", "edge"];
|
|
6
7
|
|
|
7
8
|
export function isLoopbackHost(value) {
|
|
8
9
|
return ["127.0.0.1", "localhost", "::1", "[::1]"].includes(value);
|
|
@@ -46,7 +47,12 @@ export function isLoopbackOrigin(value) {
|
|
|
46
47
|
|
|
47
48
|
export function parseBrowserFamily(value) {
|
|
48
49
|
const normalized = value?.trim().toLowerCase();
|
|
49
|
-
if (
|
|
50
|
+
if (
|
|
51
|
+
normalized === "firefox" ||
|
|
52
|
+
normalized === "chromium" ||
|
|
53
|
+
normalized === "edge" ||
|
|
54
|
+
normalized === "auto"
|
|
55
|
+
) {
|
|
50
56
|
return normalized;
|
|
51
57
|
}
|
|
52
58
|
|
|
@@ -58,15 +64,24 @@ export function loadConfig(env = process.env) {
|
|
|
58
64
|
const allowRemoteEndpoints = allowsRemoteEndpoints(env);
|
|
59
65
|
const cdpBaseUrl = normalizeBaseUrl(env.CDP_BASE_URL);
|
|
60
66
|
const firefoxBidiWsUrl = normalizeWebSocketUrl(env.FIREFOX_BIDI_WS_URL);
|
|
61
|
-
const
|
|
62
|
-
browserFamily === "firefox"
|
|
67
|
+
const endpointsToValidate =
|
|
68
|
+
browserFamily === "firefox"
|
|
69
|
+
? [["FIREFOX_BIDI_WS_URL", firefoxBidiWsUrl]]
|
|
70
|
+
: browserFamily === "auto"
|
|
71
|
+
? [
|
|
72
|
+
["CDP_BASE_URL", cdpBaseUrl],
|
|
73
|
+
["FIREFOX_BIDI_WS_URL", firefoxBidiWsUrl],
|
|
74
|
+
]
|
|
75
|
+
: [["CDP_BASE_URL", cdpBaseUrl]];
|
|
63
76
|
|
|
64
|
-
if (!allowRemoteEndpoints
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
77
|
+
if (!allowRemoteEndpoints) {
|
|
78
|
+
for (const [name, endpoint] of endpointsToValidate) {
|
|
79
|
+
if (!isLoopbackOrigin(endpoint)) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
`${name} must point to a loopback host unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1`,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
70
85
|
}
|
|
71
86
|
|
|
72
87
|
return {
|
package/src/doctor.mjs
CHANGED
|
@@ -4,6 +4,21 @@ import { createBrowserAdapter } from "./browser-adapter.mjs";
|
|
|
4
4
|
import { loadConfig } from "./config.mjs";
|
|
5
5
|
import { detectInstalledBrowsers } from "./browser-launcher.mjs";
|
|
6
6
|
|
|
7
|
+
function reportEndpoints(config) {
|
|
8
|
+
if (config.browserFamily === "firefox") {
|
|
9
|
+
return config.firefoxBidiWsUrl;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (config.browserFamily === "auto") {
|
|
13
|
+
return {
|
|
14
|
+
chromium: config.cdpBaseUrl,
|
|
15
|
+
firefox: config.firefoxBidiWsUrl,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return config.cdpBaseUrl;
|
|
20
|
+
}
|
|
21
|
+
|
|
7
22
|
async function probeUrl(url) {
|
|
8
23
|
try {
|
|
9
24
|
const response = await fetch(url);
|
|
@@ -37,10 +52,7 @@ export async function collectDoctorReport({
|
|
|
37
52
|
platform: process.platform,
|
|
38
53
|
release: os.release(),
|
|
39
54
|
browserFamily: config.browserFamily,
|
|
40
|
-
endpoint:
|
|
41
|
-
config.browserFamily === "firefox"
|
|
42
|
-
? config.firefoxBidiWsUrl
|
|
43
|
-
: config.cdpBaseUrl,
|
|
55
|
+
endpoint: reportEndpoints(config),
|
|
44
56
|
display: {
|
|
45
57
|
DISPLAY: env.DISPLAY || null,
|
|
46
58
|
WAYLAND_DISPLAY: env.WAYLAND_DISPLAY || null,
|
|
@@ -56,14 +68,22 @@ export async function collectDoctorReport({
|
|
|
56
68
|
}
|
|
57
69
|
|
|
58
70
|
export function renderDoctorReport(report) {
|
|
71
|
+
const endpointLines =
|
|
72
|
+
typeof report.endpoint === "string"
|
|
73
|
+
? [`browser endpoint: ${report.endpoint}`]
|
|
74
|
+
: Object.entries(report.endpoint).map(
|
|
75
|
+
([browserFamily, endpoint]) =>
|
|
76
|
+
`${browserFamily} endpoint: ${endpoint}`,
|
|
77
|
+
);
|
|
59
78
|
const lines = [
|
|
60
79
|
`platform: ${report.platform}`,
|
|
61
80
|
`release: ${report.release}`,
|
|
62
81
|
`browser family: ${report.browserFamily}`,
|
|
63
|
-
|
|
82
|
+
...endpointLines,
|
|
64
83
|
`DISPLAY: ${report.display.DISPLAY ?? "-"}`,
|
|
65
84
|
`WAYLAND_DISPLAY: ${report.display.WAYLAND_DISPLAY ?? "-"}`,
|
|
66
85
|
`chromium executable: ${report.installedBrowsers.chromium ?? "not found"}`,
|
|
86
|
+
`edge executable: ${report.installedBrowsers.edge ?? "not found"}`,
|
|
67
87
|
`firefox executable: ${report.installedBrowsers.firefox ?? "not found"}`,
|
|
68
88
|
`adapter available: ${report.browserStatus.available}`,
|
|
69
89
|
];
|
package/src/index.mjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export { createBrowserDevToolsApp } from "./app.mjs";
|
|
2
|
-
export {
|
|
2
|
+
export {
|
|
3
|
+
createBrowserAdapter,
|
|
4
|
+
MultiBrowserAdapter,
|
|
5
|
+
} from "./browser-adapter.mjs";
|
|
3
6
|
export {
|
|
4
7
|
buildBrowserLaunchArgs,
|
|
5
8
|
detectInstalledBrowsers,
|
package/src/mcp-server.mjs
CHANGED
|
@@ -201,7 +201,11 @@ export class McpBrowserDevToolsServer {
|
|
|
201
201
|
"Report whether the configured browser endpoint is reachable and how many active sessions are attached.",
|
|
202
202
|
inputSchema: emptyObjectSchema(),
|
|
203
203
|
},
|
|
204
|
-
handler: async () =>
|
|
204
|
+
handler: async () => ({
|
|
205
|
+
serverName: SERVER_NAME,
|
|
206
|
+
serverVersion: SERVER_VERSION,
|
|
207
|
+
...(await this.browserAdapter.getBrowserStatus()),
|
|
208
|
+
}),
|
|
205
209
|
},
|
|
206
210
|
],
|
|
207
211
|
[
|