mcp-browser-dev-tools 0.0.6 → 0.2.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/README.md +94 -312
- package/package.json +18 -2
- package/src/action-retry.mjs +50 -0
- package/src/browser-adapter.mjs +15 -4
- package/src/browser-requests.mjs +15 -0
- package/src/cdp-client.mjs +104 -10
- package/src/config.mjs +6 -1
- package/src/firefox-bidi-client.mjs +26 -3
- package/src/json-schema.mjs +99 -0
- package/src/mcp-server.mjs +88 -1465
- package/src/page-context.mjs +25 -1034
- package/src/page-script.mjs +1372 -0
- package/src/run-steps.mjs +510 -0
- package/src/run-tabs.mjs +291 -0
- package/src/screenshot-output.mjs +141 -0
- package/src/tool-results.mjs +34 -0
- package/src/tool-schemas.mjs +431 -0
- package/src/tools/action-tools.mjs +318 -0
- package/src/tools/browser-tools.mjs +221 -0
- package/src/tools/inspection-tools.mjs +287 -0
- package/src/tools/state-tools.mjs +327 -0
- package/src/top-level-await.mjs +260 -0
- package/src/wait-for.mjs +453 -47
package/src/mcp-server.mjs
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { actionTools } from "./tools/action-tools.mjs";
|
|
2
|
+
import { browserTools } from "./tools/browser-tools.mjs";
|
|
3
|
+
import { inspectionTools } from "./tools/inspection-tools.mjs";
|
|
4
|
+
import { stateTools } from "./tools/state-tools.mjs";
|
|
5
|
+
import { appendFile } from "node:fs/promises";
|
|
6
|
+
import { launchBrowser as launchLocalBrowser } from "./browser-launch-service.mjs";
|
|
3
7
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "./browser-
|
|
8
|
+
requestedBrowserFamily,
|
|
9
|
+
shouldCreateBrowserTab,
|
|
10
|
+
} from "./browser-requests.mjs";
|
|
11
|
+
import { MessageBuffer, encodeMessage } from "./json-rpc-stdio.mjs";
|
|
12
|
+
import { validateValue } from "./json-schema.mjs";
|
|
13
|
+
import { createLogger } from "./logger.mjs";
|
|
7
14
|
import { PACKAGE_NAME, PACKAGE_VERSION } from "./package-info.mjs";
|
|
15
|
+
import { StepRunner, runStepsTool, stepSchema } from "./run-steps.mjs";
|
|
16
|
+
import { TabRunner, runTabsTool } from "./run-tabs.mjs";
|
|
17
|
+
import { asToolResult } from "./tool-results.mjs";
|
|
8
18
|
|
|
9
19
|
const SERVER_NAME = PACKAGE_NAME;
|
|
20
|
+
|
|
10
21
|
const SERVER_VERSION = PACKAGE_VERSION;
|
|
11
22
|
|
|
12
23
|
function formatChunkPreview(chunk, limit = 160) {
|
|
@@ -37,462 +48,6 @@ function failure(id, code, message) {
|
|
|
37
48
|
};
|
|
38
49
|
}
|
|
39
50
|
|
|
40
|
-
function asToolResult(value) {
|
|
41
|
-
return {
|
|
42
|
-
content: [
|
|
43
|
-
{
|
|
44
|
-
type: "text",
|
|
45
|
-
text: JSON.stringify(value, null, 2),
|
|
46
|
-
},
|
|
47
|
-
],
|
|
48
|
-
structuredContent: value,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function emptyObjectSchema() {
|
|
53
|
-
return {
|
|
54
|
-
type: "object",
|
|
55
|
-
properties: {},
|
|
56
|
-
additionalProperties: false,
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function validateValue(path, value, schema) {
|
|
61
|
-
if (schema.type === "object") {
|
|
62
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
63
|
-
throw new Error(`${path} must be an object`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const properties = schema.properties ?? {};
|
|
67
|
-
const required = schema.required ?? [];
|
|
68
|
-
for (const key of required) {
|
|
69
|
-
if (value[key] === undefined) {
|
|
70
|
-
throw new Error(`${path}.${key} is required`);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (schema.additionalProperties === false) {
|
|
75
|
-
for (const key of Object.keys(value)) {
|
|
76
|
-
if (!(key in properties)) {
|
|
77
|
-
throw new Error(`${path}.${key} is not allowed`);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
for (const [key, propertySchema] of Object.entries(properties)) {
|
|
83
|
-
if (value[key] !== undefined) {
|
|
84
|
-
validateValue(`${path}.${key}`, value[key], propertySchema);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (schema.type === "string") {
|
|
92
|
-
if (typeof value !== "string") {
|
|
93
|
-
throw new Error(`${path} must be a string`);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (schema.enum && !schema.enum.includes(value)) {
|
|
97
|
-
throw new Error(`${path} must be one of: ${schema.enum.join(", ")}`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (schema.type === "boolean") {
|
|
104
|
-
if (typeof value !== "boolean") {
|
|
105
|
-
throw new Error(`${path} must be a boolean`);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (schema.type === "integer") {
|
|
112
|
-
if (!Number.isInteger(value)) {
|
|
113
|
-
throw new Error(`${path} must be an integer`);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (schema.minimum !== undefined && value < schema.minimum) {
|
|
117
|
-
throw new Error(`${path} must be >= ${schema.minimum}`);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (schema.type === "number") {
|
|
124
|
-
if (typeof value !== "number" || Number.isNaN(value)) {
|
|
125
|
-
throw new Error(`${path} must be a number`);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (schema.minimum !== undefined && value < schema.minimum) {
|
|
129
|
-
throw new Error(`${path} must be >= ${schema.minimum}`);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function screenshotFormatsFor(browserFamily) {
|
|
135
|
-
return browserFamily === "firefox"
|
|
136
|
-
? ["png", "jpeg"]
|
|
137
|
-
: ["png", "jpeg", "webp"];
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function sessionWithLimitSchema(description) {
|
|
141
|
-
return {
|
|
142
|
-
type: "object",
|
|
143
|
-
properties: {
|
|
144
|
-
sessionId: {
|
|
145
|
-
type: "string",
|
|
146
|
-
description: "Session id returned by attach_tab.",
|
|
147
|
-
},
|
|
148
|
-
limit: {
|
|
149
|
-
type: "integer",
|
|
150
|
-
minimum: 1,
|
|
151
|
-
description:
|
|
152
|
-
"Return the most recent N entries from the session buffer (default 50).",
|
|
153
|
-
},
|
|
154
|
-
},
|
|
155
|
-
required: ["sessionId"],
|
|
156
|
-
additionalProperties: false,
|
|
157
|
-
description,
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const LOCATOR_DESCRIPTION =
|
|
162
|
-
'Element locator. Plain CSS (or css=...) resolves to the first document.querySelector match. text=Foo resolves to the first visible element whose whitespace-normalized text equals Foo, falling back to the first whose text contains Foo; elements are scanned in document order, so this can resolve to a wrapper around the element you want. role=button or role=button[name="Save"] matches by explicit or implicit ARIA role and, optionally, accessible name (equal or containing), without a visibility check. name=Foo resolves to the first visible element whose accessible name (aria-label, aria-labelledby, associated label, alt, and so on) equals or contains Foo. Text comparisons are case-sensitive. Only the top-level document is searched: elements inside iframes or shadow roots are not found.';
|
|
163
|
-
|
|
164
|
-
function selectorProperty() {
|
|
165
|
-
return {
|
|
166
|
-
type: "string",
|
|
167
|
-
description: LOCATOR_DESCRIPTION,
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function sessionSchema(properties, required, description) {
|
|
172
|
-
return {
|
|
173
|
-
type: "object",
|
|
174
|
-
properties: {
|
|
175
|
-
sessionId: {
|
|
176
|
-
type: "string",
|
|
177
|
-
description: "Session id returned by attach_tab.",
|
|
178
|
-
},
|
|
179
|
-
...properties,
|
|
180
|
-
},
|
|
181
|
-
required: ["sessionId", ...required],
|
|
182
|
-
additionalProperties: false,
|
|
183
|
-
description,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function waitUntilProperty() {
|
|
188
|
-
return {
|
|
189
|
-
type: "string",
|
|
190
|
-
enum: ["none", "interactive", "complete"],
|
|
191
|
-
description:
|
|
192
|
-
"When to return: none returns once navigation starts, interactive after DOMContentLoaded, complete after the load event (default complete).",
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function waitForInputSchema() {
|
|
197
|
-
return {
|
|
198
|
-
type: "object",
|
|
199
|
-
properties: {
|
|
200
|
-
sessionId: {
|
|
201
|
-
type: "string",
|
|
202
|
-
description: "Session id returned by attach_tab.",
|
|
203
|
-
},
|
|
204
|
-
selector: selectorProperty(),
|
|
205
|
-
state: {
|
|
206
|
-
type: "string",
|
|
207
|
-
enum: ["present", "visible", "hidden"],
|
|
208
|
-
description:
|
|
209
|
-
"Selector state to wait for (default visible). present: in the DOM. hidden: absent or not visible. Requires selector.",
|
|
210
|
-
},
|
|
211
|
-
url: {
|
|
212
|
-
type: "string",
|
|
213
|
-
description: "Wait until the page URL equals this string exactly.",
|
|
214
|
-
},
|
|
215
|
-
urlIncludes: {
|
|
216
|
-
type: "string",
|
|
217
|
-
description: "Wait until the page URL contains this string.",
|
|
218
|
-
},
|
|
219
|
-
readyState: {
|
|
220
|
-
type: "string",
|
|
221
|
-
enum: ["interactive", "complete"],
|
|
222
|
-
description:
|
|
223
|
-
"Wait until document.readyState reaches this state or later.",
|
|
224
|
-
},
|
|
225
|
-
timeoutMs: {
|
|
226
|
-
type: "integer",
|
|
227
|
-
minimum: 1,
|
|
228
|
-
description: "How long to wait before failing (default 10000).",
|
|
229
|
-
},
|
|
230
|
-
pollIntervalMs: {
|
|
231
|
-
type: "integer",
|
|
232
|
-
minimum: 1,
|
|
233
|
-
description: "Delay between checks (default 100).",
|
|
234
|
-
},
|
|
235
|
-
},
|
|
236
|
-
required: ["sessionId"],
|
|
237
|
-
additionalProperties: false,
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
function compareSessionsSchema(properties, required) {
|
|
242
|
-
return {
|
|
243
|
-
type: "object",
|
|
244
|
-
properties: {
|
|
245
|
-
sessionIdA: {
|
|
246
|
-
type: "string",
|
|
247
|
-
description: "Session id returned by attach_tab.",
|
|
248
|
-
},
|
|
249
|
-
sessionIdB: {
|
|
250
|
-
type: "string",
|
|
251
|
-
description: "Session id returned by attach_tab.",
|
|
252
|
-
},
|
|
253
|
-
...properties,
|
|
254
|
-
},
|
|
255
|
-
required: ["sessionIdA", "sessionIdB", ...required],
|
|
256
|
-
additionalProperties: false,
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function storageInputSchema() {
|
|
261
|
-
return {
|
|
262
|
-
type: "object",
|
|
263
|
-
properties: {
|
|
264
|
-
sessionId: {
|
|
265
|
-
type: "string",
|
|
266
|
-
description: "Session id returned by attach_tab.",
|
|
267
|
-
},
|
|
268
|
-
area: {
|
|
269
|
-
type: "string",
|
|
270
|
-
enum: ["all", "localStorage", "sessionStorage"],
|
|
271
|
-
description: "Which storage to return (default all).",
|
|
272
|
-
},
|
|
273
|
-
},
|
|
274
|
-
required: ["sessionId"],
|
|
275
|
-
additionalProperties: false,
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
function captureDebugReportInputSchema(browserFamily) {
|
|
280
|
-
return {
|
|
281
|
-
type: "object",
|
|
282
|
-
properties: {
|
|
283
|
-
sessionId: {
|
|
284
|
-
type: "string",
|
|
285
|
-
description: "Session id returned by attach_tab.",
|
|
286
|
-
},
|
|
287
|
-
consoleLimit: {
|
|
288
|
-
type: "integer",
|
|
289
|
-
minimum: 1,
|
|
290
|
-
description: "Most recent console entries to include (default 20).",
|
|
291
|
-
},
|
|
292
|
-
networkLimit: {
|
|
293
|
-
type: "integer",
|
|
294
|
-
minimum: 1,
|
|
295
|
-
description: "Most recent network requests to include (default 20).",
|
|
296
|
-
},
|
|
297
|
-
includeScreenshot: {
|
|
298
|
-
type: "boolean",
|
|
299
|
-
description: "Include a viewport screenshot (default true).",
|
|
300
|
-
},
|
|
301
|
-
screenshotFormat: {
|
|
302
|
-
type: "string",
|
|
303
|
-
enum: screenshotFormatsFor(browserFamily),
|
|
304
|
-
description: "Screenshot image format (default png).",
|
|
305
|
-
},
|
|
306
|
-
},
|
|
307
|
-
required: ["sessionId"],
|
|
308
|
-
additionalProperties: false,
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function restoreSessionSnapshotInputSchema() {
|
|
313
|
-
return {
|
|
314
|
-
type: "object",
|
|
315
|
-
properties: {
|
|
316
|
-
sessionId: {
|
|
317
|
-
type: "string",
|
|
318
|
-
description: "Session id returned by attach_tab.",
|
|
319
|
-
},
|
|
320
|
-
snapshot: {
|
|
321
|
-
type: "string",
|
|
322
|
-
description:
|
|
323
|
-
"The JSON output of capture_session_snapshot, as a string. Only entries for the current page origin are restored.",
|
|
324
|
-
},
|
|
325
|
-
clearStorage: {
|
|
326
|
-
type: "boolean",
|
|
327
|
-
description:
|
|
328
|
-
"Clear localStorage and sessionStorage before restoring (default false).",
|
|
329
|
-
},
|
|
330
|
-
},
|
|
331
|
-
required: ["sessionId", "snapshot"],
|
|
332
|
-
additionalProperties: false,
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
function newTabInputSchema(browserFamily) {
|
|
337
|
-
const properties = {
|
|
338
|
-
url: {
|
|
339
|
-
type: "string",
|
|
340
|
-
description: "URL to open (default about:blank).",
|
|
341
|
-
},
|
|
342
|
-
};
|
|
343
|
-
const required = [];
|
|
344
|
-
|
|
345
|
-
if (browserFamily === "auto") {
|
|
346
|
-
properties.browserFamily = {
|
|
347
|
-
type: "string",
|
|
348
|
-
enum: ["chromium", "firefox"],
|
|
349
|
-
description: "Browser to open the tab in.",
|
|
350
|
-
};
|
|
351
|
-
required.push("browserFamily");
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
return {
|
|
355
|
-
type: "object",
|
|
356
|
-
properties,
|
|
357
|
-
required,
|
|
358
|
-
additionalProperties: false,
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
function unsafeArgsProperty() {
|
|
363
|
-
return {
|
|
364
|
-
type: "array",
|
|
365
|
-
items: {
|
|
366
|
-
type: "string",
|
|
367
|
-
},
|
|
368
|
-
description:
|
|
369
|
-
"Extra browser command-line flags, each starting with -. Flags that conflict with broker-managed launch options (debugging port, address, profile) are rejected.",
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
function launchBrowserInputSchema(browserFamily, enableUnsafeLaunchArgs) {
|
|
374
|
-
const properties = {
|
|
375
|
-
url: {
|
|
376
|
-
type: "string",
|
|
377
|
-
description: "URL to open in the launched browser (default about:blank).",
|
|
378
|
-
},
|
|
379
|
-
browserFamily: {
|
|
380
|
-
type: "string",
|
|
381
|
-
enum: supportedLaunchFamilies(browserFamily),
|
|
382
|
-
description:
|
|
383
|
-
"Browser to launch. Required when the broker runs in auto mode.",
|
|
384
|
-
},
|
|
385
|
-
port: {
|
|
386
|
-
type: "integer",
|
|
387
|
-
minimum: 1,
|
|
388
|
-
description:
|
|
389
|
-
"Remote debugging port to launch on (default: the port of the configured browser endpoint, usually 9222).",
|
|
390
|
-
},
|
|
391
|
-
address: {
|
|
392
|
-
type: "string",
|
|
393
|
-
description:
|
|
394
|
-
"Remote debugging bind address for Chromium and Edge (ignored for Firefox). Must be loopback unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1.",
|
|
395
|
-
},
|
|
396
|
-
userDataDir: {
|
|
397
|
-
type: "string",
|
|
398
|
-
description:
|
|
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
|
-
},
|
|
401
|
-
waitMs: {
|
|
402
|
-
type: "integer",
|
|
403
|
-
minimum: 0,
|
|
404
|
-
description:
|
|
405
|
-
"How long to wait for the debugging endpoint to answer after launch (default 5000).",
|
|
406
|
-
},
|
|
407
|
-
skipDoctor: {
|
|
408
|
-
type: "boolean",
|
|
409
|
-
description:
|
|
410
|
-
"Return right after spawning the browser, without waiting for the endpoint or running the doctor report (default false).",
|
|
411
|
-
},
|
|
412
|
-
};
|
|
413
|
-
if (enableUnsafeLaunchArgs) {
|
|
414
|
-
properties.unsafeArgs = unsafeArgsProperty();
|
|
415
|
-
}
|
|
416
|
-
const required = browserFamily === "auto" ? ["browserFamily"] : [];
|
|
417
|
-
|
|
418
|
-
return {
|
|
419
|
-
type: "object",
|
|
420
|
-
properties,
|
|
421
|
-
required,
|
|
422
|
-
additionalProperties: false,
|
|
423
|
-
};
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
function ensureBrowserInputSchema(browserFamily, enableUnsafeLaunchArgs) {
|
|
427
|
-
const properties = {
|
|
428
|
-
browserFamily: {
|
|
429
|
-
type: "string",
|
|
430
|
-
enum: supportedLaunchFamilies(browserFamily),
|
|
431
|
-
description:
|
|
432
|
-
"Browser to check or launch. Required when the broker runs in auto mode.",
|
|
433
|
-
},
|
|
434
|
-
url: {
|
|
435
|
-
type: "string",
|
|
436
|
-
description: "URL to open in a new tab once the browser is reachable.",
|
|
437
|
-
},
|
|
438
|
-
createTab: {
|
|
439
|
-
type: "boolean",
|
|
440
|
-
description:
|
|
441
|
-
"Open a tab after the browser is reachable (default: true when url is given).",
|
|
442
|
-
},
|
|
443
|
-
launchIfMissing: {
|
|
444
|
-
type: "boolean",
|
|
445
|
-
description:
|
|
446
|
-
"Launch a browser when none is reachable (default true). When false and none is reachable, return the status without launching or opening a tab.",
|
|
447
|
-
},
|
|
448
|
-
port: {
|
|
449
|
-
type: "integer",
|
|
450
|
-
minimum: 1,
|
|
451
|
-
description:
|
|
452
|
-
"Remote debugging port to launch on (default: the port of the configured browser endpoint, usually 9222).",
|
|
453
|
-
},
|
|
454
|
-
address: {
|
|
455
|
-
type: "string",
|
|
456
|
-
description:
|
|
457
|
-
"Remote debugging bind address for Chromium and Edge (ignored for Firefox). Must be loopback unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1.",
|
|
458
|
-
},
|
|
459
|
-
userDataDir: {
|
|
460
|
-
type: "string",
|
|
461
|
-
description:
|
|
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
|
-
},
|
|
464
|
-
waitMs: {
|
|
465
|
-
type: "integer",
|
|
466
|
-
minimum: 0,
|
|
467
|
-
description:
|
|
468
|
-
"How long to wait for the debugging endpoint to answer after launch (default 5000).",
|
|
469
|
-
},
|
|
470
|
-
skipDoctor: {
|
|
471
|
-
type: "boolean",
|
|
472
|
-
description:
|
|
473
|
-
"Return right after spawning the browser, without waiting for the endpoint or running the doctor report (default false).",
|
|
474
|
-
},
|
|
475
|
-
};
|
|
476
|
-
if (enableUnsafeLaunchArgs) {
|
|
477
|
-
properties.unsafeArgs = unsafeArgsProperty();
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
return {
|
|
481
|
-
type: "object",
|
|
482
|
-
properties,
|
|
483
|
-
required: browserFamily === "auto" ? ["browserFamily"] : [],
|
|
484
|
-
additionalProperties: false,
|
|
485
|
-
};
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
function requestedBrowserFamily(configuredFamily, requestedFamily) {
|
|
489
|
-
if (requestedFamily) {
|
|
490
|
-
return requestedFamily;
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
return configuredFamily === "auto" ? null : configuredFamily;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
51
|
function isRequestedBrowserAvailable(
|
|
497
52
|
status,
|
|
498
53
|
configuredFamily,
|
|
@@ -509,6 +64,7 @@ function isRequestedBrowserAvailable(
|
|
|
509
64
|
}
|
|
510
65
|
|
|
511
66
|
const STATUS_PROBE_ATTEMPTS = 3;
|
|
67
|
+
|
|
512
68
|
const LAUNCH_GRACE_MS = 30_000;
|
|
513
69
|
|
|
514
70
|
function sleep(ms) {
|
|
@@ -517,59 +73,6 @@ function sleep(ms) {
|
|
|
517
73
|
});
|
|
518
74
|
}
|
|
519
75
|
|
|
520
|
-
function shouldCreateBrowserTab(args) {
|
|
521
|
-
if (args.createTab !== undefined) {
|
|
522
|
-
return args.createTab;
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
return typeof args.url === "string" && args.url.trim() !== "";
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
function sameValue(left, right) {
|
|
529
|
-
return JSON.stringify(left) === JSON.stringify(right);
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
function compareField(left, right) {
|
|
533
|
-
return {
|
|
534
|
-
equal: sameValue(left, right),
|
|
535
|
-
a: left,
|
|
536
|
-
b: right,
|
|
537
|
-
};
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
function summarizeComparablePageState(page = {}) {
|
|
541
|
-
return {
|
|
542
|
-
browserFamily: page.browserFamily ?? null,
|
|
543
|
-
url: page.url ?? null,
|
|
544
|
-
title: page.title ?? null,
|
|
545
|
-
readyState: page.readyState ?? null,
|
|
546
|
-
visibilityState: page.visibilityState ?? null,
|
|
547
|
-
viewport: page.viewport ?? null,
|
|
548
|
-
scroll: page.scroll ?? null,
|
|
549
|
-
};
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
function summarizeComparableElement(result = {}) {
|
|
553
|
-
if (!result.found || !result.node) {
|
|
554
|
-
return {
|
|
555
|
-
found: false,
|
|
556
|
-
selector: result.selector ?? null,
|
|
557
|
-
error: result.error ?? null,
|
|
558
|
-
};
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
return {
|
|
562
|
-
found: true,
|
|
563
|
-
selector: result.selector ?? null,
|
|
564
|
-
tagName: result.node.tagName ?? null,
|
|
565
|
-
accessibleName: result.node.accessibleName ?? null,
|
|
566
|
-
role: result.node.role ?? null,
|
|
567
|
-
visible: result.node.visible ?? null,
|
|
568
|
-
disabled: result.node.disabled ?? null,
|
|
569
|
-
textContent: result.node.textContent ?? null,
|
|
570
|
-
};
|
|
571
|
-
}
|
|
572
|
-
|
|
573
76
|
export class McpBrowserDevToolsServer {
|
|
574
77
|
constructor({
|
|
575
78
|
config,
|
|
@@ -717,951 +220,31 @@ export class McpBrowserDevToolsServer {
|
|
|
717
220
|
|
|
718
221
|
createTools() {
|
|
719
222
|
const tools = [
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
name: "browser_status",
|
|
725
|
-
description:
|
|
726
|
-
"Report whether the configured browser endpoint is reachable and how many active sessions are attached.",
|
|
727
|
-
inputSchema: emptyObjectSchema(),
|
|
728
|
-
},
|
|
729
|
-
handler: async () => ({
|
|
730
|
-
serverName: SERVER_NAME,
|
|
731
|
-
serverVersion: SERVER_VERSION,
|
|
732
|
-
...(await this.browserAdapter.getBrowserStatus()),
|
|
733
|
-
}),
|
|
734
|
-
},
|
|
735
|
-
],
|
|
736
|
-
[
|
|
737
|
-
"list_tabs",
|
|
738
|
-
{
|
|
739
|
-
definition: {
|
|
740
|
-
name: "list_tabs",
|
|
741
|
-
description:
|
|
742
|
-
"List inspectable page targets exposed by the configured browser adapter.",
|
|
743
|
-
inputSchema: emptyObjectSchema(),
|
|
744
|
-
},
|
|
745
|
-
handler: async () => ({
|
|
746
|
-
tabs: await this.browserAdapter.listTargets(),
|
|
747
|
-
}),
|
|
748
|
-
},
|
|
749
|
-
],
|
|
750
|
-
[
|
|
751
|
-
"launch_browser",
|
|
752
|
-
{
|
|
753
|
-
definition: {
|
|
754
|
-
name: "launch_browser",
|
|
755
|
-
description:
|
|
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.",
|
|
757
|
-
inputSchema: launchBrowserInputSchema(
|
|
758
|
-
this.config.browserFamily,
|
|
759
|
-
this.config.enableUnsafeLaunchArgs,
|
|
760
|
-
),
|
|
761
|
-
},
|
|
762
|
-
handler: async (args) =>
|
|
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
|
-
});
|
|
806
|
-
}),
|
|
807
|
-
},
|
|
808
|
-
],
|
|
809
|
-
[
|
|
810
|
-
"ensure_browser",
|
|
811
|
-
{
|
|
812
|
-
definition: {
|
|
813
|
-
name: "ensure_browser",
|
|
814
|
-
description:
|
|
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.",
|
|
816
|
-
inputSchema: ensureBrowserInputSchema(
|
|
817
|
-
this.config.browserFamily,
|
|
818
|
-
this.config.enableUnsafeLaunchArgs,
|
|
819
|
-
),
|
|
820
|
-
},
|
|
821
|
-
handler: async (args) =>
|
|
822
|
-
this.runExclusiveLaunch(() => this.ensureBrowser(args)),
|
|
823
|
-
},
|
|
824
|
-
],
|
|
825
|
-
[
|
|
826
|
-
"list_sessions",
|
|
827
|
-
{
|
|
828
|
-
definition: {
|
|
829
|
-
name: "list_sessions",
|
|
830
|
-
description:
|
|
831
|
-
"List active attached debugging sessions held by this broker.",
|
|
832
|
-
inputSchema: emptyObjectSchema(),
|
|
833
|
-
},
|
|
834
|
-
handler: async () => ({
|
|
835
|
-
sessions: this.browserAdapter.listSessions(),
|
|
836
|
-
}),
|
|
837
|
-
},
|
|
838
|
-
],
|
|
839
|
-
[
|
|
840
|
-
"new_tab",
|
|
841
|
-
{
|
|
842
|
-
definition: {
|
|
843
|
-
name: "new_tab",
|
|
844
|
-
description:
|
|
845
|
-
"Create a new browser tab and return the resulting target metadata.",
|
|
846
|
-
inputSchema: newTabInputSchema(this.config.browserFamily),
|
|
847
|
-
},
|
|
848
|
-
handler: async (args) =>
|
|
849
|
-
this.browserAdapter.createTab(args.url, {
|
|
850
|
-
browserFamily: args.browserFamily,
|
|
851
|
-
}),
|
|
852
|
-
},
|
|
853
|
-
],
|
|
854
|
-
[
|
|
855
|
-
"close_tab",
|
|
856
|
-
{
|
|
857
|
-
definition: {
|
|
858
|
-
name: "close_tab",
|
|
859
|
-
description:
|
|
860
|
-
"Close a browser tab by target id. Any attached session for that tab will disconnect.",
|
|
861
|
-
inputSchema: {
|
|
862
|
-
type: "object",
|
|
863
|
-
properties: {
|
|
864
|
-
targetId: {
|
|
865
|
-
type: "string",
|
|
866
|
-
description: "Target id returned by list_tabs or new_tab.",
|
|
867
|
-
},
|
|
868
|
-
},
|
|
869
|
-
required: ["targetId"],
|
|
870
|
-
additionalProperties: false,
|
|
871
|
-
},
|
|
872
|
-
},
|
|
873
|
-
handler: async (args) =>
|
|
874
|
-
this.browserAdapter.closeTarget(args.targetId),
|
|
875
|
-
},
|
|
876
|
-
],
|
|
877
|
-
[
|
|
878
|
-
"attach_tab",
|
|
879
|
-
{
|
|
880
|
-
definition: {
|
|
881
|
-
name: "attach_tab",
|
|
882
|
-
description:
|
|
883
|
-
"Attach to a page target and start buffering console, log, and network events.",
|
|
884
|
-
inputSchema: {
|
|
885
|
-
type: "object",
|
|
886
|
-
properties: {
|
|
887
|
-
targetId: {
|
|
888
|
-
type: "string",
|
|
889
|
-
description: "The target id returned by list_tabs.",
|
|
890
|
-
},
|
|
891
|
-
},
|
|
892
|
-
required: ["targetId"],
|
|
893
|
-
additionalProperties: false,
|
|
894
|
-
},
|
|
895
|
-
},
|
|
896
|
-
handler: async (args) =>
|
|
897
|
-
this.browserAdapter.attachToTarget(args.targetId),
|
|
898
|
-
},
|
|
899
|
-
],
|
|
900
|
-
[
|
|
901
|
-
"detach_tab",
|
|
902
|
-
{
|
|
903
|
-
definition: {
|
|
904
|
-
name: "detach_tab",
|
|
905
|
-
description: "Close an attached debugging session.",
|
|
906
|
-
inputSchema: {
|
|
907
|
-
type: "object",
|
|
908
|
-
properties: {
|
|
909
|
-
sessionId: {
|
|
910
|
-
type: "string",
|
|
911
|
-
description: "The session id returned by attach_tab.",
|
|
912
|
-
},
|
|
913
|
-
},
|
|
914
|
-
required: ["sessionId"],
|
|
915
|
-
additionalProperties: false,
|
|
916
|
-
},
|
|
917
|
-
},
|
|
918
|
-
handler: async (args) =>
|
|
919
|
-
this.browserAdapter.detachSession(args.sessionId),
|
|
920
|
-
},
|
|
921
|
-
],
|
|
922
|
-
[
|
|
923
|
-
"get_page_state",
|
|
924
|
-
{
|
|
925
|
-
definition: {
|
|
926
|
-
name: "get_page_state",
|
|
927
|
-
description:
|
|
928
|
-
"Return the current URL, title, ready state, viewport, and scroll positions for an attached page.",
|
|
929
|
-
inputSchema: sessionSchema({}, []),
|
|
930
|
-
},
|
|
931
|
-
handler: async (args) =>
|
|
932
|
-
this.browserAdapter.getPageState(args.sessionId),
|
|
933
|
-
},
|
|
934
|
-
],
|
|
935
|
-
[
|
|
936
|
-
"compare_page_state",
|
|
937
|
-
{
|
|
938
|
-
definition: {
|
|
939
|
-
name: "compare_page_state",
|
|
940
|
-
description:
|
|
941
|
-
"Compare bounded page state across two attached sessions, useful for cross-browser checks in auto mode.",
|
|
942
|
-
inputSchema: compareSessionsSchema({}, []),
|
|
943
|
-
},
|
|
944
|
-
handler: async (args) => {
|
|
945
|
-
const [pageA, pageB] = await Promise.all([
|
|
946
|
-
this.browserAdapter.getPageState(args.sessionIdA),
|
|
947
|
-
this.browserAdapter.getPageState(args.sessionIdB),
|
|
948
|
-
]);
|
|
949
|
-
const left = summarizeComparablePageState(pageA);
|
|
950
|
-
const right = summarizeComparablePageState(pageB);
|
|
951
|
-
const fields = {
|
|
952
|
-
browserFamily: compareField(
|
|
953
|
-
left.browserFamily,
|
|
954
|
-
right.browserFamily,
|
|
955
|
-
),
|
|
956
|
-
url: compareField(left.url, right.url),
|
|
957
|
-
title: compareField(left.title, right.title),
|
|
958
|
-
readyState: compareField(left.readyState, right.readyState),
|
|
959
|
-
visibilityState: compareField(
|
|
960
|
-
left.visibilityState,
|
|
961
|
-
right.visibilityState,
|
|
962
|
-
),
|
|
963
|
-
viewport: compareField(left.viewport, right.viewport),
|
|
964
|
-
scroll: compareField(left.scroll, right.scroll),
|
|
965
|
-
};
|
|
966
|
-
|
|
967
|
-
return {
|
|
968
|
-
matches: Object.values(fields).every((field) => field.equal),
|
|
969
|
-
a: left,
|
|
970
|
-
b: right,
|
|
971
|
-
fields,
|
|
972
|
-
};
|
|
973
|
-
},
|
|
974
|
-
},
|
|
975
|
-
],
|
|
976
|
-
[
|
|
977
|
-
"compare_selector",
|
|
978
|
-
{
|
|
979
|
-
definition: {
|
|
980
|
-
name: "compare_selector",
|
|
981
|
-
description:
|
|
982
|
-
"Compare a selector across two attached sessions using a bounded element summary.",
|
|
983
|
-
inputSchema: compareSessionsSchema(
|
|
984
|
-
{
|
|
985
|
-
selector: selectorProperty(),
|
|
986
|
-
},
|
|
987
|
-
["selector"],
|
|
988
|
-
),
|
|
989
|
-
},
|
|
990
|
-
handler: async (args) => {
|
|
991
|
-
const [elementA, elementB] = await Promise.all([
|
|
992
|
-
this.browserAdapter.inspectElement(
|
|
993
|
-
args.sessionIdA,
|
|
994
|
-
args.selector,
|
|
995
|
-
),
|
|
996
|
-
this.browserAdapter.inspectElement(
|
|
997
|
-
args.sessionIdB,
|
|
998
|
-
args.selector,
|
|
999
|
-
),
|
|
1000
|
-
]);
|
|
1001
|
-
const left = summarizeComparableElement(elementA);
|
|
1002
|
-
const right = summarizeComparableElement(elementB);
|
|
1003
|
-
const fields = {
|
|
1004
|
-
found: compareField(left.found, right.found),
|
|
1005
|
-
tagName: compareField(
|
|
1006
|
-
left.tagName ?? null,
|
|
1007
|
-
right.tagName ?? null,
|
|
1008
|
-
),
|
|
1009
|
-
accessibleName: compareField(
|
|
1010
|
-
left.accessibleName ?? null,
|
|
1011
|
-
right.accessibleName ?? null,
|
|
1012
|
-
),
|
|
1013
|
-
role: compareField(left.role ?? null, right.role ?? null),
|
|
1014
|
-
visible: compareField(
|
|
1015
|
-
left.visible ?? null,
|
|
1016
|
-
right.visible ?? null,
|
|
1017
|
-
),
|
|
1018
|
-
disabled: compareField(
|
|
1019
|
-
left.disabled ?? null,
|
|
1020
|
-
right.disabled ?? null,
|
|
1021
|
-
),
|
|
1022
|
-
textContent: compareField(
|
|
1023
|
-
left.textContent ?? null,
|
|
1024
|
-
right.textContent ?? null,
|
|
1025
|
-
),
|
|
1026
|
-
};
|
|
1027
|
-
|
|
1028
|
-
return {
|
|
1029
|
-
selector: args.selector,
|
|
1030
|
-
matches: Object.values(fields).every((field) => field.equal),
|
|
1031
|
-
a: left,
|
|
1032
|
-
b: right,
|
|
1033
|
-
fields,
|
|
1034
|
-
};
|
|
1035
|
-
},
|
|
1036
|
-
},
|
|
1037
|
-
],
|
|
1038
|
-
[
|
|
1039
|
-
"wait_for",
|
|
1040
|
-
{
|
|
1041
|
-
definition: {
|
|
1042
|
-
name: "wait_for",
|
|
1043
|
-
description:
|
|
1044
|
-
"Wait until every given condition holds on an attached session: selector state, exact url, urlIncludes substring, and readyState. Give at least one condition. Polls until the conditions match or timeoutMs passes, then fails with the last observed state. A selector check scrolls the matched element into view on each poll.",
|
|
1045
|
-
inputSchema: waitForInputSchema(),
|
|
1046
|
-
},
|
|
1047
|
-
handler: async (args) => {
|
|
1048
|
-
if (
|
|
1049
|
-
!args.selector &&
|
|
1050
|
-
!args.url &&
|
|
1051
|
-
!args.urlIncludes &&
|
|
1052
|
-
!args.readyState
|
|
1053
|
-
) {
|
|
1054
|
-
throw new Error(
|
|
1055
|
-
"wait_for requires at least one of selector, url, urlIncludes, or readyState",
|
|
1056
|
-
);
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
if (!args.selector && args.state !== undefined) {
|
|
1060
|
-
throw new Error("wait_for state requires selector");
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
|
-
return this.browserAdapter.waitFor(args.sessionId, {
|
|
1064
|
-
selector: args.selector,
|
|
1065
|
-
state: args.state,
|
|
1066
|
-
url: args.url,
|
|
1067
|
-
urlIncludes: args.urlIncludes,
|
|
1068
|
-
readyState: args.readyState,
|
|
1069
|
-
timeoutMs: args.timeoutMs,
|
|
1070
|
-
pollIntervalMs: args.pollIntervalMs,
|
|
1071
|
-
});
|
|
1072
|
-
},
|
|
1073
|
-
},
|
|
1074
|
-
],
|
|
1075
|
-
[
|
|
1076
|
-
"get_cookies",
|
|
1077
|
-
{
|
|
1078
|
-
definition: {
|
|
1079
|
-
name: "get_cookies",
|
|
1080
|
-
description:
|
|
1081
|
-
"Read bounded page-visible cookies for the attached session.",
|
|
1082
|
-
inputSchema: sessionSchema({}, []),
|
|
1083
|
-
},
|
|
1084
|
-
handler: async (args) =>
|
|
1085
|
-
this.browserAdapter.getCookies(args.sessionId),
|
|
1086
|
-
},
|
|
1087
|
-
],
|
|
1088
|
-
[
|
|
1089
|
-
"get_storage",
|
|
1090
|
-
{
|
|
1091
|
-
definition: {
|
|
1092
|
-
name: "get_storage",
|
|
1093
|
-
description:
|
|
1094
|
-
"Read bounded localStorage and sessionStorage entries for the attached session.",
|
|
1095
|
-
inputSchema: storageInputSchema(),
|
|
1096
|
-
},
|
|
1097
|
-
handler: async (args) => {
|
|
1098
|
-
const result = await this.browserAdapter.getStorage(args.sessionId);
|
|
1099
|
-
if (!args.area || args.area === "all") {
|
|
1100
|
-
return result;
|
|
1101
|
-
}
|
|
1102
|
-
|
|
1103
|
-
return {
|
|
1104
|
-
browserFamily: result.browserFamily,
|
|
1105
|
-
storage: {
|
|
1106
|
-
[args.area]: result.storage?.[args.area] ?? null,
|
|
1107
|
-
},
|
|
1108
|
-
};
|
|
1109
|
-
},
|
|
1110
|
-
},
|
|
1111
|
-
],
|
|
1112
|
-
[
|
|
1113
|
-
"capture_debug_report",
|
|
1114
|
-
{
|
|
1115
|
-
definition: {
|
|
1116
|
-
name: "capture_debug_report",
|
|
1117
|
-
description:
|
|
1118
|
-
"Capture a bounded debug bundle with page state, cookies/storage summary, recent console, recent network, and an optional screenshot.",
|
|
1119
|
-
inputSchema: captureDebugReportInputSchema(
|
|
1120
|
-
this.config.browserFamily,
|
|
1121
|
-
),
|
|
1122
|
-
},
|
|
1123
|
-
handler: async (args) =>
|
|
1124
|
-
this.browserAdapter.captureDebugReport(args.sessionId, {
|
|
1125
|
-
consoleLimit: args.consoleLimit,
|
|
1126
|
-
networkLimit: args.networkLimit,
|
|
1127
|
-
includeScreenshot: args.includeScreenshot,
|
|
1128
|
-
screenshotFormat: args.screenshotFormat,
|
|
1129
|
-
}),
|
|
1130
|
-
},
|
|
1131
|
-
],
|
|
1132
|
-
[
|
|
1133
|
-
"capture_session_snapshot",
|
|
1134
|
-
{
|
|
1135
|
-
definition: {
|
|
1136
|
-
name: "capture_session_snapshot",
|
|
1137
|
-
description:
|
|
1138
|
-
"Capture a bounded session snapshot with page state, cookies, and web storage for later bounded restore on the same origin.",
|
|
1139
|
-
inputSchema: sessionSchema({}, []),
|
|
1140
|
-
},
|
|
1141
|
-
handler: async (args) =>
|
|
1142
|
-
this.browserAdapter.captureSessionSnapshot(args.sessionId),
|
|
1143
|
-
},
|
|
1144
|
-
],
|
|
1145
|
-
[
|
|
1146
|
-
"restore_session_snapshot",
|
|
1147
|
-
{
|
|
1148
|
-
definition: {
|
|
1149
|
-
name: "restore_session_snapshot",
|
|
1150
|
-
description:
|
|
1151
|
-
"Restore a bounded session snapshot into the currently attached page context. Restores only page-visible cookies plus localStorage/sessionStorage on the current origin.",
|
|
1152
|
-
inputSchema: restoreSessionSnapshotInputSchema(),
|
|
1153
|
-
},
|
|
1154
|
-
handler: async (args) => {
|
|
1155
|
-
let snapshot;
|
|
1156
|
-
try {
|
|
1157
|
-
snapshot = JSON.parse(args.snapshot);
|
|
1158
|
-
} catch {
|
|
1159
|
-
throw new Error(
|
|
1160
|
-
"restore_session_snapshot snapshot must be valid JSON",
|
|
1161
|
-
);
|
|
1162
|
-
}
|
|
1163
|
-
|
|
1164
|
-
if (!snapshot || typeof snapshot !== "object") {
|
|
1165
|
-
throw new Error(
|
|
1166
|
-
"restore_session_snapshot snapshot must decode to an object",
|
|
1167
|
-
);
|
|
1168
|
-
}
|
|
1169
|
-
|
|
1170
|
-
return this.browserAdapter.restoreSessionSnapshot(
|
|
1171
|
-
args.sessionId,
|
|
1172
|
-
snapshot,
|
|
1173
|
-
{
|
|
1174
|
-
clearStorage: args.clearStorage,
|
|
1175
|
-
},
|
|
1176
|
-
);
|
|
1177
|
-
},
|
|
1178
|
-
},
|
|
1179
|
-
],
|
|
1180
|
-
[
|
|
1181
|
-
"get_har",
|
|
1182
|
-
{
|
|
1183
|
-
definition: {
|
|
1184
|
-
name: "get_har",
|
|
1185
|
-
description:
|
|
1186
|
-
"Export a bounded HAR-like summary from buffered network activity for an attached session.",
|
|
1187
|
-
inputSchema: sessionWithLimitSchema(),
|
|
1188
|
-
},
|
|
1189
|
-
handler: async (args) =>
|
|
1190
|
-
this.browserAdapter.getHar(args.sessionId, {
|
|
1191
|
-
limit: args.limit ?? 50,
|
|
1192
|
-
}),
|
|
1193
|
-
},
|
|
1194
|
-
],
|
|
1195
|
-
[
|
|
1196
|
-
"navigate",
|
|
1197
|
-
{
|
|
1198
|
-
definition: {
|
|
1199
|
-
name: "navigate",
|
|
1200
|
-
description:
|
|
1201
|
-
"Navigate an attached tab to a URL and optionally wait for interactive or complete load state.",
|
|
1202
|
-
inputSchema: sessionSchema(
|
|
1203
|
-
{
|
|
1204
|
-
url: {
|
|
1205
|
-
type: "string",
|
|
1206
|
-
description: "Absolute URL to load.",
|
|
1207
|
-
},
|
|
1208
|
-
waitUntil: waitUntilProperty(),
|
|
1209
|
-
},
|
|
1210
|
-
["url"],
|
|
1211
|
-
),
|
|
1212
|
-
},
|
|
1213
|
-
handler: async (args) =>
|
|
1214
|
-
this.browserAdapter.navigate(args.sessionId, args.url, {
|
|
1215
|
-
waitUntil: args.waitUntil,
|
|
1216
|
-
}),
|
|
1217
|
-
},
|
|
1218
|
-
],
|
|
1219
|
-
[
|
|
1220
|
-
"reload",
|
|
1221
|
-
{
|
|
1222
|
-
definition: {
|
|
1223
|
-
name: "reload",
|
|
1224
|
-
description:
|
|
1225
|
-
"Reload the attached tab and optionally ignore cache while waiting for interactive or complete load state.",
|
|
1226
|
-
inputSchema: sessionSchema(
|
|
1227
|
-
{
|
|
1228
|
-
ignoreCache: {
|
|
1229
|
-
type: "boolean",
|
|
1230
|
-
description:
|
|
1231
|
-
"Bypass the HTTP cache for this reload (default false).",
|
|
1232
|
-
},
|
|
1233
|
-
waitUntil: waitUntilProperty(),
|
|
1234
|
-
},
|
|
1235
|
-
[],
|
|
1236
|
-
),
|
|
1237
|
-
},
|
|
1238
|
-
handler: async (args) =>
|
|
1239
|
-
this.browserAdapter.reload(args.sessionId, {
|
|
1240
|
-
ignoreCache: args.ignoreCache,
|
|
1241
|
-
waitUntil: args.waitUntil,
|
|
1242
|
-
}),
|
|
1243
|
-
},
|
|
1244
|
-
],
|
|
1245
|
-
[
|
|
1246
|
-
"click",
|
|
1247
|
-
{
|
|
1248
|
-
definition: {
|
|
1249
|
-
name: "click",
|
|
1250
|
-
description:
|
|
1251
|
-
"Click a single element located by CSS, text=..., role=..., or name=... syntax. Sends real mouse input at the element center and fails if another element covers that point. A JavaScript alert or confirm dialog that this opens is accepted automatically and a prompt is dismissed; get_events reports it as a dialog event.",
|
|
1252
|
-
inputSchema: sessionSchema(
|
|
1253
|
-
{
|
|
1254
|
-
selector: selectorProperty(),
|
|
1255
|
-
},
|
|
1256
|
-
["selector"],
|
|
1257
|
-
),
|
|
1258
|
-
},
|
|
1259
|
-
handler: async (args) =>
|
|
1260
|
-
this.browserAdapter.click(args.sessionId, args.selector),
|
|
1261
|
-
},
|
|
1262
|
-
],
|
|
1263
|
-
[
|
|
1264
|
-
"hover",
|
|
1265
|
-
{
|
|
1266
|
-
definition: {
|
|
1267
|
-
name: "hover",
|
|
1268
|
-
description:
|
|
1269
|
-
"Hover a single element located by CSS, text=..., role=..., or name=... syntax. Moves the real mouse pointer to the element center.",
|
|
1270
|
-
inputSchema: sessionSchema(
|
|
1271
|
-
{
|
|
1272
|
-
selector: selectorProperty(),
|
|
1273
|
-
},
|
|
1274
|
-
["selector"],
|
|
1275
|
-
),
|
|
1276
|
-
},
|
|
1277
|
-
handler: async (args) =>
|
|
1278
|
-
this.browserAdapter.hover(args.sessionId, args.selector),
|
|
1279
|
-
},
|
|
1280
|
-
],
|
|
1281
|
-
[
|
|
1282
|
-
"type",
|
|
1283
|
-
{
|
|
1284
|
-
definition: {
|
|
1285
|
-
name: "type",
|
|
1286
|
-
description:
|
|
1287
|
-
"Type text into an input, textarea, or contenteditable element using real text input, replacing existing content unless clear is false.",
|
|
1288
|
-
inputSchema: sessionSchema(
|
|
1289
|
-
{
|
|
1290
|
-
selector: selectorProperty(),
|
|
1291
|
-
text: {
|
|
1292
|
-
type: "string",
|
|
1293
|
-
description:
|
|
1294
|
-
"Text to enter. Each newline is sent as an Enter key press: a line break in a textarea, implicit form submission in a single-line input. Date, time, datetime-local, month, week, color, and range inputs get the value set directly, so pass it in that input's value format (for example 2026-09-27). An empty string with clear empties the field.",
|
|
1295
|
-
},
|
|
1296
|
-
clear: {
|
|
1297
|
-
type: "boolean",
|
|
1298
|
-
description:
|
|
1299
|
-
"Replace the existing content (default true). When false, the text is inserted at the end of the current content. Ignored for date, time, datetime-local, month, week, color, and range inputs, whose value is always replaced.",
|
|
1300
|
-
},
|
|
1301
|
-
},
|
|
1302
|
-
["selector", "text"],
|
|
1303
|
-
),
|
|
1304
|
-
},
|
|
1305
|
-
handler: async (args) =>
|
|
1306
|
-
this.browserAdapter.type(args.sessionId, args.selector, args.text, {
|
|
1307
|
-
clear: args.clear,
|
|
1308
|
-
}),
|
|
1309
|
-
},
|
|
1310
|
-
],
|
|
1311
|
-
[
|
|
1312
|
-
"select",
|
|
1313
|
-
{
|
|
1314
|
-
definition: {
|
|
1315
|
-
name: "select",
|
|
1316
|
-
description:
|
|
1317
|
-
"Select an option in a native <select> element and fire input and change events. Provide value, label, or both; the first option matching either is selected, and the call fails if none matches. Custom dropdowns built from other elements need click instead.",
|
|
1318
|
-
inputSchema: sessionSchema(
|
|
1319
|
-
{
|
|
1320
|
-
selector: selectorProperty(),
|
|
1321
|
-
value: {
|
|
1322
|
-
type: "string",
|
|
1323
|
-
description:
|
|
1324
|
-
"Matches an option whose value attribute or full text equals this string.",
|
|
1325
|
-
},
|
|
1326
|
-
label: {
|
|
1327
|
-
type: "string",
|
|
1328
|
-
description:
|
|
1329
|
-
"Matches an option whose visible text contains this string (case-sensitive).",
|
|
1330
|
-
},
|
|
1331
|
-
},
|
|
1332
|
-
["selector"],
|
|
1333
|
-
),
|
|
1334
|
-
},
|
|
1335
|
-
handler: async (args) => {
|
|
1336
|
-
if (!args.value && !args.label) {
|
|
1337
|
-
throw new Error("select requires either value or label");
|
|
1338
|
-
}
|
|
1339
|
-
|
|
1340
|
-
return this.browserAdapter.select(args.sessionId, args.selector, {
|
|
1341
|
-
value: args.value,
|
|
1342
|
-
label: args.label,
|
|
1343
|
-
});
|
|
1344
|
-
},
|
|
1345
|
-
},
|
|
1346
|
-
],
|
|
1347
|
-
[
|
|
1348
|
-
"press_key",
|
|
1349
|
-
{
|
|
1350
|
-
definition: {
|
|
1351
|
-
name: "press_key",
|
|
1352
|
-
description:
|
|
1353
|
-
"Press a key or key combination (for example Enter, Tab, Escape, ArrowDown, Shift+Tab, Meta+a) with real keyboard input on the focused element, or on selector after focusing it. Fails if selector cannot take focus. A JavaScript alert or confirm dialog that this opens is accepted automatically and a prompt is dismissed; get_events reports it as a dialog event.",
|
|
1354
|
-
inputSchema: sessionSchema(
|
|
1355
|
-
{
|
|
1356
|
-
key: {
|
|
1357
|
-
type: "string",
|
|
1358
|
-
description:
|
|
1359
|
-
"A single character, or a named key: Enter, Tab, Escape, Backspace, Delete, Insert, ArrowUp/Down/Left/Right, Home, End, PageUp, PageDown, Space, F1-F12, Shift, Control, Alt, or Meta. Join modifiers with + (Shift+Tab, Control+Enter, Meta+a). Names are case-insensitive; unknown names are rejected.",
|
|
1360
|
-
},
|
|
1361
|
-
selector: selectorProperty(),
|
|
1362
|
-
},
|
|
1363
|
-
["key"],
|
|
1364
|
-
),
|
|
1365
|
-
},
|
|
1366
|
-
handler: async (args) =>
|
|
1367
|
-
this.browserAdapter.pressKey(
|
|
1368
|
-
args.sessionId,
|
|
1369
|
-
args.key,
|
|
1370
|
-
args.selector,
|
|
1371
|
-
),
|
|
1372
|
-
},
|
|
1373
|
-
],
|
|
1374
|
-
[
|
|
1375
|
-
"scroll",
|
|
1376
|
-
{
|
|
1377
|
-
definition: {
|
|
1378
|
-
name: "scroll",
|
|
1379
|
-
description:
|
|
1380
|
-
"Scroll the page by deltas or scroll a specific element into view.",
|
|
1381
|
-
inputSchema: sessionSchema(
|
|
1382
|
-
{
|
|
1383
|
-
selector: selectorProperty(),
|
|
1384
|
-
deltaX: {
|
|
1385
|
-
type: "integer",
|
|
1386
|
-
description:
|
|
1387
|
-
"Horizontal scroll distance in CSS pixels; negative scrolls left. Ignored when selector is given.",
|
|
1388
|
-
},
|
|
1389
|
-
deltaY: {
|
|
1390
|
-
type: "integer",
|
|
1391
|
-
description:
|
|
1392
|
-
"Vertical scroll distance in CSS pixels; negative scrolls up. Ignored when selector is given.",
|
|
1393
|
-
},
|
|
1394
|
-
block: {
|
|
1395
|
-
type: "string",
|
|
1396
|
-
enum: ["start", "center", "end", "nearest"],
|
|
1397
|
-
description:
|
|
1398
|
-
"Vertical alignment when scrolling selector into view (default center).",
|
|
1399
|
-
},
|
|
1400
|
-
},
|
|
1401
|
-
[],
|
|
1402
|
-
),
|
|
1403
|
-
},
|
|
1404
|
-
handler: async (args) => {
|
|
1405
|
-
if (
|
|
1406
|
-
!args.selector &&
|
|
1407
|
-
args.deltaX === undefined &&
|
|
1408
|
-
args.deltaY === undefined
|
|
1409
|
-
) {
|
|
1410
|
-
throw new Error(
|
|
1411
|
-
"scroll requires either selector or deltaX/deltaY values",
|
|
1412
|
-
);
|
|
1413
|
-
}
|
|
1414
|
-
|
|
1415
|
-
return this.browserAdapter.scroll(args.sessionId, {
|
|
1416
|
-
selector: args.selector,
|
|
1417
|
-
deltaX: args.deltaX,
|
|
1418
|
-
deltaY: args.deltaY,
|
|
1419
|
-
block: args.block,
|
|
1420
|
-
});
|
|
1421
|
-
},
|
|
1422
|
-
},
|
|
1423
|
-
],
|
|
1424
|
-
[
|
|
1425
|
-
"set_viewport",
|
|
1426
|
-
{
|
|
1427
|
-
definition: {
|
|
1428
|
-
name: "set_viewport",
|
|
1429
|
-
description:
|
|
1430
|
-
"Override the page viewport to a specific width and height for responsive debugging.",
|
|
1431
|
-
inputSchema: sessionSchema(
|
|
1432
|
-
{
|
|
1433
|
-
width: {
|
|
1434
|
-
type: "integer",
|
|
1435
|
-
minimum: 1,
|
|
1436
|
-
description: "Viewport width in CSS pixels.",
|
|
1437
|
-
},
|
|
1438
|
-
height: {
|
|
1439
|
-
type: "integer",
|
|
1440
|
-
minimum: 1,
|
|
1441
|
-
description: "Viewport height in CSS pixels.",
|
|
1442
|
-
},
|
|
1443
|
-
deviceScaleFactor: {
|
|
1444
|
-
type: "number",
|
|
1445
|
-
minimum: 0.1,
|
|
1446
|
-
description:
|
|
1447
|
-
"Device pixel ratio to emulate. Chromium and Edge use 1 when omitted; Firefox keeps its current ratio.",
|
|
1448
|
-
},
|
|
1449
|
-
mobile: {
|
|
1450
|
-
type: "boolean",
|
|
1451
|
-
description:
|
|
1452
|
-
"Emulate a mobile device, including meta viewport handling (default false). Chromium and Edge only; ignored on Firefox.",
|
|
1453
|
-
},
|
|
1454
|
-
},
|
|
1455
|
-
["width", "height"],
|
|
1456
|
-
),
|
|
1457
|
-
},
|
|
1458
|
-
handler: async (args) =>
|
|
1459
|
-
this.browserAdapter.setViewport(args.sessionId, {
|
|
1460
|
-
width: args.width,
|
|
1461
|
-
height: args.height,
|
|
1462
|
-
deviceScaleFactor: args.deviceScaleFactor,
|
|
1463
|
-
mobile: args.mobile,
|
|
1464
|
-
}),
|
|
1465
|
-
},
|
|
1466
|
-
],
|
|
1467
|
-
[
|
|
1468
|
-
"get_console_messages",
|
|
1469
|
-
{
|
|
1470
|
-
definition: {
|
|
1471
|
-
name: "get_console_messages",
|
|
1472
|
-
description:
|
|
1473
|
-
"Read buffered console, log, and exception messages for an attached session.",
|
|
1474
|
-
inputSchema: sessionWithLimitSchema(),
|
|
1475
|
-
},
|
|
1476
|
-
handler: async (args) => ({
|
|
1477
|
-
messages: this.browserAdapter.getConsoleMessages(
|
|
1478
|
-
args.sessionId,
|
|
1479
|
-
args.limit ?? 50,
|
|
1480
|
-
),
|
|
1481
|
-
}),
|
|
1482
|
-
},
|
|
1483
|
-
],
|
|
1484
|
-
[
|
|
1485
|
-
"get_network_requests",
|
|
1486
|
-
{
|
|
1487
|
-
definition: {
|
|
1488
|
-
name: "get_network_requests",
|
|
1489
|
-
description:
|
|
1490
|
-
"Summarize buffered network requests for an attached session in a DevTools-network-tab style view.",
|
|
1491
|
-
inputSchema: sessionWithLimitSchema(),
|
|
1492
|
-
},
|
|
1493
|
-
handler: async (args) => ({
|
|
1494
|
-
requests: this.browserAdapter.getNetworkRequests(
|
|
1495
|
-
args.sessionId,
|
|
1496
|
-
args.limit ?? 50,
|
|
1497
|
-
),
|
|
1498
|
-
}),
|
|
1499
|
-
},
|
|
1500
|
-
],
|
|
1501
|
-
[
|
|
1502
|
-
"get_document",
|
|
1503
|
-
{
|
|
1504
|
-
definition: {
|
|
1505
|
-
name: "get_document",
|
|
1506
|
-
description: "Fetch the DOM document tree for an attached page.",
|
|
1507
|
-
inputSchema: {
|
|
1508
|
-
type: "object",
|
|
1509
|
-
properties: {
|
|
1510
|
-
sessionId: {
|
|
1511
|
-
type: "string",
|
|
1512
|
-
description: "Session id returned by attach_tab.",
|
|
1513
|
-
},
|
|
1514
|
-
depth: {
|
|
1515
|
-
type: "integer",
|
|
1516
|
-
minimum: 1,
|
|
1517
|
-
description:
|
|
1518
|
-
"How many levels of child nodes to include (default 2). Chromium and Edge only; Firefox returns the full document HTML regardless.",
|
|
1519
|
-
},
|
|
1520
|
-
},
|
|
1521
|
-
required: ["sessionId"],
|
|
1522
|
-
additionalProperties: false,
|
|
1523
|
-
},
|
|
1524
|
-
},
|
|
1525
|
-
handler: async (args) =>
|
|
1526
|
-
this.browserAdapter.getDocument(args.sessionId, args.depth ?? 2),
|
|
1527
|
-
},
|
|
1528
|
-
],
|
|
1529
|
-
[
|
|
1530
|
-
"inspect_element",
|
|
1531
|
-
{
|
|
1532
|
-
definition: {
|
|
1533
|
-
name: "inspect_element",
|
|
1534
|
-
description:
|
|
1535
|
-
"Inspect a single DOM element located by CSS, text=..., role=..., or name=... syntax and return normalized element details. Scrolls the element into view first, so the returned box reflects the scrolled position.",
|
|
1536
|
-
inputSchema: {
|
|
1537
|
-
type: "object",
|
|
1538
|
-
properties: {
|
|
1539
|
-
sessionId: {
|
|
1540
|
-
type: "string",
|
|
1541
|
-
description: "Session id returned by attach_tab.",
|
|
1542
|
-
},
|
|
1543
|
-
selector: selectorProperty(),
|
|
1544
|
-
},
|
|
1545
|
-
required: ["sessionId", "selector"],
|
|
1546
|
-
additionalProperties: false,
|
|
1547
|
-
},
|
|
1548
|
-
},
|
|
1549
|
-
handler: async (args) =>
|
|
1550
|
-
this.browserAdapter.inspectElement(args.sessionId, args.selector),
|
|
1551
|
-
},
|
|
1552
|
-
],
|
|
1553
|
-
[
|
|
1554
|
-
"take_screenshot",
|
|
1555
|
-
{
|
|
1556
|
-
definition: {
|
|
1557
|
-
name: "take_screenshot",
|
|
1558
|
-
description:
|
|
1559
|
-
"Capture a screenshot from an attached page or a single element when selector is provided.",
|
|
1560
|
-
inputSchema: {
|
|
1561
|
-
type: "object",
|
|
1562
|
-
properties: {
|
|
1563
|
-
sessionId: {
|
|
1564
|
-
type: "string",
|
|
1565
|
-
description: "Session id returned by attach_tab.",
|
|
1566
|
-
},
|
|
1567
|
-
format: {
|
|
1568
|
-
type: "string",
|
|
1569
|
-
enum: screenshotFormatsFor(this.config.browserFamily),
|
|
1570
|
-
description: "Image format (default png).",
|
|
1571
|
-
},
|
|
1572
|
-
selector: selectorProperty(),
|
|
1573
|
-
},
|
|
1574
|
-
required: ["sessionId"],
|
|
1575
|
-
additionalProperties: false,
|
|
1576
|
-
},
|
|
1577
|
-
},
|
|
1578
|
-
handler: async (args) =>
|
|
1579
|
-
this.browserAdapter.takeScreenshot(
|
|
1580
|
-
args.sessionId,
|
|
1581
|
-
args.format ?? "png",
|
|
1582
|
-
{
|
|
1583
|
-
selector: args.selector,
|
|
1584
|
-
},
|
|
1585
|
-
),
|
|
1586
|
-
},
|
|
1587
|
-
],
|
|
1588
|
-
[
|
|
1589
|
-
"get_events",
|
|
1590
|
-
{
|
|
1591
|
-
definition: {
|
|
1592
|
-
name: "get_events",
|
|
1593
|
-
description:
|
|
1594
|
-
"Read buffered console, log, exception, and network events for an attached session.",
|
|
1595
|
-
inputSchema: {
|
|
1596
|
-
type: "object",
|
|
1597
|
-
properties: {
|
|
1598
|
-
sessionId: {
|
|
1599
|
-
type: "string",
|
|
1600
|
-
description: "Session id returned by attach_tab.",
|
|
1601
|
-
},
|
|
1602
|
-
limit: {
|
|
1603
|
-
type: "integer",
|
|
1604
|
-
minimum: 1,
|
|
1605
|
-
description:
|
|
1606
|
-
"Return the most recent N events from the session buffer (default 50).",
|
|
1607
|
-
},
|
|
1608
|
-
},
|
|
1609
|
-
required: ["sessionId"],
|
|
1610
|
-
additionalProperties: false,
|
|
1611
|
-
},
|
|
1612
|
-
},
|
|
1613
|
-
handler: async (args) => ({
|
|
1614
|
-
events: this.browserAdapter.getEvents(
|
|
1615
|
-
args.sessionId,
|
|
1616
|
-
args.limit ?? 50,
|
|
1617
|
-
),
|
|
1618
|
-
}),
|
|
1619
|
-
},
|
|
1620
|
-
],
|
|
223
|
+
...browserTools(this),
|
|
224
|
+
...stateTools(this),
|
|
225
|
+
...actionTools(this),
|
|
226
|
+
...inspectionTools(this),
|
|
1621
227
|
];
|
|
1622
228
|
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
"
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
awaitPromise: {
|
|
1643
|
-
type: "boolean",
|
|
1644
|
-
description:
|
|
1645
|
-
"Wait for a returned promise to settle and return its result (default true).",
|
|
1646
|
-
},
|
|
1647
|
-
returnByValue: {
|
|
1648
|
-
type: "boolean",
|
|
1649
|
-
description:
|
|
1650
|
-
"Return the result as a JSON value (default true). When false, only a short description of the resulting object is returned. Chromium and Edge only; Firefox always returns a serialized value.",
|
|
1651
|
-
},
|
|
1652
|
-
},
|
|
1653
|
-
required: ["sessionId", "expression"],
|
|
1654
|
-
additionalProperties: false,
|
|
1655
|
-
},
|
|
1656
|
-
},
|
|
1657
|
-
handler: async (args) =>
|
|
1658
|
-
this.browserAdapter.evaluate(args.sessionId, args.expression, {
|
|
1659
|
-
awaitPromise: args.awaitPromise,
|
|
1660
|
-
returnByValue: args.returnByValue,
|
|
1661
|
-
}),
|
|
1662
|
-
},
|
|
1663
|
-
]);
|
|
1664
|
-
}
|
|
229
|
+
const stepTools = tools
|
|
230
|
+
.filter(([, tool]) =>
|
|
231
|
+
tool.definition.inputSchema.required?.includes("sessionId"),
|
|
232
|
+
)
|
|
233
|
+
.map(([name]) => name);
|
|
234
|
+
this.stepRunner = new StepRunner({
|
|
235
|
+
getTools: () => this.tools,
|
|
236
|
+
browserAdapter: this.browserAdapter,
|
|
237
|
+
stepSchema: stepSchema(stepTools),
|
|
238
|
+
conditionOptions: { expression: this.config.enableEvaluate },
|
|
239
|
+
});
|
|
240
|
+
tools.push(runStepsTool(this.stepRunner, stepTools));
|
|
241
|
+
this.tabRunner = new TabRunner({
|
|
242
|
+
stepRunner: this.stepRunner,
|
|
243
|
+
browserAdapter: this.browserAdapter,
|
|
244
|
+
});
|
|
245
|
+
tools.push(
|
|
246
|
+
runTabsTool(this.tabRunner, stepTools, this.config.browserFamily),
|
|
247
|
+
);
|
|
1665
248
|
|
|
1666
249
|
return new Map(tools);
|
|
1667
250
|
}
|
|
@@ -1749,7 +332,7 @@ export class McpBrowserDevToolsServer {
|
|
|
1749
332
|
version: SERVER_VERSION,
|
|
1750
333
|
},
|
|
1751
334
|
instructions:
|
|
1752
|
-
|
|
335
|
+
'Use the browser tools to inspect tabs, console output, network activity, DOM structure, element state, screenshots, and page interactions across Chromium CDP or Firefox BiDi. To act on a page and check the result, prefer run_steps: send the actions you already know (such as every field of a form), a wait_for, and the check (take_screenshot with output image, or inspect_element) in one call instead of several round trips, for example {"sessionId":"<id>","steps":[{"tool":"type","arguments":{"selector":"#search","text":"headphones"}},{"tool":"press_key","arguments":{"key":"Enter"}},{"tool":"wait_for","arguments":{"selector":"#results"}},{"tool":"take_screenshot","arguments":{"output":"image"}}]}. Add an if step when the page can be in more than one state.',
|
|
1753
336
|
});
|
|
1754
337
|
case "notifications/initialized":
|
|
1755
338
|
return null;
|
|
@@ -1773,14 +356,54 @@ export class McpBrowserDevToolsServer {
|
|
|
1773
356
|
}
|
|
1774
357
|
|
|
1775
358
|
async callTool(params = {}) {
|
|
1776
|
-
const
|
|
1777
|
-
|
|
1778
|
-
|
|
359
|
+
const startedAt = Date.now();
|
|
360
|
+
let response = null;
|
|
361
|
+
try {
|
|
362
|
+
const tool = this.tools.get(params.name);
|
|
363
|
+
if (!tool) {
|
|
364
|
+
throw new Error(`Unknown tool: ${params.name}`);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const args = params.arguments ?? {};
|
|
368
|
+
validateValue("arguments", args, tool.definition.inputSchema);
|
|
369
|
+
tool.validate?.(args);
|
|
370
|
+
const result = await tool.handler(args);
|
|
371
|
+
response = (tool.formatResult ?? asToolResult)(result, args);
|
|
372
|
+
return response;
|
|
373
|
+
} finally {
|
|
374
|
+
await this.logTiming(params.name, startedAt, response);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Appends one JSON line per tool call when MCP_BROWSER_TIMING_LOG is set.
|
|
379
|
+
// responseChars and images approximate what the call costs the client.
|
|
380
|
+
async logTiming(name, startedAt, response) {
|
|
381
|
+
if (!this.config.timingLogFile) {
|
|
382
|
+
return;
|
|
1779
383
|
}
|
|
1780
384
|
|
|
1781
|
-
const
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
385
|
+
const content = response?.content ?? [];
|
|
386
|
+
const entry = {
|
|
387
|
+
ts: new Date(startedAt).toISOString(),
|
|
388
|
+
tool: name ?? null,
|
|
389
|
+
ms: Date.now() - startedAt,
|
|
390
|
+
ok: response !== null,
|
|
391
|
+
responseChars: content
|
|
392
|
+
.filter((block) => block.type === "text")
|
|
393
|
+
.reduce((total, block) => total + block.text.length, 0),
|
|
394
|
+
images: content.filter((block) => block.type === "image").length,
|
|
395
|
+
};
|
|
396
|
+
// ok is whether the call returned; a batch that stopped at a failing
|
|
397
|
+
// step still returns, so its own outcome is logged as stepsOk.
|
|
398
|
+
if (name === "run_steps" && response?.structuredContent) {
|
|
399
|
+
entry.steps = response.structuredContent.ranSteps;
|
|
400
|
+
entry.stepsOk = response.structuredContent.ok;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
try {
|
|
404
|
+
await appendFile(this.config.timingLogFile, `${JSON.stringify(entry)}\n`);
|
|
405
|
+
} catch (error) {
|
|
406
|
+
this.logger.error(`failed to write timing log: ${error.message}`);
|
|
407
|
+
}
|
|
1785
408
|
}
|
|
1786
409
|
}
|