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 +21 -0
- package/README.md +97 -27
- package/package.json +4 -1
- package/src/app.mjs +9 -1
- package/src/browser-adapter.mjs +305 -0
- package/src/browser-launcher.mjs +32 -0
- package/src/cdp-client.mjs +174 -9
- package/src/cli.mjs +16 -5
- package/src/config.mjs +50 -10
- package/src/doctor.mjs +25 -5
- package/src/firefox-bidi-client.mjs +71 -0
- package/src/index.mjs +7 -1
- package/src/logger.mjs +53 -0
- package/src/mcp-server.mjs +161 -13
- package/src/tcp-relay.mjs +7 -1
- package/src/wait-for.mjs +201 -0
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
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
filterConsoleMessages,
|
|
3
3
|
summarizeNetworkRequests,
|
|
4
4
|
} from "./session-events.mjs";
|
|
5
|
+
import { waitForPageCondition } from "./wait-for.mjs";
|
|
5
6
|
import { buildPageContextExpression } from "./page-context.mjs";
|
|
6
7
|
|
|
7
8
|
function toErrorMessage(error) {
|
|
@@ -19,6 +20,23 @@ function normalizeTarget(target) {
|
|
|
19
20
|
};
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
function inferTitle(url) {
|
|
24
|
+
if (!url) {
|
|
25
|
+
return "New Tab";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const parsed = new URL(url);
|
|
30
|
+
return parsed.hostname || url;
|
|
31
|
+
} catch {
|
|
32
|
+
return url;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function cdpBrowserFamily(config) {
|
|
37
|
+
return config.browserFamily === "edge" ? "edge" : "chromium";
|
|
38
|
+
}
|
|
39
|
+
|
|
22
40
|
function summarizeRemoteObject(object) {
|
|
23
41
|
if (!object) {
|
|
24
42
|
return null;
|
|
@@ -294,6 +312,7 @@ async function readWebSocketData(data) {
|
|
|
294
312
|
export class CdpSession {
|
|
295
313
|
constructor(target, options = {}) {
|
|
296
314
|
this.id = crypto.randomUUID();
|
|
315
|
+
this.config = options.config ?? { browserFamily: "chromium" };
|
|
297
316
|
this.target = target;
|
|
298
317
|
this.eventBufferSize = options.eventBufferSize ?? 200;
|
|
299
318
|
this.onClosed = options.onClosed ?? null;
|
|
@@ -562,7 +581,7 @@ export class CdpSession {
|
|
|
562
581
|
const result = await this.evaluateRuntime(
|
|
563
582
|
buildPageContextExpression(
|
|
564
583
|
{
|
|
565
|
-
browserFamily:
|
|
584
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
566
585
|
...payload,
|
|
567
586
|
},
|
|
568
587
|
{ serialize: true },
|
|
@@ -588,7 +607,7 @@ export class CdpSession {
|
|
|
588
607
|
});
|
|
589
608
|
|
|
590
609
|
return {
|
|
591
|
-
browserFamily:
|
|
610
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
592
611
|
...result.page,
|
|
593
612
|
lastNavigationAt: this.lastNavigationAt,
|
|
594
613
|
lastReloadAt: this.lastReloadAt,
|
|
@@ -606,7 +625,7 @@ export class CdpSession {
|
|
|
606
625
|
|
|
607
626
|
if (!inspected.found) {
|
|
608
627
|
return {
|
|
609
|
-
browserFamily:
|
|
628
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
610
629
|
format,
|
|
611
630
|
scope: "element",
|
|
612
631
|
selector: options.selector,
|
|
@@ -628,7 +647,7 @@ export class CdpSession {
|
|
|
628
647
|
});
|
|
629
648
|
|
|
630
649
|
return formatScreenshotResult(screenshot.data, format, {
|
|
631
|
-
browserFamily:
|
|
650
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
632
651
|
scope: "element",
|
|
633
652
|
selector: options.selector,
|
|
634
653
|
clip,
|
|
@@ -637,7 +656,7 @@ export class CdpSession {
|
|
|
637
656
|
|
|
638
657
|
const screenshot = await this.send("Page.captureScreenshot", { format });
|
|
639
658
|
return formatScreenshotResult(screenshot.data, format, {
|
|
640
|
-
browserFamily:
|
|
659
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
641
660
|
scope: "page",
|
|
642
661
|
});
|
|
643
662
|
}
|
|
@@ -746,7 +765,7 @@ export class CdpSession {
|
|
|
746
765
|
|
|
747
766
|
const page = lifecycleWaiter ? await this.getPageState() : null;
|
|
748
767
|
return {
|
|
749
|
-
browserFamily:
|
|
768
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
750
769
|
url,
|
|
751
770
|
frameId: result.frameId ?? null,
|
|
752
771
|
loaderId: result.loaderId ?? null,
|
|
@@ -784,7 +803,7 @@ export class CdpSession {
|
|
|
784
803
|
this.lastReloadAt = reloadedAt;
|
|
785
804
|
const page = lifecycleWaiter ? await this.getPageState() : null;
|
|
786
805
|
return {
|
|
787
|
-
browserFamily:
|
|
806
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
788
807
|
url: this.target.url,
|
|
789
808
|
waitUntil,
|
|
790
809
|
ignoreCache: options.ignoreCache ?? false,
|
|
@@ -812,7 +831,7 @@ export class CdpSession {
|
|
|
812
831
|
};
|
|
813
832
|
|
|
814
833
|
return {
|
|
815
|
-
browserFamily:
|
|
834
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
816
835
|
applied: true,
|
|
817
836
|
viewport: this.viewportOverride,
|
|
818
837
|
page: await this.getPageState(),
|
|
@@ -855,7 +874,7 @@ export class CdpSession {
|
|
|
855
874
|
targetId: this.target.targetId,
|
|
856
875
|
title: this.target.title,
|
|
857
876
|
url: this.target.url,
|
|
858
|
-
browserFamily:
|
|
877
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
859
878
|
connectedAt: this.connectedAt,
|
|
860
879
|
bufferedEvents: this.bufferedEvents.length,
|
|
861
880
|
lastNavigationAt: this.lastNavigationAt,
|
|
@@ -886,6 +905,92 @@ export class CdpSessionManager {
|
|
|
886
905
|
return parseJson(response);
|
|
887
906
|
}
|
|
888
907
|
|
|
908
|
+
async sendBrowserCommand(method, params = {}) {
|
|
909
|
+
const info = await this.fetchJson("/json/version");
|
|
910
|
+
if (!info.webSocketDebuggerUrl) {
|
|
911
|
+
throw new Error("Browser endpoint does not expose a debugger websocket");
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
const websocket = new WebSocket(info.webSocketDebuggerUrl);
|
|
915
|
+
|
|
916
|
+
return new Promise((resolve, reject) => {
|
|
917
|
+
let settled = false;
|
|
918
|
+
|
|
919
|
+
const cleanup = () => {
|
|
920
|
+
websocket.removeEventListener("open", handleOpen);
|
|
921
|
+
websocket.removeEventListener("message", handleMessage);
|
|
922
|
+
websocket.removeEventListener("error", handleError);
|
|
923
|
+
websocket.removeEventListener("close", handleClose);
|
|
924
|
+
};
|
|
925
|
+
|
|
926
|
+
const finish = (callback) => {
|
|
927
|
+
if (settled) {
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
settled = true;
|
|
932
|
+
cleanup();
|
|
933
|
+
callback();
|
|
934
|
+
|
|
935
|
+
if (websocket.readyState < WebSocket.CLOSING) {
|
|
936
|
+
websocket.close();
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
const handleOpen = () => {
|
|
941
|
+
try {
|
|
942
|
+
websocket.send(
|
|
943
|
+
JSON.stringify({
|
|
944
|
+
id: 1,
|
|
945
|
+
method,
|
|
946
|
+
params,
|
|
947
|
+
}),
|
|
948
|
+
);
|
|
949
|
+
} catch (error) {
|
|
950
|
+
finish(() => reject(error));
|
|
951
|
+
}
|
|
952
|
+
};
|
|
953
|
+
|
|
954
|
+
const handleMessage = async (event) => {
|
|
955
|
+
const raw = await readWebSocketData(event.data);
|
|
956
|
+
const message = JSON.parse(raw);
|
|
957
|
+
if (message.id !== 1) {
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
if (message.error) {
|
|
962
|
+
finish(() =>
|
|
963
|
+
reject(
|
|
964
|
+
new Error(
|
|
965
|
+
`${message.error.message} (code ${message.error.code})`,
|
|
966
|
+
),
|
|
967
|
+
),
|
|
968
|
+
);
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
finish(() => resolve(message.result ?? {}));
|
|
973
|
+
};
|
|
974
|
+
|
|
975
|
+
const handleError = () => {
|
|
976
|
+
finish(() =>
|
|
977
|
+
reject(
|
|
978
|
+
new Error(`Failed to connect to ${info.webSocketDebuggerUrl}`),
|
|
979
|
+
),
|
|
980
|
+
);
|
|
981
|
+
};
|
|
982
|
+
|
|
983
|
+
const handleClose = () => {
|
|
984
|
+
finish(() => reject(new Error("Browser debugger websocket closed")));
|
|
985
|
+
};
|
|
986
|
+
|
|
987
|
+
websocket.addEventListener("open", handleOpen, { once: true });
|
|
988
|
+
websocket.addEventListener("message", handleMessage);
|
|
989
|
+
websocket.addEventListener("error", handleError, { once: true });
|
|
990
|
+
websocket.addEventListener("close", handleClose, { once: true });
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
|
|
889
994
|
async getBrowserStatus() {
|
|
890
995
|
try {
|
|
891
996
|
const info = await this.fetchJson("/json/version");
|
|
@@ -920,6 +1025,33 @@ export class CdpSessionManager {
|
|
|
920
1025
|
);
|
|
921
1026
|
}
|
|
922
1027
|
|
|
1028
|
+
async createTab(url = "about:blank") {
|
|
1029
|
+
const targetUrl =
|
|
1030
|
+
typeof url === "string" && url.trim() ? url.trim() : "about:blank";
|
|
1031
|
+
const result = await this.sendBrowserCommand("Target.createTarget", {
|
|
1032
|
+
url: targetUrl,
|
|
1033
|
+
});
|
|
1034
|
+
const targetId = result.targetId;
|
|
1035
|
+
if (!targetId) {
|
|
1036
|
+
throw new Error("Browser did not return a target id for the new tab");
|
|
1037
|
+
}
|
|
1038
|
+
const target = (await this.listTargets()).find(
|
|
1039
|
+
(candidate) => candidate.targetId === targetId,
|
|
1040
|
+
);
|
|
1041
|
+
|
|
1042
|
+
return {
|
|
1043
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
1044
|
+
...(target ?? {
|
|
1045
|
+
targetId,
|
|
1046
|
+
type: "page",
|
|
1047
|
+
title: inferTitle(targetUrl),
|
|
1048
|
+
url: targetUrl,
|
|
1049
|
+
attached: false,
|
|
1050
|
+
webSocketDebuggerUrl: null,
|
|
1051
|
+
}),
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
|
|
923
1055
|
async attachToTarget(targetId) {
|
|
924
1056
|
const targets = await this.listTargets();
|
|
925
1057
|
const target = targets.find((candidate) => candidate.targetId === targetId);
|
|
@@ -928,6 +1060,7 @@ export class CdpSessionManager {
|
|
|
928
1060
|
}
|
|
929
1061
|
|
|
930
1062
|
const session = new CdpSession(target, {
|
|
1063
|
+
config: this.config,
|
|
931
1064
|
eventBufferSize: this.config.eventBufferSize,
|
|
932
1065
|
onClosed: (closedSession) => {
|
|
933
1066
|
this.sessions.delete(closedSession.id);
|
|
@@ -957,6 +1090,30 @@ export class CdpSessionManager {
|
|
|
957
1090
|
};
|
|
958
1091
|
}
|
|
959
1092
|
|
|
1093
|
+
async closeTarget(targetId) {
|
|
1094
|
+
const result = await this.sendBrowserCommand("Target.closeTarget", {
|
|
1095
|
+
targetId,
|
|
1096
|
+
});
|
|
1097
|
+
if (result.success === false) {
|
|
1098
|
+
throw new Error(`Failed to close target ${targetId}`);
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
const detachedSessions = Array.from(this.sessions.values())
|
|
1102
|
+
.filter((session) => session.target.targetId === targetId)
|
|
1103
|
+
.map((session) => {
|
|
1104
|
+
session.markClosed?.();
|
|
1105
|
+
this.sessions.delete(session.id);
|
|
1106
|
+
return { sessionId: session.id };
|
|
1107
|
+
});
|
|
1108
|
+
|
|
1109
|
+
return {
|
|
1110
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
1111
|
+
closed: true,
|
|
1112
|
+
targetId,
|
|
1113
|
+
detachedSessions,
|
|
1114
|
+
};
|
|
1115
|
+
}
|
|
1116
|
+
|
|
960
1117
|
async evaluate(sessionId, expression, options) {
|
|
961
1118
|
return this.getSession(sessionId).evaluate(expression, options);
|
|
962
1119
|
}
|
|
@@ -969,6 +1126,14 @@ export class CdpSessionManager {
|
|
|
969
1126
|
return this.getSession(sessionId).getPageState();
|
|
970
1127
|
}
|
|
971
1128
|
|
|
1129
|
+
async waitFor(sessionId, options = {}) {
|
|
1130
|
+
return waitForPageCondition({
|
|
1131
|
+
getPageState: () => this.getPageState(sessionId),
|
|
1132
|
+
inspectElement: (selector) => this.inspectElement(sessionId, selector),
|
|
1133
|
+
options,
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
1136
|
+
|
|
972
1137
|
async navigate(sessionId, url, options) {
|
|
973
1138
|
return this.getSession(sessionId).navigate(url, options);
|
|
974
1139
|
}
|
package/src/cli.mjs
CHANGED
|
@@ -10,8 +10,9 @@ import {
|
|
|
10
10
|
buildBrowserLaunchArgs,
|
|
11
11
|
findBrowserExecutable,
|
|
12
12
|
} from "./browser-launcher.mjs";
|
|
13
|
-
import { isLoopbackHost, loadConfig } from "./config.mjs";
|
|
13
|
+
import { isLoopbackHost, loadConfig, loadLoggingConfig } from "./config.mjs";
|
|
14
14
|
import { collectDoctorReport, renderDoctorReport } from "./doctor.mjs";
|
|
15
|
+
import { createLogger } from "./logger.mjs";
|
|
15
16
|
import { PACKAGE_NAME, PACKAGE_VERSION } from "./package-info.mjs";
|
|
16
17
|
import { resolveRelayOptions, startTcpRelay } from "./tcp-relay.mjs";
|
|
17
18
|
|
|
@@ -67,7 +68,7 @@ function printUsage() {
|
|
|
67
68
|
" --version Print the package version",
|
|
68
69
|
"",
|
|
69
70
|
"Open options:",
|
|
70
|
-
" --family <name> chromium or firefox",
|
|
71
|
+
" --family <name> chromium, edge, or firefox",
|
|
71
72
|
" --port <port> remote debugging port",
|
|
72
73
|
" --address <host> remote debugging address for Chromium",
|
|
73
74
|
" --user-data-dir <p> optional Chromium user data dir or Firefox profile dir",
|
|
@@ -217,7 +218,8 @@ async function runOpen(positional, options) {
|
|
|
217
218
|
const family =
|
|
218
219
|
typeof options.family === "string" ? options.family : config.browserFamily;
|
|
219
220
|
const requestedAddress =
|
|
220
|
-
family === "chromium"
|
|
221
|
+
(family === "chromium" || family === "edge") &&
|
|
222
|
+
typeof options.address === "string"
|
|
221
223
|
? options.address.trim()
|
|
222
224
|
: null;
|
|
223
225
|
|
|
@@ -324,11 +326,20 @@ async function runOpen(positional, options) {
|
|
|
324
326
|
}
|
|
325
327
|
|
|
326
328
|
async function runRelay(options) {
|
|
329
|
+
const logging = loadLoggingConfig(process.env);
|
|
330
|
+
const logger = createLogger({
|
|
331
|
+
level: logging.logLevel,
|
|
332
|
+
output: process.stderr,
|
|
333
|
+
name: PACKAGE_NAME,
|
|
334
|
+
});
|
|
327
335
|
const relayOptions = resolveRelayOptions(options);
|
|
328
|
-
const relay = await startTcpRelay(
|
|
336
|
+
const relay = await startTcpRelay({
|
|
337
|
+
...relayOptions,
|
|
338
|
+
logger,
|
|
339
|
+
});
|
|
329
340
|
|
|
330
341
|
const close = async (signal) => {
|
|
331
|
-
|
|
342
|
+
logger.info(`relay shutting down after ${signal}`);
|
|
332
343
|
await relay.close();
|
|
333
344
|
};
|
|
334
345
|
|
package/src/config.mjs
CHANGED
|
@@ -3,6 +3,8 @@ 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 DEFAULT_LOG_LEVEL = "error";
|
|
7
|
+
export const CDP_BROWSER_FAMILIES = ["chromium", "edge"];
|
|
6
8
|
|
|
7
9
|
export function isLoopbackHost(value) {
|
|
8
10
|
return ["127.0.0.1", "localhost", "::1", "[::1]"].includes(value);
|
|
@@ -46,27 +48,63 @@ export function isLoopbackOrigin(value) {
|
|
|
46
48
|
|
|
47
49
|
export function parseBrowserFamily(value) {
|
|
48
50
|
const normalized = value?.trim().toLowerCase();
|
|
49
|
-
if (
|
|
51
|
+
if (
|
|
52
|
+
normalized === "firefox" ||
|
|
53
|
+
normalized === "chromium" ||
|
|
54
|
+
normalized === "edge" ||
|
|
55
|
+
normalized === "auto"
|
|
56
|
+
) {
|
|
50
57
|
return normalized;
|
|
51
58
|
}
|
|
52
59
|
|
|
53
60
|
return DEFAULT_BROWSER_FAMILY;
|
|
54
61
|
}
|
|
55
62
|
|
|
63
|
+
export function parseLogLevel(value) {
|
|
64
|
+
const normalized = value?.trim().toLowerCase();
|
|
65
|
+
if (
|
|
66
|
+
normalized === "error" ||
|
|
67
|
+
normalized === "warn" ||
|
|
68
|
+
normalized === "info" ||
|
|
69
|
+
normalized === "debug"
|
|
70
|
+
) {
|
|
71
|
+
return normalized;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return DEFAULT_LOG_LEVEL;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function loadLoggingConfig(env = process.env) {
|
|
78
|
+
return {
|
|
79
|
+
logLevel: parseLogLevel(env.MCP_BROWSER_LOG_LEVEL),
|
|
80
|
+
debugStdio: isTruthyFlag(env.MCP_BROWSER_DEBUG_STDIO),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
56
84
|
export function loadConfig(env = process.env) {
|
|
57
85
|
const browserFamily = parseBrowserFamily(env.MCP_BROWSER_FAMILY);
|
|
58
86
|
const allowRemoteEndpoints = allowsRemoteEndpoints(env);
|
|
59
87
|
const cdpBaseUrl = normalizeBaseUrl(env.CDP_BASE_URL);
|
|
60
88
|
const firefoxBidiWsUrl = normalizeWebSocketUrl(env.FIREFOX_BIDI_WS_URL);
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
89
|
+
const logging = loadLoggingConfig(env);
|
|
90
|
+
const endpointsToValidate =
|
|
91
|
+
browserFamily === "firefox"
|
|
92
|
+
? [["FIREFOX_BIDI_WS_URL", firefoxBidiWsUrl]]
|
|
93
|
+
: browserFamily === "auto"
|
|
94
|
+
? [
|
|
95
|
+
["CDP_BASE_URL", cdpBaseUrl],
|
|
96
|
+
["FIREFOX_BIDI_WS_URL", firefoxBidiWsUrl],
|
|
97
|
+
]
|
|
98
|
+
: [["CDP_BASE_URL", cdpBaseUrl]];
|
|
99
|
+
|
|
100
|
+
if (!allowRemoteEndpoints) {
|
|
101
|
+
for (const [name, endpoint] of endpointsToValidate) {
|
|
102
|
+
if (!isLoopbackOrigin(endpoint)) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`${name} must point to a loopback host unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
70
108
|
}
|
|
71
109
|
|
|
72
110
|
return {
|
|
@@ -80,6 +118,8 @@ export function loadConfig(env = process.env) {
|
|
|
80
118
|
env.MCP_BROWSER_EVENT_BUFFER_SIZE,
|
|
81
119
|
DEFAULT_EVENT_BUFFER_SIZE,
|
|
82
120
|
),
|
|
121
|
+
logLevel: logging.logLevel,
|
|
122
|
+
debugStdio: logging.debugStdio,
|
|
83
123
|
protocolVersion:
|
|
84
124
|
env.MCP_PROTOCOL_VERSION?.trim() || DEFAULT_PROTOCOL_VERSION,
|
|
85
125
|
};
|
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
|
];
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
filterConsoleMessages,
|
|
3
3
|
summarizeNetworkRequests,
|
|
4
4
|
} from "./session-events.mjs";
|
|
5
|
+
import { waitForPageCondition } from "./wait-for.mjs";
|
|
5
6
|
import { buildPageContextExpression } from "./page-context.mjs";
|
|
6
7
|
|
|
7
8
|
function toErrorMessage(error) {
|
|
@@ -620,6 +621,46 @@ export class FirefoxBidiSessionManager {
|
|
|
620
621
|
);
|
|
621
622
|
}
|
|
622
623
|
|
|
624
|
+
async createTab(url = "about:blank") {
|
|
625
|
+
await this.ensureConnected();
|
|
626
|
+
const targetUrl =
|
|
627
|
+
typeof url === "string" && url.trim() ? url.trim() : "about:blank";
|
|
628
|
+
const result = await this.send("browsingContext.create", {
|
|
629
|
+
type: "tab",
|
|
630
|
+
});
|
|
631
|
+
const targetId = result.context;
|
|
632
|
+
if (!targetId) {
|
|
633
|
+
throw new Error(
|
|
634
|
+
"Firefox did not return a browsing context id for the new tab",
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
if (targetUrl !== "about:blank") {
|
|
639
|
+
await this.send("browsingContext.navigate", {
|
|
640
|
+
context: targetId,
|
|
641
|
+
url: targetUrl,
|
|
642
|
+
wait: "complete",
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
const target = (await this.listTargets()).find(
|
|
647
|
+
(candidate) => candidate.targetId === targetId,
|
|
648
|
+
);
|
|
649
|
+
|
|
650
|
+
return {
|
|
651
|
+
browserFamily: "firefox",
|
|
652
|
+
...(target ?? {
|
|
653
|
+
targetId,
|
|
654
|
+
type: "page",
|
|
655
|
+
title: inferTitle(targetUrl),
|
|
656
|
+
url: targetUrl,
|
|
657
|
+
attached: false,
|
|
658
|
+
userContext: null,
|
|
659
|
+
clientWindow: null,
|
|
660
|
+
}),
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
|
|
623
664
|
getSession(sessionId) {
|
|
624
665
|
const session = this.sessions.get(sessionId);
|
|
625
666
|
if (!session) {
|
|
@@ -674,6 +715,28 @@ export class FirefoxBidiSessionManager {
|
|
|
674
715
|
};
|
|
675
716
|
}
|
|
676
717
|
|
|
718
|
+
async closeTarget(targetId) {
|
|
719
|
+
await this.ensureConnected();
|
|
720
|
+
await this.send("browsingContext.close", {
|
|
721
|
+
context: targetId,
|
|
722
|
+
});
|
|
723
|
+
|
|
724
|
+
const detachedSessions = Array.from(this.sessions.values())
|
|
725
|
+
.filter((session) => session.target.targetId === targetId)
|
|
726
|
+
.map((session) => {
|
|
727
|
+
session.closed = true;
|
|
728
|
+
this.sessions.delete(session.id);
|
|
729
|
+
return { sessionId: session.id };
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
return {
|
|
733
|
+
browserFamily: "firefox",
|
|
734
|
+
closed: true,
|
|
735
|
+
targetId,
|
|
736
|
+
detachedSessions,
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
|
|
677
740
|
async evaluate(sessionId, expression, options = {}) {
|
|
678
741
|
const session = this.getSession(sessionId);
|
|
679
742
|
const result = await this.evaluateInContext(session, expression, options);
|
|
@@ -784,6 +847,14 @@ export class FirefoxBidiSessionManager {
|
|
|
784
847
|
};
|
|
785
848
|
}
|
|
786
849
|
|
|
850
|
+
async waitFor(sessionId, options = {}) {
|
|
851
|
+
return waitForPageCondition({
|
|
852
|
+
getPageState: () => this.getPageState(sessionId),
|
|
853
|
+
inspectElement: (selector) => this.inspectElement(sessionId, selector),
|
|
854
|
+
options,
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
|
|
787
858
|
async navigate(sessionId, url, options = {}) {
|
|
788
859
|
const session = this.getSession(sessionId);
|
|
789
860
|
const waitUntil = normalizeWaitUntil(options.waitUntil);
|
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,
|
|
@@ -11,14 +14,17 @@ export {
|
|
|
11
14
|
DEFAULT_CDP_BASE_URL,
|
|
12
15
|
DEFAULT_EVENT_BUFFER_SIZE,
|
|
13
16
|
DEFAULT_FIREFOX_BIDI_WS_URL,
|
|
17
|
+
DEFAULT_LOG_LEVEL,
|
|
14
18
|
DEFAULT_PROTOCOL_VERSION,
|
|
15
19
|
isLoopbackHost,
|
|
16
20
|
isLoopbackOrigin,
|
|
17
21
|
isTruthyFlag,
|
|
18
22
|
loadConfig,
|
|
23
|
+
loadLoggingConfig,
|
|
19
24
|
normalizeBaseUrl,
|
|
20
25
|
normalizeWebSocketUrl,
|
|
21
26
|
parseBrowserFamily,
|
|
27
|
+
parseLogLevel,
|
|
22
28
|
parsePositiveInteger,
|
|
23
29
|
} from "./config.mjs";
|
|
24
30
|
export { collectDoctorReport, renderDoctorReport } from "./doctor.mjs";
|