mcp-browser-dev-tools 0.2.2 → 0.3.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.
@@ -1,12 +1,33 @@
1
1
  import { retryUntilActionable } from "../action-retry.mjs";
2
2
  import { parseKeyCombo } from "../keyboard.mjs";
3
+ import {
4
+ DEFAULT_DRAG_STEPS,
5
+ MOUSE_BUTTONS,
6
+ parseFrameRef,
7
+ } from "../page-context.mjs";
8
+ import { allowedUploadDirs, resolveUploadPaths } from "../upload-files.mjs";
3
9
  import {
4
10
  actionTimeoutProperty,
11
+ pointProperties,
12
+ pointerTarget,
5
13
  selectorProperty,
6
14
  sessionSchema,
7
15
  waitUntilProperty,
8
16
  } from "../tool-schemas.mjs";
9
17
 
18
+ // Runs an action and adds what it changed on the page as changes, unless the
19
+ // caller (run_steps) reports the changes of the whole batch instead.
20
+ function reportChanges(server, args, options, action) {
21
+ if (options?.reportChanges === false || !server.changeTracker) {
22
+ return action();
23
+ }
24
+ return server.changeTracker.around(
25
+ args.sessionId,
26
+ action,
27
+ parseFrameRef(args.selector)?.frameKey ?? null,
28
+ );
29
+ }
30
+
10
31
  export function actionTools(server) {
11
32
  return [
12
33
  [
@@ -14,13 +35,12 @@ export function actionTools(server) {
14
35
  {
15
36
  definition: {
16
37
  name: "navigate",
17
- description:
18
- "Navigate an attached tab to a URL and optionally wait for interactive or complete load state.",
38
+ description: "Load a URL.",
19
39
  inputSchema: sessionSchema(
20
40
  {
21
41
  url: {
22
42
  type: "string",
23
- description: "Absolute URL to load.",
43
+ description: "Absolute URL.",
24
44
  },
25
45
  waitUntil: waitUntilProperty(),
26
46
  },
@@ -38,14 +58,12 @@ export function actionTools(server) {
38
58
  {
39
59
  definition: {
40
60
  name: "reload",
41
- description:
42
- "Reload the attached tab and optionally ignore cache while waiting for interactive or complete load state.",
61
+ description: "Reload the tab.",
43
62
  inputSchema: sessionSchema(
44
63
  {
45
64
  ignoreCache: {
46
65
  type: "boolean",
47
- description:
48
- "Bypass the HTTP cache for this reload (default false).",
66
+ description: "Bypass the cache.",
49
67
  },
50
68
  waitUntil: waitUntilProperty(),
51
69
  },
@@ -65,21 +83,43 @@ export function actionTools(server) {
65
83
  definition: {
66
84
  name: "click",
67
85
  description:
68
- "Click an element with real mouse input at its center; fails if another element covers that point. Accepts an alert or confirm dialog it opens and dismisses a prompt (get_events reports it). Check the result in the same call with run_steps.",
86
+ "Click with real mouse input at an element's center (fails if something covers it) or at x, y. Accepts alert and confirm dialogs, dismisses prompts. Returns changes: what changed on the page.",
69
87
  inputSchema: sessionSchema(
70
88
  {
71
89
  selector: selectorProperty(),
90
+ ...pointProperties(),
91
+ button: {
92
+ type: "string",
93
+ enum: MOUSE_BUTTONS,
94
+ description: "Default left.",
95
+ },
96
+ clickCount: {
97
+ type: "integer",
98
+ minimum: 1,
99
+ maximum: 3,
100
+ description: "2 for a double click.",
101
+ },
72
102
  timeoutMs: actionTimeoutProperty(
73
103
  ", covered, outside the viewport, or disabled",
74
104
  ),
75
105
  },
76
- ["selector"],
106
+ [],
77
107
  ),
78
108
  },
79
- handler: async (args) =>
80
- retryUntilActionable(
81
- () => server.browserAdapter.click(args.sessionId, args.selector),
82
- args.timeoutMs,
109
+ validate: (args) => {
110
+ pointerTarget(args);
111
+ },
112
+ handler: async (args, options) =>
113
+ reportChanges(server, args, options, () =>
114
+ retryUntilActionable(
115
+ () =>
116
+ server.browserAdapter.click(
117
+ args.sessionId,
118
+ pointerTarget(args),
119
+ { button: args.button, clickCount: args.clickCount },
120
+ ),
121
+ args.timeoutMs,
122
+ ),
83
123
  ),
84
124
  },
85
125
  ],
@@ -89,21 +129,79 @@ export function actionTools(server) {
89
129
  definition: {
90
130
  name: "hover",
91
131
  description:
92
- "Hover a single element located by CSS, text=..., role=..., or name=... syntax. Moves the real mouse pointer to the element center.",
132
+ "Move the real mouse pointer to an element's center or x, y. Returns changes.",
93
133
  inputSchema: sessionSchema(
94
134
  {
95
135
  selector: selectorProperty(),
136
+ ...pointProperties(),
96
137
  timeoutMs: actionTimeoutProperty(
97
138
  ", covered, or outside the viewport",
98
139
  ),
99
140
  },
100
- ["selector"],
141
+ [],
101
142
  ),
102
143
  },
103
- handler: async (args) =>
104
- retryUntilActionable(
105
- () => server.browserAdapter.hover(args.sessionId, args.selector),
106
- args.timeoutMs,
144
+ validate: (args) => {
145
+ pointerTarget(args);
146
+ },
147
+ handler: async (args, options) =>
148
+ reportChanges(server, args, options, () =>
149
+ retryUntilActionable(
150
+ () =>
151
+ server.browserAdapter.hover(
152
+ args.sessionId,
153
+ pointerTarget(args),
154
+ ),
155
+ args.timeoutMs,
156
+ ),
157
+ ),
158
+ },
159
+ ],
160
+ [
161
+ "drag",
162
+ {
163
+ definition: {
164
+ name: "drag",
165
+ description:
166
+ "Drag with real mouse input from selector or x, y to toSelector or toX, toY, both in view; handles HTML5 and pointer-driven drags. Returns changes.",
167
+ inputSchema: sessionSchema(
168
+ {
169
+ selector: selectorProperty(),
170
+ ...pointProperties(),
171
+ toSelector: {
172
+ type: "string",
173
+ description: "Drop target locator.",
174
+ },
175
+ ...pointProperties("to"),
176
+ steps: {
177
+ type: "integer",
178
+ minimum: 1,
179
+ maximum: 100,
180
+ description: `Moves (default ${DEFAULT_DRAG_STEPS}).`,
181
+ },
182
+ timeoutMs: actionTimeoutProperty(
183
+ ", covered, or outside the viewport",
184
+ ),
185
+ },
186
+ [],
187
+ ),
188
+ },
189
+ validate: (args) => {
190
+ pointerTarget(args);
191
+ pointerTarget(args, "to");
192
+ },
193
+ handler: async (args, options) =>
194
+ reportChanges(server, args, options, () =>
195
+ retryUntilActionable(
196
+ () =>
197
+ server.browserAdapter.drag(
198
+ args.sessionId,
199
+ pointerTarget(args),
200
+ pointerTarget(args, "to"),
201
+ { steps: args.steps },
202
+ ),
203
+ args.timeoutMs,
204
+ ),
107
205
  ),
108
206
  },
109
207
  ],
@@ -113,57 +211,97 @@ export function actionTools(server) {
113
211
  definition: {
114
212
  name: "type",
115
213
  description:
116
- "Type into an input, textarea, or contenteditable with real text input, replacing its content unless clear is false. Check the result in the same call with run_steps.",
214
+ "Type with real text input into an input, textarea, or contenteditable, replacing its content unless clear is false. Returns changes.",
117
215
  inputSchema: sessionSchema(
118
216
  {
119
217
  selector: selectorProperty(),
120
218
  text: {
121
219
  type: "string",
122
220
  description:
123
- "Text to enter; each newline is an Enter press (a line break in a textarea, form submission in a single-line input). Date, time, datetime-local, month, week, color, and range inputs get the value set directly, in their value format (for example 2026-09-27). An empty string with clear empties the field.",
221
+ "Each newline presses Enter. Picker inputs (date, time, datetime-local, month, week, color, range) take their value format, e.g. 2026-09-27.",
124
222
  },
125
223
  clear: {
126
224
  type: "boolean",
127
225
  description:
128
- "Replace the existing content (default true); false inserts at the end. The picker inputs listed under text are always replaced.",
226
+ "false appends (default true); picker inputs are always replaced.",
129
227
  },
130
228
  timeoutMs: actionTimeoutProperty(""),
131
229
  },
132
230
  ["selector", "text"],
133
231
  ),
134
232
  },
135
- handler: async (args) =>
136
- retryUntilActionable(
137
- () =>
138
- server.browserAdapter.type(
139
- args.sessionId,
140
- args.selector,
141
- args.text,
142
- { clear: args.clear },
143
- ),
144
- args.timeoutMs,
233
+ handler: async (args, options) =>
234
+ reportChanges(server, args, options, () =>
235
+ retryUntilActionable(
236
+ () =>
237
+ server.browserAdapter.type(
238
+ args.sessionId,
239
+ args.selector,
240
+ args.text,
241
+ { clear: args.clear },
242
+ ),
243
+ args.timeoutMs,
244
+ ),
145
245
  ),
146
246
  },
147
247
  ],
248
+ [
249
+ "upload_file",
250
+ {
251
+ definition: {
252
+ name: "upload_file",
253
+ description:
254
+ "Set files on a file input (or its label, or an element holding one; on Chromium also a button that opens a file chooser) and fire change events. Files must be in the working or temp directory or MCP_BROWSER_UPLOAD_DIRS. Returns changes.",
255
+ inputSchema: sessionSchema(
256
+ {
257
+ selector: selectorProperty(),
258
+ paths: {
259
+ type: "array",
260
+ maxItems: 20,
261
+ items: { type: "string" },
262
+ description: "Absolute file paths; [] clears the input.",
263
+ },
264
+ timeoutMs: actionTimeoutProperty(""),
265
+ },
266
+ ["selector", "paths"],
267
+ ),
268
+ },
269
+ handler: async (args, options) => {
270
+ const files = await resolveUploadPaths(
271
+ args.paths,
272
+ await allowedUploadDirs(server.config),
273
+ );
274
+ return reportChanges(server, args, options, () =>
275
+ retryUntilActionable(
276
+ () =>
277
+ server.browserAdapter.uploadFiles(
278
+ args.sessionId,
279
+ args.selector,
280
+ files,
281
+ ),
282
+ args.timeoutMs,
283
+ ),
284
+ );
285
+ },
286
+ },
287
+ ],
148
288
  [
149
289
  "select",
150
290
  {
151
291
  definition: {
152
292
  name: "select",
153
293
  description:
154
- "Select an option in a native <select> by value or label (the first option matching either) and fire input and change events; fails if none matches. Use click for custom dropdowns. Check the result in the same call with run_steps.",
294
+ "Pick a native <select> option by value or label and fire change events; use click for custom dropdowns. Returns changes.",
155
295
  inputSchema: sessionSchema(
156
296
  {
157
297
  selector: selectorProperty(),
158
298
  value: {
159
299
  type: "string",
160
- description:
161
- "Matches an option whose value attribute or full text equals this string.",
300
+ description: "Option value or full text.",
162
301
  },
163
302
  label: {
164
303
  type: "string",
165
- description:
166
- "Matches an option whose visible text contains this string (case-sensitive).",
304
+ description: "Text the option contains.",
167
305
  },
168
306
  timeoutMs: actionTimeoutProperty(""),
169
307
  },
@@ -175,14 +313,16 @@ export function actionTools(server) {
175
313
  throw new Error("select requires either value or label");
176
314
  }
177
315
  },
178
- handler: async (args) =>
179
- retryUntilActionable(
180
- () =>
181
- server.browserAdapter.select(args.sessionId, args.selector, {
182
- value: args.value,
183
- label: args.label,
184
- }),
185
- args.timeoutMs,
316
+ handler: async (args, options) =>
317
+ reportChanges(server, args, options, () =>
318
+ retryUntilActionable(
319
+ () =>
320
+ server.browserAdapter.select(args.sessionId, args.selector, {
321
+ value: args.value,
322
+ label: args.label,
323
+ }),
324
+ args.timeoutMs,
325
+ ),
186
326
  ),
187
327
  },
188
328
  ],
@@ -192,16 +332,16 @@ export function actionTools(server) {
192
332
  definition: {
193
333
  name: "press_key",
194
334
  description:
195
- "Press a key or combination with real keyboard input on the focused element, or on selector after focusing it (fails if it cannot take focus). Accepts an alert or confirm dialog it opens and dismisses a prompt (get_events reports it). Check the result in the same call with run_steps.",
335
+ "Press a key or combination with real keyboard input on selector (focused first) or the focused element. Returns changes.",
196
336
  inputSchema: sessionSchema(
197
337
  {
198
338
  key: {
199
339
  type: "string",
200
340
  description:
201
- "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.",
341
+ "A character or key name (Enter, Tab, Escape, ArrowDown, PageUp, F5, ...); join modifiers with +, e.g. Shift+Tab, Meta+a.",
202
342
  },
203
343
  selector: selectorProperty(),
204
- timeoutMs: actionTimeoutProperty("; only applies with selector"),
344
+ timeoutMs: actionTimeoutProperty(""),
205
345
  },
206
346
  ["key"],
207
347
  ),
@@ -209,15 +349,17 @@ export function actionTools(server) {
209
349
  validate: (args) => {
210
350
  parseKeyCombo(args.key);
211
351
  },
212
- handler: async (args) =>
213
- retryUntilActionable(
214
- () =>
215
- server.browserAdapter.pressKey(
216
- args.sessionId,
217
- args.key,
218
- args.selector,
219
- ),
220
- args.selector ? args.timeoutMs : 0,
352
+ handler: async (args, options) =>
353
+ reportChanges(server, args, options, () =>
354
+ retryUntilActionable(
355
+ () =>
356
+ server.browserAdapter.pressKey(
357
+ args.sessionId,
358
+ args.key,
359
+ args.selector,
360
+ ),
361
+ args.selector ? args.timeoutMs : 0,
362
+ ),
221
363
  ),
222
364
  },
223
365
  ],
@@ -227,44 +369,45 @@ export function actionTools(server) {
227
369
  definition: {
228
370
  name: "scroll",
229
371
  description:
230
- "Scroll the page by deltas or scroll a specific element into view.",
372
+ "Scroll the page by deltas, an element into view, or with a real wheel at x, y or over selector with deltas (scrolls what is under the pointer).",
231
373
  inputSchema: sessionSchema(
232
374
  {
233
375
  selector: selectorProperty(),
376
+ ...pointProperties(),
234
377
  deltaX: {
235
378
  type: "integer",
236
- description:
237
- "Horizontal scroll distance in CSS pixels; negative scrolls left. Ignored when selector is given.",
379
+ description: "CSS px; negative is left.",
238
380
  },
239
381
  deltaY: {
240
382
  type: "integer",
241
- description:
242
- "Vertical scroll distance in CSS pixels; negative scrolls up. Ignored when selector is given.",
383
+ description: "CSS px; negative is up.",
243
384
  },
244
385
  block: {
245
386
  type: "string",
246
387
  enum: ["start", "center", "end", "nearest"],
247
- description:
248
- "Vertical alignment when scrolling selector into view (default center).",
388
+ description: "Default center.",
249
389
  },
250
390
  },
251
391
  [],
252
392
  ),
253
393
  },
254
394
  validate: (args) => {
255
- if (
256
- !args.selector &&
257
- args.deltaX === undefined &&
258
- args.deltaY === undefined
259
- ) {
395
+ const target = pointerTarget(args, "", false);
396
+ const deltas = args.deltaX !== undefined || args.deltaY !== undefined;
397
+ if (!target && !deltas) {
260
398
  throw new Error(
261
399
  "scroll requires either selector or deltaX/deltaY values",
262
400
  );
263
401
  }
402
+ if (target && typeof target !== "string" && !deltas) {
403
+ throw new Error("scroll at x, y requires deltaX or deltaY");
404
+ }
264
405
  },
265
406
  handler: async (args) =>
266
407
  server.browserAdapter.scroll(args.sessionId, {
267
408
  selector: args.selector,
409
+ x: args.x,
410
+ y: args.y,
268
411
  deltaX: args.deltaX,
269
412
  deltaY: args.deltaY,
270
413
  block: args.block,
@@ -276,30 +419,27 @@ export function actionTools(server) {
276
419
  {
277
420
  definition: {
278
421
  name: "set_viewport",
279
- description:
280
- "Override the page viewport to a specific width and height for responsive debugging.",
422
+ description: "Override the viewport size.",
281
423
  inputSchema: sessionSchema(
282
424
  {
283
425
  width: {
284
426
  type: "integer",
285
427
  minimum: 1,
286
- description: "Viewport width in CSS pixels.",
428
+ description: "CSS px.",
287
429
  },
288
430
  height: {
289
431
  type: "integer",
290
432
  minimum: 1,
291
- description: "Viewport height in CSS pixels.",
433
+ description: "CSS px.",
292
434
  },
293
435
  deviceScaleFactor: {
294
436
  type: "number",
295
437
  minimum: 0.1,
296
- description:
297
- "Device pixel ratio to emulate. Chromium and Edge use 1 when omitted; Firefox keeps its current ratio.",
438
+ description: "Device pixel ratio (Chromium default 1).",
298
439
  },
299
440
  mobile: {
300
441
  type: "boolean",
301
- description:
302
- "Emulate a mobile device, including meta viewport handling (default false). Chromium and Edge only; ignored on Firefox.",
442
+ description: "Mobile emulation (Chromium only).",
303
443
  },
304
444
  },
305
445
  ["width", "height"],
@@ -21,7 +21,7 @@ export function browserTools(server) {
21
21
  definition: {
22
22
  name: "browser_status",
23
23
  description:
24
- "Report whether the configured browser endpoint is reachable and how many active sessions are attached.",
24
+ "Whether the browser endpoint answers, and the attached sessions.",
25
25
  inputSchema: emptyObjectSchema(),
26
26
  },
27
27
  handler: async () => ({
@@ -36,8 +36,7 @@ export function browserTools(server) {
36
36
  {
37
37
  definition: {
38
38
  name: "list_tabs",
39
- description:
40
- "List inspectable page targets exposed by the configured browser adapter.",
39
+ description: "List the browser's tabs.",
41
40
  inputSchema: emptyObjectSchema(),
42
41
  },
43
42
  handler: async () => ({
@@ -51,7 +50,7 @@ export function browserTools(server) {
51
50
  definition: {
52
51
  name: "launch_browser",
53
52
  description:
54
- "Launch a local debug-enabled browser for this server's configuration and return launch details and an optional doctor report. When port, address, userDataDir, and unsafeArgs are omitted and a browser of this family is reachable, returns it with reused: true, opening url in a new tab if given. On macOS and Linux it never opens a second browser on a profile already in use. Prefer ensure_browser.",
53
+ "Launch a debug-enabled browser, or reuse a reachable one unless port, address, userDataDir, or unsafeArgs is given; on macOS and Linux, never a second browser on a profile in use. Prefer ensure_browser.",
55
54
  inputSchema: launchBrowserInputSchema(
56
55
  server.config.browserFamily,
57
56
  server.config.enableUnsafeLaunchArgs,
@@ -110,7 +109,7 @@ export function browserTools(server) {
110
109
  definition: {
111
110
  name: "ensure_browser",
112
111
  description:
113
- "Reuse a reachable browser or launch one (with the MCP_BROWSER_USER_DATA_DIR profile when set), optionally opening url in a new tab. Checks three times before launching, does not launch again for 30 seconds while a browser it launched is starting, and on macOS and Linux never opens a second browser on a profile already in use.",
112
+ "Reuse a reachable browser or launch one (with the MCP_BROWSER_USER_DATA_DIR profile when set), optionally opening url in a new tab. On macOS and Linux it never launches twice on one profile.",
114
113
  inputSchema: ensureBrowserInputSchema(
115
114
  server.config.browserFamily,
116
115
  server.config.enableUnsafeLaunchArgs,
@@ -125,8 +124,7 @@ export function browserTools(server) {
125
124
  {
126
125
  definition: {
127
126
  name: "list_sessions",
128
- description:
129
- "List active attached debugging sessions held by this broker.",
127
+ description: "List attached sessions.",
130
128
  inputSchema: emptyObjectSchema(),
131
129
  },
132
130
  handler: async () => ({
@@ -139,8 +137,7 @@ export function browserTools(server) {
139
137
  {
140
138
  definition: {
141
139
  name: "new_tab",
142
- description:
143
- "Create a new browser tab and return the resulting target metadata.",
140
+ description: "Open a tab; returns its target id.",
144
141
  inputSchema: newTabInputSchema(server.config.browserFamily),
145
142
  },
146
143
  handler: async (args) =>
@@ -154,14 +151,13 @@ export function browserTools(server) {
154
151
  {
155
152
  definition: {
156
153
  name: "close_tab",
157
- description:
158
- "Close a browser tab by target id. Any attached session for that tab will disconnect.",
154
+ description: "Close a tab and its session.",
159
155
  inputSchema: {
160
156
  type: "object",
161
157
  properties: {
162
158
  targetId: {
163
159
  type: "string",
164
- description: "Target id returned by list_tabs or new_tab.",
160
+ description: "From list_tabs or new_tab.",
165
161
  },
166
162
  },
167
163
  required: ["targetId"],
@@ -178,13 +174,13 @@ export function browserTools(server) {
178
174
  definition: {
179
175
  name: "attach_tab",
180
176
  description:
181
- "Attach to a page target and start buffering console, log, and network events.",
177
+ "Attach to a tab: returns the sessionId other tools take, and buffers its console and network events from now on.",
182
178
  inputSchema: {
183
179
  type: "object",
184
180
  properties: {
185
181
  targetId: {
186
182
  type: "string",
187
- description: "The target id returned by list_tabs.",
183
+ description: "From list_tabs or new_tab.",
188
184
  },
189
185
  },
190
186
  required: ["targetId"],
@@ -200,14 +196,11 @@ export function browserTools(server) {
200
196
  {
201
197
  definition: {
202
198
  name: "detach_tab",
203
- description: "Close an attached debugging session.",
199
+ description: "Detach a session; the tab stays open.",
204
200
  inputSchema: {
205
201
  type: "object",
206
202
  properties: {
207
- sessionId: {
208
- type: "string",
209
- description: "The session id returned by attach_tab.",
210
- },
203
+ sessionId: { type: "string" },
211
204
  },
212
205
  required: ["sessionId"],
213
206
  additionalProperties: false,