mcp-browser-dev-tools 0.0.1-beta.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 +141 -0
- package/cli.mjs +9 -0
- package/package.json +87 -0
- package/src/app.mjs +56 -0
- package/src/browser-adapter.mjs +10 -0
- package/src/browser-launcher.mjs +170 -0
- package/src/cdp-client.mjs +599 -0
- package/src/cli.mjs +199 -0
- package/src/config.mjs +86 -0
- package/src/doctor.mjs +88 -0
- package/src/firefox-bidi-client.mjs +645 -0
- package/src/index.mjs +31 -0
- package/src/json-rpc-stdio.mjs +56 -0
- package/src/mcp-server.mjs +506 -0
- package/src/package-info.mjs +5 -0
- package/src/server.mjs +5 -0
- package/src/session-events.mjs +76 -0
package/src/cli.mjs
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
|
|
6
|
+
import { createBrowserDevToolsApp } from "./app.mjs";
|
|
7
|
+
import {
|
|
8
|
+
buildBrowserLaunchArgs,
|
|
9
|
+
findBrowserExecutable,
|
|
10
|
+
} from "./browser-launcher.mjs";
|
|
11
|
+
import { isLoopbackHost, loadConfig } from "./config.mjs";
|
|
12
|
+
import { collectDoctorReport, renderDoctorReport } from "./doctor.mjs";
|
|
13
|
+
import { PACKAGE_NAME, PACKAGE_VERSION } from "./package-info.mjs";
|
|
14
|
+
|
|
15
|
+
export function parseCliArgs(argv) {
|
|
16
|
+
const args = [...argv];
|
|
17
|
+
const command = args[0] && !args[0].startsWith("--") ? args.shift() : "serve";
|
|
18
|
+
const positional = [];
|
|
19
|
+
const options = {};
|
|
20
|
+
|
|
21
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
22
|
+
const value = args[index];
|
|
23
|
+
if (!value.startsWith("--")) {
|
|
24
|
+
positional.push(value);
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const [rawKey, inlineValue] = value.slice(2).split("=", 2);
|
|
29
|
+
if (inlineValue !== undefined) {
|
|
30
|
+
options[rawKey] = inlineValue;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const nextValue = args[index + 1];
|
|
35
|
+
if (!nextValue || nextValue.startsWith("--")) {
|
|
36
|
+
options[rawKey] = true;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
options[rawKey] = nextValue;
|
|
41
|
+
index += 1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return { command, positional, options };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function printUsage() {
|
|
48
|
+
process.stdout.write(
|
|
49
|
+
[
|
|
50
|
+
`${PACKAGE_NAME} ${PACKAGE_VERSION}`,
|
|
51
|
+
"",
|
|
52
|
+
`Usage: ${PACKAGE_NAME} <command> [options]`,
|
|
53
|
+
"",
|
|
54
|
+
"Commands:",
|
|
55
|
+
" serve Run the MCP broker over stdio",
|
|
56
|
+
" doctor [--url URL] Check browser endpoint and optional page reachability",
|
|
57
|
+
" open <url> Launch a local browser with remote debugging enabled",
|
|
58
|
+
" version Print the package version",
|
|
59
|
+
" help Print usage information",
|
|
60
|
+
"",
|
|
61
|
+
"Global options:",
|
|
62
|
+
" --help Print usage information",
|
|
63
|
+
" --version Print the package version",
|
|
64
|
+
"",
|
|
65
|
+
"Open options:",
|
|
66
|
+
" --family <name> chromium or firefox",
|
|
67
|
+
" --port <port> remote debugging port",
|
|
68
|
+
" --address <host> remote debugging address for Chromium",
|
|
69
|
+
" --user-data-dir <p> optional browser profile directory",
|
|
70
|
+
"",
|
|
71
|
+
].join("\n"),
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function printVersion() {
|
|
76
|
+
process.stdout.write(`${PACKAGE_VERSION}\n`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function runServe() {
|
|
80
|
+
const app = createBrowserDevToolsApp();
|
|
81
|
+
app.installSignalHandlers();
|
|
82
|
+
app.start();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function runDoctor(options) {
|
|
86
|
+
const report = await collectDoctorReport({
|
|
87
|
+
url: typeof options.url === "string" ? options.url : null,
|
|
88
|
+
});
|
|
89
|
+
process.stdout.write(`${renderDoctorReport(report)}\n`);
|
|
90
|
+
if (
|
|
91
|
+
!report.browserStatus.available ||
|
|
92
|
+
(report.targetUrl && !report.targetUrl.reachable)
|
|
93
|
+
) {
|
|
94
|
+
process.exitCode = 1;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function runOpen(positional, options) {
|
|
99
|
+
const url = positional[0];
|
|
100
|
+
if (!url) {
|
|
101
|
+
throw new Error("open requires a URL");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const config = loadConfig();
|
|
105
|
+
const family =
|
|
106
|
+
typeof options.family === "string" ? options.family : config.browserFamily;
|
|
107
|
+
const requestedAddress =
|
|
108
|
+
family === "chromium" && typeof options.address === "string"
|
|
109
|
+
? options.address.trim()
|
|
110
|
+
: null;
|
|
111
|
+
|
|
112
|
+
if (
|
|
113
|
+
requestedAddress &&
|
|
114
|
+
!isLoopbackHost(requestedAddress) &&
|
|
115
|
+
!config.allowRemoteEndpoints
|
|
116
|
+
) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
"--address must be loopback unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1",
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const executable = await findBrowserExecutable(family);
|
|
123
|
+
if (!executable) {
|
|
124
|
+
throw new Error(`No local ${family} browser executable found`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const defaultPort =
|
|
128
|
+
family === "firefox"
|
|
129
|
+
? new URL(config.firefoxBidiWsUrl).port || "9222"
|
|
130
|
+
: new URL(config.cdpBaseUrl).port || "9222";
|
|
131
|
+
|
|
132
|
+
const args = buildBrowserLaunchArgs({
|
|
133
|
+
family,
|
|
134
|
+
url,
|
|
135
|
+
remoteDebuggingPort:
|
|
136
|
+
typeof options.port === "string" ? options.port : defaultPort,
|
|
137
|
+
remoteDebuggingAddress: requestedAddress || undefined,
|
|
138
|
+
userDataDir:
|
|
139
|
+
family === "chromium" && typeof options["user-data-dir"] === "string"
|
|
140
|
+
? options["user-data-dir"]
|
|
141
|
+
: undefined,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const child = spawn(executable, args, {
|
|
145
|
+
detached: true,
|
|
146
|
+
stdio: "ignore",
|
|
147
|
+
});
|
|
148
|
+
child.unref();
|
|
149
|
+
|
|
150
|
+
process.stdout.write(
|
|
151
|
+
`launched ${family} browser: ${executable} ${args.join(" ")}\n`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export async function runCli(argv = process.argv.slice(2)) {
|
|
156
|
+
const { command, positional, options } = parseCliArgs(argv);
|
|
157
|
+
|
|
158
|
+
if (options.version === true) {
|
|
159
|
+
printVersion();
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (options.help === true) {
|
|
164
|
+
printUsage();
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
switch (command) {
|
|
169
|
+
case "serve":
|
|
170
|
+
await runServe();
|
|
171
|
+
return;
|
|
172
|
+
case "doctor":
|
|
173
|
+
await runDoctor(options);
|
|
174
|
+
return;
|
|
175
|
+
case "open":
|
|
176
|
+
await runOpen(positional, options);
|
|
177
|
+
return;
|
|
178
|
+
case "version":
|
|
179
|
+
case "-v":
|
|
180
|
+
printVersion();
|
|
181
|
+
return;
|
|
182
|
+
case "help":
|
|
183
|
+
case "-h":
|
|
184
|
+
printUsage();
|
|
185
|
+
return;
|
|
186
|
+
default:
|
|
187
|
+
throw new Error(`Unknown command: ${command}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (
|
|
192
|
+
process.argv[1] &&
|
|
193
|
+
import.meta.url === pathToFileURL(process.argv[1]).href
|
|
194
|
+
) {
|
|
195
|
+
runCli().catch((error) => {
|
|
196
|
+
process.stderr.write(`${error.message}\n`);
|
|
197
|
+
process.exit(1);
|
|
198
|
+
});
|
|
199
|
+
}
|
package/src/config.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export const DEFAULT_CDP_BASE_URL = "http://127.0.0.1:9222";
|
|
2
|
+
export const DEFAULT_FIREFOX_BIDI_WS_URL = "ws://127.0.0.1:9222";
|
|
3
|
+
export const DEFAULT_EVENT_BUFFER_SIZE = 200;
|
|
4
|
+
export const DEFAULT_PROTOCOL_VERSION = "2024-11-05";
|
|
5
|
+
export const DEFAULT_BROWSER_FAMILY = "chromium";
|
|
6
|
+
|
|
7
|
+
export function isLoopbackHost(value) {
|
|
8
|
+
return ["127.0.0.1", "localhost", "::1", "[::1]"].includes(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function allowsRemoteEndpoints(env = process.env) {
|
|
12
|
+
return (
|
|
13
|
+
isTruthyFlag(env.MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS) ||
|
|
14
|
+
isTruthyFlag(env.MCP_BROWSER_ALLOW_REMOTE_CDP)
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function parsePositiveInteger(value, fallback) {
|
|
19
|
+
const normalized = typeof value === "string" ? value.trim() : "";
|
|
20
|
+
if (!/^\d+$/.test(normalized)) {
|
|
21
|
+
return fallback;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const parsed = Number.parseInt(normalized, 10);
|
|
25
|
+
return parsed > 0 ? parsed : fallback;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function normalizeBaseUrl(value) {
|
|
29
|
+
const baseUrl = (value || DEFAULT_CDP_BASE_URL).trim();
|
|
30
|
+
return baseUrl.replace(/\/+$/, "");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function normalizeWebSocketUrl(value) {
|
|
34
|
+
const websocketUrl = (value || DEFAULT_FIREFOX_BIDI_WS_URL).trim();
|
|
35
|
+
return websocketUrl.replace(/\/+$/, "");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isTruthyFlag(value) {
|
|
39
|
+
return value === "1" || value === "true";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function isLoopbackOrigin(value) {
|
|
43
|
+
const url = new URL(value);
|
|
44
|
+
return isLoopbackHost(url.hostname);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function parseBrowserFamily(value) {
|
|
48
|
+
const normalized = value?.trim().toLowerCase();
|
|
49
|
+
if (normalized === "firefox" || normalized === "chromium") {
|
|
50
|
+
return normalized;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return DEFAULT_BROWSER_FAMILY;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function loadConfig(env = process.env) {
|
|
57
|
+
const browserFamily = parseBrowserFamily(env.MCP_BROWSER_FAMILY);
|
|
58
|
+
const allowRemoteEndpoints = allowsRemoteEndpoints(env);
|
|
59
|
+
const cdpBaseUrl = normalizeBaseUrl(env.CDP_BASE_URL);
|
|
60
|
+
const firefoxBidiWsUrl = normalizeWebSocketUrl(env.FIREFOX_BIDI_WS_URL);
|
|
61
|
+
const selectedEndpoint =
|
|
62
|
+
browserFamily === "firefox" ? firefoxBidiWsUrl : cdpBaseUrl;
|
|
63
|
+
|
|
64
|
+
if (!allowRemoteEndpoints && !isLoopbackOrigin(selectedEndpoint)) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`${
|
|
67
|
+
browserFamily === "firefox" ? "FIREFOX_BIDI_WS_URL" : "CDP_BASE_URL"
|
|
68
|
+
} must point to a loopback host unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1`,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
browserFamily,
|
|
74
|
+
cdpBaseUrl,
|
|
75
|
+
firefoxBidiWsUrl,
|
|
76
|
+
allowRemoteEndpoints,
|
|
77
|
+
allowRemoteCdp: allowRemoteEndpoints,
|
|
78
|
+
enableEvaluate: isTruthyFlag(env.MCP_BROWSER_ENABLE_EVAL),
|
|
79
|
+
eventBufferSize: parsePositiveInteger(
|
|
80
|
+
env.MCP_BROWSER_EVENT_BUFFER_SIZE,
|
|
81
|
+
DEFAULT_EVENT_BUFFER_SIZE,
|
|
82
|
+
),
|
|
83
|
+
protocolVersion:
|
|
84
|
+
env.MCP_PROTOCOL_VERSION?.trim() || DEFAULT_PROTOCOL_VERSION,
|
|
85
|
+
};
|
|
86
|
+
}
|
package/src/doctor.mjs
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import os from "node:os";
|
|
2
|
+
|
|
3
|
+
import { createBrowserAdapter } from "./browser-adapter.mjs";
|
|
4
|
+
import { loadConfig } from "./config.mjs";
|
|
5
|
+
import { detectInstalledBrowsers } from "./browser-launcher.mjs";
|
|
6
|
+
|
|
7
|
+
async function probeUrl(url) {
|
|
8
|
+
try {
|
|
9
|
+
const response = await fetch(url);
|
|
10
|
+
return {
|
|
11
|
+
reachable: true,
|
|
12
|
+
status: response.status,
|
|
13
|
+
url,
|
|
14
|
+
};
|
|
15
|
+
} catch (error) {
|
|
16
|
+
return {
|
|
17
|
+
reachable: false,
|
|
18
|
+
url,
|
|
19
|
+
error: error instanceof Error ? error.message : String(error),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function collectDoctorReport({
|
|
25
|
+
env = process.env,
|
|
26
|
+
url = null,
|
|
27
|
+
} = {}) {
|
|
28
|
+
const config = loadConfig(env);
|
|
29
|
+
const installedBrowsers = await detectInstalledBrowsers(env);
|
|
30
|
+
const adapter = createBrowserAdapter(config);
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const browserStatus = await adapter.getBrowserStatus();
|
|
34
|
+
const targetUrl = url ? await probeUrl(url) : null;
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
platform: process.platform,
|
|
38
|
+
release: os.release(),
|
|
39
|
+
browserFamily: config.browserFamily,
|
|
40
|
+
endpoint:
|
|
41
|
+
config.browserFamily === "firefox"
|
|
42
|
+
? config.firefoxBidiWsUrl
|
|
43
|
+
: config.cdpBaseUrl,
|
|
44
|
+
display: {
|
|
45
|
+
DISPLAY: env.DISPLAY || null,
|
|
46
|
+
WAYLAND_DISPLAY: env.WAYLAND_DISPLAY || null,
|
|
47
|
+
XDG_RUNTIME_DIR: env.XDG_RUNTIME_DIR || null,
|
|
48
|
+
},
|
|
49
|
+
installedBrowsers,
|
|
50
|
+
browserStatus,
|
|
51
|
+
targetUrl,
|
|
52
|
+
};
|
|
53
|
+
} finally {
|
|
54
|
+
await adapter.closeAll();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function renderDoctorReport(report) {
|
|
59
|
+
const lines = [
|
|
60
|
+
`platform: ${report.platform}`,
|
|
61
|
+
`release: ${report.release}`,
|
|
62
|
+
`browser family: ${report.browserFamily}`,
|
|
63
|
+
`browser endpoint: ${report.endpoint}`,
|
|
64
|
+
`DISPLAY: ${report.display.DISPLAY ?? "-"}`,
|
|
65
|
+
`WAYLAND_DISPLAY: ${report.display.WAYLAND_DISPLAY ?? "-"}`,
|
|
66
|
+
`chromium executable: ${report.installedBrowsers.chromium ?? "not found"}`,
|
|
67
|
+
`firefox executable: ${report.installedBrowsers.firefox ?? "not found"}`,
|
|
68
|
+
`adapter available: ${report.browserStatus.available}`,
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
if (report.browserStatus.error) {
|
|
72
|
+
lines.push(`adapter error: ${report.browserStatus.error}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (report.targetUrl) {
|
|
76
|
+
lines.push(`target url: ${report.targetUrl.url}`);
|
|
77
|
+
lines.push(
|
|
78
|
+
`target reachable: ${report.targetUrl.reachable}${
|
|
79
|
+
report.targetUrl.status ? ` (${report.targetUrl.status})` : ""
|
|
80
|
+
}`,
|
|
81
|
+
);
|
|
82
|
+
if (report.targetUrl.error) {
|
|
83
|
+
lines.push(`target error: ${report.targetUrl.error}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return lines.join("\n");
|
|
88
|
+
}
|