mcp-browser-dev-tools 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/package.json +2 -1
- package/src/cdp-client.mjs +2 -1
- package/src/firefox-bidi-client.mjs +2 -1
- package/src/mcp-server.mjs +61 -6
- package/src/session-ids.mjs +26 -0
- package/src/tools/browser-tools.mjs +86 -4
- package/src/tools/inspection-tools.mjs +1 -1
- package/src/tools/more-tools.mjs +8 -4
package/README.md
CHANGED
|
@@ -47,6 +47,8 @@ Your agent gets 46 browser tools over MCP: open or attach to tabs, click, type,
|
|
|
47
47
|
- **Right where you work.** It attaches to the browser you already use, with its logins, tabs, and extensions, and speaks to Chrome, Edge, and Firefox with the same tools.
|
|
48
48
|
- **A debugger at hand.** Console messages with stack traces, network requests and HAR export, cookies and storage, and a one-call debug report.
|
|
49
49
|
|
|
50
|
+
Against Playwright MCP and Chrome DevTools MCP, on the same tasks with six models, it took the fewest turns and the least time with every model, while Playwright MCP cost less with five of the six: see the [comparison](https://hinaser.github.io/mcp-browser-dev-tools/).
|
|
51
|
+
|
|
50
52
|
## One Call, Start To Finish
|
|
51
53
|
|
|
52
54
|
A `run_steps` call that dismisses a cookie banner if one is showing, fills in and submits a form, waits for either the success or the error message, and reads it:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-browser-dev-tools",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Browser MCP server for AI agents: batch actions, waits, and checks into one call on Chrome, Edge, or Firefox (CDP and WebDriver BiDi)",
|
|
5
5
|
"author": "Hinaser <rindo.hinase@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"src/package-info.mjs",
|
|
43
43
|
"src/serve-bootstrap.mjs",
|
|
44
44
|
"src/session-events.mjs",
|
|
45
|
+
"src/session-ids.mjs",
|
|
45
46
|
"src/server.mjs",
|
|
46
47
|
"src/tcp-relay.mjs",
|
|
47
48
|
"src/top-level-await.mjs",
|
package/src/cdp-client.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { newSessionId } from "./session-ids.mjs";
|
|
2
3
|
import { tmpdir } from "node:os";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
|
|
@@ -480,7 +481,7 @@ function videoGapWarnings(state) {
|
|
|
480
481
|
|
|
481
482
|
export class CdpSession {
|
|
482
483
|
constructor(target, options = {}) {
|
|
483
|
-
this.id =
|
|
484
|
+
this.id = newSessionId();
|
|
484
485
|
this.config = options.config ?? { browserFamily: "chromium" };
|
|
485
486
|
this.target = target;
|
|
486
487
|
this.eventBufferSize = options.eventBufferSize ?? 200;
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
InFlightRequests,
|
|
5
5
|
summarizeNetworkRequests,
|
|
6
6
|
} from "./session-events.mjs";
|
|
7
|
+
import { newSessionId } from "./session-ids.mjs";
|
|
7
8
|
import { DEFAULT_FIREFOX_BIDI_WS_URL } from "./config.mjs";
|
|
8
9
|
import {
|
|
9
10
|
answerDialog,
|
|
@@ -440,7 +441,7 @@ function joinUrlPath(baseUrl, pathname) {
|
|
|
440
441
|
|
|
441
442
|
class FirefoxBidiTabSession {
|
|
442
443
|
constructor(target, options = {}) {
|
|
443
|
-
this.id =
|
|
444
|
+
this.id = newSessionId();
|
|
444
445
|
this.target = target;
|
|
445
446
|
this.eventBufferSize = options.eventBufferSize ?? 200;
|
|
446
447
|
this.connectedAt = new Date().toISOString();
|
package/src/mcp-server.mjs
CHANGED
|
@@ -76,8 +76,10 @@ const STATUS_PROBE_ATTEMPTS = 3;
|
|
|
76
76
|
const LAUNCH_GRACE_MS = 30_000;
|
|
77
77
|
|
|
78
78
|
// The definition clients see: additionalProperties: false tells a model
|
|
79
|
-
// nothing it acts on, so it is left out of what every turn pays for
|
|
80
|
-
//
|
|
79
|
+
// nothing it acts on, so it is left out of what every turn pays for, and
|
|
80
|
+
// sessionId is not required, since a call without it uses the default
|
|
81
|
+
// session (see defaultSessionId). Calls are still validated against the full
|
|
82
|
+
// schema once the default is filled in.
|
|
81
83
|
function advertisedDefinition(definition) {
|
|
82
84
|
const strip = (value) => {
|
|
83
85
|
if (Array.isArray(value)) {
|
|
@@ -94,7 +96,16 @@ function advertisedDefinition(definition) {
|
|
|
94
96
|
.map(([key, item]) => [key, strip(item)]),
|
|
95
97
|
);
|
|
96
98
|
};
|
|
97
|
-
|
|
99
|
+
const advertised = strip(definition);
|
|
100
|
+
const required = advertised.inputSchema?.required;
|
|
101
|
+
if (required?.includes("sessionId")) {
|
|
102
|
+
const rest = required.filter((name) => name !== "sessionId");
|
|
103
|
+
advertised.inputSchema = { ...advertised.inputSchema, required: rest };
|
|
104
|
+
if (rest.length === 0) {
|
|
105
|
+
delete advertised.inputSchema.required;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return advertised;
|
|
98
109
|
}
|
|
99
110
|
|
|
100
111
|
function sleep(ms) {
|
|
@@ -301,7 +312,11 @@ export class McpBrowserDevToolsServer {
|
|
|
301
312
|
runTabsTool(this.tabRunner, stepTools, this.config.browserFamily),
|
|
302
313
|
);
|
|
303
314
|
if (hidden.size > 0) {
|
|
304
|
-
tools.push(
|
|
315
|
+
tools.push(
|
|
316
|
+
moreToolsTool(hidden, advertisedDefinition, (schema, args) =>
|
|
317
|
+
this.withDefaultSession(schema, args),
|
|
318
|
+
),
|
|
319
|
+
);
|
|
305
320
|
}
|
|
306
321
|
|
|
307
322
|
return new Map(tools);
|
|
@@ -389,7 +404,7 @@ export class McpBrowserDevToolsServer {
|
|
|
389
404
|
name: SERVER_NAME,
|
|
390
405
|
version: SERVER_VERSION,
|
|
391
406
|
},
|
|
392
|
-
instructions: `
|
|
407
|
+
instructions: `Start with attach_tab, by the tab's url or a targetId from list_tabs or new_tab: it returns the page's snapshot, one line per visible heading and control with a ref such as e12 (f1e3 inside an iframe, which only refs reach). Tools then act on the tab attached last unless given another sessionId. To act, prefer run_steps: send the actions you already know, such as every field of a form, in one call: {"steps":[{"tool":"type","arguments":{"selector":"ref=e3","text":"Ada"}},{"tool":"click","arguments":{"selector":"ref=e4"}}]}. Actions return what changed once the page settles, which is usually the check; add wait_for only for something later. Read several pages at once with run_tabs. Locators, case-sensitive: ref=e12 until the page navigates; CSS; text=Foo and name=Foo, the first visible element whose text or accessible name equals, else contains, Foo (a label finds its control); role=button[name="Save"]; a >> b for b inside a, and has-text=Foo to filter: tr >> has-text=Ada >> text=Delete. more_tools lists and calls the other tools (drag, cookies, request mocking, HAR, performance).`,
|
|
393
408
|
});
|
|
394
409
|
case "notifications/initialized":
|
|
395
410
|
return null;
|
|
@@ -414,6 +429,43 @@ export class McpBrowserDevToolsServer {
|
|
|
414
429
|
}
|
|
415
430
|
}
|
|
416
431
|
|
|
432
|
+
// A tool that needs a session and got none uses the tab attached last,
|
|
433
|
+
// while it is still attached, else the only attached tab. Leaving the id
|
|
434
|
+
// out saves the model writing it into every call.
|
|
435
|
+
withDefaultSession(schema, args) {
|
|
436
|
+
// Arguments that are not an object are left for validation to reject.
|
|
437
|
+
if (
|
|
438
|
+
!args ||
|
|
439
|
+
typeof args !== "object" ||
|
|
440
|
+
Array.isArray(args) ||
|
|
441
|
+
args.sessionId !== undefined ||
|
|
442
|
+
!schema?.required?.includes("sessionId")
|
|
443
|
+
) {
|
|
444
|
+
return args;
|
|
445
|
+
}
|
|
446
|
+
return { ...args, sessionId: this.defaultSessionId() };
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
defaultSessionId() {
|
|
450
|
+
const sessions = this.browserAdapter.listSessions();
|
|
451
|
+
if (
|
|
452
|
+
this.lastAttachedSessionId &&
|
|
453
|
+
sessions.some(
|
|
454
|
+
(session) => session.sessionId === this.lastAttachedSessionId,
|
|
455
|
+
)
|
|
456
|
+
) {
|
|
457
|
+
return this.lastAttachedSessionId;
|
|
458
|
+
}
|
|
459
|
+
if (sessions.length === 1) {
|
|
460
|
+
return sessions[0].sessionId;
|
|
461
|
+
}
|
|
462
|
+
throw new Error(
|
|
463
|
+
sessions.length === 0
|
|
464
|
+
? "No tab is attached: call attach_tab first"
|
|
465
|
+
: `${sessions.length} tabs are attached and the one attached last is gone; pass the sessionId of one: ${sessions.map((session) => session.sessionId).join(", ")}`,
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
|
|
417
469
|
async callTool(params = {}) {
|
|
418
470
|
const startedAt = Date.now();
|
|
419
471
|
let response = null;
|
|
@@ -432,7 +484,10 @@ export class McpBrowserDevToolsServer {
|
|
|
432
484
|
);
|
|
433
485
|
}
|
|
434
486
|
|
|
435
|
-
const args =
|
|
487
|
+
const args = this.withDefaultSession(
|
|
488
|
+
tool.definition.inputSchema,
|
|
489
|
+
params.arguments ?? {},
|
|
490
|
+
);
|
|
436
491
|
validateValue("arguments", args, tool.definition.inputSchema);
|
|
437
492
|
tool.validate?.(args);
|
|
438
493
|
const result = await tool.handler(args);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Session ids go into nearly every tool call an agent writes, and the tokens
|
|
2
|
+
// a model writes cost the most, so they are short: "s" and seven base-36
|
|
3
|
+
// characters, unique within the process. A stale id from an earlier process
|
|
4
|
+
// is very unlikely to name a live session.
|
|
5
|
+
const ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
6
|
+
const LENGTH = 7;
|
|
7
|
+
const issued = new Set();
|
|
8
|
+
|
|
9
|
+
export function newSessionId() {
|
|
10
|
+
for (;;) {
|
|
11
|
+
let id = "s";
|
|
12
|
+
while (id.length <= LENGTH) {
|
|
13
|
+
for (const byte of crypto.getRandomValues(new Uint8Array(LENGTH))) {
|
|
14
|
+
// 252 is the largest multiple of 36 below 256, so every character
|
|
15
|
+
// is equally likely.
|
|
16
|
+
if (byte < 252 && id.length <= LENGTH) {
|
|
17
|
+
id += ALPHABET[byte % 36];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (!issued.has(id)) {
|
|
22
|
+
issued.add(id);
|
|
23
|
+
return id;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
launchBrowserInputSchema,
|
|
10
10
|
newTabInputSchema,
|
|
11
11
|
} from "../tool-schemas.mjs";
|
|
12
|
+
import { DEFAULT_SNAPSHOT_NODES } from "./inspection-tools.mjs";
|
|
12
13
|
|
|
13
14
|
const SERVER_NAME = PACKAGE_NAME;
|
|
14
15
|
const SERVER_VERSION = PACKAGE_VERSION;
|
|
@@ -179,7 +180,7 @@ export function browserTools(server) {
|
|
|
179
180
|
definition: {
|
|
180
181
|
name: "attach_tab",
|
|
181
182
|
description:
|
|
182
|
-
"Attach to a tab: returns the sessionId other tools take, and buffers its console and network events from now on.",
|
|
183
|
+
"Attach to a tab: returns the sessionId other tools take and the page's snapshot, as get_snapshot lists it, and buffers its console and network events from now on.",
|
|
183
184
|
inputSchema: {
|
|
184
185
|
type: "object",
|
|
185
186
|
properties: {
|
|
@@ -187,13 +188,16 @@ export function browserTools(server) {
|
|
|
187
188
|
type: "string",
|
|
188
189
|
description: "From list_tabs or new_tab.",
|
|
189
190
|
},
|
|
191
|
+
url: {
|
|
192
|
+
type: "string",
|
|
193
|
+
description:
|
|
194
|
+
"Instead of targetId: the tab whose URL is this, else the one whose URL contains it.",
|
|
195
|
+
},
|
|
190
196
|
},
|
|
191
|
-
required: ["targetId"],
|
|
192
197
|
additionalProperties: false,
|
|
193
198
|
},
|
|
194
199
|
},
|
|
195
|
-
handler: async (args) =>
|
|
196
|
-
server.browserAdapter.attachToTarget(args.targetId),
|
|
200
|
+
handler: async (args) => attachTab(server, args),
|
|
197
201
|
},
|
|
198
202
|
],
|
|
199
203
|
[
|
|
@@ -217,3 +221,81 @@ export function browserTools(server) {
|
|
|
217
221
|
],
|
|
218
222
|
];
|
|
219
223
|
}
|
|
224
|
+
|
|
225
|
+
// Attaches by targetId, or by url: the tab whose URL equals it, else the one
|
|
226
|
+
// whose URL contains it. Several matches, or none, fail with the tabs to
|
|
227
|
+
// choose from, so the agent needs no list_tabs call to try again. The result
|
|
228
|
+
// carries the page's snapshot, so the agent can act without another call; a
|
|
229
|
+
// snapshot that fails leaves the attach in place and says why.
|
|
230
|
+
async function attachTab(server, { targetId, url }) {
|
|
231
|
+
if ((targetId === undefined) === (url === undefined)) {
|
|
232
|
+
throw new Error("attach_tab takes either targetId or url");
|
|
233
|
+
}
|
|
234
|
+
const session = await server.browserAdapter.attachToTarget(
|
|
235
|
+
targetId ?? (await tabForUrl(server, url)),
|
|
236
|
+
);
|
|
237
|
+
server.lastAttachedSessionId = session.sessionId;
|
|
238
|
+
const settled = await settleAfterAttach(server, session.sessionId);
|
|
239
|
+
try {
|
|
240
|
+
const snapshot = await server.browserAdapter.snapshotPage(
|
|
241
|
+
session.sessionId,
|
|
242
|
+
{ limit: DEFAULT_SNAPSHOT_NODES, headings: true },
|
|
243
|
+
);
|
|
244
|
+
return {
|
|
245
|
+
...session,
|
|
246
|
+
nodes: snapshot.nodes,
|
|
247
|
+
...(snapshot.more > 0 ? { nodesMore: snapshot.more } : {}),
|
|
248
|
+
...(settled?.settled === false ? { settled: false } : {}),
|
|
249
|
+
};
|
|
250
|
+
} catch (error) {
|
|
251
|
+
return { ...session, snapshotError: error.message };
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Waits for the page to settle before the snapshot, as a batch does before a
|
|
256
|
+
// step that reads the page: a page still loading or rendering is listed once
|
|
257
|
+
// its DOM has been quiet for a moment, for at most 2 s (10 s while it
|
|
258
|
+
// navigates). Work the page set going before the attach, such as a timer or
|
|
259
|
+
// a request already in flight, is not seen. Returns null when the page could
|
|
260
|
+
// not be watched.
|
|
261
|
+
async function settleAfterAttach(server, sessionId) {
|
|
262
|
+
const runner = server.stepRunner;
|
|
263
|
+
if (!runner?.changeTracker) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
const context = { sessionId, settles: true };
|
|
267
|
+
try {
|
|
268
|
+
await runner.watchPage(context);
|
|
269
|
+
const now = Date.now();
|
|
270
|
+
context.settling = { endedAt: now, since: now };
|
|
271
|
+
return await runner.settlePage(context);
|
|
272
|
+
} catch {
|
|
273
|
+
await runner.stopWatching(context).catch(() => {});
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const MAX_LISTED_TABS = 10;
|
|
279
|
+
|
|
280
|
+
async function tabForUrl(server, url) {
|
|
281
|
+
const tabs = (await server.browserAdapter.listTargets()).filter(
|
|
282
|
+
(tab) =>
|
|
283
|
+
(tab.type === undefined || tab.type === "page") &&
|
|
284
|
+
typeof tab.url === "string",
|
|
285
|
+
);
|
|
286
|
+
const exact = tabs.filter((tab) => tab.url === url);
|
|
287
|
+
const matches =
|
|
288
|
+
exact.length > 0 ? exact : tabs.filter((tab) => tab.url.includes(url));
|
|
289
|
+
if (matches.length === 1) {
|
|
290
|
+
return matches[0].targetId;
|
|
291
|
+
}
|
|
292
|
+
const listed = (matches.length > 1 ? matches : tabs)
|
|
293
|
+
.slice(0, MAX_LISTED_TABS)
|
|
294
|
+
.map((tab) => `${tab.targetId} ${tab.url}`)
|
|
295
|
+
.join("; ");
|
|
296
|
+
throw new Error(
|
|
297
|
+
matches.length > 1
|
|
298
|
+
? `${matches.length} tabs match ${url}; pass the targetId of one: ${listed}`
|
|
299
|
+
: `No tab's URL contains ${url}. Open tabs: ${listed || "none"}`,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
import { selectorProperty, sessionWithLimitSchema } from "../tool-schemas.mjs";
|
|
16
16
|
|
|
17
17
|
const DEFAULT_READ_CHARS = 8000;
|
|
18
|
-
const DEFAULT_SNAPSHOT_NODES = 100;
|
|
18
|
+
export const DEFAULT_SNAPSHOT_NODES = 100;
|
|
19
19
|
const MAX_SNAPSHOT_NODES = 500;
|
|
20
20
|
|
|
21
21
|
// The snapshot's nodes are lines already; send them as plain text rather than
|
package/src/tools/more-tools.mjs
CHANGED
|
@@ -21,7 +21,7 @@ function firstSentence(text) {
|
|
|
21
21
|
// lists them, describes one, or calls one, so an agent can find and use a
|
|
22
22
|
// tool without every turn paying for its definition. run_steps and
|
|
23
23
|
// run_tabs steps can use them too.
|
|
24
|
-
export function moreToolsTool(hidden, advertise) {
|
|
24
|
+
export function moreToolsTool(hidden, advertise, withDefaultSession) {
|
|
25
25
|
const lookup = (name) => {
|
|
26
26
|
const tool = hidden.get(name);
|
|
27
27
|
if (!tool) {
|
|
@@ -72,13 +72,17 @@ export function moreToolsTool(hidden, advertise) {
|
|
|
72
72
|
group: groupOf(args.name),
|
|
73
73
|
};
|
|
74
74
|
}
|
|
75
|
+
const toolArgs = withDefaultSession(
|
|
76
|
+
tool.definition.inputSchema,
|
|
77
|
+
args.arguments,
|
|
78
|
+
);
|
|
75
79
|
validateValue(
|
|
76
80
|
"arguments.arguments",
|
|
77
|
-
|
|
81
|
+
toolArgs,
|
|
78
82
|
tool.definition.inputSchema,
|
|
79
83
|
);
|
|
80
|
-
tool.validate?.(
|
|
81
|
-
return tool.handler(
|
|
84
|
+
tool.validate?.(toolArgs);
|
|
85
|
+
return tool.handler(toolArgs, options);
|
|
82
86
|
},
|
|
83
87
|
// A call's result is formatted as the tool itself would format it;
|
|
84
88
|
// the list goes as one line per tool, which costs less than JSON.
|