mcp-browser-dev-tools 0.1.0 → 0.2.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.
@@ -0,0 +1,449 @@
1
+ import { DEFAULT_ACTION_TIMEOUT_MS } from "./action-retry.mjs";
2
+ import { supportedLaunchFamilies } from "./browser-launch-service.mjs";
3
+ import { screenshotFormatsFor } from "./screenshot-output.mjs";
4
+
5
+ export function emptyObjectSchema() {
6
+ return {
7
+ type: "object",
8
+ properties: {},
9
+ additionalProperties: false,
10
+ };
11
+ }
12
+
13
+ export function sessionWithLimitSchema(description) {
14
+ return {
15
+ type: "object",
16
+ properties: {
17
+ sessionId: {
18
+ type: "string",
19
+ description: "Session id returned by attach_tab.",
20
+ },
21
+ limit: {
22
+ type: "integer",
23
+ minimum: 1,
24
+ description:
25
+ "Return the most recent N entries from the session buffer (default 50).",
26
+ },
27
+ },
28
+ required: ["sessionId"],
29
+ additionalProperties: false,
30
+ description,
31
+ };
32
+ }
33
+
34
+ export const LOCATOR_DESCRIPTION =
35
+ 'Locator: CSS, text=Visible text, role=button[name="Save"], or name=Accessible name.';
36
+
37
+ // A copy of schema without descriptions, for a schema repeated inside another
38
+ // whose fields are already described once; validation is unchanged.
39
+ export function withoutDescriptions(schema) {
40
+ if (Array.isArray(schema)) {
41
+ return schema.map(withoutDescriptions);
42
+ }
43
+ if (!schema || typeof schema !== "object") {
44
+ return schema;
45
+ }
46
+ return Object.fromEntries(
47
+ Object.entries(schema)
48
+ .filter(
49
+ ([key, value]) => key !== "description" || typeof value !== "string",
50
+ )
51
+ .map(([key, value]) => [key, withoutDescriptions(value)]),
52
+ );
53
+ }
54
+
55
+ export function selectorProperty() {
56
+ return {
57
+ type: "string",
58
+ description: LOCATOR_DESCRIPTION,
59
+ };
60
+ }
61
+
62
+ export function sessionSchema(properties, required, description) {
63
+ return {
64
+ type: "object",
65
+ properties: {
66
+ sessionId: {
67
+ type: "string",
68
+ description: "Session id returned by attach_tab.",
69
+ },
70
+ ...properties,
71
+ },
72
+ required: ["sessionId", ...required],
73
+ additionalProperties: false,
74
+ description,
75
+ };
76
+ }
77
+
78
+ export function waitUntilProperty() {
79
+ return {
80
+ type: "string",
81
+ enum: ["none", "interactive", "complete"],
82
+ description:
83
+ "When to return: none returns once navigation starts, interactive after DOMContentLoaded, complete after the load event (default complete).",
84
+ };
85
+ }
86
+
87
+ // With expression, conditions also take a JavaScript expression; it is left
88
+ // out when evaluate_js is turned off.
89
+ export function pageConditionProperties({ expression = false } = {}) {
90
+ return {
91
+ selector: selectorProperty(),
92
+ state: {
93
+ type: "string",
94
+ enum: ["present", "visible", "hidden"],
95
+ description:
96
+ "Selector state (default visible). present: in the DOM. hidden: absent or not visible. Requires selector.",
97
+ },
98
+ url: {
99
+ type: "string",
100
+ description: "The page URL equals this string exactly.",
101
+ },
102
+ urlIncludes: {
103
+ type: "string",
104
+ description: "The page URL contains this string.",
105
+ },
106
+ readyState: {
107
+ type: "string",
108
+ enum: ["interactive", "complete"],
109
+ description: "document.readyState has reached this state or later.",
110
+ },
111
+ textEquals: {
112
+ type: "string",
113
+ description:
114
+ "The selector's full visible text equals this, with whitespace collapsed. Requires selector; not with state hidden.",
115
+ },
116
+ textIncludes: {
117
+ type: "string",
118
+ description:
119
+ "The selector's full visible text contains this, with whitespace collapsed. Requires selector; not with state hidden.",
120
+ },
121
+ textExcludes: {
122
+ type: "string",
123
+ description:
124
+ "The selector's visible text no longer contains this, for example Processing; never holds for a missing element. Requires selector; not with state hidden.",
125
+ },
126
+ ...(expression
127
+ ? {
128
+ expression: {
129
+ type: "string",
130
+ description:
131
+ "JavaScript in the page's main world; holds when its result (awaited if a promise) is truthy, for example document.querySelectorAll('.row').length >= 5. Throwing fails the check; each evaluation must settle within the remaining wait (5000ms in if and repeat).",
132
+ },
133
+ }
134
+ : {}),
135
+ };
136
+ }
137
+
138
+ export function conditionSchema(options) {
139
+ return {
140
+ type: "object",
141
+ properties: {
142
+ ...pageConditionProperties(options),
143
+ anyOf: anyOfProperty(options),
144
+ },
145
+ additionalProperties: false,
146
+ };
147
+ }
148
+
149
+ export function anyOfProperty(options) {
150
+ return {
151
+ type: "array",
152
+ minItems: 1,
153
+ items: {
154
+ type: "object",
155
+ properties: withoutDescriptions(pageConditionProperties(options)),
156
+ additionalProperties: false,
157
+ },
158
+ description:
159
+ "Alternative conditions with the fields above, instead of them; holds when any one holds, checked in order, for example a status of either Paid or Declined. The result reports matchedIndex.",
160
+ };
161
+ }
162
+
163
+ export function waitForInputSchema(options) {
164
+ return {
165
+ type: "object",
166
+ properties: {
167
+ sessionId: {
168
+ type: "string",
169
+ description: "Session id returned by attach_tab.",
170
+ },
171
+ ...pageConditionProperties(options),
172
+ anyOf: anyOfProperty(options),
173
+ timeoutMs: {
174
+ type: "integer",
175
+ minimum: 1,
176
+ description: "How long to wait before failing (default 10000).",
177
+ },
178
+ pollIntervalMs: {
179
+ type: "integer",
180
+ minimum: 1,
181
+ description: "Delay between checks (default 100).",
182
+ },
183
+ },
184
+ required: ["sessionId"],
185
+ additionalProperties: false,
186
+ };
187
+ }
188
+
189
+ export function actionTimeoutProperty(what) {
190
+ return {
191
+ type: "integer",
192
+ minimum: 0,
193
+ maximum: 30_000,
194
+ description: `How long to keep retrying while the element is missing${what}, before failing (default ${DEFAULT_ACTION_TIMEOUT_MS}; 0 fails at once).`,
195
+ };
196
+ }
197
+
198
+ export function compareSessionsSchema(properties, required) {
199
+ return {
200
+ type: "object",
201
+ properties: {
202
+ sessionIdA: {
203
+ type: "string",
204
+ description: "Session id returned by attach_tab.",
205
+ },
206
+ sessionIdB: {
207
+ type: "string",
208
+ description: "Session id returned by attach_tab.",
209
+ },
210
+ ...properties,
211
+ },
212
+ required: ["sessionIdA", "sessionIdB", ...required],
213
+ additionalProperties: false,
214
+ };
215
+ }
216
+
217
+ export function storageInputSchema() {
218
+ return {
219
+ type: "object",
220
+ properties: {
221
+ sessionId: {
222
+ type: "string",
223
+ description: "Session id returned by attach_tab.",
224
+ },
225
+ area: {
226
+ type: "string",
227
+ enum: ["all", "localStorage", "sessionStorage"],
228
+ description: "Which storage to return (default all).",
229
+ },
230
+ },
231
+ required: ["sessionId"],
232
+ additionalProperties: false,
233
+ };
234
+ }
235
+
236
+ export function captureDebugReportInputSchema(browserFamily) {
237
+ return {
238
+ type: "object",
239
+ properties: {
240
+ sessionId: {
241
+ type: "string",
242
+ description: "Session id returned by attach_tab.",
243
+ },
244
+ consoleLimit: {
245
+ type: "integer",
246
+ minimum: 1,
247
+ description: "Most recent console entries to include (default 20).",
248
+ },
249
+ networkLimit: {
250
+ type: "integer",
251
+ minimum: 1,
252
+ description: "Most recent network requests to include (default 20).",
253
+ },
254
+ includeScreenshot: {
255
+ type: "boolean",
256
+ description: "Include a viewport screenshot (default true).",
257
+ },
258
+ screenshotFormat: {
259
+ type: "string",
260
+ enum: screenshotFormatsFor(browserFamily),
261
+ description: "Screenshot image format (default png).",
262
+ },
263
+ },
264
+ required: ["sessionId"],
265
+ additionalProperties: false,
266
+ };
267
+ }
268
+
269
+ export function restoreSessionSnapshotInputSchema() {
270
+ return {
271
+ type: "object",
272
+ properties: {
273
+ sessionId: {
274
+ type: "string",
275
+ description: "Session id returned by attach_tab.",
276
+ },
277
+ snapshot: {
278
+ type: "string",
279
+ description:
280
+ "The JSON output of capture_session_snapshot, as a string. Only entries for the current page origin are restored.",
281
+ },
282
+ clearStorage: {
283
+ type: "boolean",
284
+ description:
285
+ "Clear localStorage and sessionStorage before restoring (default false).",
286
+ },
287
+ },
288
+ required: ["sessionId", "snapshot"],
289
+ additionalProperties: false,
290
+ };
291
+ }
292
+
293
+ export function newTabInputSchema(browserFamily) {
294
+ const properties = {
295
+ url: {
296
+ type: "string",
297
+ description: "URL to open (default about:blank).",
298
+ },
299
+ };
300
+ const required = [];
301
+
302
+ if (browserFamily === "auto") {
303
+ properties.browserFamily = {
304
+ type: "string",
305
+ enum: ["chromium", "firefox"],
306
+ description: "Browser to open the tab in.",
307
+ };
308
+ required.push("browserFamily");
309
+ }
310
+
311
+ return {
312
+ type: "object",
313
+ properties,
314
+ required,
315
+ additionalProperties: false,
316
+ };
317
+ }
318
+
319
+ export function unsafeArgsProperty() {
320
+ return {
321
+ type: "array",
322
+ items: {
323
+ type: "string",
324
+ },
325
+ description:
326
+ "Extra browser command-line flags, each starting with -. Flags that conflict with broker-managed launch options (debugging port, address, profile) are rejected.",
327
+ };
328
+ }
329
+
330
+ export function launchBrowserInputSchema(
331
+ browserFamily,
332
+ enableUnsafeLaunchArgs,
333
+ ) {
334
+ const properties = {
335
+ url: {
336
+ type: "string",
337
+ description: "URL to open in the launched browser (default about:blank).",
338
+ },
339
+ browserFamily: {
340
+ type: "string",
341
+ enum: supportedLaunchFamilies(browserFamily),
342
+ description:
343
+ "Browser to launch. Required when the broker runs in auto mode.",
344
+ },
345
+ port: {
346
+ type: "integer",
347
+ minimum: 1,
348
+ description:
349
+ "Remote debugging port to launch on (default: the port of the configured browser endpoint, usually 9222).",
350
+ },
351
+ address: {
352
+ type: "string",
353
+ description:
354
+ "Remote debugging bind address for Chromium and Edge (ignored for Firefox). Must be loopback unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1.",
355
+ },
356
+ userDataDir: {
357
+ type: "string",
358
+ description:
359
+ "Profile directory for a new launch (default: MCP_BROWSER_USER_DATA_DIR). Without either, Chromium and Edge use a temporary profile if already running, else the default one.",
360
+ },
361
+ waitMs: {
362
+ type: "integer",
363
+ minimum: 0,
364
+ description:
365
+ "How long to wait for the debugging endpoint to answer after launch (default 5000).",
366
+ },
367
+ skipDoctor: {
368
+ type: "boolean",
369
+ description:
370
+ "Return right after spawning the browser, without waiting for the endpoint or running the doctor report (default false).",
371
+ },
372
+ };
373
+ if (enableUnsafeLaunchArgs) {
374
+ properties.unsafeArgs = unsafeArgsProperty();
375
+ }
376
+ const required = browserFamily === "auto" ? ["browserFamily"] : [];
377
+
378
+ return {
379
+ type: "object",
380
+ properties,
381
+ required,
382
+ additionalProperties: false,
383
+ };
384
+ }
385
+
386
+ export function ensureBrowserInputSchema(
387
+ browserFamily,
388
+ enableUnsafeLaunchArgs,
389
+ ) {
390
+ const properties = {
391
+ browserFamily: {
392
+ type: "string",
393
+ enum: supportedLaunchFamilies(browserFamily),
394
+ description:
395
+ "Browser to check or launch. Required when the broker runs in auto mode.",
396
+ },
397
+ url: {
398
+ type: "string",
399
+ description: "URL to open in a new tab once the browser is reachable.",
400
+ },
401
+ createTab: {
402
+ type: "boolean",
403
+ description:
404
+ "Open a tab after the browser is reachable (default: true when url is given).",
405
+ },
406
+ launchIfMissing: {
407
+ type: "boolean",
408
+ description:
409
+ "Launch a browser when none is reachable (default true). When false and none is reachable, return the status without launching or opening a tab.",
410
+ },
411
+ port: {
412
+ type: "integer",
413
+ minimum: 1,
414
+ description:
415
+ "Remote debugging port to launch on (default: the port of the configured browser endpoint, usually 9222).",
416
+ },
417
+ address: {
418
+ type: "string",
419
+ description:
420
+ "Remote debugging bind address for Chromium and Edge (ignored for Firefox). Must be loopback unless MCP_BROWSER_ALLOW_REMOTE_ENDPOINTS=1.",
421
+ },
422
+ userDataDir: {
423
+ type: "string",
424
+ description:
425
+ "Profile directory for a new launch (default: MCP_BROWSER_USER_DATA_DIR). Without either, Chromium and Edge use a temporary profile if already running, else the default one.",
426
+ },
427
+ waitMs: {
428
+ type: "integer",
429
+ minimum: 0,
430
+ description:
431
+ "How long to wait for the debugging endpoint to answer after launch (default 5000).",
432
+ },
433
+ skipDoctor: {
434
+ type: "boolean",
435
+ description:
436
+ "Return right after spawning the browser, without waiting for the endpoint or running the doctor report (default false).",
437
+ },
438
+ };
439
+ if (enableUnsafeLaunchArgs) {
440
+ properties.unsafeArgs = unsafeArgsProperty();
441
+ }
442
+
443
+ return {
444
+ type: "object",
445
+ properties,
446
+ required: browserFamily === "auto" ? ["browserFamily"] : [],
447
+ additionalProperties: false,
448
+ };
449
+ }