mcp-browser-dev-tools 0.0.1-beta.4 → 0.0.1-beta.6
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 +2 -1
- package/package.json +1 -1
- package/src/browser-launcher.mjs +3 -0
- package/src/cli.mjs +11 -5
- package/src/firefox-bidi-client.mjs +108 -7
- package/src/page-context.mjs +18 -3
package/README.md
CHANGED
|
@@ -217,6 +217,7 @@ Examples:
|
|
|
217
217
|
mbdt doctor
|
|
218
218
|
mbdt doctor --url http://127.0.0.1:3000
|
|
219
219
|
mbdt open http://127.0.0.1:3000 --family chromium
|
|
220
|
+
mbdt open about:blank --family firefox --user-data-dir /tmp/mbdt-firefox
|
|
220
221
|
mbdt relay --wsl
|
|
221
222
|
```
|
|
222
223
|
|
|
@@ -224,7 +225,7 @@ mbdt relay --wsl
|
|
|
224
225
|
|
|
225
226
|
- `MCP_BROWSER_FAMILY` defaults to `chromium`; set `firefox` for Firefox BiDi
|
|
226
227
|
- `CDP_BASE_URL` defaults to `http://127.0.0.1:9222`
|
|
227
|
-
- `FIREFOX_BIDI_WS_URL` defaults to `ws://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
|
|
228
229
|
- `MCP_BROWSER_EVENT_BUFFER_SIZE` sets the per-session buffered event limit
|
|
229
230
|
- `MCP_BROWSER_ENABLE_EVAL=1` enables `evaluate_js`
|
|
230
231
|
- `MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1` allows non-loopback CDP or BiDi endpoints
|
package/package.json
CHANGED
package/src/browser-launcher.mjs
CHANGED
|
@@ -149,6 +149,9 @@ export function buildBrowserLaunchArgs({
|
|
|
149
149
|
|
|
150
150
|
const args = [];
|
|
151
151
|
if (family === "firefox") {
|
|
152
|
+
if (userDataDir) {
|
|
153
|
+
args.push("-new-instance", "-profile", userDataDir);
|
|
154
|
+
}
|
|
152
155
|
if (remoteDebuggingPort) {
|
|
153
156
|
args.push(`--remote-debugging-port=${remoteDebuggingPort}`);
|
|
154
157
|
}
|
package/src/cli.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
|
+
import { mkdir } from "node:fs/promises";
|
|
3
4
|
import process from "node:process";
|
|
4
5
|
import { pathToFileURL } from "node:url";
|
|
5
6
|
|
|
@@ -68,7 +69,7 @@ function printUsage() {
|
|
|
68
69
|
" --family <name> chromium or firefox",
|
|
69
70
|
" --port <port> remote debugging port",
|
|
70
71
|
" --address <host> remote debugging address for Chromium",
|
|
71
|
-
" --user-data-dir <p> optional
|
|
72
|
+
" --user-data-dir <p> optional Chromium user data dir or Firefox profile dir",
|
|
72
73
|
" --wait-ms <ms> wait for the debug endpoint and print doctor output",
|
|
73
74
|
" --no-doctor skip the post-launch doctor check",
|
|
74
75
|
"",
|
|
@@ -156,16 +157,21 @@ async function runOpen(positional, options) {
|
|
|
156
157
|
: new URL(config.cdpBaseUrl).port || "9222";
|
|
157
158
|
const resolvedPort =
|
|
158
159
|
typeof options.port === "string" ? options.port : defaultPort;
|
|
160
|
+
const userDataDir =
|
|
161
|
+
typeof options["user-data-dir"] === "string"
|
|
162
|
+
? options["user-data-dir"]
|
|
163
|
+
: undefined;
|
|
164
|
+
|
|
165
|
+
if (family === "firefox" && userDataDir) {
|
|
166
|
+
await mkdir(userDataDir, { recursive: true });
|
|
167
|
+
}
|
|
159
168
|
|
|
160
169
|
const args = buildBrowserLaunchArgs({
|
|
161
170
|
family,
|
|
162
171
|
url,
|
|
163
172
|
remoteDebuggingPort: resolvedPort,
|
|
164
173
|
remoteDebuggingAddress: requestedAddress || undefined,
|
|
165
|
-
userDataDir
|
|
166
|
-
family === "chromium" && typeof options["user-data-dir"] === "string"
|
|
167
|
-
? options["user-data-dir"]
|
|
168
|
-
: undefined,
|
|
174
|
+
userDataDir,
|
|
169
175
|
});
|
|
170
176
|
|
|
171
177
|
const child = spawn(executable, args, {
|
|
@@ -239,6 +239,37 @@ function normalizeRequestedDepth(depth) {
|
|
|
239
239
|
return Number.isInteger(depth) && depth > 0 ? depth : 2;
|
|
240
240
|
}
|
|
241
241
|
|
|
242
|
+
function isWebSocketProtocol(protocol) {
|
|
243
|
+
return protocol === "ws:" || protocol === "wss:";
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function isHttpProtocol(protocol) {
|
|
247
|
+
return protocol === "http:" || protocol === "https:";
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function isRootPathname(pathname) {
|
|
251
|
+
return pathname === "" || pathname === "/";
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function toHttpEndpointUrl(endpointUrl) {
|
|
255
|
+
const url = new URL(endpointUrl);
|
|
256
|
+
if (url.protocol === "ws:") {
|
|
257
|
+
url.protocol = "http:";
|
|
258
|
+
} else if (url.protocol === "wss:") {
|
|
259
|
+
url.protocol = "https:";
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return url;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function joinUrlPath(baseUrl, pathname) {
|
|
266
|
+
const url = new URL(baseUrl);
|
|
267
|
+
const basePath = url.pathname.replace(/\/+$/, "");
|
|
268
|
+
const nextPath = pathname.replace(/^\/+/, "");
|
|
269
|
+
url.pathname = `${basePath}/${nextPath}`.replace(/\/{2,}/g, "/");
|
|
270
|
+
return url;
|
|
271
|
+
}
|
|
272
|
+
|
|
242
273
|
class FirefoxBidiTabSession {
|
|
243
274
|
constructor(target, options = {}) {
|
|
244
275
|
this.id = crypto.randomUUID();
|
|
@@ -310,12 +341,14 @@ export class FirefoxBidiSessionManager {
|
|
|
310
341
|
this.sessions = new Map();
|
|
311
342
|
this.websocketFactory =
|
|
312
343
|
options.websocketFactory ?? ((url) => new WebSocket(url));
|
|
344
|
+
this.fetchImpl = options.fetchImpl ?? fetch;
|
|
313
345
|
this.pending = new Map();
|
|
314
346
|
this.nextMessageId = 1;
|
|
315
347
|
this.websocket = null;
|
|
316
348
|
this.browserSessionId = null;
|
|
317
349
|
this.capabilities = null;
|
|
318
350
|
this.connectPromise = null;
|
|
351
|
+
this.resolvedWebSocketUrl = null;
|
|
319
352
|
}
|
|
320
353
|
|
|
321
354
|
async ensureConnected() {
|
|
@@ -339,7 +372,9 @@ export class FirefoxBidiSessionManager {
|
|
|
339
372
|
}
|
|
340
373
|
|
|
341
374
|
async openConnection() {
|
|
342
|
-
|
|
375
|
+
const endpoint = await this.resolveConnectionEndpoint();
|
|
376
|
+
this.resolvedWebSocketUrl = endpoint.webSocketUrl;
|
|
377
|
+
this.websocket = this.websocketFactory(endpoint.webSocketUrl);
|
|
343
378
|
|
|
344
379
|
await new Promise((resolve, reject) => {
|
|
345
380
|
let settled = false;
|
|
@@ -387,14 +422,80 @@ export class FirefoxBidiSessionManager {
|
|
|
387
422
|
});
|
|
388
423
|
});
|
|
389
424
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
425
|
+
if (endpoint.requiresSessionNew) {
|
|
426
|
+
const result = await this.send("session.new", {
|
|
427
|
+
capabilities: {
|
|
428
|
+
alwaysMatch: {},
|
|
429
|
+
},
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
this.browserSessionId = result.sessionId;
|
|
433
|
+
this.capabilities = result.capabilities ?? {};
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
this.browserSessionId = endpoint.sessionId;
|
|
438
|
+
this.capabilities = endpoint.capabilities ?? {};
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
async resolveConnectionEndpoint() {
|
|
442
|
+
const configuredUrl = new URL(this.config.firefoxBidiWsUrl);
|
|
443
|
+
if (
|
|
444
|
+
isHttpProtocol(configuredUrl.protocol) ||
|
|
445
|
+
(isWebSocketProtocol(configuredUrl.protocol) &&
|
|
446
|
+
isRootPathname(configuredUrl.pathname))
|
|
447
|
+
) {
|
|
448
|
+
return this.createWebDriverSession(configuredUrl);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return {
|
|
452
|
+
webSocketUrl: configuredUrl.toString(),
|
|
453
|
+
requiresSessionNew: true,
|
|
454
|
+
sessionId: null,
|
|
455
|
+
capabilities: null,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
async createWebDriverSession(endpointUrl) {
|
|
460
|
+
const sessionUrl = joinUrlPath(toHttpEndpointUrl(endpointUrl), "/session");
|
|
461
|
+
const response = await this.fetchImpl(sessionUrl, {
|
|
462
|
+
method: "POST",
|
|
463
|
+
headers: {
|
|
464
|
+
"content-type": "application/json",
|
|
393
465
|
},
|
|
466
|
+
body: JSON.stringify({
|
|
467
|
+
capabilities: {
|
|
468
|
+
alwaysMatch: {
|
|
469
|
+
webSocketUrl: true,
|
|
470
|
+
},
|
|
471
|
+
},
|
|
472
|
+
}),
|
|
394
473
|
});
|
|
395
474
|
|
|
396
|
-
|
|
397
|
-
|
|
475
|
+
if (!response.ok) {
|
|
476
|
+
throw new Error(
|
|
477
|
+
`Failed to create Firefox WebDriver session at ${sessionUrl}: ${response.status} ${response.statusText}`,
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const payload = await response.json();
|
|
482
|
+
const value = payload?.value ?? payload;
|
|
483
|
+
const sessionId = value?.sessionId ?? null;
|
|
484
|
+
const capabilities = value?.capabilities ?? {};
|
|
485
|
+
const webSocketUrl = capabilities?.webSocketUrl ?? null;
|
|
486
|
+
|
|
487
|
+
if (!sessionId || !webSocketUrl) {
|
|
488
|
+
throw new Error(
|
|
489
|
+
`Firefox WebDriver session at ${sessionUrl} did not return a webSocketUrl`,
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
return {
|
|
494
|
+
webSocketUrl,
|
|
495
|
+
requiresSessionNew: false,
|
|
496
|
+
sessionId,
|
|
497
|
+
capabilities,
|
|
498
|
+
};
|
|
398
499
|
}
|
|
399
500
|
|
|
400
501
|
rejectPending(error) {
|
|
@@ -475,7 +576,7 @@ export class FirefoxBidiSessionManager {
|
|
|
475
576
|
browser: this.capabilities?.browserName || "firefox",
|
|
476
577
|
browserVersion: this.capabilities?.browserVersion ?? null,
|
|
477
578
|
protocol: "webdriver-bidi",
|
|
478
|
-
webSocketUrl: this.config.firefoxBidiWsUrl,
|
|
579
|
+
webSocketUrl: this.resolvedWebSocketUrl ?? this.config.firefoxBidiWsUrl,
|
|
479
580
|
};
|
|
480
581
|
} catch (error) {
|
|
481
582
|
return {
|
package/src/page-context.mjs
CHANGED
|
@@ -214,6 +214,21 @@ export function buildPageContextExpression(
|
|
|
214
214
|
);
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
+
function summarizeLocator(locator) {
|
|
218
|
+
if (!locator || typeof locator !== "object") {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const summary = {};
|
|
223
|
+
for (const key of ["locator", "strategy", "query", "role", "name", "error"]) {
|
|
224
|
+
if (locator[key] !== undefined) {
|
|
225
|
+
summary[key] = locator[key];
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return summary;
|
|
230
|
+
}
|
|
231
|
+
|
|
217
232
|
function describeElement(element, locator) {
|
|
218
233
|
const rect = element.getBoundingClientRect();
|
|
219
234
|
const style = getComputedStyle(element);
|
|
@@ -221,7 +236,7 @@ export function buildPageContextExpression(
|
|
|
221
236
|
const accessibleName = getAccessibleName(element);
|
|
222
237
|
|
|
223
238
|
return {
|
|
224
|
-
locator,
|
|
239
|
+
locator: summarizeLocator(locator),
|
|
225
240
|
tagName: element.tagName,
|
|
226
241
|
id: element.getAttribute("id"),
|
|
227
242
|
className: element.getAttribute("class"),
|
|
@@ -413,7 +428,7 @@ export function buildPageContextExpression(
|
|
|
413
428
|
browserFamily: payload.browserFamily,
|
|
414
429
|
selector: rawLocator,
|
|
415
430
|
found: false,
|
|
416
|
-
locator: resolved,
|
|
431
|
+
locator: summarizeLocator(resolved),
|
|
417
432
|
error: resolved.error,
|
|
418
433
|
};
|
|
419
434
|
}
|
|
@@ -423,7 +438,7 @@ export function buildPageContextExpression(
|
|
|
423
438
|
browserFamily: payload.browserFamily,
|
|
424
439
|
selector: rawLocator,
|
|
425
440
|
found: false,
|
|
426
|
-
locator: resolved,
|
|
441
|
+
locator: summarizeLocator(resolved),
|
|
427
442
|
};
|
|
428
443
|
}
|
|
429
444
|
|