mcp-browser-dev-tools 0.0.6-beta.0 → 0.0.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 +10 -0
- package/package.json +1 -1
- package/src/browser-adapter.mjs +5 -2
- package/src/browser-launch-service.mjs +146 -9
- package/src/cli.mjs +3 -1
- package/src/config.mjs +1 -0
- package/src/mcp-server.mjs +182 -74
package/README.md
CHANGED
|
@@ -70,6 +70,15 @@ Once the server is registered, the normal workflow is to ask the agent for brows
|
|
|
70
70
|
|
|
71
71
|
The agent should usually start with `ensure_browser`. That tool can confirm browser availability, launch one if needed, and optionally open the requested URL in one step.
|
|
72
72
|
|
|
73
|
+
Both `ensure_browser` and `launch_browser` try to keep one browser running instead of starting more:
|
|
74
|
+
|
|
75
|
+
- a browser that is already reachable is reused, whatever profile it runs on; `launch_browser` returns `reused: true` instead of launching when `port`, `address`, `userDataDir`, and `unsafeArgs` are all omitted, and opens `url` in a new tab of that browser
|
|
76
|
+
- before reusing or launching, the browser status is checked three times, so one slow answer does not trigger a launch
|
|
77
|
+
- after launching, the broker will not launch the same browser again for 30 seconds while it waits for the endpoint, and launches are handled one at a time, so parallel tool calls do not start one browser each
|
|
78
|
+
- on macOS and Linux, the broker never launches a second browser on a profile that is already open (or, for Firefox without `userDataDir`, while any Firefox is running), because that only opens another window in the running browser; it waits for that browser's endpoint instead, and fails with an explanation if the endpoint stays down. On Windows and WSL the broker cannot read browser command lines, so it cannot detect which profile a running Chrome or Edge uses; the Firefox check and the automatic temporary Chrome/Edge profile still apply there
|
|
79
|
+
|
|
80
|
+
To make every launch use the same profile, set `MCP_BROWSER_USER_DATA_DIR` in the MCP server environment instead of passing `userDataDir` on every call. It applies only when a browser actually has to be launched; a browser that is already reachable is reused as is.
|
|
81
|
+
|
|
73
82
|
If the agent launches Chrome or Edge without `userDataDir`, the broker checks whether the same browser family is already running. If it is, the broker automatically creates a temporary profile directory before launching so the new debug flags are not swallowed by the already-running profile.
|
|
74
83
|
|
|
75
84
|
Typical temporary profile locations:
|
|
@@ -223,6 +232,7 @@ If you use WSL with a Windows Chrome or Edge browser, prefer `serve --bootstrap-
|
|
|
223
232
|
- `CDP_BASE_URL` defaults to `http://127.0.0.1:9222`; when left at that default the broker probes loopback ports `9222` through `9226` for a reachable CDP browser endpoint
|
|
224
233
|
- `FIREFOX_BIDI_WS_URL` defaults to `ws://127.0.0.1:9222`; when left at that default the broker probes loopback ports `9222` through `9226`, and when pointed at the root Firefox remote debugging port it connects to the `/session` websocket and creates a BiDi session there
|
|
225
234
|
- in `auto` mode, assign CDP and Firefox different ports so both browsers can run at once
|
|
235
|
+
- `MCP_BROWSER_USER_DATA_DIR` sets the browser profile directory that `ensure_browser` and `launch_browser` use when `userDataDir` is not passed; a leading `~/` expands to the home directory
|
|
226
236
|
- `MCP_BROWSER_EVENT_BUFFER_SIZE` sets the per-session buffered event limit
|
|
227
237
|
- `MCP_BROWSER_LOG_LEVEL` controls diagnostic logging to `stderr`: `error`, `warn`, `info`, or `debug`
|
|
228
238
|
- `MCP_BROWSER_DEBUG_STDIO=1` emits raw MCP stdio transport diagnostics to `stderr`
|
package/package.json
CHANGED
package/src/browser-adapter.mjs
CHANGED
|
@@ -65,7 +65,9 @@ export class MultiBrowserAdapter {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
getAdapter(browserFamily) {
|
|
68
|
-
|
|
68
|
+
// Edge is served by the Chromium adapter.
|
|
69
|
+
const adapter =
|
|
70
|
+
this.adapters[browserFamily === "edge" ? "chromium" : browserFamily];
|
|
69
71
|
if (!adapter) {
|
|
70
72
|
throw new Error(`Unsupported browser family: ${browserFamily}`);
|
|
71
73
|
}
|
|
@@ -162,7 +164,8 @@ export class MultiBrowserAdapter {
|
|
|
162
164
|
}
|
|
163
165
|
|
|
164
166
|
async createTab(url, options = {}) {
|
|
165
|
-
const browserFamily =
|
|
167
|
+
const browserFamily =
|
|
168
|
+
options.browserFamily === "edge" ? "chromium" : options.browserFamily;
|
|
166
169
|
if (!browserFamily) {
|
|
167
170
|
throw new Error(
|
|
168
171
|
"browserFamily is required when multiple browsers are configured",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { execFile, spawn } from "node:child_process";
|
|
2
|
-
import { mkdir, mkdtemp, readFile } from "node:fs/promises";
|
|
2
|
+
import { mkdir, mkdtemp, readFile, realpath } from "node:fs/promises";
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import process from "node:process";
|
|
5
5
|
import path from "node:path";
|
|
@@ -73,6 +73,111 @@ function normalizeStringValue(value) {
|
|
|
73
73
|
return trimmed ? trimmed : null;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
function expandHomeDir(value) {
|
|
77
|
+
if (value === "~") {
|
|
78
|
+
return os.homedir();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return value?.startsWith("~/")
|
|
82
|
+
? path.join(os.homedir(), value.slice(2))
|
|
83
|
+
: value;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function profileArgument(family, userDataDir) {
|
|
87
|
+
return family === "firefox"
|
|
88
|
+
? `-profile ${userDataDir}`
|
|
89
|
+
: `--user-data-dir=${userDataDir}`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Starting a browser binary on a profile that is already open does not start
|
|
93
|
+
// a second browser: the running instance just opens another window. Callers
|
|
94
|
+
// use this to reuse that instance instead.
|
|
95
|
+
// Executable names that identify browser processes, so a command line that
|
|
96
|
+
// merely mentions the profile (such as this broker's own CLI) is not mistaken
|
|
97
|
+
// for a running browser.
|
|
98
|
+
function isBrowserExecutable(family, executable) {
|
|
99
|
+
const name = path.basename(executable).toLowerCase();
|
|
100
|
+
const hints =
|
|
101
|
+
family === "firefox"
|
|
102
|
+
? ["firefox"]
|
|
103
|
+
: ["chrome", "chromium", "msedge", "microsoft edge", "microsoft-edge"];
|
|
104
|
+
return hints.some((hint) => name.includes(hint));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function parsePidColumns(stdout) {
|
|
108
|
+
const rows = new Map();
|
|
109
|
+
for (const line of stdout.split("\n")) {
|
|
110
|
+
const match = line.match(/^\s*(\d+)\s+(.*)$/);
|
|
111
|
+
if (match) {
|
|
112
|
+
rows.set(match[1], match[2]);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return rows;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function mentionsProfile(commandLine, needles) {
|
|
119
|
+
return needles.some((needle) => {
|
|
120
|
+
let index = commandLine.indexOf(needle);
|
|
121
|
+
while (index !== -1) {
|
|
122
|
+
const rest = commandLine.slice(index + needle.length);
|
|
123
|
+
if (rest === "" || rest.startsWith(" ") || /^\/(\s|$)/.test(rest)) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
index = commandLine.indexOf(needle, index + 1);
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Starting a browser binary on a profile that is already open does not start
|
|
133
|
+
// a second browser: the running instance just opens another window. Callers
|
|
134
|
+
// use this to reuse that instance instead.
|
|
135
|
+
async function findProfileProcesses(
|
|
136
|
+
family,
|
|
137
|
+
userDataDir,
|
|
138
|
+
{ env = process.env, execFileFn = execFileAsync, realpathFn = realpath } = {},
|
|
139
|
+
) {
|
|
140
|
+
const platform = detectRuntimePlatform(env);
|
|
141
|
+
// Windows process listings do not include command lines, and WSL cannot see
|
|
142
|
+
// the command lines of the Windows browsers it launches.
|
|
143
|
+
if (platform !== "darwin" && platform !== "linux") {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let executables;
|
|
148
|
+
let commandLines;
|
|
149
|
+
try {
|
|
150
|
+
const options = { maxBuffer: 8 * 1024 * 1024 };
|
|
151
|
+
executables = parsePidColumns(
|
|
152
|
+
(await execFileFn("ps", ["-A", "-o", "pid=,comm="], options)).stdout,
|
|
153
|
+
);
|
|
154
|
+
commandLines = parsePidColumns(
|
|
155
|
+
(await execFileFn("ps", ["-A", "-o", "pid=,command="], options)).stdout,
|
|
156
|
+
);
|
|
157
|
+
} catch {
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const trimmed = userDataDir.replace(/\/+$/, "") || userDataDir;
|
|
162
|
+
const spellings = [trimmed, path.resolve(trimmed)];
|
|
163
|
+
try {
|
|
164
|
+
spellings.push(await realpathFn(trimmed));
|
|
165
|
+
} catch {
|
|
166
|
+
// A profile directory that does not exist yet cannot be in use.
|
|
167
|
+
}
|
|
168
|
+
const needles = [
|
|
169
|
+
...new Set(spellings.map((dir) => profileArgument(family, dir))),
|
|
170
|
+
];
|
|
171
|
+
|
|
172
|
+
return [...commandLines.entries()]
|
|
173
|
+
.filter(
|
|
174
|
+
([pid, commandLine]) =>
|
|
175
|
+
isBrowserExecutable(family, executables.get(pid) ?? "") &&
|
|
176
|
+
mentionsProfile(commandLine, needles),
|
|
177
|
+
)
|
|
178
|
+
.map(([, commandLine]) => commandLine);
|
|
179
|
+
}
|
|
180
|
+
|
|
76
181
|
const MANAGED_UNSAFE_LAUNCH_ARG_PREFIXES = [
|
|
77
182
|
"--remote-debugging-address",
|
|
78
183
|
"--remote-debugging-port",
|
|
@@ -162,7 +267,7 @@ function browserProcessMatchers(family) {
|
|
|
162
267
|
}
|
|
163
268
|
|
|
164
269
|
if (family === "firefox") {
|
|
165
|
-
return ["firefox", "firefox.exe"];
|
|
270
|
+
return ["firefox", "firefox.exe", "firefox-bin"];
|
|
166
271
|
}
|
|
167
272
|
|
|
168
273
|
return [
|
|
@@ -432,6 +537,7 @@ export async function launchBrowser(
|
|
|
432
537
|
findBrowserExecutableFn = findBrowserExecutable,
|
|
433
538
|
collectDoctorReportFn = collectDoctorReport,
|
|
434
539
|
resolveFirefoxDoctorEndpointFn = resolveFirefoxDoctorEndpoint,
|
|
540
|
+
findProfileProcessesFn = findProfileProcesses,
|
|
435
541
|
sleepFn = sleep,
|
|
436
542
|
} = {},
|
|
437
543
|
) {
|
|
@@ -465,7 +571,11 @@ export async function launchBrowser(
|
|
|
465
571
|
throw new Error(`No local ${family} browser executable found`);
|
|
466
572
|
}
|
|
467
573
|
|
|
468
|
-
const providedUserDataDir =
|
|
574
|
+
const providedUserDataDir =
|
|
575
|
+
expandHomeDir(
|
|
576
|
+
normalizeStringValue(userDataDir) ??
|
|
577
|
+
normalizeStringValue(config.userDataDir),
|
|
578
|
+
) ?? undefined;
|
|
469
579
|
const {
|
|
470
580
|
userDataDir: normalizedUserDataDir,
|
|
471
581
|
profileStrategy,
|
|
@@ -494,11 +604,26 @@ export async function launchBrowser(
|
|
|
494
604
|
unsafeArgs: normalizedUnsafeArgs,
|
|
495
605
|
});
|
|
496
606
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
607
|
+
// Firefox started without a profile hands its command line to a Firefox
|
|
608
|
+
// that is already running, which opens a window there and ignores the
|
|
609
|
+
// debugging flag.
|
|
610
|
+
const profileAlreadyRunning = normalizedUserDataDir
|
|
611
|
+
? (
|
|
612
|
+
await findProfileProcessesFn(family, normalizedUserDataDir, {
|
|
613
|
+
env,
|
|
614
|
+
execFileFn,
|
|
615
|
+
})
|
|
616
|
+
).length > 0
|
|
617
|
+
: family === "firefox" && existingBrowserProcess.detected;
|
|
618
|
+
|
|
619
|
+
let child = null;
|
|
620
|
+
if (!profileAlreadyRunning) {
|
|
621
|
+
child = spawnProcess(executable, args, {
|
|
622
|
+
detached: true,
|
|
623
|
+
stdio: "ignore",
|
|
624
|
+
});
|
|
625
|
+
child.unref?.();
|
|
626
|
+
}
|
|
502
627
|
|
|
503
628
|
const launchedAt = new Date().toISOString();
|
|
504
629
|
const result = {
|
|
@@ -506,7 +631,9 @@ export async function launchBrowser(
|
|
|
506
631
|
url: targetUrl,
|
|
507
632
|
executable,
|
|
508
633
|
args,
|
|
509
|
-
pid: child
|
|
634
|
+
pid: child?.pid ?? null,
|
|
635
|
+
launched: !profileAlreadyRunning,
|
|
636
|
+
profileAlreadyRunning,
|
|
510
637
|
launchedAt,
|
|
511
638
|
endpoint:
|
|
512
639
|
family === "firefox"
|
|
@@ -571,5 +698,15 @@ export async function launchBrowser(
|
|
|
571
698
|
? doctorEnv.FIREFOX_BIDI_WS_URL
|
|
572
699
|
: doctorEnv.CDP_BASE_URL;
|
|
573
700
|
result.doctorReport = report;
|
|
701
|
+
|
|
702
|
+
if (profileAlreadyRunning && !report.browserStatus.available) {
|
|
703
|
+
const running = normalizedUserDataDir
|
|
704
|
+
? `A browser is already running with profile ${normalizedUserDataDir}`
|
|
705
|
+
: `${family} is already running`;
|
|
706
|
+
throw new Error(
|
|
707
|
+
`${running}, but its debugging endpoint ${result.endpoint} is not answering. Launching it again would only open another window in that browser, so nothing was launched. Quit that browser and retry, or restart it with remote debugging on port ${resolvedPort}.`,
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
|
|
574
711
|
return result;
|
|
575
712
|
}
|
package/src/cli.mjs
CHANGED
|
@@ -160,7 +160,9 @@ async function runOpen(positional, options) {
|
|
|
160
160
|
});
|
|
161
161
|
|
|
162
162
|
process.stdout.write(
|
|
163
|
-
|
|
163
|
+
launch.launched === false
|
|
164
|
+
? `${launch.browserFamily} is already running${launch.userDataDir ? ` with profile ${launch.userDataDir}` : ""}; reusing it\n`
|
|
165
|
+
: `launched ${launch.browserFamily} browser: ${launch.executable} ${launch.args.join(" ")}\n`,
|
|
164
166
|
);
|
|
165
167
|
|
|
166
168
|
if (launch.profileStrategy === "temporary" && launch.userDataDir) {
|
package/src/config.mjs
CHANGED
|
@@ -125,6 +125,7 @@ export function loadConfig(env = process.env) {
|
|
|
125
125
|
logLevel: logging.logLevel,
|
|
126
126
|
debugStdio: logging.debugStdio,
|
|
127
127
|
logFile: logging.logFile,
|
|
128
|
+
userDataDir: env.MCP_BROWSER_USER_DATA_DIR?.trim() || null,
|
|
128
129
|
protocolVersion:
|
|
129
130
|
env.MCP_PROTOCOL_VERSION?.trim() || DEFAULT_PROTOCOL_VERSION,
|
|
130
131
|
};
|
package/src/mcp-server.mjs
CHANGED
|
@@ -396,7 +396,7 @@ function launchBrowserInputSchema(browserFamily, enableUnsafeLaunchArgs) {
|
|
|
396
396
|
userDataDir: {
|
|
397
397
|
type: "string",
|
|
398
398
|
description:
|
|
399
|
-
"Browser profile directory.
|
|
399
|
+
"Browser profile directory for a new launch (default: MCP_BROWSER_USER_DATA_DIR when set). Without either, Chromium and Edge get a fresh temporary profile if that browser is already running, and the default profile otherwise.",
|
|
400
400
|
},
|
|
401
401
|
waitMs: {
|
|
402
402
|
type: "integer",
|
|
@@ -459,7 +459,7 @@ function ensureBrowserInputSchema(browserFamily, enableUnsafeLaunchArgs) {
|
|
|
459
459
|
userDataDir: {
|
|
460
460
|
type: "string",
|
|
461
461
|
description:
|
|
462
|
-
"Browser profile directory.
|
|
462
|
+
"Browser profile directory for a new launch (default: MCP_BROWSER_USER_DATA_DIR when set). Without either, Chromium and Edge get a fresh temporary profile if that browser is already running, and the default profile otherwise.",
|
|
463
463
|
},
|
|
464
464
|
waitMs: {
|
|
465
465
|
type: "integer",
|
|
@@ -502,7 +502,19 @@ function isRequestedBrowserAvailable(
|
|
|
502
502
|
return Boolean(status?.available);
|
|
503
503
|
}
|
|
504
504
|
|
|
505
|
-
|
|
505
|
+
// Edge shares the Chromium adapter in auto mode.
|
|
506
|
+
const adapterFamily =
|
|
507
|
+
requestedFamily === "edge" ? "chromium" : requestedFamily;
|
|
508
|
+
return Boolean(status?.browsers?.[adapterFamily]?.available);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const STATUS_PROBE_ATTEMPTS = 3;
|
|
512
|
+
const LAUNCH_GRACE_MS = 30_000;
|
|
513
|
+
|
|
514
|
+
function sleep(ms) {
|
|
515
|
+
return new Promise((resolve) => {
|
|
516
|
+
setTimeout(resolve, ms);
|
|
517
|
+
});
|
|
506
518
|
}
|
|
507
519
|
|
|
508
520
|
function shouldCreateBrowserTab(args) {
|
|
@@ -563,6 +575,7 @@ export class McpBrowserDevToolsServer {
|
|
|
563
575
|
config,
|
|
564
576
|
browserAdapter,
|
|
565
577
|
launchBrowser = launchLocalBrowser,
|
|
578
|
+
statusProbeRetryMs = 500,
|
|
566
579
|
input = process.stdin,
|
|
567
580
|
output = process.stdout,
|
|
568
581
|
errorOutput = process.stderr,
|
|
@@ -575,6 +588,9 @@ export class McpBrowserDevToolsServer {
|
|
|
575
588
|
this.config = config;
|
|
576
589
|
this.browserAdapter = browserAdapter;
|
|
577
590
|
this.launchBrowser = launchBrowser;
|
|
591
|
+
this.statusProbeRetryMs = statusProbeRetryMs;
|
|
592
|
+
this.launchQueue = Promise.resolve();
|
|
593
|
+
this.unconfirmedLaunches = new Map();
|
|
578
594
|
this.input = input;
|
|
579
595
|
this.output = output;
|
|
580
596
|
this.errorOutput = errorOutput;
|
|
@@ -583,6 +599,122 @@ export class McpBrowserDevToolsServer {
|
|
|
583
599
|
this.tools = this.createTools();
|
|
584
600
|
}
|
|
585
601
|
|
|
602
|
+
// Runs launch decisions one at a time, so parallel tool calls see the browser
|
|
603
|
+
// the first call started instead of each launching their own.
|
|
604
|
+
runExclusiveLaunch(task) {
|
|
605
|
+
const run = this.launchQueue.then(task, task);
|
|
606
|
+
this.launchQueue = run.catch(() => {});
|
|
607
|
+
return run;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// A busy browser can miss a single status check, and treating that as "not
|
|
611
|
+
// running" would launch a second browser, so retry before giving up.
|
|
612
|
+
async probeBrowser(browserFamily) {
|
|
613
|
+
let status = null;
|
|
614
|
+
for (let attempt = 0; attempt < STATUS_PROBE_ATTEMPTS; attempt += 1) {
|
|
615
|
+
if (attempt > 0) {
|
|
616
|
+
await sleep(this.statusProbeRetryMs);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
status = await this.browserAdapter.getBrowserStatus();
|
|
620
|
+
if (
|
|
621
|
+
isRequestedBrowserAvailable(
|
|
622
|
+
status,
|
|
623
|
+
this.config.browserFamily,
|
|
624
|
+
browserFamily,
|
|
625
|
+
)
|
|
626
|
+
) {
|
|
627
|
+
this.unconfirmedLaunches.delete(
|
|
628
|
+
browserFamily ?? this.config.browserFamily,
|
|
629
|
+
);
|
|
630
|
+
return { status, available: true };
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
return { status, available: false };
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// A browser can take a while to expose its endpoint after launch. Launching
|
|
638
|
+
// again in that window would start a second browser, so refuse until the
|
|
639
|
+
// first one answers or the grace period passes.
|
|
640
|
+
async launchUnlessPending(browserFamily, options) {
|
|
641
|
+
const key = browserFamily ?? this.config.browserFamily;
|
|
642
|
+
const launchedAt = this.unconfirmedLaunches.get(key);
|
|
643
|
+
if (launchedAt !== undefined && Date.now() - launchedAt < LAUNCH_GRACE_MS) {
|
|
644
|
+
const seconds = Math.round((Date.now() - launchedAt) / 1000);
|
|
645
|
+
throw new Error(
|
|
646
|
+
`A ${key} browser launched ${seconds}s ago has not exposed its debugging endpoint yet, so another one was not launched. Wait a few seconds and call ensure_browser again.`,
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
const launch = await this.launchBrowser(options);
|
|
651
|
+
const confirmed =
|
|
652
|
+
launch?.launched === false ||
|
|
653
|
+
launch?.doctorReport?.browserStatus?.available === true;
|
|
654
|
+
if (confirmed) {
|
|
655
|
+
this.unconfirmedLaunches.delete(key);
|
|
656
|
+
} else {
|
|
657
|
+
this.unconfirmedLaunches.set(key, Date.now());
|
|
658
|
+
}
|
|
659
|
+
return launch;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
async ensureBrowser(args) {
|
|
663
|
+
const browserFamily = requestedBrowserFamily(
|
|
664
|
+
this.config.browserFamily,
|
|
665
|
+
args.browserFamily,
|
|
666
|
+
);
|
|
667
|
+
const { status: currentStatus, available } =
|
|
668
|
+
await this.probeBrowser(browserFamily);
|
|
669
|
+
let launch = null;
|
|
670
|
+
let status = currentStatus;
|
|
671
|
+
|
|
672
|
+
if (!available) {
|
|
673
|
+
if (args.launchIfMissing === false) {
|
|
674
|
+
return {
|
|
675
|
+
browserFamily,
|
|
676
|
+
available: false,
|
|
677
|
+
launched: false,
|
|
678
|
+
status,
|
|
679
|
+
tab: null,
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
launch = await this.launchUnlessPending(browserFamily, {
|
|
684
|
+
config: this.config,
|
|
685
|
+
browserFamily,
|
|
686
|
+
url: args.url,
|
|
687
|
+
port: args.port,
|
|
688
|
+
address: args.address,
|
|
689
|
+
userDataDir: args.userDataDir,
|
|
690
|
+
unsafeArgs: args.unsafeArgs,
|
|
691
|
+
waitMs: args.waitMs,
|
|
692
|
+
skipDoctor: args.skipDoctor,
|
|
693
|
+
});
|
|
694
|
+
({ status } = await this.probeBrowser(browserFamily));
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
let tab = null;
|
|
698
|
+
if (shouldCreateBrowserTab(args)) {
|
|
699
|
+
tab = await this.browserAdapter.createTab(args.url, {
|
|
700
|
+
browserFamily,
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
return {
|
|
705
|
+
browserFamily,
|
|
706
|
+
available: isRequestedBrowserAvailable(
|
|
707
|
+
status,
|
|
708
|
+
this.config.browserFamily,
|
|
709
|
+
browserFamily,
|
|
710
|
+
),
|
|
711
|
+
launched: Boolean(launch) && launch.launched !== false,
|
|
712
|
+
launch,
|
|
713
|
+
status,
|
|
714
|
+
tab,
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
|
|
586
718
|
createTools() {
|
|
587
719
|
const tools = [
|
|
588
720
|
[
|
|
@@ -621,23 +753,56 @@ export class McpBrowserDevToolsServer {
|
|
|
621
753
|
definition: {
|
|
622
754
|
name: "launch_browser",
|
|
623
755
|
description:
|
|
624
|
-
"Launch a local debug-enabled browser process that matches the current broker configuration and return launch details plus an optional doctor report.",
|
|
756
|
+
"Launch a local debug-enabled browser process that matches the current broker configuration and return launch details plus an optional doctor report. When port, address, userDataDir, and unsafeArgs are all omitted and a browser of this family is already reachable, returns it with reused: true instead of launching, opening url in a new tab if given. On macOS and Linux it never starts a second browser on a profile that is already open. Prefer ensure_browser.",
|
|
625
757
|
inputSchema: launchBrowserInputSchema(
|
|
626
758
|
this.config.browserFamily,
|
|
627
759
|
this.config.enableUnsafeLaunchArgs,
|
|
628
760
|
),
|
|
629
761
|
},
|
|
630
762
|
handler: async (args) =>
|
|
631
|
-
this.
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
763
|
+
this.runExclusiveLaunch(async () => {
|
|
764
|
+
const browserFamily = requestedBrowserFamily(
|
|
765
|
+
this.config.browserFamily,
|
|
766
|
+
args.browserFamily,
|
|
767
|
+
);
|
|
768
|
+
// A requested port, address, profile, or flags ask for a
|
|
769
|
+
// specific browser, so only a plain launch may reuse the
|
|
770
|
+
// running one.
|
|
771
|
+
if (
|
|
772
|
+
args.port === undefined &&
|
|
773
|
+
args.address === undefined &&
|
|
774
|
+
args.userDataDir === undefined &&
|
|
775
|
+
args.unsafeArgs === undefined
|
|
776
|
+
) {
|
|
777
|
+
const { status, available } =
|
|
778
|
+
await this.probeBrowser(browserFamily);
|
|
779
|
+
if (available) {
|
|
780
|
+
const tab = shouldCreateBrowserTab(args)
|
|
781
|
+
? await this.browserAdapter.createTab(args.url, {
|
|
782
|
+
browserFamily,
|
|
783
|
+
})
|
|
784
|
+
: null;
|
|
785
|
+
return {
|
|
786
|
+
browserFamily,
|
|
787
|
+
launched: false,
|
|
788
|
+
reused: true,
|
|
789
|
+
status,
|
|
790
|
+
tab,
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
return this.launchUnlessPending(browserFamily, {
|
|
796
|
+
config: this.config,
|
|
797
|
+
browserFamily: args.browserFamily,
|
|
798
|
+
url: args.url,
|
|
799
|
+
port: args.port,
|
|
800
|
+
address: args.address,
|
|
801
|
+
userDataDir: args.userDataDir,
|
|
802
|
+
unsafeArgs: args.unsafeArgs,
|
|
803
|
+
waitMs: args.waitMs,
|
|
804
|
+
skipDoctor: args.skipDoctor,
|
|
805
|
+
});
|
|
641
806
|
}),
|
|
642
807
|
},
|
|
643
808
|
],
|
|
@@ -647,71 +812,14 @@ export class McpBrowserDevToolsServer {
|
|
|
647
812
|
definition: {
|
|
648
813
|
name: "ensure_browser",
|
|
649
814
|
description:
|
|
650
|
-
"Ensure a compatible browser is reachable through the current broker. If
|
|
815
|
+
"Ensure a compatible browser is reachable through the current broker, reusing a running one when possible. If none answers after three status checks, launch one locally, using the MCP_BROWSER_USER_DATA_DIR profile when set, and optionally open a tab for the requested URL. Will not launch again for 30 seconds while a browser it launched is still starting, and on macOS and Linux never starts a second browser on a profile that is already open.",
|
|
651
816
|
inputSchema: ensureBrowserInputSchema(
|
|
652
817
|
this.config.browserFamily,
|
|
653
818
|
this.config.enableUnsafeLaunchArgs,
|
|
654
819
|
),
|
|
655
820
|
},
|
|
656
|
-
handler: async (args) =>
|
|
657
|
-
|
|
658
|
-
this.config.browserFamily,
|
|
659
|
-
args.browserFamily,
|
|
660
|
-
);
|
|
661
|
-
const currentStatus = await this.browserAdapter.getBrowserStatus();
|
|
662
|
-
const available = isRequestedBrowserAvailable(
|
|
663
|
-
currentStatus,
|
|
664
|
-
this.config.browserFamily,
|
|
665
|
-
browserFamily,
|
|
666
|
-
);
|
|
667
|
-
let launch = null;
|
|
668
|
-
let status = currentStatus;
|
|
669
|
-
|
|
670
|
-
if (!available) {
|
|
671
|
-
if (args.launchIfMissing === false) {
|
|
672
|
-
return {
|
|
673
|
-
browserFamily,
|
|
674
|
-
available: false,
|
|
675
|
-
launched: false,
|
|
676
|
-
status,
|
|
677
|
-
tab: null,
|
|
678
|
-
};
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
launch = await this.launchBrowser({
|
|
682
|
-
config: this.config,
|
|
683
|
-
browserFamily,
|
|
684
|
-
url: args.url,
|
|
685
|
-
port: args.port,
|
|
686
|
-
address: args.address,
|
|
687
|
-
userDataDir: args.userDataDir,
|
|
688
|
-
unsafeArgs: args.unsafeArgs,
|
|
689
|
-
waitMs: args.waitMs,
|
|
690
|
-
skipDoctor: args.skipDoctor,
|
|
691
|
-
});
|
|
692
|
-
status = await this.browserAdapter.getBrowserStatus();
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
let tab = null;
|
|
696
|
-
if (shouldCreateBrowserTab(args)) {
|
|
697
|
-
tab = await this.browserAdapter.createTab(args.url, {
|
|
698
|
-
browserFamily,
|
|
699
|
-
});
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
return {
|
|
703
|
-
browserFamily,
|
|
704
|
-
available: isRequestedBrowserAvailable(
|
|
705
|
-
status,
|
|
706
|
-
this.config.browserFamily,
|
|
707
|
-
browserFamily,
|
|
708
|
-
),
|
|
709
|
-
launched: Boolean(launch),
|
|
710
|
-
launch,
|
|
711
|
-
status,
|
|
712
|
-
tab,
|
|
713
|
-
};
|
|
714
|
-
},
|
|
821
|
+
handler: async (args) =>
|
|
822
|
+
this.runExclusiveLaunch(() => this.ensureBrowser(args)),
|
|
715
823
|
},
|
|
716
824
|
],
|
|
717
825
|
[
|