mcp-browser-dev-tools 0.1.0 → 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.
@@ -0,0 +1,287 @@
1
+ import {
2
+ screenshotFileExtensions,
3
+ takeScreenshot,
4
+ } from "../screenshot-output.mjs";
5
+ import {
6
+ screenshotFormatsFor,
7
+ screenshotTarget,
8
+ } from "../screenshot-output.mjs";
9
+ import {
10
+ asImageToolResult,
11
+ asToolResult,
12
+ moveScreenshotImage,
13
+ } from "../tool-results.mjs";
14
+ import { selectorProperty, sessionWithLimitSchema } from "../tool-schemas.mjs";
15
+
16
+ const DEFAULT_READ_CHARS = 8000;
17
+ const MAX_READ_CHARS = 100_000;
18
+ const DEFAULT_READ_LINKS = 50;
19
+ const MAX_READ_LINKS = 200;
20
+
21
+ export function inspectionTools(server) {
22
+ const tools = [
23
+ [
24
+ "get_console_messages",
25
+ {
26
+ definition: {
27
+ name: "get_console_messages",
28
+ description:
29
+ "Read buffered console, log, and exception messages for an attached session.",
30
+ inputSchema: sessionWithLimitSchema(),
31
+ },
32
+ handler: async (args) => ({
33
+ messages: server.browserAdapter.getConsoleMessages(
34
+ args.sessionId,
35
+ args.limit ?? 50,
36
+ ),
37
+ }),
38
+ },
39
+ ],
40
+ [
41
+ "get_network_requests",
42
+ {
43
+ definition: {
44
+ name: "get_network_requests",
45
+ description:
46
+ "Summarize buffered network requests for an attached session in a DevTools-network-tab style view.",
47
+ inputSchema: sessionWithLimitSchema(),
48
+ },
49
+ handler: async (args) => ({
50
+ requests: server.browserAdapter.getNetworkRequests(
51
+ args.sessionId,
52
+ args.limit ?? 50,
53
+ ),
54
+ }),
55
+ },
56
+ ],
57
+ [
58
+ "get_document",
59
+ {
60
+ definition: {
61
+ name: "get_document",
62
+ description: "Fetch the DOM document tree for an attached page.",
63
+ inputSchema: {
64
+ type: "object",
65
+ properties: {
66
+ sessionId: {
67
+ type: "string",
68
+ description: "Session id returned by attach_tab.",
69
+ },
70
+ depth: {
71
+ type: "integer",
72
+ minimum: 1,
73
+ description:
74
+ "How many levels of child nodes to include (default 2). Chromium and Edge only; Firefox returns the full document HTML regardless.",
75
+ },
76
+ },
77
+ required: ["sessionId"],
78
+ additionalProperties: false,
79
+ },
80
+ },
81
+ handler: async (args) =>
82
+ server.browserAdapter.getDocument(args.sessionId, args.depth ?? 2),
83
+ },
84
+ ],
85
+ [
86
+ "inspect_element",
87
+ {
88
+ definition: {
89
+ name: "inspect_element",
90
+ description:
91
+ "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.",
92
+ inputSchema: {
93
+ type: "object",
94
+ properties: {
95
+ sessionId: {
96
+ type: "string",
97
+ description: "Session id returned by attach_tab.",
98
+ },
99
+ selector: selectorProperty(),
100
+ },
101
+ required: ["sessionId", "selector"],
102
+ additionalProperties: false,
103
+ },
104
+ },
105
+ handler: async (args) =>
106
+ server.browserAdapter.inspectElement(args.sessionId, args.selector),
107
+ },
108
+ ],
109
+ [
110
+ "read_text",
111
+ {
112
+ definition: {
113
+ name: "read_text",
114
+ description: `Read the visible text of the page's main content (main or role=main, else a single article, else the body), or of one element with selector, as plain lines up to maxChars. With links, also list the links in it with their text and absolute URL, for example to collect search results. Use it to read articles and results pages; inspect_element clips text to 400 characters.`,
115
+ inputSchema: {
116
+ type: "object",
117
+ properties: {
118
+ sessionId: {
119
+ type: "string",
120
+ description: "Session id returned by attach_tab.",
121
+ },
122
+ selector: selectorProperty(),
123
+ maxChars: {
124
+ type: "integer",
125
+ minimum: 1,
126
+ maximum: MAX_READ_CHARS,
127
+ description: `Most characters of text to return (default ${DEFAULT_READ_CHARS}, at most ${MAX_READ_CHARS}); totalChars and truncated tell whether there was more.`,
128
+ },
129
+ links: {
130
+ type: "boolean",
131
+ description: `Also return the http(s) links inside, deduplicated, at most maxLinks (default false).`,
132
+ },
133
+ maxLinks: {
134
+ type: "integer",
135
+ minimum: 1,
136
+ maximum: MAX_READ_LINKS,
137
+ description: `Most links to return (default ${DEFAULT_READ_LINKS}, at most ${MAX_READ_LINKS}); moreLinks counts the rest.`,
138
+ },
139
+ },
140
+ required: ["sessionId"],
141
+ additionalProperties: false,
142
+ },
143
+ },
144
+ handler: async (args) =>
145
+ server.browserAdapter.readText(args.sessionId, {
146
+ selector: args.selector,
147
+ maxChars: args.maxChars ?? DEFAULT_READ_CHARS,
148
+ links: args.links === true,
149
+ maxLinks: args.maxLinks ?? DEFAULT_READ_LINKS,
150
+ }),
151
+ },
152
+ ],
153
+ [
154
+ "take_screenshot",
155
+ {
156
+ definition: {
157
+ name: "take_screenshot",
158
+ description:
159
+ "Capture a screenshot from an attached page or a single element when selector is provided. By default the image comes back as base64 data in the JSON result; output image returns it as image content instead, and output file (or path) writes it to a file and returns the path.",
160
+ inputSchema: {
161
+ type: "object",
162
+ properties: {
163
+ sessionId: {
164
+ type: "string",
165
+ description: "Session id returned by attach_tab.",
166
+ },
167
+ format: {
168
+ type: "string",
169
+ enum: screenshotFormatsFor(server.config.browserFamily),
170
+ description:
171
+ "Image format (default png). With path, it must match the extension.",
172
+ },
173
+ selector: selectorProperty(),
174
+ output: {
175
+ type: "string",
176
+ enum: ["data", "image", "file"],
177
+ description:
178
+ "How to return the image: data puts base64 data in the JSON result, image returns an image content block with only metadata in the JSON, file writes the decoded image to a file and returns its path (default data, or file when path is given).",
179
+ },
180
+ path: {
181
+ type: "string",
182
+ description: `Absolute file path for output file, ending in ${screenshotFileExtensions(server.config.browserFamily).join(", ")}; the extension sets the format. Missing parent directories are created. Default: a new file in a private directory under the system temp directory.`,
183
+ },
184
+ overwrite: {
185
+ type: "boolean",
186
+ description:
187
+ "For output file: replace path if it already exists (default false: fail instead).",
188
+ },
189
+ },
190
+ required: ["sessionId"],
191
+ additionalProperties: false,
192
+ },
193
+ },
194
+ validate: (args) => {
195
+ screenshotTarget(args, server.config.browserFamily);
196
+ },
197
+ handler: async (args) => takeScreenshot(server, args),
198
+ formatResult: (result, args) => {
199
+ if (args.output !== "image") {
200
+ return asToolResult(result);
201
+ }
202
+ const images = [];
203
+ const value = moveScreenshotImage(result, images);
204
+ return asImageToolResult({ value, images });
205
+ },
206
+ },
207
+ ],
208
+ [
209
+ "get_events",
210
+ {
211
+ definition: {
212
+ name: "get_events",
213
+ description:
214
+ "Read buffered console, log, exception, and network events for an attached session.",
215
+ inputSchema: {
216
+ type: "object",
217
+ properties: {
218
+ sessionId: {
219
+ type: "string",
220
+ description: "Session id returned by attach_tab.",
221
+ },
222
+ limit: {
223
+ type: "integer",
224
+ minimum: 1,
225
+ description:
226
+ "Return the most recent N events from the session buffer (default 50).",
227
+ },
228
+ },
229
+ required: ["sessionId"],
230
+ additionalProperties: false,
231
+ },
232
+ },
233
+ handler: async (args) => ({
234
+ events: server.browserAdapter.getEvents(
235
+ args.sessionId,
236
+ args.limit ?? 50,
237
+ ),
238
+ }),
239
+ },
240
+ ],
241
+ ];
242
+
243
+ if (server.config.enableEvaluate) {
244
+ tools.push([
245
+ "evaluate_js",
246
+ {
247
+ definition: {
248
+ name: "evaluate_js",
249
+ description: "Evaluate JavaScript in the attached page context.",
250
+ inputSchema: {
251
+ type: "object",
252
+ properties: {
253
+ sessionId: {
254
+ type: "string",
255
+ description: "Session id returned by attach_tab.",
256
+ },
257
+ expression: {
258
+ type: "string",
259
+ description:
260
+ "JavaScript to evaluate in the page's main world. Top-level await works, and the value of the last expression statement is returned.",
261
+ },
262
+ awaitPromise: {
263
+ type: "boolean",
264
+ description:
265
+ "Wait for a returned promise to settle and return its result (default true).",
266
+ },
267
+ returnByValue: {
268
+ type: "boolean",
269
+ description:
270
+ "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.",
271
+ },
272
+ },
273
+ required: ["sessionId", "expression"],
274
+ additionalProperties: false,
275
+ },
276
+ },
277
+ handler: async (args) =>
278
+ server.browserAdapter.evaluate(args.sessionId, args.expression, {
279
+ awaitPromise: args.awaitPromise,
280
+ returnByValue: args.returnByValue,
281
+ }),
282
+ },
283
+ ]);
284
+ }
285
+
286
+ return tools;
287
+ }
@@ -0,0 +1,327 @@
1
+ import {
2
+ captureDebugReportInputSchema,
3
+ compareSessionsSchema,
4
+ restoreSessionSnapshotInputSchema,
5
+ selectorProperty,
6
+ sessionSchema,
7
+ sessionWithLimitSchema,
8
+ storageInputSchema,
9
+ waitForInputSchema,
10
+ } from "../tool-schemas.mjs";
11
+ import { normalizeWaitForOptions } from "../wait-for.mjs";
12
+
13
+ function parseSessionSnapshot(text) {
14
+ let snapshot;
15
+ try {
16
+ snapshot = JSON.parse(text);
17
+ } catch {
18
+ throw new Error("restore_session_snapshot snapshot must be valid JSON");
19
+ }
20
+
21
+ if (!snapshot || typeof snapshot !== "object") {
22
+ throw new Error(
23
+ "restore_session_snapshot snapshot must decode to an object",
24
+ );
25
+ }
26
+
27
+ return snapshot;
28
+ }
29
+
30
+ function sameValue(left, right) {
31
+ return JSON.stringify(left) === JSON.stringify(right);
32
+ }
33
+
34
+ function compareField(left, right) {
35
+ return {
36
+ equal: sameValue(left, right),
37
+ a: left,
38
+ b: right,
39
+ };
40
+ }
41
+
42
+ function summarizeComparablePageState(page = {}) {
43
+ return {
44
+ browserFamily: page.browserFamily ?? null,
45
+ url: page.url ?? null,
46
+ title: page.title ?? null,
47
+ readyState: page.readyState ?? null,
48
+ visibilityState: page.visibilityState ?? null,
49
+ viewport: page.viewport ?? null,
50
+ scroll: page.scroll ?? null,
51
+ };
52
+ }
53
+
54
+ function summarizeComparableElement(result = {}) {
55
+ if (!result.found || !result.node) {
56
+ return {
57
+ found: false,
58
+ selector: result.selector ?? null,
59
+ error: result.error ?? null,
60
+ };
61
+ }
62
+
63
+ return {
64
+ found: true,
65
+ selector: result.selector ?? null,
66
+ tagName: result.node.tagName ?? null,
67
+ accessibleName: result.node.accessibleName ?? null,
68
+ role: result.node.role ?? null,
69
+ visible: result.node.visible ?? null,
70
+ disabled: result.node.disabled ?? null,
71
+ textContent: result.node.textContent ?? null,
72
+ };
73
+ }
74
+
75
+ export function stateTools(server) {
76
+ return [
77
+ [
78
+ "get_page_state",
79
+ {
80
+ definition: {
81
+ name: "get_page_state",
82
+ description:
83
+ "Return the current URL, title, ready state, viewport, and scroll positions for an attached page.",
84
+ inputSchema: sessionSchema({}, []),
85
+ },
86
+ handler: async (args) =>
87
+ server.browserAdapter.getPageState(args.sessionId),
88
+ },
89
+ ],
90
+ [
91
+ "compare_page_state",
92
+ {
93
+ definition: {
94
+ name: "compare_page_state",
95
+ description:
96
+ "Compare bounded page state across two attached sessions, useful for cross-browser checks in auto mode.",
97
+ inputSchema: compareSessionsSchema({}, []),
98
+ },
99
+ handler: async (args) => {
100
+ const [pageA, pageB] = await Promise.all([
101
+ server.browserAdapter.getPageState(args.sessionIdA),
102
+ server.browserAdapter.getPageState(args.sessionIdB),
103
+ ]);
104
+ const left = summarizeComparablePageState(pageA);
105
+ const right = summarizeComparablePageState(pageB);
106
+ const fields = {
107
+ browserFamily: compareField(
108
+ left.browserFamily,
109
+ right.browserFamily,
110
+ ),
111
+ url: compareField(left.url, right.url),
112
+ title: compareField(left.title, right.title),
113
+ readyState: compareField(left.readyState, right.readyState),
114
+ visibilityState: compareField(
115
+ left.visibilityState,
116
+ right.visibilityState,
117
+ ),
118
+ viewport: compareField(left.viewport, right.viewport),
119
+ scroll: compareField(left.scroll, right.scroll),
120
+ };
121
+
122
+ return {
123
+ matches: Object.values(fields).every((field) => field.equal),
124
+ a: left,
125
+ b: right,
126
+ fields,
127
+ };
128
+ },
129
+ },
130
+ ],
131
+ [
132
+ "compare_selector",
133
+ {
134
+ definition: {
135
+ name: "compare_selector",
136
+ description:
137
+ "Compare a selector across two attached sessions using a bounded element summary.",
138
+ inputSchema: compareSessionsSchema(
139
+ {
140
+ selector: selectorProperty(),
141
+ },
142
+ ["selector"],
143
+ ),
144
+ },
145
+ handler: async (args) => {
146
+ const [elementA, elementB] = await Promise.all([
147
+ server.browserAdapter.inspectElement(
148
+ args.sessionIdA,
149
+ args.selector,
150
+ ),
151
+ server.browserAdapter.inspectElement(
152
+ args.sessionIdB,
153
+ args.selector,
154
+ ),
155
+ ]);
156
+ const left = summarizeComparableElement(elementA);
157
+ const right = summarizeComparableElement(elementB);
158
+ const fields = {
159
+ found: compareField(left.found, right.found),
160
+ tagName: compareField(left.tagName ?? null, right.tagName ?? null),
161
+ accessibleName: compareField(
162
+ left.accessibleName ?? null,
163
+ right.accessibleName ?? null,
164
+ ),
165
+ role: compareField(left.role ?? null, right.role ?? null),
166
+ visible: compareField(left.visible ?? null, right.visible ?? null),
167
+ disabled: compareField(
168
+ left.disabled ?? null,
169
+ right.disabled ?? null,
170
+ ),
171
+ textContent: compareField(
172
+ left.textContent ?? null,
173
+ right.textContent ?? null,
174
+ ),
175
+ };
176
+
177
+ return {
178
+ selector: args.selector,
179
+ matches: Object.values(fields).every((field) => field.equal),
180
+ a: left,
181
+ b: right,
182
+ fields,
183
+ };
184
+ },
185
+ },
186
+ ],
187
+ [
188
+ "wait_for",
189
+ {
190
+ definition: {
191
+ name: "wait_for",
192
+ description: server.config.enableEvaluate
193
+ ? "Wait until every given condition holds on an attached session: selector state, selector text (textEquals, textIncludes, textExcludes), exact url, urlIncludes substring, readyState, and a JavaScript expression. Give at least one of selector, url, urlIncludes, readyState, or expression, or give anyOf to wait until any one of several conditions holds. 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."
194
+ : "Wait until every given condition holds on an attached session: selector state, selector text (textEquals, textIncludes, textExcludes), exact url, urlIncludes substring, and readyState. Give at least one of selector, url, urlIncludes, or readyState, or give anyOf to wait until any one of several conditions holds. 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.",
195
+ inputSchema: waitForInputSchema({
196
+ expression: server.config.enableEvaluate,
197
+ }),
198
+ },
199
+ validate: (args) => {
200
+ normalizeWaitForOptions(args);
201
+ },
202
+ handler: async (args) =>
203
+ server.browserAdapter.waitFor(args.sessionId, {
204
+ selector: args.selector,
205
+ state: args.state,
206
+ url: args.url,
207
+ urlIncludes: args.urlIncludes,
208
+ readyState: args.readyState,
209
+ textEquals: args.textEquals,
210
+ textIncludes: args.textIncludes,
211
+ textExcludes: args.textExcludes,
212
+ expression: args.expression,
213
+ anyOf: args.anyOf,
214
+ timeoutMs: args.timeoutMs,
215
+ pollIntervalMs: args.pollIntervalMs,
216
+ }),
217
+ },
218
+ ],
219
+ [
220
+ "get_cookies",
221
+ {
222
+ definition: {
223
+ name: "get_cookies",
224
+ description:
225
+ "Read bounded page-visible cookies for the attached session.",
226
+ inputSchema: sessionSchema({}, []),
227
+ },
228
+ handler: async (args) =>
229
+ server.browserAdapter.getCookies(args.sessionId),
230
+ },
231
+ ],
232
+ [
233
+ "get_storage",
234
+ {
235
+ definition: {
236
+ name: "get_storage",
237
+ description:
238
+ "Read bounded localStorage and sessionStorage entries for the attached session.",
239
+ inputSchema: storageInputSchema(),
240
+ },
241
+ handler: async (args) => {
242
+ const result = await server.browserAdapter.getStorage(args.sessionId);
243
+ if (!args.area || args.area === "all") {
244
+ return result;
245
+ }
246
+
247
+ return {
248
+ browserFamily: result.browserFamily,
249
+ storage: {
250
+ [args.area]: result.storage?.[args.area] ?? null,
251
+ },
252
+ };
253
+ },
254
+ },
255
+ ],
256
+ [
257
+ "capture_debug_report",
258
+ {
259
+ definition: {
260
+ name: "capture_debug_report",
261
+ description:
262
+ "Capture a bounded debug bundle with page state, cookies/storage summary, recent console, recent network, and an optional screenshot.",
263
+ inputSchema: captureDebugReportInputSchema(
264
+ server.config.browserFamily,
265
+ ),
266
+ },
267
+ handler: async (args) =>
268
+ server.browserAdapter.captureDebugReport(args.sessionId, {
269
+ consoleLimit: args.consoleLimit,
270
+ networkLimit: args.networkLimit,
271
+ includeScreenshot: args.includeScreenshot,
272
+ screenshotFormat: args.screenshotFormat,
273
+ }),
274
+ },
275
+ ],
276
+ [
277
+ "capture_session_snapshot",
278
+ {
279
+ definition: {
280
+ name: "capture_session_snapshot",
281
+ description:
282
+ "Capture a bounded session snapshot with page state, cookies, and web storage for later bounded restore on the same origin.",
283
+ inputSchema: sessionSchema({}, []),
284
+ },
285
+ handler: async (args) =>
286
+ server.browserAdapter.captureSessionSnapshot(args.sessionId),
287
+ },
288
+ ],
289
+ [
290
+ "restore_session_snapshot",
291
+ {
292
+ definition: {
293
+ name: "restore_session_snapshot",
294
+ description:
295
+ "Restore a bounded session snapshot into the currently attached page context. Restores only page-visible cookies plus localStorage/sessionStorage on the current origin.",
296
+ inputSchema: restoreSessionSnapshotInputSchema(),
297
+ },
298
+ validate: (args) => {
299
+ parseSessionSnapshot(args.snapshot);
300
+ },
301
+ handler: async (args) =>
302
+ server.browserAdapter.restoreSessionSnapshot(
303
+ args.sessionId,
304
+ parseSessionSnapshot(args.snapshot),
305
+ {
306
+ clearStorage: args.clearStorage,
307
+ },
308
+ ),
309
+ },
310
+ ],
311
+ [
312
+ "get_har",
313
+ {
314
+ definition: {
315
+ name: "get_har",
316
+ description:
317
+ "Export a bounded HAR-like summary from buffered network activity for an attached session.",
318
+ inputSchema: sessionWithLimitSchema(),
319
+ },
320
+ handler: async (args) =>
321
+ server.browserAdapter.getHar(args.sessionId, {
322
+ limit: args.limit ?? 50,
323
+ }),
324
+ },
325
+ ],
326
+ ];
327
+ }