mcp-browser-dev-tools 0.0.5 → 0.0.6-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 +10 -0
- package/package.json +2 -1
- package/src/browser-adapter.mjs +5 -2
- package/src/browser-launch-service.mjs +146 -9
- package/src/cdp-client.mjs +120 -20
- package/src/cli.mjs +3 -1
- package/src/config.mjs +1 -0
- package/src/firefox-bidi-client.mjs +127 -22
- package/src/keyboard.mjs +278 -0
- package/src/mcp-server.mjs +312 -109
- package/src/page-context.mjs +210 -88
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-browser-dev-tools",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6-beta.1",
|
|
4
4
|
"description": "Local MCP server for Chromium DevTools Protocol and Firefox WebDriver BiDi",
|
|
5
5
|
"author": "Hinaser <rindo.hinase@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"src/firefox-bidi-client.mjs",
|
|
33
33
|
"src/index.mjs",
|
|
34
34
|
"src/json-rpc-stdio.mjs",
|
|
35
|
+
"src/keyboard.mjs",
|
|
35
36
|
"src/logger.mjs",
|
|
36
37
|
"src/mcp-server.mjs",
|
|
37
38
|
"src/page-context.mjs",
|
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/cdp-client.mjs
CHANGED
|
@@ -5,7 +5,11 @@ import {
|
|
|
5
5
|
} from "./session-events.mjs";
|
|
6
6
|
import { DEFAULT_CDP_BASE_URL } from "./config.mjs";
|
|
7
7
|
import { waitForPageCondition } from "./wait-for.mjs";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
assertPointerTarget,
|
|
10
|
+
buildPageContextExpression,
|
|
11
|
+
} from "./page-context.mjs";
|
|
12
|
+
import { buildCdpKeyEvents } from "./keyboard.mjs";
|
|
9
13
|
|
|
10
14
|
function toErrorMessage(error) {
|
|
11
15
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -807,36 +811,120 @@ export class CdpSession {
|
|
|
807
811
|
});
|
|
808
812
|
}
|
|
809
813
|
|
|
814
|
+
async resolvePointerTarget(selector) {
|
|
815
|
+
const target = await this.runPageAction({
|
|
816
|
+
action: "pointer_target",
|
|
817
|
+
selector,
|
|
818
|
+
});
|
|
819
|
+
assertPointerTarget(target);
|
|
820
|
+
return target;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
async dispatchMouse(type, point, extra = {}) {
|
|
824
|
+
await this.send("Input.dispatchMouseEvent", {
|
|
825
|
+
type,
|
|
826
|
+
x: point.x,
|
|
827
|
+
y: point.y,
|
|
828
|
+
...extra,
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
async dispatchKeyEvents(events) {
|
|
833
|
+
for (const params of events) {
|
|
834
|
+
await this.send("Input.dispatchKeyEvent", params);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
|
|
810
838
|
async click(selector) {
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
);
|
|
839
|
+
const target = await this.resolvePointerTarget(selector);
|
|
840
|
+
if (!target.found) {
|
|
841
|
+
return target;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
const press = { button: "left", clickCount: 1 };
|
|
845
|
+
await this.dispatchMouse("mouseMoved", target.point);
|
|
846
|
+
await this.dispatchMouse("mousePressed", target.point, {
|
|
847
|
+
...press,
|
|
848
|
+
buttons: 1,
|
|
849
|
+
});
|
|
850
|
+
await this.dispatchMouse("mouseReleased", target.point, {
|
|
851
|
+
...press,
|
|
852
|
+
buttons: 0,
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
return {
|
|
856
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
857
|
+
selector,
|
|
858
|
+
found: true,
|
|
859
|
+
clicked: true,
|
|
860
|
+
point: target.point,
|
|
861
|
+
node: target.node,
|
|
862
|
+
};
|
|
818
863
|
}
|
|
819
864
|
|
|
820
865
|
async hover(selector) {
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
866
|
+
const target = await this.resolvePointerTarget(selector);
|
|
867
|
+
if (!target.found) {
|
|
868
|
+
return target;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
await this.dispatchMouse("mouseMoved", target.point);
|
|
872
|
+
|
|
873
|
+
return {
|
|
874
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
875
|
+
selector,
|
|
876
|
+
found: true,
|
|
877
|
+
hovered: true,
|
|
878
|
+
point: target.point,
|
|
879
|
+
node: target.node,
|
|
880
|
+
};
|
|
828
881
|
}
|
|
829
882
|
|
|
830
883
|
async type(selector, text, options = {}) {
|
|
831
|
-
|
|
884
|
+
const prepared = await this.runPageAction(
|
|
832
885
|
{
|
|
833
|
-
action: "
|
|
886
|
+
action: "prepare_type",
|
|
834
887
|
selector,
|
|
835
888
|
text,
|
|
836
889
|
clear: options.clear,
|
|
837
890
|
},
|
|
838
891
|
{ userGesture: true },
|
|
839
892
|
);
|
|
893
|
+
if (!prepared.found || prepared.method !== "native") {
|
|
894
|
+
return prepared;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
// insertText goes through the browser's editing pipeline, so frameworks
|
|
898
|
+
// see trusted beforeinput/input events and the real value change.
|
|
899
|
+
// Newlines become Enter presses, matching the Firefox path: a line break
|
|
900
|
+
// in a textarea, implicit submission in a single-line input.
|
|
901
|
+
if (text) {
|
|
902
|
+
const lines = text.replace(/\r\n?/g, "\n").split("\n");
|
|
903
|
+
for (const [index, line] of lines.entries()) {
|
|
904
|
+
if (index > 0) {
|
|
905
|
+
await this.dispatchKeyEvents(buildCdpKeyEvents("Enter"));
|
|
906
|
+
}
|
|
907
|
+
if (line) {
|
|
908
|
+
await this.send("Input.insertText", { text: line });
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
} else if (options.clear !== false) {
|
|
912
|
+
await this.dispatchKeyEvents(buildCdpKeyEvents("Delete"));
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
const typed = await this.runPageAction({
|
|
916
|
+
action: "inspect",
|
|
917
|
+
selector,
|
|
918
|
+
scrollIntoView: false,
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
return {
|
|
922
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
923
|
+
selector,
|
|
924
|
+
found: true,
|
|
925
|
+
typedText: text,
|
|
926
|
+
node: typed.found ? typed.node : prepared.node,
|
|
927
|
+
};
|
|
840
928
|
}
|
|
841
929
|
|
|
842
930
|
async select(selector, options = {}) {
|
|
@@ -852,14 +940,26 @@ export class CdpSession {
|
|
|
852
940
|
}
|
|
853
941
|
|
|
854
942
|
async pressKey(key, selector = null) {
|
|
855
|
-
|
|
943
|
+
const events = buildCdpKeyEvents(key);
|
|
944
|
+
const focused = await this.runPageAction(
|
|
856
945
|
{
|
|
857
|
-
action: "
|
|
858
|
-
key,
|
|
946
|
+
action: "focus",
|
|
859
947
|
selector,
|
|
860
948
|
},
|
|
861
949
|
{ userGesture: true },
|
|
862
950
|
);
|
|
951
|
+
if (!focused.found) {
|
|
952
|
+
return focused;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
await this.dispatchKeyEvents(events);
|
|
956
|
+
|
|
957
|
+
return {
|
|
958
|
+
browserFamily: cdpBrowserFamily(this.config),
|
|
959
|
+
key,
|
|
960
|
+
dispatched: true,
|
|
961
|
+
target: focused.target,
|
|
962
|
+
};
|
|
863
963
|
}
|
|
864
964
|
|
|
865
965
|
async scroll(options = {}) {
|
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
|
};
|
|
@@ -5,7 +5,26 @@ import {
|
|
|
5
5
|
} from "./session-events.mjs";
|
|
6
6
|
import { DEFAULT_FIREFOX_BIDI_WS_URL } from "./config.mjs";
|
|
7
7
|
import { waitForPageCondition } from "./wait-for.mjs";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
assertPointerTarget,
|
|
10
|
+
buildPageContextExpression,
|
|
11
|
+
} from "./page-context.mjs";
|
|
12
|
+
import { buildBidiKeyActions, buildBidiTextActions } from "./keyboard.mjs";
|
|
13
|
+
|
|
14
|
+
const POINTER_SOURCE = {
|
|
15
|
+
type: "pointer",
|
|
16
|
+
id: "mcp-mouse",
|
|
17
|
+
parameters: { pointerType: "mouse" },
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function pointerMove(point) {
|
|
21
|
+
return {
|
|
22
|
+
type: "pointerMove",
|
|
23
|
+
x: Math.round(point.x),
|
|
24
|
+
y: Math.round(point.y),
|
|
25
|
+
origin: "viewport",
|
|
26
|
+
};
|
|
27
|
+
}
|
|
9
28
|
|
|
10
29
|
function toErrorMessage(error) {
|
|
11
30
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -1013,39 +1032,112 @@ export class FirefoxBidiSessionManager {
|
|
|
1013
1032
|
};
|
|
1014
1033
|
}
|
|
1015
1034
|
|
|
1035
|
+
async performActions(session, actions) {
|
|
1036
|
+
await this.send("input.performActions", {
|
|
1037
|
+
context: session.target.targetId,
|
|
1038
|
+
actions,
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
async performKeyActions(session, actions) {
|
|
1043
|
+
await this.performActions(session, [
|
|
1044
|
+
{ type: "key", id: "mcp-keyboard", actions },
|
|
1045
|
+
]);
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
async resolvePointerTarget(session, selector) {
|
|
1049
|
+
const target = await this.runPageAction(session, {
|
|
1050
|
+
action: "pointer_target",
|
|
1051
|
+
selector,
|
|
1052
|
+
});
|
|
1053
|
+
assertPointerTarget(target);
|
|
1054
|
+
return target;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1016
1057
|
async click(sessionId, selector) {
|
|
1017
|
-
|
|
1018
|
-
|
|
1058
|
+
const session = this.getSession(sessionId);
|
|
1059
|
+
const target = await this.resolvePointerTarget(session, selector);
|
|
1060
|
+
if (!target.found) {
|
|
1061
|
+
return target;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
await this.performActions(session, [
|
|
1019
1065
|
{
|
|
1020
|
-
|
|
1021
|
-
|
|
1066
|
+
...POINTER_SOURCE,
|
|
1067
|
+
actions: [
|
|
1068
|
+
pointerMove(target.point),
|
|
1069
|
+
{ type: "pointerDown", button: 0 },
|
|
1070
|
+
{ type: "pointerUp", button: 0 },
|
|
1071
|
+
],
|
|
1022
1072
|
},
|
|
1023
|
-
|
|
1024
|
-
|
|
1073
|
+
]);
|
|
1074
|
+
|
|
1075
|
+
return {
|
|
1076
|
+
browserFamily: "firefox",
|
|
1077
|
+
selector,
|
|
1078
|
+
found: true,
|
|
1079
|
+
clicked: true,
|
|
1080
|
+
point: target.point,
|
|
1081
|
+
node: target.node,
|
|
1082
|
+
};
|
|
1025
1083
|
}
|
|
1026
1084
|
|
|
1027
1085
|
async hover(sessionId, selector) {
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1086
|
+
const session = this.getSession(sessionId);
|
|
1087
|
+
const target = await this.resolvePointerTarget(session, selector);
|
|
1088
|
+
if (!target.found) {
|
|
1089
|
+
return target;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
await this.performActions(session, [
|
|
1093
|
+
{ ...POINTER_SOURCE, actions: [pointerMove(target.point)] },
|
|
1094
|
+
]);
|
|
1095
|
+
|
|
1096
|
+
return {
|
|
1097
|
+
browserFamily: "firefox",
|
|
1098
|
+
selector,
|
|
1099
|
+
found: true,
|
|
1100
|
+
hovered: true,
|
|
1101
|
+
point: target.point,
|
|
1102
|
+
node: target.node,
|
|
1103
|
+
};
|
|
1036
1104
|
}
|
|
1037
1105
|
|
|
1038
1106
|
async type(sessionId, selector, text, options = {}) {
|
|
1039
|
-
|
|
1040
|
-
|
|
1107
|
+
const session = this.getSession(sessionId);
|
|
1108
|
+
const prepared = await this.runPageAction(
|
|
1109
|
+
session,
|
|
1041
1110
|
{
|
|
1042
|
-
action: "
|
|
1111
|
+
action: "prepare_type",
|
|
1043
1112
|
selector,
|
|
1044
1113
|
text,
|
|
1045
1114
|
clear: options.clear,
|
|
1046
1115
|
},
|
|
1047
1116
|
{ userActivation: true },
|
|
1048
1117
|
);
|
|
1118
|
+
if (!prepared.found || prepared.method !== "native") {
|
|
1119
|
+
return prepared;
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
if (text) {
|
|
1123
|
+
await this.performKeyActions(session, buildBidiTextActions(text));
|
|
1124
|
+
} else if (options.clear !== false) {
|
|
1125
|
+
await this.performKeyActions(session, buildBidiKeyActions("Backspace"));
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
const typed = await this.runPageAction(session, {
|
|
1129
|
+
action: "inspect",
|
|
1130
|
+
selector,
|
|
1131
|
+
scrollIntoView: false,
|
|
1132
|
+
});
|
|
1133
|
+
|
|
1134
|
+
return {
|
|
1135
|
+
browserFamily: "firefox",
|
|
1136
|
+
selector,
|
|
1137
|
+
found: true,
|
|
1138
|
+
typedText: text,
|
|
1139
|
+
node: typed.found ? typed.node : prepared.node,
|
|
1140
|
+
};
|
|
1049
1141
|
}
|
|
1050
1142
|
|
|
1051
1143
|
async select(sessionId, selector, options = {}) {
|
|
@@ -1062,15 +1154,28 @@ export class FirefoxBidiSessionManager {
|
|
|
1062
1154
|
}
|
|
1063
1155
|
|
|
1064
1156
|
async pressKey(sessionId, key, selector = null) {
|
|
1065
|
-
|
|
1066
|
-
|
|
1157
|
+
const session = this.getSession(sessionId);
|
|
1158
|
+
const actions = buildBidiKeyActions(key);
|
|
1159
|
+
const focused = await this.runPageAction(
|
|
1160
|
+
session,
|
|
1067
1161
|
{
|
|
1068
|
-
action: "
|
|
1069
|
-
key,
|
|
1162
|
+
action: "focus",
|
|
1070
1163
|
selector,
|
|
1071
1164
|
},
|
|
1072
1165
|
{ userActivation: true },
|
|
1073
1166
|
);
|
|
1167
|
+
if (!focused.found) {
|
|
1168
|
+
return focused;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
await this.performKeyActions(session, actions);
|
|
1172
|
+
|
|
1173
|
+
return {
|
|
1174
|
+
browserFamily: "firefox",
|
|
1175
|
+
key,
|
|
1176
|
+
dispatched: true,
|
|
1177
|
+
target: focused.target,
|
|
1178
|
+
};
|
|
1074
1179
|
}
|
|
1075
1180
|
|
|
1076
1181
|
async scroll(sessionId, options = {}) {
|