mcp-browser-dev-tools 0.5.0 → 0.5.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-browser-dev-tools",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Browser MCP server for AI agents: batch actions, waits, and checks into one call on Chrome, Edge, or Firefox (CDP and WebDriver BiDi)",
5
5
  "author": "Hinaser <rindo.hinase@gmail.com>",
6
6
  "license": "MIT",
@@ -389,7 +389,7 @@ export class McpBrowserDevToolsServer {
389
389
  name: SERVER_NAME,
390
390
  version: SERVER_VERSION,
391
391
  },
392
- instructions: `Browser tools for Chromium (CDP) and Firefox (BiDi). Call attach_tab first; tools that take sessionId use the one it returns. To see a page, call get_snapshot: one line per visible heading and control (iframes too), each with a ref such as e12 or f1e3, far cheaper than a screenshot. To act, prefer run_steps: send the actions you already know, such as every field of a form, in one call: {"sessionId":"<id>","steps":[{"tool":"type","arguments":{"selector":"ref=e3","text":"Ada"}},{"tool":"click","arguments":{"selector":"ref=e4"}}]}. Actions return changes once the page settles: controls added, removed, or updated, new text such as an error or a toast, URL and title, console errors, and after a navigation the new page's snapshot. That is usually the check; add wait_for only for something later. Read several pages at once with run_tabs and read_text. Locators: ref=e12 is a get_snapshot element, until the page navigates; plain CSS (or css=) takes the first match; text=Foo the first visible element whose text equals, else contains, Foo; role=button[name="Save"] matches role and accessible name; name=Foo the first visible element whose accessible name equals or contains Foo (a label resolves to its control). a >> b is b inside a, and has-text=Foo filters by text: tr >> has-text=Ada >> text=Delete. Matching is case-sensitive; only refs reach into iframes. x and y are viewport CSS pixels; a screenshot's cssRect and scale convert image pixels. more_tools lists the tools not listed here (drag, cookies, request mocking, HAR, performance) and calls them; they also work as run_steps steps.`,
392
+ instructions: `Browser tools for Chromium (CDP) and Firefox (BiDi). Start with attach_tab, by the tab's url or a targetId from list_tabs or new_tab: it returns the sessionId other tools take and the page's snapshot, one line per visible heading and control (iframes too) with a ref such as e12 or f1e3. get_snapshot reads the page again, far cheaper than a screenshot. To act, prefer run_steps: send the actions you already know, such as every field of a form, in one call: {"sessionId":"<id>","steps":[{"tool":"type","arguments":{"selector":"ref=e3","text":"Ada"}},{"tool":"click","arguments":{"selector":"ref=e4"}}]}. Actions return what changed once the page settles (controls, new text such as a toast, URL, console errors, a new page's snapshot); that is usually the check, so add wait_for only for something later. Read several pages at once with run_tabs and read_text. Locators: ref=e12, until the page navigates; CSS, the first match; text=Foo and name=Foo, the first visible element whose text, or accessible name, equals, else contains, Foo (a label finds its control); role=button[name="Save"], by role and accessible name; a >> b, b inside a; has-text=Foo filters: tr >> has-text=Ada >> text=Delete. Case-sensitive; only refs reach into iframes. more_tools lists and calls the other tools (drag, cookies, request mocking, HAR, performance); they also work as run_steps steps.`,
393
393
  });
394
394
  case "notifications/initialized":
395
395
  return null;
@@ -9,6 +9,7 @@ import {
9
9
  launchBrowserInputSchema,
10
10
  newTabInputSchema,
11
11
  } from "../tool-schemas.mjs";
12
+ import { DEFAULT_SNAPSHOT_NODES } from "./inspection-tools.mjs";
12
13
 
13
14
  const SERVER_NAME = PACKAGE_NAME;
14
15
  const SERVER_VERSION = PACKAGE_VERSION;
@@ -179,7 +180,7 @@ export function browserTools(server) {
179
180
  definition: {
180
181
  name: "attach_tab",
181
182
  description:
182
- "Attach to a tab: returns the sessionId other tools take, and buffers its console and network events from now on.",
183
+ "Attach to a tab: returns the sessionId other tools take and the page's snapshot, as get_snapshot lists it, and buffers its console and network events from now on.",
183
184
  inputSchema: {
184
185
  type: "object",
185
186
  properties: {
@@ -187,13 +188,16 @@ export function browserTools(server) {
187
188
  type: "string",
188
189
  description: "From list_tabs or new_tab.",
189
190
  },
191
+ url: {
192
+ type: "string",
193
+ description:
194
+ "Instead of targetId: the tab whose URL is this, else the one whose URL contains it.",
195
+ },
190
196
  },
191
- required: ["targetId"],
192
197
  additionalProperties: false,
193
198
  },
194
199
  },
195
- handler: async (args) =>
196
- server.browserAdapter.attachToTarget(args.targetId),
200
+ handler: async (args) => attachTab(server, args),
197
201
  },
198
202
  ],
199
203
  [
@@ -217,3 +221,80 @@ export function browserTools(server) {
217
221
  ],
218
222
  ];
219
223
  }
224
+
225
+ // Attaches by targetId, or by url: the tab whose URL equals it, else the one
226
+ // whose URL contains it. Several matches, or none, fail with the tabs to
227
+ // choose from, so the agent needs no list_tabs call to try again. The result
228
+ // carries the page's snapshot, so the agent can act without another call; a
229
+ // snapshot that fails leaves the attach in place and says why.
230
+ async function attachTab(server, { targetId, url }) {
231
+ if ((targetId === undefined) === (url === undefined)) {
232
+ throw new Error("attach_tab takes either targetId or url");
233
+ }
234
+ const session = await server.browserAdapter.attachToTarget(
235
+ targetId ?? (await tabForUrl(server, url)),
236
+ );
237
+ const settled = await settleAfterAttach(server, session.sessionId);
238
+ try {
239
+ const snapshot = await server.browserAdapter.snapshotPage(
240
+ session.sessionId,
241
+ { limit: DEFAULT_SNAPSHOT_NODES, headings: true },
242
+ );
243
+ return {
244
+ ...session,
245
+ nodes: snapshot.nodes,
246
+ ...(snapshot.more > 0 ? { nodesMore: snapshot.more } : {}),
247
+ ...(settled?.settled === false ? { settled: false } : {}),
248
+ };
249
+ } catch (error) {
250
+ return { ...session, snapshotError: error.message };
251
+ }
252
+ }
253
+
254
+ // Waits for the page to settle before the snapshot, as a batch does before a
255
+ // step that reads the page: a page still loading or rendering is listed once
256
+ // its DOM has been quiet for a moment, for at most 2 s (10 s while it
257
+ // navigates). Work the page set going before the attach, such as a timer or
258
+ // a request already in flight, is not seen. Returns null when the page could
259
+ // not be watched.
260
+ async function settleAfterAttach(server, sessionId) {
261
+ const runner = server.stepRunner;
262
+ if (!runner?.changeTracker) {
263
+ return null;
264
+ }
265
+ const context = { sessionId, settles: true };
266
+ try {
267
+ await runner.watchPage(context);
268
+ const now = Date.now();
269
+ context.settling = { endedAt: now, since: now };
270
+ return await runner.settlePage(context);
271
+ } catch {
272
+ await runner.stopWatching(context).catch(() => {});
273
+ return null;
274
+ }
275
+ }
276
+
277
+ const MAX_LISTED_TABS = 10;
278
+
279
+ async function tabForUrl(server, url) {
280
+ const tabs = (await server.browserAdapter.listTargets()).filter(
281
+ (tab) =>
282
+ (tab.type === undefined || tab.type === "page") &&
283
+ typeof tab.url === "string",
284
+ );
285
+ const exact = tabs.filter((tab) => tab.url === url);
286
+ const matches =
287
+ exact.length > 0 ? exact : tabs.filter((tab) => tab.url.includes(url));
288
+ if (matches.length === 1) {
289
+ return matches[0].targetId;
290
+ }
291
+ const listed = (matches.length > 1 ? matches : tabs)
292
+ .slice(0, MAX_LISTED_TABS)
293
+ .map((tab) => `${tab.targetId} ${tab.url}`)
294
+ .join("; ");
295
+ throw new Error(
296
+ matches.length > 1
297
+ ? `${matches.length} tabs match ${url}; pass the targetId of one: ${listed}`
298
+ : `No tab's URL contains ${url}. Open tabs: ${listed || "none"}`,
299
+ );
300
+ }
@@ -15,7 +15,7 @@ import {
15
15
  import { selectorProperty, sessionWithLimitSchema } from "../tool-schemas.mjs";
16
16
 
17
17
  const DEFAULT_READ_CHARS = 8000;
18
- const DEFAULT_SNAPSHOT_NODES = 100;
18
+ export const DEFAULT_SNAPSHOT_NODES = 100;
19
19
  const MAX_SNAPSHOT_NODES = 500;
20
20
 
21
21
  // The snapshot's nodes are lines already; send them as plain text rather than