mcp-browser-dev-tools 0.0.2-beta.0 → 0.0.2
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 +93 -139
- package/package.json +3 -1
- package/src/app.mjs +22 -1
- package/src/browser-adapter.mjs +27 -0
- package/src/browser-launch-service.mjs +515 -0
- package/src/cdp-client.mjs +261 -5
- package/src/cli.mjs +57 -202
- package/src/config.mjs +1 -1
- package/src/firefox-bidi-client.mjs +180 -16
- package/src/mcp-server.mjs +553 -0
- package/src/page-context.mjs +217 -0
- package/src/serve-bootstrap.mjs +290 -0
- package/src/session-events.mjs +83 -0
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
+
exportHarLikeSummary,
|
|
2
3
|
filterConsoleMessages,
|
|
3
4
|
summarizeNetworkRequests,
|
|
4
5
|
} from "./session-events.mjs";
|
|
6
|
+
import { DEFAULT_FIREFOX_BIDI_WS_URL } from "./config.mjs";
|
|
5
7
|
import { waitForPageCondition } from "./wait-for.mjs";
|
|
6
8
|
import { buildPageContextExpression } from "./page-context.mjs";
|
|
7
9
|
|
|
@@ -259,6 +261,36 @@ function toWebSocketEndpointUrl(endpointUrl) {
|
|
|
259
261
|
return url;
|
|
260
262
|
}
|
|
261
263
|
|
|
264
|
+
function buildDiscoveryWebSocketUrls(configuredUrl) {
|
|
265
|
+
const configured = new URL(configuredUrl);
|
|
266
|
+
const candidatePorts = [
|
|
267
|
+
configured.port || "9222",
|
|
268
|
+
"9223",
|
|
269
|
+
"9224",
|
|
270
|
+
"9225",
|
|
271
|
+
"9226",
|
|
272
|
+
];
|
|
273
|
+
const seen = new Set();
|
|
274
|
+
const candidates = [];
|
|
275
|
+
|
|
276
|
+
for (const port of candidatePorts) {
|
|
277
|
+
const candidate = new URL(configured.toString());
|
|
278
|
+
candidate.port = port;
|
|
279
|
+
candidate.pathname = "/";
|
|
280
|
+
candidate.search = "";
|
|
281
|
+
candidate.hash = "";
|
|
282
|
+
const normalized = candidate.toString().replace(/\/+$/, "");
|
|
283
|
+
if (seen.has(normalized)) {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
seen.add(normalized);
|
|
288
|
+
candidates.push(normalized);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return candidates;
|
|
292
|
+
}
|
|
293
|
+
|
|
262
294
|
function joinUrlPath(baseUrl, pathname) {
|
|
263
295
|
const url = new URL(baseUrl);
|
|
264
296
|
const basePath = url.pathname.replace(/\/+$/, "");
|
|
@@ -345,9 +377,14 @@ export class FirefoxBidiSessionManager {
|
|
|
345
377
|
this.browserSessionId = null;
|
|
346
378
|
this.capabilities = null;
|
|
347
379
|
this.connectPromise = null;
|
|
380
|
+
this.discoveredEndpointUrl = null;
|
|
348
381
|
this.resolvedWebSocketUrl = null;
|
|
349
382
|
}
|
|
350
383
|
|
|
384
|
+
shouldAutoDiscoverEndpoint() {
|
|
385
|
+
return this.config.firefoxBidiWsUrl === DEFAULT_FIREFOX_BIDI_WS_URL;
|
|
386
|
+
}
|
|
387
|
+
|
|
351
388
|
async ensureConnected() {
|
|
352
389
|
if (
|
|
353
390
|
this.browserSessionId &&
|
|
@@ -369,9 +406,31 @@ export class FirefoxBidiSessionManager {
|
|
|
369
406
|
}
|
|
370
407
|
|
|
371
408
|
async openConnection() {
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
409
|
+
const candidates = this.shouldAutoDiscoverEndpoint()
|
|
410
|
+
? buildDiscoveryWebSocketUrls(this.config.firefoxBidiWsUrl)
|
|
411
|
+
: [this.config.firefoxBidiWsUrl];
|
|
412
|
+
let lastError = null;
|
|
413
|
+
|
|
414
|
+
for (const candidate of candidates) {
|
|
415
|
+
const endpoint = this.resolveConnectionEndpoint(candidate);
|
|
416
|
+
try {
|
|
417
|
+
await this.openConnectionAt(endpoint);
|
|
418
|
+
this.discoveredEndpointUrl = candidate;
|
|
419
|
+
return;
|
|
420
|
+
} catch (error) {
|
|
421
|
+
lastError = error;
|
|
422
|
+
await this.resetConnectionState();
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
throw (
|
|
427
|
+
lastError ?? new Error("Unable to connect to a Firefox BiDi endpoint")
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
async openConnectionAt(endpoint) {
|
|
432
|
+
const websocket = this.websocketFactory(endpoint.webSocketUrl);
|
|
433
|
+
this.websocket = websocket;
|
|
375
434
|
|
|
376
435
|
await new Promise((resolve, reject) => {
|
|
377
436
|
let settled = false;
|
|
@@ -381,7 +440,7 @@ export class FirefoxBidiSessionManager {
|
|
|
381
440
|
}
|
|
382
441
|
|
|
383
442
|
settled = true;
|
|
384
|
-
|
|
443
|
+
websocket.close?.();
|
|
385
444
|
reject(
|
|
386
445
|
new Error(
|
|
387
446
|
`Timed out connecting to Firefox BiDi at ${endpoint.webSocketUrl}`,
|
|
@@ -400,7 +459,7 @@ export class FirefoxBidiSessionManager {
|
|
|
400
459
|
callback();
|
|
401
460
|
};
|
|
402
461
|
|
|
403
|
-
|
|
462
|
+
websocket.addEventListener(
|
|
404
463
|
"open",
|
|
405
464
|
() => {
|
|
406
465
|
settle(resolve);
|
|
@@ -408,27 +467,35 @@ export class FirefoxBidiSessionManager {
|
|
|
408
467
|
{ once: true },
|
|
409
468
|
);
|
|
410
469
|
|
|
411
|
-
|
|
470
|
+
websocket.addEventListener(
|
|
412
471
|
"error",
|
|
413
472
|
() => {
|
|
414
473
|
settle(() => {
|
|
415
|
-
reject(
|
|
416
|
-
new Error(`Failed to connect to ${this.config.firefoxBidiWsUrl}`),
|
|
417
|
-
);
|
|
474
|
+
reject(new Error(`Failed to connect to ${endpoint.webSocketUrl}`));
|
|
418
475
|
});
|
|
419
476
|
},
|
|
420
477
|
{ once: true },
|
|
421
478
|
);
|
|
422
479
|
|
|
423
|
-
|
|
480
|
+
websocket.addEventListener("message", (event) => {
|
|
481
|
+
if (this.websocket !== websocket) {
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
|
|
424
485
|
void this.handleMessage(event.data);
|
|
425
486
|
});
|
|
426
487
|
|
|
427
|
-
|
|
488
|
+
websocket.addEventListener("close", () => {
|
|
489
|
+
if (this.websocket !== websocket) {
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
|
|
428
493
|
this.rejectPending(new Error("Firefox BiDi connection closed"));
|
|
429
494
|
this.websocket = null;
|
|
430
495
|
this.browserSessionId = null;
|
|
431
496
|
this.capabilities = null;
|
|
497
|
+
this.discoveredEndpointUrl = null;
|
|
498
|
+
this.resolvedWebSocketUrl = null;
|
|
432
499
|
this.sessions.clear();
|
|
433
500
|
settle(() => {
|
|
434
501
|
reject(
|
|
@@ -447,15 +514,31 @@ export class FirefoxBidiSessionManager {
|
|
|
447
514
|
|
|
448
515
|
this.browserSessionId = result.sessionId;
|
|
449
516
|
this.capabilities = result.capabilities ?? {};
|
|
517
|
+
this.resolvedWebSocketUrl = endpoint.webSocketUrl;
|
|
450
518
|
return;
|
|
451
519
|
}
|
|
452
520
|
|
|
453
521
|
this.browserSessionId = endpoint.sessionId;
|
|
454
522
|
this.capabilities = endpoint.capabilities ?? {};
|
|
523
|
+
this.resolvedWebSocketUrl = endpoint.webSocketUrl;
|
|
455
524
|
}
|
|
456
525
|
|
|
457
|
-
async
|
|
458
|
-
|
|
526
|
+
async resetConnectionState() {
|
|
527
|
+
if (this.websocket?.readyState < WebSocket.CLOSING) {
|
|
528
|
+
this.websocket.close();
|
|
529
|
+
await this.waitForWebSocketClose(this.websocket, 50);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
this.rejectPending(new Error("Firefox BiDi connection closed"));
|
|
533
|
+
this.websocket = null;
|
|
534
|
+
this.browserSessionId = null;
|
|
535
|
+
this.capabilities = null;
|
|
536
|
+
this.discoveredEndpointUrl = null;
|
|
537
|
+
this.resolvedWebSocketUrl = null;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
resolveConnectionEndpoint(endpointUrl = this.config.firefoxBidiWsUrl) {
|
|
541
|
+
const configuredUrl = new URL(endpointUrl);
|
|
459
542
|
|
|
460
543
|
if (isHttpProtocol(configuredUrl.protocol)) {
|
|
461
544
|
return {
|
|
@@ -589,17 +672,20 @@ export class FirefoxBidiSessionManager {
|
|
|
589
672
|
await this.ensureConnected();
|
|
590
673
|
return {
|
|
591
674
|
available: true,
|
|
592
|
-
endpoint: this.config.firefoxBidiWsUrl,
|
|
675
|
+
endpoint: this.discoveredEndpointUrl ?? this.config.firefoxBidiWsUrl,
|
|
593
676
|
sessionCount: this.sessions.size,
|
|
594
677
|
browser: this.capabilities?.browserName || "firefox",
|
|
595
678
|
browserVersion: this.capabilities?.browserVersion ?? null,
|
|
596
679
|
protocol: "webdriver-bidi",
|
|
597
|
-
webSocketUrl:
|
|
680
|
+
webSocketUrl:
|
|
681
|
+
this.resolvedWebSocketUrl ??
|
|
682
|
+
this.discoveredEndpointUrl ??
|
|
683
|
+
this.config.firefoxBidiWsUrl,
|
|
598
684
|
};
|
|
599
685
|
} catch (error) {
|
|
600
686
|
return {
|
|
601
687
|
available: false,
|
|
602
|
-
endpoint: this.config.firefoxBidiWsUrl,
|
|
688
|
+
endpoint: this.discoveredEndpointUrl ?? this.config.firefoxBidiWsUrl,
|
|
603
689
|
sessionCount: this.sessions.size,
|
|
604
690
|
protocol: "webdriver-bidi",
|
|
605
691
|
error: toErrorMessage(error),
|
|
@@ -1064,6 +1150,84 @@ export class FirefoxBidiSessionManager {
|
|
|
1064
1150
|
return this.getSession(sessionId).getNetworkRequests(limit);
|
|
1065
1151
|
}
|
|
1066
1152
|
|
|
1153
|
+
async getCookies(sessionId) {
|
|
1154
|
+
return this.runPageAction(this.getSession(sessionId), {
|
|
1155
|
+
action: "cookie_snapshot",
|
|
1156
|
+
});
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
async getStorage(sessionId) {
|
|
1160
|
+
return this.runPageAction(this.getSession(sessionId), {
|
|
1161
|
+
action: "storage_snapshot",
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
async captureDebugReport(sessionId, options = {}) {
|
|
1166
|
+
const session = this.getSession(sessionId);
|
|
1167
|
+
const consoleLimit =
|
|
1168
|
+
Number.isInteger(options.consoleLimit) && options.consoleLimit > 0
|
|
1169
|
+
? options.consoleLimit
|
|
1170
|
+
: 20;
|
|
1171
|
+
const networkLimit =
|
|
1172
|
+
Number.isInteger(options.networkLimit) && options.networkLimit > 0
|
|
1173
|
+
? options.networkLimit
|
|
1174
|
+
: 20;
|
|
1175
|
+
const includeScreenshot = options.includeScreenshot !== false;
|
|
1176
|
+
const screenshotFormat = options.screenshotFormat ?? "png";
|
|
1177
|
+
const snapshot = await this.runPageAction(session, {
|
|
1178
|
+
action: "debug_report",
|
|
1179
|
+
});
|
|
1180
|
+
|
|
1181
|
+
return {
|
|
1182
|
+
browserFamily: "firefox",
|
|
1183
|
+
capturedAt: new Date().toISOString(),
|
|
1184
|
+
page: {
|
|
1185
|
+
...(await this.getPageState(sessionId)),
|
|
1186
|
+
},
|
|
1187
|
+
cookies: snapshot.cookies,
|
|
1188
|
+
storage: snapshot.storage,
|
|
1189
|
+
console: session.getConsoleMessages(consoleLimit),
|
|
1190
|
+
network: session.getNetworkRequests(networkLimit),
|
|
1191
|
+
screenshot: includeScreenshot
|
|
1192
|
+
? await this.takeScreenshot(sessionId, screenshotFormat)
|
|
1193
|
+
: null,
|
|
1194
|
+
};
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
getHar(sessionId, options = {}) {
|
|
1198
|
+
const session = this.getSession(sessionId);
|
|
1199
|
+
return exportHarLikeSummary(session.bufferedEvents, {
|
|
1200
|
+
limit: options.limit ?? 50,
|
|
1201
|
+
page: {
|
|
1202
|
+
title: session.target.title,
|
|
1203
|
+
url: session.target.url,
|
|
1204
|
+
},
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
async captureSessionSnapshot(sessionId) {
|
|
1209
|
+
return {
|
|
1210
|
+
browserFamily: "firefox",
|
|
1211
|
+
capturedAt: new Date().toISOString(),
|
|
1212
|
+
page: await this.getPageState(sessionId),
|
|
1213
|
+
...(await this.getCookies(sessionId)),
|
|
1214
|
+
...(await this.getStorage(sessionId)),
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
async restoreSessionSnapshot(sessionId, snapshot, options = {}) {
|
|
1219
|
+
const result = await this.runPageAction(this.getSession(sessionId), {
|
|
1220
|
+
action: "restore_snapshot",
|
|
1221
|
+
snapshot,
|
|
1222
|
+
clearStorage: options.clearStorage === true,
|
|
1223
|
+
});
|
|
1224
|
+
|
|
1225
|
+
return {
|
|
1226
|
+
...result,
|
|
1227
|
+
page: await this.getPageState(sessionId),
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1067
1231
|
async inspectElement(sessionId, selector) {
|
|
1068
1232
|
return this.runPageAction(this.getSession(sessionId), {
|
|
1069
1233
|
action: "inspect",
|