mcp-scraper 0.2.19 → 0.2.21
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 +36 -5
- package/dist/bin/api-server.cjs +1072 -349
- package/dist/bin/api-server.cjs.map +1 -1
- package/dist/bin/api-server.js +3 -3
- package/dist/bin/browser-agent-stdio-server.cjs +1274 -75
- package/dist/bin/browser-agent-stdio-server.cjs.map +1 -1
- package/dist/bin/browser-agent-stdio-server.js +4 -2
- package/dist/bin/browser-agent-stdio-server.js.map +1 -1
- package/dist/bin/mcp-scraper-cli.cjs +659 -210
- package/dist/bin/mcp-scraper-cli.cjs.map +1 -1
- package/dist/bin/mcp-scraper-cli.js +108 -9
- package/dist/bin/mcp-scraper-cli.js.map +1 -1
- package/dist/bin/mcp-scraper-combined-stdio-server.cjs +2104 -566
- package/dist/bin/mcp-scraper-combined-stdio-server.cjs.map +1 -1
- package/dist/bin/mcp-scraper-combined-stdio-server.js +7 -4
- package/dist/bin/mcp-scraper-combined-stdio-server.js.map +1 -1
- package/dist/bin/mcp-scraper-install.cjs +3 -3
- package/dist/bin/mcp-scraper-install.cjs.map +1 -1
- package/dist/bin/mcp-scraper-install.js +2 -2
- package/dist/bin/mcp-stdio-server.cjs +726 -387
- package/dist/bin/mcp-stdio-server.cjs.map +1 -1
- package/dist/bin/mcp-stdio-server.js +3 -2
- package/dist/bin/mcp-stdio-server.js.map +1 -1
- package/dist/bin/paa-harvest.cjs +5 -3
- package/dist/bin/paa-harvest.cjs.map +1 -1
- package/dist/bin/paa-harvest.js +5 -2
- package/dist/bin/paa-harvest.js.map +1 -1
- package/dist/{chunk-TL7YTFLH.js → chunk-DBQDG7EH.js} +99 -27
- package/dist/chunk-DBQDG7EH.js.map +1 -0
- package/dist/chunk-DUEW4EOO.js +214 -0
- package/dist/chunk-DUEW4EOO.js.map +1 -0
- package/dist/{chunk-F44RBOJ5.js → chunk-H2R232HK.js} +216 -108
- package/dist/chunk-H2R232HK.js.map +1 -0
- package/dist/chunk-HS6OKELW.js +1786 -0
- package/dist/chunk-HS6OKELW.js.map +1 -0
- package/dist/{chunk-CQTAKXBN.js → chunk-IPW4LFOT.js} +11 -116
- package/dist/chunk-IPW4LFOT.js.map +1 -0
- package/dist/chunk-LFATOGDF.js +111 -0
- package/dist/chunk-LFATOGDF.js.map +1 -0
- package/dist/{chunk-MCBOZ2JF.js → chunk-MLNCKQ2B.js} +3 -3
- package/dist/chunk-MLNCKQ2B.js.map +1 -0
- package/dist/{chunk-4UEVYYDW.js → chunk-O5J7E4VX.js} +394 -57
- package/dist/chunk-O5J7E4VX.js.map +1 -0
- package/dist/{chunk-BSYPATSM.js → chunk-SXTXMFEQ.js} +2 -2
- package/dist/chunk-WN7PBKMV.js +28 -0
- package/dist/chunk-WN7PBKMV.js.map +1 -0
- package/dist/chunk-XKUDVN2E.js +7 -0
- package/dist/chunk-XKUDVN2E.js.map +1 -0
- package/dist/{db-P5X6UQ3E.js → db-BE4JVB3V.js} +8 -2
- package/dist/index.cjs +5 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/{server-CRUSZA2Q.js → server-24XIQ6W6.js} +248 -40
- package/dist/server-24XIQ6W6.js.map +1 -0
- package/dist/{worker-OZSWIS3F.js → worker-SLQ375UG.js} +8 -5
- package/dist/{worker-OZSWIS3F.js.map → worker-SLQ375UG.js.map} +1 -1
- package/docs/adr/0001-in-page-graphql-interception-for-anti-bot-scraping.md +2 -2
- package/docs/mcp-tool-craft-lint.generated.md +51 -0
- package/docs/mcp-tool-manifest.generated.json +1534 -0
- package/docs/specs/local-competitive-audit-spec.md +1 -2
- package/docs/specs/mcp-tool-definition-quality-audit-spec.md +1602 -0
- package/package.json +4 -1
- package/dist/chunk-4UEVYYDW.js.map +0 -1
- package/dist/chunk-76P4DKWR.js +0 -793
- package/dist/chunk-76P4DKWR.js.map +0 -1
- package/dist/chunk-CQTAKXBN.js.map +0 -1
- package/dist/chunk-F44RBOJ5.js.map +0 -1
- package/dist/chunk-MCBOZ2JF.js.map +0 -1
- package/dist/chunk-RU2HZD55.js +0 -7
- package/dist/chunk-RU2HZD55.js.map +0 -1
- package/dist/chunk-TL7YTFLH.js.map +0 -1
- package/dist/server-CRUSZA2Q.js.map +0 -1
- /package/dist/{chunk-BSYPATSM.js.map → chunk-SXTXMFEQ.js.map} +0 -0
- /package/dist/{db-P5X6UQ3E.js.map → db-BE4JVB3V.js.map} +0 -0
|
@@ -0,0 +1,1786 @@
|
|
|
1
|
+
import {
|
|
2
|
+
browserServiceProfileName,
|
|
3
|
+
browserServiceProfileSaveChanges
|
|
4
|
+
} from "./chunk-WN7PBKMV.js";
|
|
5
|
+
import {
|
|
6
|
+
defaultChromeExecutablePath,
|
|
7
|
+
importChromeProfile,
|
|
8
|
+
listLocalChromeProfiles,
|
|
9
|
+
loadImportedBrowserProfile,
|
|
10
|
+
localBrowserModeEnabled,
|
|
11
|
+
syncImportedChromeProfile
|
|
12
|
+
} from "./chunk-DUEW4EOO.js";
|
|
13
|
+
import {
|
|
14
|
+
PACKAGE_VERSION
|
|
15
|
+
} from "./chunk-XKUDVN2E.js";
|
|
16
|
+
|
|
17
|
+
// src/mcp/browser-agent-mcp-server.ts
|
|
18
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
19
|
+
import { mkdirSync, writeFileSync } from "fs";
|
|
20
|
+
import { homedir } from "os";
|
|
21
|
+
import { join as join2 } from "path";
|
|
22
|
+
|
|
23
|
+
// src/services/browser-agent/local-browser-agent-service.ts
|
|
24
|
+
import { randomUUID } from "crypto";
|
|
25
|
+
import { existsSync } from "fs";
|
|
26
|
+
import { basename } from "path";
|
|
27
|
+
import { chromium } from "playwright";
|
|
28
|
+
function directProfileDir() {
|
|
29
|
+
return process.env.MCP_SCRAPER_BROWSER_PROFILE_DIR?.trim() || void 0;
|
|
30
|
+
}
|
|
31
|
+
function configuredProfileName(profile) {
|
|
32
|
+
return profile?.trim() || process.env.MCP_SCRAPER_BROWSER_PROFILE?.trim() || (directProfileDir() ? basename(directProfileDir()) : "default");
|
|
33
|
+
}
|
|
34
|
+
function launchExecutablePath(profile) {
|
|
35
|
+
const configured = process.env.MCP_SCRAPER_BROWSER_EXECUTABLE?.trim() || profile.browserExecutablePath || defaultChromeExecutablePath();
|
|
36
|
+
return configured && existsSync(configured) ? configured : void 0;
|
|
37
|
+
}
|
|
38
|
+
function ignoreDefaultArgsFor(executablePath) {
|
|
39
|
+
if (!executablePath) return void 0;
|
|
40
|
+
if (process.platform !== "darwin") return void 0;
|
|
41
|
+
if (!/Google Chrome\.app\/Contents\/MacOS\/Google Chrome$/.test(executablePath)) return void 0;
|
|
42
|
+
return ["--password-store=basic", "--use-mock-keychain"];
|
|
43
|
+
}
|
|
44
|
+
function normalizeKeyCombo(value) {
|
|
45
|
+
return value.split("+").map((part) => {
|
|
46
|
+
const trimmed = part.trim();
|
|
47
|
+
const lower = trimmed.toLowerCase();
|
|
48
|
+
if (lower === "ctrl" || lower === "control") return "Control";
|
|
49
|
+
if (lower === "cmd" || lower === "command" || lower === "meta") return "Meta";
|
|
50
|
+
if (lower === "option" || lower === "alt") return "Alt";
|
|
51
|
+
if (lower === "return") return "Enter";
|
|
52
|
+
if (lower === "esc") return "Escape";
|
|
53
|
+
if (trimmed.length === 1) return trimmed.toUpperCase();
|
|
54
|
+
return trimmed;
|
|
55
|
+
}).join("+");
|
|
56
|
+
}
|
|
57
|
+
function sessionSummary(session) {
|
|
58
|
+
return {
|
|
59
|
+
session_id: session.sessionId,
|
|
60
|
+
status: session.status,
|
|
61
|
+
label: session.label,
|
|
62
|
+
profile: session.profileName,
|
|
63
|
+
user_data_dir: session.userDataDir,
|
|
64
|
+
created_at: session.createdAt,
|
|
65
|
+
last_action_at: session.lastActionAt,
|
|
66
|
+
closed_at: session.closedAt,
|
|
67
|
+
watch_url: `local://browser-session/${session.sessionId}`,
|
|
68
|
+
url: session.status === "open" ? session.page.url() : null
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
var LocalBrowserAgentService = class {
|
|
72
|
+
contexts = /* @__PURE__ */ new Map();
|
|
73
|
+
sessions = /* @__PURE__ */ new Map();
|
|
74
|
+
async open(input) {
|
|
75
|
+
const profile = await this.resolveProfile(input.profile);
|
|
76
|
+
const contextState = await this.ensureContext(profile);
|
|
77
|
+
const pages = contextState.context.pages();
|
|
78
|
+
const page = pages.length === 1 && !this.hasOpenSessionForContext(contextState.key) ? pages[0] : await contextState.context.newPage();
|
|
79
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
80
|
+
const sessionId = `lbs_${randomUUID().replace(/-/g, "").slice(0, 20)}`;
|
|
81
|
+
const session = {
|
|
82
|
+
sessionId,
|
|
83
|
+
label: input.label ?? null,
|
|
84
|
+
page,
|
|
85
|
+
contextKey: contextState.key,
|
|
86
|
+
profileName: profile.name,
|
|
87
|
+
userDataDir: profile.userDataDir,
|
|
88
|
+
createdAt: now,
|
|
89
|
+
lastActionAt: now,
|
|
90
|
+
closedAt: null,
|
|
91
|
+
status: "open"
|
|
92
|
+
};
|
|
93
|
+
const timeoutSeconds = input.timeout_seconds ?? 600;
|
|
94
|
+
if (timeoutSeconds > 0) {
|
|
95
|
+
session.timeout = setTimeout(() => {
|
|
96
|
+
void this.close(sessionId);
|
|
97
|
+
}, timeoutSeconds * 1e3);
|
|
98
|
+
session.timeout.unref();
|
|
99
|
+
}
|
|
100
|
+
this.sessions.set(sessionId, session);
|
|
101
|
+
if (input.url) await this.goto(sessionId, input.url);
|
|
102
|
+
return {
|
|
103
|
+
session_id: sessionId,
|
|
104
|
+
watch_url: `local://browser-session/${sessionId}`,
|
|
105
|
+
live_view_url: null,
|
|
106
|
+
profile: profile.name,
|
|
107
|
+
user_data_dir: profile.userDataDir,
|
|
108
|
+
url: input.url ?? null,
|
|
109
|
+
mode: "local"
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
async screenshot(sessionId) {
|
|
113
|
+
const session = this.requireOpenSession(sessionId);
|
|
114
|
+
await this.waitForPage(session.page);
|
|
115
|
+
const image = await session.page.screenshot({ type: "png", fullPage: false });
|
|
116
|
+
const page = await this.read(sessionId);
|
|
117
|
+
return {
|
|
118
|
+
...page,
|
|
119
|
+
image_base64: image.toString("base64"),
|
|
120
|
+
mime_type: "image/png"
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
async read(sessionId) {
|
|
124
|
+
const session = this.requireOpenSession(sessionId);
|
|
125
|
+
await this.waitForPage(session.page);
|
|
126
|
+
this.touch(session);
|
|
127
|
+
const title = await session.page.title().catch(() => "");
|
|
128
|
+
const snapshot = await session.page.evaluate(() => {
|
|
129
|
+
const cssEscape = (value) => {
|
|
130
|
+
const escapeFn = globalThis.CSS?.escape;
|
|
131
|
+
return escapeFn ? escapeFn(value) : value.replace(/["\\#.:>+~[\]()]/g, "\\$&");
|
|
132
|
+
};
|
|
133
|
+
const textFor = (element) => {
|
|
134
|
+
const html = element;
|
|
135
|
+
return (element.getAttribute("aria-label") || element.getAttribute("title") || html.placeholder || html.value || html.innerText || element.textContent || "").replace(/\s+/g, " ").trim();
|
|
136
|
+
};
|
|
137
|
+
const selectorFor = (element) => {
|
|
138
|
+
const id = element.id;
|
|
139
|
+
if (id) return `#${cssEscape(id)}`;
|
|
140
|
+
const name = element.getAttribute("name");
|
|
141
|
+
const tag = element.tagName.toLowerCase();
|
|
142
|
+
if (name) return `${tag}[name="${name.replace(/"/g, '\\"')}"]`;
|
|
143
|
+
const parts = [];
|
|
144
|
+
let current = element;
|
|
145
|
+
while (current && current !== document.body && parts.length < 4) {
|
|
146
|
+
const currentTag = current.tagName.toLowerCase();
|
|
147
|
+
const parent = current.parentElement;
|
|
148
|
+
if (!parent) {
|
|
149
|
+
parts.unshift(currentTag);
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
const children = Array.from(parent.children);
|
|
153
|
+
const siblings = children.filter((child) => child.tagName === current.tagName);
|
|
154
|
+
const index = siblings.indexOf(current) + 1;
|
|
155
|
+
parts.unshift(siblings.length > 1 ? `${currentTag}:nth-of-type(${index})` : currentTag);
|
|
156
|
+
current = parent;
|
|
157
|
+
}
|
|
158
|
+
return parts.join(" > ") || tag;
|
|
159
|
+
};
|
|
160
|
+
const candidates = Array.from(document.querySelectorAll(
|
|
161
|
+
'a, button, input, textarea, select, summary, [role], [contenteditable="true"], [tabindex]:not([tabindex="-1"])'
|
|
162
|
+
));
|
|
163
|
+
const elements = candidates.map((element, index) => {
|
|
164
|
+
const rect = element.getBoundingClientRect();
|
|
165
|
+
const style = window.getComputedStyle(element);
|
|
166
|
+
const visible = rect.width > 0 && rect.height > 0 && style.display !== "none" && style.visibility !== "hidden" && Number(style.opacity || "1") > 0;
|
|
167
|
+
if (!visible) return null;
|
|
168
|
+
const label = textFor(element);
|
|
169
|
+
return {
|
|
170
|
+
index,
|
|
171
|
+
tag: element.tagName.toLowerCase(),
|
|
172
|
+
role: element.getAttribute("role") || null,
|
|
173
|
+
text: label.slice(0, 180),
|
|
174
|
+
selector: selectorFor(element),
|
|
175
|
+
x: Math.round(rect.left + rect.width / 2),
|
|
176
|
+
y: Math.round(rect.top + rect.height / 2),
|
|
177
|
+
left: Math.round(rect.left),
|
|
178
|
+
top: Math.round(rect.top),
|
|
179
|
+
width: Math.round(rect.width),
|
|
180
|
+
height: Math.round(rect.height)
|
|
181
|
+
};
|
|
182
|
+
}).filter(Boolean).slice(0, 120);
|
|
183
|
+
return {
|
|
184
|
+
text: (document.body?.innerText || "").replace(/\n{3,}/g, "\n\n").slice(0, 3e4),
|
|
185
|
+
elements,
|
|
186
|
+
viewport: {
|
|
187
|
+
width: window.innerWidth,
|
|
188
|
+
height: window.innerHeight,
|
|
189
|
+
devicePixelRatio: window.devicePixelRatio,
|
|
190
|
+
scrollX: window.scrollX,
|
|
191
|
+
scrollY: window.scrollY
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
});
|
|
195
|
+
return {
|
|
196
|
+
url: session.page.url(),
|
|
197
|
+
title,
|
|
198
|
+
text: snapshot.text,
|
|
199
|
+
elements: snapshot.elements,
|
|
200
|
+
viewport: snapshot.viewport
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
async locate(sessionId, targets) {
|
|
204
|
+
const session = this.requireOpenSession(sessionId);
|
|
205
|
+
this.touch(session);
|
|
206
|
+
const located = [];
|
|
207
|
+
for (const target of targets) {
|
|
208
|
+
try {
|
|
209
|
+
const locator = target.selector ? session.page.locator(target.selector) : session.page.getByText(target.text ?? "", { exact: target.match === "exact" });
|
|
210
|
+
const selected = locator.nth(target.index ?? 0);
|
|
211
|
+
await selected.waitFor({ state: "visible", timeout: 3e3 }).catch(() => void 0);
|
|
212
|
+
const box = await selected.boundingBox();
|
|
213
|
+
const text = await selected.textContent({ timeout: 1e3 }).catch(() => null);
|
|
214
|
+
located.push({
|
|
215
|
+
input: target,
|
|
216
|
+
found: Boolean(box),
|
|
217
|
+
element: box ? {
|
|
218
|
+
left: Math.round(box.x),
|
|
219
|
+
top: Math.round(box.y),
|
|
220
|
+
width: Math.round(box.width),
|
|
221
|
+
height: Math.round(box.height),
|
|
222
|
+
x: Math.round(box.x + box.width / 2),
|
|
223
|
+
y: Math.round(box.y + box.height / 2),
|
|
224
|
+
text: (text ?? "").replace(/\s+/g, " ").trim().slice(0, 220)
|
|
225
|
+
} : null
|
|
226
|
+
});
|
|
227
|
+
} catch (err) {
|
|
228
|
+
located.push({
|
|
229
|
+
input: target,
|
|
230
|
+
found: false,
|
|
231
|
+
element: null,
|
|
232
|
+
error: err instanceof Error ? err.message : String(err)
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const read = await this.read(sessionId);
|
|
237
|
+
return {
|
|
238
|
+
url: read.url,
|
|
239
|
+
title: read.title,
|
|
240
|
+
viewport: read.viewport,
|
|
241
|
+
replay: null,
|
|
242
|
+
targets: located
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
async goto(sessionId, url) {
|
|
246
|
+
const session = this.requireOpenSession(sessionId);
|
|
247
|
+
this.touch(session);
|
|
248
|
+
const response = await session.page.goto(url, { waitUntil: "domcontentloaded", timeout: 45e3 }).catch((err) => {
|
|
249
|
+
throw new Error(err instanceof Error ? err.message : String(err));
|
|
250
|
+
});
|
|
251
|
+
await this.waitForPage(session.page);
|
|
252
|
+
return {
|
|
253
|
+
ok: true,
|
|
254
|
+
url: session.page.url(),
|
|
255
|
+
status: response?.status() ?? null,
|
|
256
|
+
mode: "local"
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
async click(sessionId, input) {
|
|
260
|
+
const session = this.requireOpenSession(sessionId);
|
|
261
|
+
this.touch(session);
|
|
262
|
+
await session.page.mouse.click(input.x, input.y, {
|
|
263
|
+
button: input.button ?? "left",
|
|
264
|
+
clickCount: input.num_clicks ?? 1
|
|
265
|
+
});
|
|
266
|
+
await this.waitForPage(session.page);
|
|
267
|
+
return { ok: true, mode: "local", url: session.page.url() };
|
|
268
|
+
}
|
|
269
|
+
async type(sessionId, input) {
|
|
270
|
+
const session = this.requireOpenSession(sessionId);
|
|
271
|
+
this.touch(session);
|
|
272
|
+
await session.page.keyboard.type(input.text, { delay: input.delay });
|
|
273
|
+
return { ok: true, mode: "local", url: session.page.url() };
|
|
274
|
+
}
|
|
275
|
+
async scroll(sessionId, input) {
|
|
276
|
+
const session = this.requireOpenSession(sessionId);
|
|
277
|
+
this.touch(session);
|
|
278
|
+
if (typeof input.x === "number" && typeof input.y === "number") {
|
|
279
|
+
await session.page.mouse.move(input.x, input.y);
|
|
280
|
+
}
|
|
281
|
+
await session.page.mouse.wheel((input.delta_x ?? 0) * 120, (input.delta_y ?? 5) * 120);
|
|
282
|
+
await session.page.waitForTimeout(150).catch(() => void 0);
|
|
283
|
+
return { ok: true, mode: "local", url: session.page.url() };
|
|
284
|
+
}
|
|
285
|
+
async press(sessionId, input) {
|
|
286
|
+
const session = this.requireOpenSession(sessionId);
|
|
287
|
+
this.touch(session);
|
|
288
|
+
for (const key of input.keys) {
|
|
289
|
+
await session.page.keyboard.press(normalizeKeyCombo(key));
|
|
290
|
+
}
|
|
291
|
+
await this.waitForPage(session.page);
|
|
292
|
+
return { ok: true, mode: "local", url: session.page.url() };
|
|
293
|
+
}
|
|
294
|
+
async close(sessionId) {
|
|
295
|
+
const session = this.sessions.get(sessionId);
|
|
296
|
+
if (!session || session.status === "closed") {
|
|
297
|
+
return { ok: true, closed: true, already_closed: true, mode: "local" };
|
|
298
|
+
}
|
|
299
|
+
if (session.timeout) clearTimeout(session.timeout);
|
|
300
|
+
session.status = "closed";
|
|
301
|
+
session.closedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
302
|
+
await session.page.close().catch(() => void 0);
|
|
303
|
+
await this.closeContextIfUnused(session.contextKey);
|
|
304
|
+
return { ok: true, closed: true, mode: "local" };
|
|
305
|
+
}
|
|
306
|
+
list(includeClosed = false) {
|
|
307
|
+
return Array.from(this.sessions.values()).filter((session) => includeClosed || session.status === "open").map(sessionSummary);
|
|
308
|
+
}
|
|
309
|
+
async resolveProfile(profile) {
|
|
310
|
+
const directDir = directProfileDir();
|
|
311
|
+
const requestedName = configuredProfileName(profile);
|
|
312
|
+
if (directDir && (!profile || requestedName === process.env.MCP_SCRAPER_BROWSER_PROFILE?.trim())) {
|
|
313
|
+
return {
|
|
314
|
+
name: requestedName,
|
|
315
|
+
userDataDir: directDir,
|
|
316
|
+
browserExecutablePath: process.env.MCP_SCRAPER_BROWSER_EXECUTABLE?.trim() || defaultChromeExecutablePath() || null,
|
|
317
|
+
manifest: null
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
const manifest = await loadImportedBrowserProfile(requestedName);
|
|
321
|
+
return {
|
|
322
|
+
name: manifest.name,
|
|
323
|
+
userDataDir: manifest.userDataDir,
|
|
324
|
+
browserExecutablePath: manifest.browserExecutablePath,
|
|
325
|
+
manifest
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
async ensureContext(profile) {
|
|
329
|
+
const existing = this.contexts.get(profile.userDataDir);
|
|
330
|
+
if (existing) return existing;
|
|
331
|
+
const executablePath = launchExecutablePath(profile);
|
|
332
|
+
const context = await chromium.launchPersistentContext(profile.userDataDir, {
|
|
333
|
+
headless: false,
|
|
334
|
+
viewport: { width: 1440, height: 900 },
|
|
335
|
+
acceptDownloads: true,
|
|
336
|
+
...executablePath ? { executablePath } : {},
|
|
337
|
+
...ignoreDefaultArgsFor(executablePath) ? { ignoreDefaultArgs: ignoreDefaultArgsFor(executablePath) } : {}
|
|
338
|
+
});
|
|
339
|
+
const state = { key: profile.userDataDir, context, profile };
|
|
340
|
+
context.on("close", () => {
|
|
341
|
+
this.contexts.delete(profile.userDataDir);
|
|
342
|
+
const closedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
343
|
+
for (const session of this.sessions.values()) {
|
|
344
|
+
if (session.contextKey === profile.userDataDir && session.status === "open") {
|
|
345
|
+
session.status = "closed";
|
|
346
|
+
session.closedAt = closedAt;
|
|
347
|
+
if (session.timeout) clearTimeout(session.timeout);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
this.contexts.set(profile.userDataDir, state);
|
|
352
|
+
return state;
|
|
353
|
+
}
|
|
354
|
+
requireOpenSession(sessionId) {
|
|
355
|
+
const session = this.sessions.get(sessionId);
|
|
356
|
+
if (!session || session.status !== "open") throw new Error(`Local browser session not found or closed: ${sessionId}`);
|
|
357
|
+
return session;
|
|
358
|
+
}
|
|
359
|
+
touch(session) {
|
|
360
|
+
session.lastActionAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
361
|
+
}
|
|
362
|
+
hasOpenSessionForContext(contextKey) {
|
|
363
|
+
return Array.from(this.sessions.values()).some((session) => session.contextKey === contextKey && session.status === "open");
|
|
364
|
+
}
|
|
365
|
+
async closeContextIfUnused(contextKey) {
|
|
366
|
+
if (this.hasOpenSessionForContext(contextKey)) return;
|
|
367
|
+
const state = this.contexts.get(contextKey);
|
|
368
|
+
if (!state) return;
|
|
369
|
+
this.contexts.delete(contextKey);
|
|
370
|
+
await state.context.close().catch(() => void 0);
|
|
371
|
+
}
|
|
372
|
+
async waitForPage(page) {
|
|
373
|
+
await page.waitForLoadState("domcontentloaded", { timeout: 5e3 }).catch(() => void 0);
|
|
374
|
+
await page.waitForTimeout(200).catch(() => void 0);
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
// src/mcp/browser-agent-tool-schemas.ts
|
|
379
|
+
import { z } from "zod";
|
|
380
|
+
var NullableString = z.string().nullable();
|
|
381
|
+
var BrowserRawObject = z.record(z.unknown());
|
|
382
|
+
var BrowserElementOutput = z.record(z.unknown());
|
|
383
|
+
var BrowserBaseOutput = {
|
|
384
|
+
ok: z.boolean().describe("Whether the browser-agent action succeeded."),
|
|
385
|
+
tool: z.string().describe("Browser Agent MCP tool that produced this response."),
|
|
386
|
+
session_id: NullableString.describe("Browser session id when the response is scoped to a session.")
|
|
387
|
+
};
|
|
388
|
+
var BrowserReplayBaseOutput = {
|
|
389
|
+
...BrowserBaseOutput,
|
|
390
|
+
replay_id: NullableString.describe("Replay id when the response is scoped to a replay.")
|
|
391
|
+
};
|
|
392
|
+
var BrowserOpenInputSchema = {
|
|
393
|
+
label: z.string().optional().describe("Optional human label for this session, shown in the watch console."),
|
|
394
|
+
url: z.string().url().optional().describe("Optional URL to navigate to immediately after opening."),
|
|
395
|
+
profile: z.string().optional().describe("Optional saved profile name to load a logged-in session for a site."),
|
|
396
|
+
save_profile_changes: z.boolean().optional().describe("Persist cookies and local storage back to the named profile when the session is closed. Use this for profile setup or intentional auth refreshes; avoid parallel sessions writing to the same profile."),
|
|
397
|
+
timeout_seconds: z.number().int().min(60).max(259200).optional().describe("How long the session may live before auto-termination. Defaults to 600. The browser idles into a zero-cost standby between actions, so a longer timeout is cheap.")
|
|
398
|
+
};
|
|
399
|
+
var BrowserProfileListInputSchema = {
|
|
400
|
+
email: z.string().optional().describe("Optional Chrome account email or displayed profile name to filter for, for example seo@example.com."),
|
|
401
|
+
user_data_dir: z.string().optional().describe("Optional Chrome user data directory. Defaults to the current OS Chrome profile root.")
|
|
402
|
+
};
|
|
403
|
+
var BrowserProfileOnboardInputSchema = {
|
|
404
|
+
email: z.string().optional().describe("Optional local Chrome account email to find and convert into a suggested Kernel profile name."),
|
|
405
|
+
profile: z.string().optional().describe("Optional Kernel browser profile name to create/load. If omitted, email is used to derive one."),
|
|
406
|
+
user_data_dir: z.string().optional().describe("Optional Chrome user data directory for email lookup. Defaults to the current OS Chrome profile root."),
|
|
407
|
+
url: z.string().url().optional().describe("Setup URL to open after creating the browser. Defaults to https://accounts.google.com/."),
|
|
408
|
+
label: z.string().optional().describe("Optional human label for this setup session."),
|
|
409
|
+
timeout_seconds: z.number().int().min(60).max(259200).optional().describe("How long the setup session may live before auto-termination. Defaults to 600.")
|
|
410
|
+
};
|
|
411
|
+
var BrowserProfileImportInputSchema = {
|
|
412
|
+
email: z.string().optional().describe("Optional Chrome account email or displayed profile name to select from Chrome Local State."),
|
|
413
|
+
name: z.string().optional().describe("Name for the managed MCP Scraper browser profile. Defaults to a slug derived from the selected Chrome account."),
|
|
414
|
+
chrome_profile_directory: z.string().optional().describe("Optional Chrome profile directory to import, for example Default or Profile 1."),
|
|
415
|
+
user_data_dir: z.string().optional().describe("Optional source Chrome user data directory. Defaults to the current OS Chrome profile root."),
|
|
416
|
+
output_dir: z.string().optional().describe("Optional managed profile base directory. Defaults to ~/.mcp-scraper/browser-profiles."),
|
|
417
|
+
browser_executable_path: z.string().optional().describe("Optional Chrome executable path for local browser launches. Defaults to the installed Google Chrome path when found."),
|
|
418
|
+
overwrite: z.boolean().default(false).describe("Overwrite an existing managed profile clone with the same name.")
|
|
419
|
+
};
|
|
420
|
+
var BrowserProfileSyncInputSchema = {
|
|
421
|
+
name: z.string().describe("Managed MCP Scraper browser profile name to refresh from its recorded source Chrome profile."),
|
|
422
|
+
output_dir: z.string().optional().describe("Optional managed profile base directory. Defaults to ~/.mcp-scraper/browser-profiles."),
|
|
423
|
+
browser_executable_path: z.string().optional().describe("Optional Chrome executable path to store in the refreshed profile manifest.")
|
|
424
|
+
};
|
|
425
|
+
var BrowserSessionInputSchema = {
|
|
426
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself.")
|
|
427
|
+
};
|
|
428
|
+
var BrowserLocateTargetSchema = z.object({
|
|
429
|
+
name: z.string().optional().describe("Optional label for this target, echoed in the result."),
|
|
430
|
+
selector: z.string().optional().describe('CSS selector for the exact DOM element to locate, for example h1, input[name="q"], or [data-testid="result"].'),
|
|
431
|
+
text: z.string().optional().describe("Visible text to locate when a selector is not known. The tool returns the text range bounds when possible."),
|
|
432
|
+
match: z.enum(["contains", "exact"]).default("contains").describe("How to match text targets. Defaults to contains."),
|
|
433
|
+
index: z.number().int().min(0).default(0).describe("Zero-based match index when multiple elements match.")
|
|
434
|
+
}).refine((value) => Boolean(value.selector || value.text), {
|
|
435
|
+
message: "target requires selector or text"
|
|
436
|
+
});
|
|
437
|
+
var BrowserLocateInputSchema = {
|
|
438
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself."),
|
|
439
|
+
targets: z.array(BrowserLocateTargetSchema).min(1).max(20).describe("DOM targets to locate in the current viewport. Use selectors for exact elements, or text for visible text ranges.")
|
|
440
|
+
};
|
|
441
|
+
var BrowserGotoInputSchema = {
|
|
442
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself."),
|
|
443
|
+
url: z.string().url().describe("URL to navigate the browser to.")
|
|
444
|
+
};
|
|
445
|
+
var BrowserClickInputSchema = {
|
|
446
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself."),
|
|
447
|
+
x: z.number().describe("X coordinate to click, in screenshot pixels. Use only coordinates from the latest browser_screenshot, browser_read, or browser_locate result; do not guess."),
|
|
448
|
+
y: z.number().describe("Y coordinate to click, in screenshot pixels. Use only coordinates from the latest browser_screenshot, browser_read, or browser_locate result; do not guess."),
|
|
449
|
+
button: z.enum(["left", "right", "middle"]).default("left").describe("Mouse button."),
|
|
450
|
+
num_clicks: z.number().int().min(1).max(3).optional().describe("Number of clicks, e.g. 2 for double-click.")
|
|
451
|
+
};
|
|
452
|
+
var BrowserTypeInputSchema = {
|
|
453
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself."),
|
|
454
|
+
text: z.string().describe("Text to type at the current focus. Click a field first to focus it."),
|
|
455
|
+
delay: z.number().int().min(0).max(500).optional().describe("Optional per-keystroke delay in ms for human-like typing.")
|
|
456
|
+
};
|
|
457
|
+
var BrowserScrollInputSchema = {
|
|
458
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself."),
|
|
459
|
+
delta_y: z.number().default(5).describe("Vertical scroll in wheel units. Positive scrolls down, negative up."),
|
|
460
|
+
delta_x: z.number().default(0).describe("Horizontal scroll in wheel units."),
|
|
461
|
+
x: z.number().optional().describe("X position to scroll at. Defaults to screen center."),
|
|
462
|
+
y: z.number().optional().describe("Y position to scroll at. Defaults to screen center.")
|
|
463
|
+
};
|
|
464
|
+
var BrowserPressInputSchema = {
|
|
465
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself."),
|
|
466
|
+
keys: z.array(z.string()).min(1).describe('Keys or combinations to press, e.g. ["Return"], ["Ctrl+a"], ["Ctrl+Shift+Tab"].')
|
|
467
|
+
};
|
|
468
|
+
var BrowserReplayStopInputSchema = {
|
|
469
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself."),
|
|
470
|
+
replay_id: z.string().describe("The replay id returned by browser_replay_start or browser_list_replays. Use only a returned replay_id; do not construct one yourself.")
|
|
471
|
+
};
|
|
472
|
+
var BrowserReplayDownloadInputSchema = {
|
|
473
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself."),
|
|
474
|
+
replay_id: z.string().describe("The replay id returned by browser_replay_start or browser_list_replays. Use only a returned replay_id; do not construct one yourself."),
|
|
475
|
+
filename: z.string().optional().describe("Optional local MP4 filename. Defaults to a timestamped replay filename.")
|
|
476
|
+
};
|
|
477
|
+
var BrowserReplayAnnotationSchema = z.object({
|
|
478
|
+
type: z.enum(["box", "circle", "underline", "arrow", "label"]).default("box").describe("Annotation style to draw over the replay."),
|
|
479
|
+
start_seconds: z.number().min(0).default(0).describe("When the annotation should appear in the replay."),
|
|
480
|
+
end_seconds: z.number().min(0).optional().describe("When the annotation should disappear. Defaults to two seconds after start_seconds."),
|
|
481
|
+
left: z.number().optional().describe("Target left edge in browser screenshot pixels. Use element.left from browser_screenshot/browser_read."),
|
|
482
|
+
top: z.number().optional().describe("Target top edge in browser screenshot pixels. Use element.top from browser_screenshot/browser_read."),
|
|
483
|
+
width: z.number().positive().optional().describe("Target width in browser screenshot pixels. Use element.width from browser_screenshot/browser_read."),
|
|
484
|
+
height: z.number().positive().optional().describe("Target height in browser screenshot pixels. Use element.height from browser_screenshot/browser_read."),
|
|
485
|
+
x: z.number().optional().describe("Point target x coordinate in browser screenshot pixels when no box is available."),
|
|
486
|
+
y: z.number().optional().describe("Point target y coordinate in browser screenshot pixels when no box is available."),
|
|
487
|
+
from_x: z.number().optional().describe("Arrow start x coordinate in browser screenshot pixels. Defaults near the target."),
|
|
488
|
+
from_y: z.number().optional().describe("Arrow start y coordinate in browser screenshot pixels. Defaults near the target."),
|
|
489
|
+
to_x: z.number().optional().describe("Arrow target x coordinate in browser screenshot pixels. Defaults to the target box center."),
|
|
490
|
+
to_y: z.number().optional().describe("Arrow target y coordinate in browser screenshot pixels. Defaults to the target box center."),
|
|
491
|
+
label: z.string().max(120).optional().describe("Optional text callout to render near the annotation."),
|
|
492
|
+
color: z.string().regex(/^#?[0-9a-fA-F]{6}$/).optional().describe("Annotation color as hex, for example #ff3b30."),
|
|
493
|
+
thickness: z.number().min(1).max(24).optional().describe("Stroke thickness in pixels. Defaults to 5.")
|
|
494
|
+
});
|
|
495
|
+
var BrowserReplayMarkInputSchema = {
|
|
496
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself. A replay must already be recording."),
|
|
497
|
+
target: BrowserLocateTargetSchema.describe("The exact DOM element or text range to mark in the current viewport."),
|
|
498
|
+
type: z.enum(["box", "circle", "underline", "arrow"]).default("box").describe("Annotation style to generate. Labels are included on the returned annotation when label is provided."),
|
|
499
|
+
label: z.string().max(120).optional().describe("Optional callout text to render near the target."),
|
|
500
|
+
color: z.string().regex(/^#?[0-9a-fA-F]{6}$/).optional().describe("Annotation color as hex, for example #ff3b30."),
|
|
501
|
+
thickness: z.number().min(1).max(24).optional().describe("Stroke thickness in pixels. Defaults to 5."),
|
|
502
|
+
padding: z.number().min(0).max(80).default(8).describe("Pixels to expand the DOM bounds so the highlight does not touch the text edge."),
|
|
503
|
+
start_offset_seconds: z.number().min(-5).max(10).default(-0.25).describe("Offset from the current replay time. Negative values make the callout appear just before the mark action is captured."),
|
|
504
|
+
duration_seconds: z.number().min(0.5).max(30).default(4).describe("How long the generated annotation should remain visible.")
|
|
505
|
+
};
|
|
506
|
+
var BrowserReplayAnnotateInputSchema = {
|
|
507
|
+
session_id: z.string().describe("The session id returned by browser_open or browser_list_sessions. Use only a returned session_id; do not construct one yourself."),
|
|
508
|
+
replay_id: z.string().describe("The replay id returned by browser_replay_start or browser_list_replays. Use only a returned replay_id; do not construct one yourself."),
|
|
509
|
+
annotations: z.array(BrowserReplayAnnotationSchema).min(1).max(50).describe("Timed overlay annotations to render on the replay. Prefer annotations returned by browser_replay_mark; otherwise use exact DOM bounds from browser_locate/browser_screenshot/browser_read."),
|
|
510
|
+
filename: z.string().optional().describe("Optional output MP4 filename. Defaults to a timestamped annotated replay filename."),
|
|
511
|
+
source_width: z.number().positive().optional().describe("Width of the screenshot coordinate space used for annotations. Defaults to the replay video width."),
|
|
512
|
+
source_height: z.number().positive().optional().describe("Height of the coordinate space used for annotations. When this is a page viewport height smaller than the replay video height, the annotator infers the browser chrome top offset."),
|
|
513
|
+
source_left_offset: z.number().min(0).optional().describe("Optional explicit X offset from annotation coordinates to replay video coordinates. Usually omitted."),
|
|
514
|
+
source_top_offset: z.number().min(0).optional().describe("Optional explicit Y offset from annotation coordinates to replay video coordinates. Usually omitted because browser chrome offset is inferred when source_height is a page viewport height.")
|
|
515
|
+
};
|
|
516
|
+
var BrowserListInputSchema = {
|
|
517
|
+
include_closed: z.boolean().default(false).describe("Include closed sessions in the list.")
|
|
518
|
+
};
|
|
519
|
+
var BrowserOpenOutputSchema = {
|
|
520
|
+
ok: z.boolean(),
|
|
521
|
+
tool: z.literal("browser_open"),
|
|
522
|
+
session_id: z.string().describe("Session id returned by browser_open. Use only this exact value in later browser_* calls; do not construct one yourself."),
|
|
523
|
+
watch_url: z.string().describe("Human watch/takeover URL for this browser session."),
|
|
524
|
+
live_view_url: NullableString.describe("Raw live-view URL from the browser service when available."),
|
|
525
|
+
url: NullableString.describe("Initial URL requested by the caller, when provided."),
|
|
526
|
+
hint: z.string(),
|
|
527
|
+
raw: BrowserRawObject.optional()
|
|
528
|
+
};
|
|
529
|
+
var BrowserProfileListOutputSchema = {
|
|
530
|
+
ok: z.boolean(),
|
|
531
|
+
tool: z.literal("browser_profile_list"),
|
|
532
|
+
session_id: z.null(),
|
|
533
|
+
profiles: z.array(BrowserRawObject),
|
|
534
|
+
count: z.number().int().min(0),
|
|
535
|
+
hint: z.string()
|
|
536
|
+
};
|
|
537
|
+
var BrowserProfileOnboardOutputSchema = {
|
|
538
|
+
ok: z.boolean(),
|
|
539
|
+
tool: z.literal("browser_profile_onboard"),
|
|
540
|
+
session_id: z.string(),
|
|
541
|
+
watch_url: z.string(),
|
|
542
|
+
live_view_url: NullableString,
|
|
543
|
+
profile: z.string(),
|
|
544
|
+
setup_url: z.string(),
|
|
545
|
+
chrome_profile: BrowserRawObject.nullable(),
|
|
546
|
+
next_steps: z.array(z.string()),
|
|
547
|
+
raw: BrowserRawObject.optional()
|
|
548
|
+
};
|
|
549
|
+
var BrowserProfileImportOutputSchema = {
|
|
550
|
+
ok: z.boolean(),
|
|
551
|
+
tool: z.literal("browser_profile_import"),
|
|
552
|
+
session_id: z.null(),
|
|
553
|
+
profile: BrowserRawObject,
|
|
554
|
+
source_profile: BrowserRawObject,
|
|
555
|
+
next_steps: z.array(z.string()),
|
|
556
|
+
hint: z.string()
|
|
557
|
+
};
|
|
558
|
+
var BrowserProfileSyncOutputSchema = {
|
|
559
|
+
ok: z.boolean(),
|
|
560
|
+
tool: z.literal("browser_profile_sync"),
|
|
561
|
+
session_id: z.null(),
|
|
562
|
+
profile: BrowserRawObject,
|
|
563
|
+
source_profile: BrowserRawObject,
|
|
564
|
+
hint: z.string()
|
|
565
|
+
};
|
|
566
|
+
var BrowserScreenshotOutputSchema = {
|
|
567
|
+
...BrowserBaseOutput,
|
|
568
|
+
tool: z.literal("browser_screenshot"),
|
|
569
|
+
url: NullableString,
|
|
570
|
+
title: NullableString,
|
|
571
|
+
text: z.string(),
|
|
572
|
+
elements: z.array(BrowserElementOutput),
|
|
573
|
+
screenshot: z.object({
|
|
574
|
+
mime_type: z.string(),
|
|
575
|
+
inline: z.boolean()
|
|
576
|
+
}).nullable()
|
|
577
|
+
};
|
|
578
|
+
var BrowserReadOutputSchema = {
|
|
579
|
+
...BrowserBaseOutput,
|
|
580
|
+
tool: z.literal("browser_read"),
|
|
581
|
+
url: NullableString,
|
|
582
|
+
title: NullableString,
|
|
583
|
+
text: z.string(),
|
|
584
|
+
elements: z.array(BrowserElementOutput),
|
|
585
|
+
raw: BrowserRawObject.optional()
|
|
586
|
+
};
|
|
587
|
+
var BrowserLocateOutputSchema = {
|
|
588
|
+
...BrowserBaseOutput,
|
|
589
|
+
tool: z.literal("browser_locate"),
|
|
590
|
+
url: NullableString,
|
|
591
|
+
title: NullableString,
|
|
592
|
+
viewport: BrowserRawObject.nullable(),
|
|
593
|
+
replay: BrowserRawObject.nullable(),
|
|
594
|
+
targets: z.array(BrowserRawObject),
|
|
595
|
+
raw: BrowserRawObject.optional()
|
|
596
|
+
};
|
|
597
|
+
var BrowserActionOutputSchema = {
|
|
598
|
+
...BrowserBaseOutput,
|
|
599
|
+
result: BrowserRawObject.describe("Provider action result. Check ok and follow with browser_screenshot/browser_read when page state matters."),
|
|
600
|
+
nextRecommendedTool: z.string().nullable()
|
|
601
|
+
};
|
|
602
|
+
var BrowserReplayStartOutputSchema = {
|
|
603
|
+
...BrowserReplayBaseOutput,
|
|
604
|
+
tool: z.literal("browser_replay_start"),
|
|
605
|
+
view_url: NullableString,
|
|
606
|
+
download_url: NullableString,
|
|
607
|
+
raw: BrowserRawObject.optional()
|
|
608
|
+
};
|
|
609
|
+
var BrowserReplayStopOutputSchema = {
|
|
610
|
+
...BrowserReplayBaseOutput,
|
|
611
|
+
tool: z.literal("browser_replay_stop"),
|
|
612
|
+
view_url: NullableString,
|
|
613
|
+
download_url: NullableString,
|
|
614
|
+
raw: BrowserRawObject.optional()
|
|
615
|
+
};
|
|
616
|
+
var BrowserListReplaysOutputSchema = {
|
|
617
|
+
...BrowserBaseOutput,
|
|
618
|
+
tool: z.literal("browser_list_replays"),
|
|
619
|
+
replays: z.array(BrowserRawObject),
|
|
620
|
+
count: z.number().int().min(0)
|
|
621
|
+
};
|
|
622
|
+
var BrowserReplayDownloadOutputSchema = {
|
|
623
|
+
...BrowserReplayBaseOutput,
|
|
624
|
+
tool: z.literal("browser_replay_download"),
|
|
625
|
+
file_path: NullableString,
|
|
626
|
+
bytes: z.number().int().min(0).nullable(),
|
|
627
|
+
mime_type: NullableString,
|
|
628
|
+
download_url: NullableString
|
|
629
|
+
};
|
|
630
|
+
var BrowserReplayMarkOutputSchema = {
|
|
631
|
+
...BrowserReplayBaseOutput,
|
|
632
|
+
tool: z.literal("browser_replay_mark"),
|
|
633
|
+
annotation: BrowserRawObject,
|
|
634
|
+
source_width: z.number().nullable(),
|
|
635
|
+
source_height: z.number().nullable(),
|
|
636
|
+
target: BrowserRawObject.nullable(),
|
|
637
|
+
hint: z.string()
|
|
638
|
+
};
|
|
639
|
+
var BrowserReplayAnnotateOutputSchema = {
|
|
640
|
+
...BrowserReplayBaseOutput,
|
|
641
|
+
tool: z.literal("browser_replay_annotate"),
|
|
642
|
+
source_file_path: NullableString,
|
|
643
|
+
annotated_file_path: NullableString,
|
|
644
|
+
bytes: z.number().int().min(0).nullable(),
|
|
645
|
+
width: z.number().int().min(0).nullable(),
|
|
646
|
+
height: z.number().int().min(0).nullable(),
|
|
647
|
+
annotation_count: z.number().int().min(0).nullable(),
|
|
648
|
+
mime_type: NullableString
|
|
649
|
+
};
|
|
650
|
+
var BrowserCloseOutputSchema = {
|
|
651
|
+
...BrowserBaseOutput,
|
|
652
|
+
tool: z.literal("browser_close"),
|
|
653
|
+
closed: z.boolean(),
|
|
654
|
+
raw: BrowserRawObject.optional()
|
|
655
|
+
};
|
|
656
|
+
var BrowserListSessionsOutputSchema = {
|
|
657
|
+
ok: z.boolean(),
|
|
658
|
+
tool: z.literal("browser_list_sessions"),
|
|
659
|
+
session_id: z.null(),
|
|
660
|
+
sessions: z.array(BrowserRawObject),
|
|
661
|
+
count: z.number().int().min(0)
|
|
662
|
+
};
|
|
663
|
+
|
|
664
|
+
// src/mcp/replay-annotator.ts
|
|
665
|
+
import { execFile } from "child_process";
|
|
666
|
+
import { mkdtemp, rm, stat, writeFile } from "fs/promises";
|
|
667
|
+
import { tmpdir } from "os";
|
|
668
|
+
import { join } from "path";
|
|
669
|
+
import { promisify } from "util";
|
|
670
|
+
var execFileAsync = promisify(execFile);
|
|
671
|
+
function finiteNumber(value) {
|
|
672
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
673
|
+
}
|
|
674
|
+
function clamp(value, min, max) {
|
|
675
|
+
return Math.min(max, Math.max(min, value));
|
|
676
|
+
}
|
|
677
|
+
function formatAssTime(seconds) {
|
|
678
|
+
const safe = Math.max(0, seconds);
|
|
679
|
+
const hours = Math.floor(safe / 3600);
|
|
680
|
+
const minutes = Math.floor(safe % 3600 / 60);
|
|
681
|
+
const wholeSeconds = Math.floor(safe % 60);
|
|
682
|
+
const centiseconds = Math.floor((safe - Math.floor(safe)) * 100);
|
|
683
|
+
return `${hours}:${String(minutes).padStart(2, "0")}:${String(wholeSeconds).padStart(2, "0")}.${String(centiseconds).padStart(2, "0")}`;
|
|
684
|
+
}
|
|
685
|
+
function cssHexToAssColor(color) {
|
|
686
|
+
const normalized = color?.trim().match(/^#?([0-9a-fA-F]{6})$/)?.[1] ?? "ff3b30";
|
|
687
|
+
const red = normalized.slice(0, 2);
|
|
688
|
+
const green = normalized.slice(2, 4);
|
|
689
|
+
const blue = normalized.slice(4, 6);
|
|
690
|
+
return `&H${blue}${green}${red}&`;
|
|
691
|
+
}
|
|
692
|
+
function escapeAssText(value) {
|
|
693
|
+
return value.replace(/\\/g, "\\\\").replace(/\{/g, "\\{").replace(/\}/g, "\\}").replace(/\r?\n/g, "\\N");
|
|
694
|
+
}
|
|
695
|
+
function resolveCoordinateTransform(video, source, options) {
|
|
696
|
+
const explicitOffsetX = finiteNumber(options.sourceLeftOffset) ? options.sourceLeftOffset : null;
|
|
697
|
+
const explicitOffsetY = finiteNumber(options.sourceTopOffset) ? options.sourceTopOffset : null;
|
|
698
|
+
const inferredTopOffset = explicitOffsetY == null && Math.round(source.width) === Math.round(video.width) && source.height < video.height && video.height - source.height <= 220 ? video.height - source.height : 0;
|
|
699
|
+
const offsetX = explicitOffsetX ?? 0;
|
|
700
|
+
const offsetY = explicitOffsetY ?? inferredTopOffset;
|
|
701
|
+
return {
|
|
702
|
+
scaleX: (video.width - offsetX) / source.width,
|
|
703
|
+
scaleY: (video.height - offsetY) / source.height,
|
|
704
|
+
offsetX,
|
|
705
|
+
offsetY
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
function transformPoint(x, y, transform) {
|
|
709
|
+
return {
|
|
710
|
+
x: x * transform.scaleX + transform.offsetX,
|
|
711
|
+
y: y * transform.scaleY + transform.offsetY
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
function scaleRect(annotation, transform) {
|
|
715
|
+
if (finiteNumber(annotation.left) && finiteNumber(annotation.top) && finiteNumber(annotation.width) && finiteNumber(annotation.height)) {
|
|
716
|
+
const origin = transformPoint(annotation.left, annotation.top, transform);
|
|
717
|
+
return {
|
|
718
|
+
left: origin.x,
|
|
719
|
+
top: origin.y,
|
|
720
|
+
width: annotation.width * transform.scaleX,
|
|
721
|
+
height: annotation.height * transform.scaleY
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
if (finiteNumber(annotation.x) && finiteNumber(annotation.y)) {
|
|
725
|
+
const radius = finiteNumber(annotation.width) ? annotation.width / 2 : 28;
|
|
726
|
+
const point = transformPoint(annotation.x, annotation.y, transform);
|
|
727
|
+
return {
|
|
728
|
+
left: point.x - radius * transform.scaleX,
|
|
729
|
+
top: point.y - radius * transform.scaleY,
|
|
730
|
+
width: radius * 2 * transform.scaleX,
|
|
731
|
+
height: radius * 2 * transform.scaleY
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
throw new Error("annotation needs either left/top/width/height or x/y");
|
|
735
|
+
}
|
|
736
|
+
function rectPath(rect) {
|
|
737
|
+
const x1 = Math.round(rect.left);
|
|
738
|
+
const y1 = Math.round(rect.top);
|
|
739
|
+
const x2 = Math.round(rect.left + rect.width);
|
|
740
|
+
const y2 = Math.round(rect.top + rect.height);
|
|
741
|
+
return `m ${x1} ${y1} l ${x2} ${y1} l ${x2} ${y2} l ${x1} ${y2} l ${x1} ${y1}`;
|
|
742
|
+
}
|
|
743
|
+
function ellipsePath(rect) {
|
|
744
|
+
const cx = rect.left + rect.width / 2;
|
|
745
|
+
const cy = rect.top + rect.height / 2;
|
|
746
|
+
const rx = Math.max(4, rect.width / 2);
|
|
747
|
+
const ry = Math.max(4, rect.height / 2);
|
|
748
|
+
const points = [];
|
|
749
|
+
for (let i = 0; i <= 32; i += 1) {
|
|
750
|
+
const t = Math.PI * 2 * i / 32;
|
|
751
|
+
const x = Math.round(cx + Math.cos(t) * rx);
|
|
752
|
+
const y = Math.round(cy + Math.sin(t) * ry);
|
|
753
|
+
points.push(`${i === 0 ? "m" : "l"} ${x} ${y}`);
|
|
754
|
+
}
|
|
755
|
+
return points.join(" ");
|
|
756
|
+
}
|
|
757
|
+
function filledRectPath(left, top, width, height) {
|
|
758
|
+
return rectPath({ left, top, width, height });
|
|
759
|
+
}
|
|
760
|
+
function arrowPath(fromX, fromY, toX, toY, thickness) {
|
|
761
|
+
const dx = toX - fromX;
|
|
762
|
+
const dy = toY - fromY;
|
|
763
|
+
const length = Math.max(1, Math.hypot(dx, dy));
|
|
764
|
+
const ux = dx / length;
|
|
765
|
+
const uy = dy / length;
|
|
766
|
+
const px = -uy;
|
|
767
|
+
const py = ux;
|
|
768
|
+
const half = thickness / 2;
|
|
769
|
+
const head = Math.max(14, thickness * 4);
|
|
770
|
+
const baseX = toX - ux * head;
|
|
771
|
+
const baseY = toY - uy * head;
|
|
772
|
+
const p1x = Math.round(fromX + px * half);
|
|
773
|
+
const p1y = Math.round(fromY + py * half);
|
|
774
|
+
const p2x = Math.round(baseX + px * half);
|
|
775
|
+
const p2y = Math.round(baseY + py * half);
|
|
776
|
+
const p3x = Math.round(baseX + px * head * 0.55);
|
|
777
|
+
const p3y = Math.round(baseY + py * head * 0.55);
|
|
778
|
+
const p4x = Math.round(toX);
|
|
779
|
+
const p4y = Math.round(toY);
|
|
780
|
+
const p5x = Math.round(baseX - px * head * 0.55);
|
|
781
|
+
const p5y = Math.round(baseY - py * head * 0.55);
|
|
782
|
+
const p6x = Math.round(baseX - px * half);
|
|
783
|
+
const p6y = Math.round(baseY - py * half);
|
|
784
|
+
const p7x = Math.round(fromX - px * half);
|
|
785
|
+
const p7y = Math.round(fromY - py * half);
|
|
786
|
+
return `m ${p1x} ${p1y} l ${p2x} ${p2y} l ${p3x} ${p3y} l ${p4x} ${p4y} l ${p5x} ${p5y} l ${p6x} ${p6y} l ${p7x} ${p7y} l ${p1x} ${p1y}`;
|
|
787
|
+
}
|
|
788
|
+
function eventLine(start, end, body) {
|
|
789
|
+
return `Dialogue: 0,${formatAssTime(start)},${formatAssTime(end)},Default,,0,0,0,,${body}`;
|
|
790
|
+
}
|
|
791
|
+
function labelLine(start, end, x, y, label) {
|
|
792
|
+
const safeX = Math.round(x);
|
|
793
|
+
const safeY = Math.round(y);
|
|
794
|
+
return eventLine(
|
|
795
|
+
start,
|
|
796
|
+
end,
|
|
797
|
+
`{\\an7\\pos(${safeX},${safeY})\\fs30\\bord4\\shad0\\c&HFFFFFF&\\3c&H000000&}${escapeAssText(label)}`
|
|
798
|
+
);
|
|
799
|
+
}
|
|
800
|
+
function shapeLine(start, end, path, color, thickness, filled = false) {
|
|
801
|
+
const fillAlpha = filled ? "&H00&" : "&HFF&";
|
|
802
|
+
const border = filled ? 0 : thickness;
|
|
803
|
+
return eventLine(
|
|
804
|
+
start,
|
|
805
|
+
end,
|
|
806
|
+
`{\\an7\\pos(0,0)\\p1\\bord${border}\\shad0\\1a${fillAlpha}\\1c${color}\\3c${color}}${path}`
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
function buildAssSubtitle(options, video) {
|
|
810
|
+
const source = {
|
|
811
|
+
width: options.sourceWidth && options.sourceWidth > 0 ? options.sourceWidth : video.width,
|
|
812
|
+
height: options.sourceHeight && options.sourceHeight > 0 ? options.sourceHeight : video.height
|
|
813
|
+
};
|
|
814
|
+
const transform = resolveCoordinateTransform(video, source, options);
|
|
815
|
+
const lines = [];
|
|
816
|
+
for (const annotation of options.annotations) {
|
|
817
|
+
const start = Math.max(0, annotation.start_seconds ?? 0);
|
|
818
|
+
const end = annotation.end_seconds && annotation.end_seconds > start ? annotation.end_seconds : start + 2;
|
|
819
|
+
const type = annotation.type ?? "box";
|
|
820
|
+
const color = cssHexToAssColor(annotation.color);
|
|
821
|
+
const thickness = Math.round(clamp(annotation.thickness ?? 5, 2, 24));
|
|
822
|
+
if (type === "label") {
|
|
823
|
+
if (!annotation.label) throw new Error("label annotation needs label");
|
|
824
|
+
const rect2 = scaleRect(annotation, transform);
|
|
825
|
+
lines.push(labelLine(start, end, rect2.left, Math.max(8, rect2.top), annotation.label));
|
|
826
|
+
continue;
|
|
827
|
+
}
|
|
828
|
+
if (type === "arrow") {
|
|
829
|
+
const rect2 = finiteNumber(annotation.to_x) && finiteNumber(annotation.to_y) ? null : scaleRect(annotation, transform);
|
|
830
|
+
const toPoint = finiteNumber(annotation.to_x) && finiteNumber(annotation.to_y) ? transformPoint(annotation.to_x, annotation.to_y, transform) : null;
|
|
831
|
+
const toX = toPoint ? toPoint.x : rect2.left + rect2.width / 2;
|
|
832
|
+
const toY = toPoint ? toPoint.y : rect2.top + rect2.height / 2;
|
|
833
|
+
const fromPoint = finiteNumber(annotation.from_x) && finiteNumber(annotation.from_y) ? transformPoint(annotation.from_x, annotation.from_y, transform) : null;
|
|
834
|
+
const fromX = fromPoint ? fromPoint.x : clamp(toX - 120, 12, video.width - 12);
|
|
835
|
+
const fromY = fromPoint ? fromPoint.y : clamp(toY - 90, 12, video.height - 12);
|
|
836
|
+
lines.push(shapeLine(start, end, arrowPath(fromX, fromY, toX, toY, thickness), color, thickness, true));
|
|
837
|
+
if (annotation.label) lines.push(labelLine(start, end, fromX + 8, Math.max(96, fromY - 36), annotation.label));
|
|
838
|
+
continue;
|
|
839
|
+
}
|
|
840
|
+
const rect = scaleRect(annotation, transform);
|
|
841
|
+
if (type === "underline") {
|
|
842
|
+
const underlineTop = rect.top + rect.height + thickness;
|
|
843
|
+
lines.push(shapeLine(start, end, filledRectPath(rect.left, underlineTop, rect.width, thickness), color, 0, true));
|
|
844
|
+
} else if (type === "circle") {
|
|
845
|
+
lines.push(shapeLine(start, end, ellipsePath(rect), color, thickness));
|
|
846
|
+
} else {
|
|
847
|
+
lines.push(shapeLine(start, end, rectPath(rect), color, thickness));
|
|
848
|
+
}
|
|
849
|
+
if (annotation.label) lines.push(labelLine(start, end, rect.left, Math.max(8, rect.top - 38), annotation.label));
|
|
850
|
+
}
|
|
851
|
+
return [
|
|
852
|
+
"[Script Info]",
|
|
853
|
+
"ScriptType: v4.00+",
|
|
854
|
+
`PlayResX: ${Math.round(video.width)}`,
|
|
855
|
+
`PlayResY: ${Math.round(video.height)}`,
|
|
856
|
+
"ScaledBorderAndShadow: yes",
|
|
857
|
+
"",
|
|
858
|
+
"[V4+ Styles]",
|
|
859
|
+
"Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding",
|
|
860
|
+
"Style: Default,Arial,30,&H00FFFFFF,&H000000FF,&H00000000,&H90000000,0,0,0,0,100,100,0,0,1,2,0,7,0,0,0,1",
|
|
861
|
+
"",
|
|
862
|
+
"[Events]",
|
|
863
|
+
"Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text",
|
|
864
|
+
...lines,
|
|
865
|
+
""
|
|
866
|
+
].join("\n");
|
|
867
|
+
}
|
|
868
|
+
async function videoSize(inputFilePath) {
|
|
869
|
+
const { stdout } = await execFileAsync("ffprobe", [
|
|
870
|
+
"-v",
|
|
871
|
+
"error",
|
|
872
|
+
"-select_streams",
|
|
873
|
+
"v:0",
|
|
874
|
+
"-show_entries",
|
|
875
|
+
"stream=width,height",
|
|
876
|
+
"-of",
|
|
877
|
+
"json",
|
|
878
|
+
inputFilePath
|
|
879
|
+
], { maxBuffer: 1024 * 1024 });
|
|
880
|
+
const parsed = JSON.parse(stdout);
|
|
881
|
+
const stream = parsed.streams?.[0];
|
|
882
|
+
if (!stream?.width || !stream.height) throw new Error("could not read replay video dimensions");
|
|
883
|
+
return { width: stream.width, height: stream.height };
|
|
884
|
+
}
|
|
885
|
+
function ffmpegFilterPath(path) {
|
|
886
|
+
return path.replace(/\\/g, "\\\\").replace(/:/g, "\\:");
|
|
887
|
+
}
|
|
888
|
+
async function annotateReplayVideo(inputFilePath, outputFilePath, options) {
|
|
889
|
+
if (!options.annotations.length) throw new Error("annotations must include at least one item");
|
|
890
|
+
const size = await videoSize(inputFilePath);
|
|
891
|
+
const tmp = await mkdtemp(join(tmpdir(), "mcp-scraper-ass-"));
|
|
892
|
+
const assPath = join(tmp, "annotations.ass");
|
|
893
|
+
try {
|
|
894
|
+
await writeFile(assPath, buildAssSubtitle(options, size), "utf8");
|
|
895
|
+
await execFileAsync("ffmpeg", [
|
|
896
|
+
"-y",
|
|
897
|
+
"-i",
|
|
898
|
+
inputFilePath,
|
|
899
|
+
"-vf",
|
|
900
|
+
`ass=${ffmpegFilterPath(assPath)}`,
|
|
901
|
+
"-c:v",
|
|
902
|
+
"libx264",
|
|
903
|
+
"-pix_fmt",
|
|
904
|
+
"yuv420p",
|
|
905
|
+
"-movflags",
|
|
906
|
+
"+faststart",
|
|
907
|
+
"-c:a",
|
|
908
|
+
"copy",
|
|
909
|
+
outputFilePath
|
|
910
|
+
], { maxBuffer: 1024 * 1024 * 20 });
|
|
911
|
+
const out = await stat(outputFilePath);
|
|
912
|
+
return {
|
|
913
|
+
filePath: outputFilePath,
|
|
914
|
+
bytes: out.size,
|
|
915
|
+
width: size.width,
|
|
916
|
+
height: size.height,
|
|
917
|
+
annotationCount: options.annotations.length
|
|
918
|
+
};
|
|
919
|
+
} finally {
|
|
920
|
+
await rm(tmp, { recursive: true, force: true });
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
// src/mcp/browser-agent-mcp-server.ts
|
|
925
|
+
function structuredResult(value, isError = false) {
|
|
926
|
+
return {
|
|
927
|
+
content: [{ type: "text", text: JSON.stringify(value) }],
|
|
928
|
+
structuredContent: value,
|
|
929
|
+
isError
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
function errorMessage(value) {
|
|
933
|
+
if (value && typeof value === "object") {
|
|
934
|
+
const data = value;
|
|
935
|
+
if (typeof data.error === "string") return data.error;
|
|
936
|
+
if (typeof data.message === "string") return data.message;
|
|
937
|
+
}
|
|
938
|
+
return typeof value === "string" ? value : "Browser Agent request failed";
|
|
939
|
+
}
|
|
940
|
+
function errorResult(tool, value, sessionId = null, replayId = null) {
|
|
941
|
+
return structuredResult({
|
|
942
|
+
ok: false,
|
|
943
|
+
tool,
|
|
944
|
+
session_id: sessionId,
|
|
945
|
+
...replayId !== null ? { replay_id: replayId } : {},
|
|
946
|
+
error: errorMessage(value),
|
|
947
|
+
raw: value && typeof value === "object" ? value : { value }
|
|
948
|
+
}, true);
|
|
949
|
+
}
|
|
950
|
+
function actionResult(tool, sessionId, ok, data, nextRecommendedTool = "browser_screenshot") {
|
|
951
|
+
if (!ok) return errorResult(tool, data, sessionId);
|
|
952
|
+
return structuredResult({
|
|
953
|
+
ok: true,
|
|
954
|
+
tool,
|
|
955
|
+
session_id: sessionId,
|
|
956
|
+
result: data && typeof data === "object" ? data : { value: data },
|
|
957
|
+
nextRecommendedTool
|
|
958
|
+
});
|
|
959
|
+
}
|
|
960
|
+
function outputBaseDir() {
|
|
961
|
+
return process.env.MCP_SCRAPER_OUTPUT_DIR?.trim() || join2(homedir(), "Downloads", "mcp-scraper");
|
|
962
|
+
}
|
|
963
|
+
function safeFilePart(value) {
|
|
964
|
+
return value.replace(/[^a-zA-Z0-9._-]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 120) || "replay";
|
|
965
|
+
}
|
|
966
|
+
function replayFilePath(sessionId, replayId, filename) {
|
|
967
|
+
const requested = filename?.trim();
|
|
968
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
969
|
+
const name = requested ? safeFilePart(requested).replace(/\.mp4$/i, "") : `${stamp}-${safeFilePart(sessionId)}-${safeFilePart(replayId)}`;
|
|
970
|
+
return join2(outputBaseDir(), "browser-replays", `${name}.mp4`);
|
|
971
|
+
}
|
|
972
|
+
function annotatedReplayFilePath(sessionId, replayId, filename) {
|
|
973
|
+
const requested = filename?.trim();
|
|
974
|
+
if (requested) return replayFilePath(sessionId, replayId, requested);
|
|
975
|
+
const stamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
976
|
+
return replayFilePath(sessionId, replayId, `${stamp}-${safeFilePart(sessionId)}-${safeFilePart(replayId)}-annotated`);
|
|
977
|
+
}
|
|
978
|
+
function finiteNumber2(value) {
|
|
979
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
980
|
+
}
|
|
981
|
+
function expandElementBounds(element, viewport, padding) {
|
|
982
|
+
const sourceWidth = finiteNumber2(viewport?.width) && viewport.width > 0 ? viewport.width : 1920;
|
|
983
|
+
const sourceHeight = finiteNumber2(viewport?.height) && viewport.height > 0 ? viewport.height : 1080;
|
|
984
|
+
const left = Math.max(0, element.left - padding);
|
|
985
|
+
const top = Math.max(0, element.top - padding);
|
|
986
|
+
const right = Math.min(sourceWidth, element.left + element.width + padding);
|
|
987
|
+
const bottom = Math.min(sourceHeight, element.top + element.height + padding);
|
|
988
|
+
return {
|
|
989
|
+
left,
|
|
990
|
+
top,
|
|
991
|
+
width: Math.max(1, right - left),
|
|
992
|
+
height: Math.max(1, bottom - top),
|
|
993
|
+
sourceWidth,
|
|
994
|
+
sourceHeight
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
function buildBrowserAgentMcpServer(opts) {
|
|
998
|
+
const server = new McpServer({ name: "browser-agent", version: PACKAGE_VERSION });
|
|
999
|
+
registerBrowserAgentMcpTools(server, opts);
|
|
1000
|
+
return server;
|
|
1001
|
+
}
|
|
1002
|
+
function registerBrowserAgentMcpTools(server, opts) {
|
|
1003
|
+
const baseUrl = opts.baseUrl.replace(/\/$/, "");
|
|
1004
|
+
const consoleBase = (opts.consoleBaseUrl ?? opts.baseUrl).replace(/\/$/, "");
|
|
1005
|
+
const timeoutMs = opts.timeoutMs ?? 9e4;
|
|
1006
|
+
const localBrowser = new LocalBrowserAgentService();
|
|
1007
|
+
async function req(method, path, body) {
|
|
1008
|
+
try {
|
|
1009
|
+
const res = await fetch(`${baseUrl}${path}`, {
|
|
1010
|
+
method,
|
|
1011
|
+
headers: { "Content-Type": "application/json", "x-api-key": opts.apiKey },
|
|
1012
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
1013
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
1014
|
+
});
|
|
1015
|
+
const data = await res.json().catch(() => ({}));
|
|
1016
|
+
return { ok: res.ok, data };
|
|
1017
|
+
} catch (err) {
|
|
1018
|
+
return { ok: false, data: { error: err instanceof Error ? err.message : String(err) } };
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
async function downloadReplay(sessionId, replayId, filename) {
|
|
1022
|
+
const path = `/agent/sessions/${encodeURIComponent(sessionId)}/replays/${encodeURIComponent(replayId)}/download`;
|
|
1023
|
+
try {
|
|
1024
|
+
const res = await fetch(`${baseUrl}${path}`, {
|
|
1025
|
+
method: "GET",
|
|
1026
|
+
headers: { "x-api-key": opts.apiKey },
|
|
1027
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
1028
|
+
});
|
|
1029
|
+
if (!res.ok) {
|
|
1030
|
+
const data = await res.json().catch(async () => ({ error: await res.text().catch(() => `HTTP ${res.status}`) }));
|
|
1031
|
+
return { ok: false, data };
|
|
1032
|
+
}
|
|
1033
|
+
const bytes = Buffer.from(await res.arrayBuffer());
|
|
1034
|
+
const filePath = replayFilePath(sessionId, replayId, filename);
|
|
1035
|
+
mkdirSync(join2(outputBaseDir(), "browser-replays"), { recursive: true });
|
|
1036
|
+
writeFileSync(filePath, bytes);
|
|
1037
|
+
return {
|
|
1038
|
+
ok: true,
|
|
1039
|
+
data: {
|
|
1040
|
+
replay_id: replayId,
|
|
1041
|
+
file_path: filePath,
|
|
1042
|
+
bytes: bytes.length,
|
|
1043
|
+
mime_type: res.headers.get("content-type") ?? "video/mp4",
|
|
1044
|
+
download_url: `${baseUrl}${path}`
|
|
1045
|
+
}
|
|
1046
|
+
};
|
|
1047
|
+
} catch (err) {
|
|
1048
|
+
return { ok: false, data: { error: err instanceof Error ? err.message : String(err) } };
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
const annotations = (title, readOnly = false) => ({
|
|
1052
|
+
title,
|
|
1053
|
+
readOnlyHint: readOnly,
|
|
1054
|
+
destructiveHint: false,
|
|
1055
|
+
idempotentHint: false,
|
|
1056
|
+
openWorldHint: true
|
|
1057
|
+
});
|
|
1058
|
+
function selectChromeProfile(matches, email) {
|
|
1059
|
+
const wanted = email?.trim().toLowerCase();
|
|
1060
|
+
if (!wanted) return matches[0] ?? null;
|
|
1061
|
+
return matches.find((profile) => profile.email.toLowerCase() === wanted) ?? matches[0] ?? null;
|
|
1062
|
+
}
|
|
1063
|
+
function localReplayUnsupported(tool, sessionId = null, replayId = null) {
|
|
1064
|
+
return errorResult(tool, {
|
|
1065
|
+
error: "Local browser mode does not support replay recording yet. Use hosted mode for browser_replay_* tools.",
|
|
1066
|
+
mode: "local"
|
|
1067
|
+
}, sessionId, replayId);
|
|
1068
|
+
}
|
|
1069
|
+
server.registerTool(
|
|
1070
|
+
"browser_profile_list",
|
|
1071
|
+
{
|
|
1072
|
+
title: "List Local Chrome Profiles",
|
|
1073
|
+
description: "List local Chrome profile metadata visible to this MCP server: account email, Chrome profile directory, local path, and a suggested managed browser profile name. This reads Chrome Local State only; it does not read cookies, passwords, browsing history, or copy local browser state. Use browser_profile_import when the user wants MCP Scraper to clone a local Chrome profile for local browser mode.",
|
|
1074
|
+
inputSchema: BrowserProfileListInputSchema,
|
|
1075
|
+
outputSchema: BrowserProfileListOutputSchema,
|
|
1076
|
+
annotations: annotations("List Local Chrome Profiles", true)
|
|
1077
|
+
},
|
|
1078
|
+
async (input) => {
|
|
1079
|
+
try {
|
|
1080
|
+
const profiles = await listLocalChromeProfiles(input.user_data_dir, input.email);
|
|
1081
|
+
return structuredResult({
|
|
1082
|
+
ok: true,
|
|
1083
|
+
tool: "browser_profile_list",
|
|
1084
|
+
session_id: null,
|
|
1085
|
+
profiles,
|
|
1086
|
+
count: profiles.length,
|
|
1087
|
+
hint: "Use browser_profile_import to clone one profile for local mode. Use browser_profile_onboard only for hosted Kernel profile setup."
|
|
1088
|
+
});
|
|
1089
|
+
} catch (err) {
|
|
1090
|
+
return errorResult("browser_profile_list", { error: err instanceof Error ? err.message : String(err) });
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
);
|
|
1094
|
+
server.registerTool(
|
|
1095
|
+
"browser_profile_import",
|
|
1096
|
+
{
|
|
1097
|
+
title: "Import Local Chrome Profile",
|
|
1098
|
+
description: "Clone one local Chrome profile into MCP Scraper managed storage for local browser mode. This copies browser state files such as cookies, history, local storage, session storage, and password database metadata into ~/.mcp-scraper/browser-profiles by default; it skips cache and lock files and never prints cookie or password values. Use this when the user wants browser_open to run locally with their existing logged-in Chrome state.",
|
|
1099
|
+
inputSchema: BrowserProfileImportInputSchema,
|
|
1100
|
+
outputSchema: BrowserProfileImportOutputSchema,
|
|
1101
|
+
annotations: { title: "Import Local Chrome Profile", readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false }
|
|
1102
|
+
},
|
|
1103
|
+
async (input) => {
|
|
1104
|
+
try {
|
|
1105
|
+
const result = await importChromeProfile({
|
|
1106
|
+
email: input.email,
|
|
1107
|
+
name: input.name,
|
|
1108
|
+
chromeProfileDirectory: input.chrome_profile_directory,
|
|
1109
|
+
sourceUserDataDir: input.user_data_dir,
|
|
1110
|
+
outputDir: input.output_dir,
|
|
1111
|
+
overwrite: input.overwrite,
|
|
1112
|
+
browserExecutablePath: input.browser_executable_path
|
|
1113
|
+
});
|
|
1114
|
+
return structuredResult({
|
|
1115
|
+
ok: true,
|
|
1116
|
+
tool: "browser_profile_import",
|
|
1117
|
+
session_id: null,
|
|
1118
|
+
profile: result.profile,
|
|
1119
|
+
source_profile: result.sourceProfile,
|
|
1120
|
+
next_steps: [
|
|
1121
|
+
`Set MCP_SCRAPER_BROWSER_MODE=local in the MCP server config.`,
|
|
1122
|
+
`Set MCP_SCRAPER_BROWSER_PROFILE=${result.profile.name}.`,
|
|
1123
|
+
"Restart the MCP client so it starts the local browser mode process.",
|
|
1124
|
+
"Call browser_open with the same MCP server; it will open a local Chrome window using the cloned profile."
|
|
1125
|
+
],
|
|
1126
|
+
hint: "The clone is local to this machine. Re-run browser_profile_sync after logging into new sites in normal Chrome."
|
|
1127
|
+
});
|
|
1128
|
+
} catch (err) {
|
|
1129
|
+
return errorResult("browser_profile_import", { error: err instanceof Error ? err.message : String(err) });
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
);
|
|
1133
|
+
server.registerTool(
|
|
1134
|
+
"browser_profile_sync",
|
|
1135
|
+
{
|
|
1136
|
+
title: "Sync Local Chrome Profile",
|
|
1137
|
+
description: "Refresh an existing MCP Scraper managed local browser profile from the source Chrome profile recorded in its manifest. This overwrites the managed clone, copies browser state files again, skips cache and lock files, and never prints cookie or password values.",
|
|
1138
|
+
inputSchema: BrowserProfileSyncInputSchema,
|
|
1139
|
+
outputSchema: BrowserProfileSyncOutputSchema,
|
|
1140
|
+
annotations: { title: "Sync Local Chrome Profile", readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false }
|
|
1141
|
+
},
|
|
1142
|
+
async (input) => {
|
|
1143
|
+
try {
|
|
1144
|
+
const result = await syncImportedChromeProfile(input.name, {
|
|
1145
|
+
outputDir: input.output_dir,
|
|
1146
|
+
browserExecutablePath: input.browser_executable_path
|
|
1147
|
+
});
|
|
1148
|
+
return structuredResult({
|
|
1149
|
+
ok: true,
|
|
1150
|
+
tool: "browser_profile_sync",
|
|
1151
|
+
session_id: null,
|
|
1152
|
+
profile: result.profile,
|
|
1153
|
+
source_profile: result.sourceProfile,
|
|
1154
|
+
hint: `Refreshed ${result.profile.name}. Restart local browser sessions that were using this profile.`
|
|
1155
|
+
});
|
|
1156
|
+
} catch (err) {
|
|
1157
|
+
return errorResult("browser_profile_sync", { error: err instanceof Error ? err.message : String(err) });
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
);
|
|
1161
|
+
server.registerTool(
|
|
1162
|
+
"browser_profile_onboard",
|
|
1163
|
+
{
|
|
1164
|
+
title: "Onboard Hosted Browser Profile",
|
|
1165
|
+
description: "Create or load a saved hosted Kernel browser profile and open a live setup browser with profile saving enabled. Use this only for hosted browser mode. If email is provided, the tool reads local Chrome profile metadata only to suggest the Kernel profile name; it does not import local cookies. For local browser mode with real Chrome state, use browser_profile_import instead.",
|
|
1166
|
+
inputSchema: BrowserProfileOnboardInputSchema,
|
|
1167
|
+
outputSchema: BrowserProfileOnboardOutputSchema,
|
|
1168
|
+
annotations: annotations("Onboard Hosted Browser Profile")
|
|
1169
|
+
},
|
|
1170
|
+
async (input) => {
|
|
1171
|
+
let chromeProfile = null;
|
|
1172
|
+
if (input.email) {
|
|
1173
|
+
try {
|
|
1174
|
+
chromeProfile = selectChromeProfile(await listLocalChromeProfiles(input.user_data_dir, input.email), input.email);
|
|
1175
|
+
} catch (err) {
|
|
1176
|
+
return errorResult("browser_profile_onboard", { error: err instanceof Error ? err.message : String(err) });
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
const profile = input.profile?.trim() || chromeProfile?.recommendedKernelProfileName || browserServiceProfileName();
|
|
1180
|
+
if (!profile) {
|
|
1181
|
+
return errorResult("browser_profile_onboard", {
|
|
1182
|
+
error: "profile is required when no email match or BROWSER_AGENT_PROFILE_NAME is available"
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
const setupUrl = input.url ?? "https://accounts.google.com/";
|
|
1186
|
+
const open = await req("POST", "/agent/sessions", {
|
|
1187
|
+
label: input.label ?? `Profile setup: ${profile}`,
|
|
1188
|
+
profile,
|
|
1189
|
+
save_profile_changes: true,
|
|
1190
|
+
timeout_seconds: input.timeout_seconds
|
|
1191
|
+
});
|
|
1192
|
+
if (!open.ok) return errorResult("browser_profile_onboard", open.data);
|
|
1193
|
+
const session = open.data;
|
|
1194
|
+
const goto = await req("POST", `/agent/sessions/${session.session_id}/goto`, { url: setupUrl });
|
|
1195
|
+
if (!goto.ok) return errorResult("browser_profile_onboard", goto.data, session.session_id);
|
|
1196
|
+
return structuredResult({
|
|
1197
|
+
ok: true,
|
|
1198
|
+
tool: "browser_profile_onboard",
|
|
1199
|
+
session_id: session.session_id,
|
|
1200
|
+
watch_url: `${consoleBase}/console/${session.session_id}`,
|
|
1201
|
+
live_view_url: session.live_view_url ?? null,
|
|
1202
|
+
profile,
|
|
1203
|
+
setup_url: setupUrl,
|
|
1204
|
+
chrome_profile: chromeProfile,
|
|
1205
|
+
next_steps: [
|
|
1206
|
+
"Open the watch_url and complete the login manually.",
|
|
1207
|
+
"After login is complete, call browser_close with this session_id to persist cookies and local storage to the Kernel profile.",
|
|
1208
|
+
"Use BROWSER_AGENT_PROFILE_NAME or browser_open.profile with this profile name for future authenticated sessions."
|
|
1209
|
+
],
|
|
1210
|
+
raw: session
|
|
1211
|
+
});
|
|
1212
|
+
}
|
|
1213
|
+
);
|
|
1214
|
+
server.registerTool(
|
|
1215
|
+
"browser_open",
|
|
1216
|
+
{
|
|
1217
|
+
title: "Open Browser Session",
|
|
1218
|
+
description: "Open a browser you can drive. By default this creates a hosted cloud browser with a watch_url; when MCP_SCRAPER_BROWSER_MODE=local it opens a local Google Chrome window against an imported MCP Scraper Chrome profile. Returns a session_id used by all other browser_* tools. In hosted mode, anti-bot stealth and automatic CAPTCHA/Cloudflare solving are on by default. Call browser_close when done.",
|
|
1219
|
+
inputSchema: BrowserOpenInputSchema,
|
|
1220
|
+
outputSchema: BrowserOpenOutputSchema,
|
|
1221
|
+
annotations: annotations("Open Browser Session")
|
|
1222
|
+
},
|
|
1223
|
+
async (input) => {
|
|
1224
|
+
if (localBrowserModeEnabled()) {
|
|
1225
|
+
try {
|
|
1226
|
+
const session2 = await localBrowser.open(input);
|
|
1227
|
+
return structuredResult({
|
|
1228
|
+
ok: true,
|
|
1229
|
+
tool: "browser_open",
|
|
1230
|
+
session_id: session2.session_id,
|
|
1231
|
+
watch_url: session2.watch_url,
|
|
1232
|
+
live_view_url: null,
|
|
1233
|
+
url: input.url ?? null,
|
|
1234
|
+
hint: "Local Chrome is open on this machine. Call browser_screenshot to see the page and click by x,y coordinates.",
|
|
1235
|
+
raw: session2
|
|
1236
|
+
});
|
|
1237
|
+
} catch (err) {
|
|
1238
|
+
return errorResult("browser_open", { error: err instanceof Error ? err.message : String(err), mode: "local" });
|
|
1239
|
+
}
|
|
1240
|
+
}
|
|
1241
|
+
const profile = input.profile ?? browserServiceProfileName();
|
|
1242
|
+
const saveProfileChanges = input.save_profile_changes ?? browserServiceProfileSaveChanges();
|
|
1243
|
+
const open = await req("POST", "/agent/sessions", {
|
|
1244
|
+
label: input.label,
|
|
1245
|
+
...profile ? { profile } : {},
|
|
1246
|
+
...profile && typeof saveProfileChanges === "boolean" ? { save_profile_changes: saveProfileChanges } : {},
|
|
1247
|
+
timeout_seconds: input.timeout_seconds
|
|
1248
|
+
});
|
|
1249
|
+
if (!open.ok) return errorResult("browser_open", open.data);
|
|
1250
|
+
const session = open.data;
|
|
1251
|
+
if (input.url) {
|
|
1252
|
+
await req("POST", `/agent/sessions/${session.session_id}/goto`, { url: input.url });
|
|
1253
|
+
}
|
|
1254
|
+
return structuredResult({
|
|
1255
|
+
ok: true,
|
|
1256
|
+
tool: "browser_open",
|
|
1257
|
+
session_id: session.session_id,
|
|
1258
|
+
watch_url: `${consoleBase}/console/${session.session_id}`,
|
|
1259
|
+
live_view_url: session.live_view_url ?? null,
|
|
1260
|
+
url: input.url ?? null,
|
|
1261
|
+
hint: "Call browser_screenshot to see the page. Click by the x,y of an element from the snapshot.",
|
|
1262
|
+
raw: session
|
|
1263
|
+
});
|
|
1264
|
+
}
|
|
1265
|
+
);
|
|
1266
|
+
server.registerTool(
|
|
1267
|
+
"browser_screenshot",
|
|
1268
|
+
{
|
|
1269
|
+
title: "See Page (Screenshot + Elements)",
|
|
1270
|
+
description: "Capture what the browser currently shows. Returns a screenshot image PLUS a text snapshot listing interactive elements with their center x,y coordinates, the page url and title, and visible text. This is your primary way to perceive the page. Click elements by their listed x,y. If a Cloudflare/CAPTCHA challenge is visible, wait and screenshot again rather than clicking it.",
|
|
1271
|
+
inputSchema: BrowserSessionInputSchema,
|
|
1272
|
+
outputSchema: BrowserScreenshotOutputSchema,
|
|
1273
|
+
annotations: annotations("See Page", true)
|
|
1274
|
+
},
|
|
1275
|
+
async (input) => {
|
|
1276
|
+
if (localBrowserModeEnabled()) {
|
|
1277
|
+
try {
|
|
1278
|
+
const res2 = await localBrowser.screenshot(input.session_id);
|
|
1279
|
+
const content2 = [];
|
|
1280
|
+
if (res2.image_base64) content2.push({ type: "image", data: res2.image_base64, mimeType: res2.mime_type });
|
|
1281
|
+
const structured2 = {
|
|
1282
|
+
ok: true,
|
|
1283
|
+
tool: "browser_screenshot",
|
|
1284
|
+
session_id: input.session_id,
|
|
1285
|
+
url: res2.url ?? null,
|
|
1286
|
+
title: res2.title ?? null,
|
|
1287
|
+
text: res2.text,
|
|
1288
|
+
elements: res2.elements,
|
|
1289
|
+
screenshot: { mime_type: res2.mime_type, inline: true }
|
|
1290
|
+
};
|
|
1291
|
+
content2.push({ type: "text", text: JSON.stringify(structured2) });
|
|
1292
|
+
return { content: content2, structuredContent: structured2 };
|
|
1293
|
+
} catch (err) {
|
|
1294
|
+
return errorResult("browser_screenshot", { error: err instanceof Error ? err.message : String(err), mode: "local" }, input.session_id);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/screenshot`);
|
|
1298
|
+
if (!res.ok) return errorResult("browser_screenshot", res.data, input.session_id);
|
|
1299
|
+
const { image_base64, mime_type, url, title, elements, text } = res.data;
|
|
1300
|
+
const content = [];
|
|
1301
|
+
if (image_base64) content.push({ type: "image", data: image_base64, mimeType: mime_type ?? "image/png" });
|
|
1302
|
+
const structured = {
|
|
1303
|
+
ok: true,
|
|
1304
|
+
tool: "browser_screenshot",
|
|
1305
|
+
session_id: input.session_id,
|
|
1306
|
+
url: url ?? null,
|
|
1307
|
+
title: title ?? null,
|
|
1308
|
+
text: typeof text === "string" ? text : "",
|
|
1309
|
+
elements: Array.isArray(elements) ? elements : [],
|
|
1310
|
+
screenshot: image_base64 ? { mime_type: mime_type ?? "image/png", inline: true } : null
|
|
1311
|
+
};
|
|
1312
|
+
content.push({
|
|
1313
|
+
type: "text",
|
|
1314
|
+
text: JSON.stringify(structured)
|
|
1315
|
+
});
|
|
1316
|
+
return { content, structuredContent: structured };
|
|
1317
|
+
}
|
|
1318
|
+
);
|
|
1319
|
+
server.registerTool(
|
|
1320
|
+
"browser_read",
|
|
1321
|
+
{
|
|
1322
|
+
title: "Read Page Text + Elements",
|
|
1323
|
+
description: "Return the page url, title, visible text, and the list of interactive elements (with x,y) without an image. Cheaper than browser_screenshot when you only need to read content or find a target element to click.",
|
|
1324
|
+
inputSchema: BrowserSessionInputSchema,
|
|
1325
|
+
outputSchema: BrowserReadOutputSchema,
|
|
1326
|
+
annotations: annotations("Read Page", true)
|
|
1327
|
+
},
|
|
1328
|
+
async (input) => {
|
|
1329
|
+
if (localBrowserModeEnabled()) {
|
|
1330
|
+
try {
|
|
1331
|
+
const res2 = await localBrowser.read(input.session_id);
|
|
1332
|
+
return structuredResult({
|
|
1333
|
+
ok: true,
|
|
1334
|
+
tool: "browser_read",
|
|
1335
|
+
session_id: input.session_id,
|
|
1336
|
+
url: res2.url ?? null,
|
|
1337
|
+
title: res2.title ?? null,
|
|
1338
|
+
text: res2.text,
|
|
1339
|
+
elements: res2.elements,
|
|
1340
|
+
raw: res2
|
|
1341
|
+
});
|
|
1342
|
+
} catch (err) {
|
|
1343
|
+
return errorResult("browser_read", { error: err instanceof Error ? err.message : String(err), mode: "local" }, input.session_id);
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/read`);
|
|
1347
|
+
if (!res.ok) return errorResult("browser_read", res.data, input.session_id);
|
|
1348
|
+
return structuredResult({
|
|
1349
|
+
ok: true,
|
|
1350
|
+
tool: "browser_read",
|
|
1351
|
+
session_id: input.session_id,
|
|
1352
|
+
url: res.data?.url ?? null,
|
|
1353
|
+
title: res.data?.title ?? null,
|
|
1354
|
+
text: typeof res.data?.text === "string" ? res.data.text : "",
|
|
1355
|
+
elements: Array.isArray(res.data?.elements) ? res.data.elements : [],
|
|
1356
|
+
raw: res.data
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
);
|
|
1360
|
+
server.registerTool(
|
|
1361
|
+
"browser_locate",
|
|
1362
|
+
{
|
|
1363
|
+
title: "Locate DOM Targets",
|
|
1364
|
+
description: "Locate exact visible DOM elements or text ranges in the current browser viewport and return left/top/width/height bounds in screenshot pixels. Use this before drawing annotations or when a callout must literally circle, box, underline, or point to a real element. Prefer CSS selectors for exact UI elements; use text when selector is unknown. When a replay is actively recording, the result includes replay_elapsed_seconds for timing.",
|
|
1365
|
+
inputSchema: BrowserLocateInputSchema,
|
|
1366
|
+
outputSchema: BrowserLocateOutputSchema,
|
|
1367
|
+
annotations: annotations("Locate DOM Targets", true)
|
|
1368
|
+
},
|
|
1369
|
+
async (input) => {
|
|
1370
|
+
if (localBrowserModeEnabled()) {
|
|
1371
|
+
try {
|
|
1372
|
+
const res2 = await localBrowser.locate(input.session_id, input.targets);
|
|
1373
|
+
return structuredResult({
|
|
1374
|
+
ok: true,
|
|
1375
|
+
tool: "browser_locate",
|
|
1376
|
+
session_id: input.session_id,
|
|
1377
|
+
url: res2.url ?? null,
|
|
1378
|
+
title: res2.title ?? null,
|
|
1379
|
+
viewport: res2.viewport ?? null,
|
|
1380
|
+
replay: null,
|
|
1381
|
+
targets: Array.isArray(res2.targets) ? res2.targets : [],
|
|
1382
|
+
raw: res2
|
|
1383
|
+
});
|
|
1384
|
+
} catch (err) {
|
|
1385
|
+
return errorResult("browser_locate", { error: err instanceof Error ? err.message : String(err), mode: "local" }, input.session_id);
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/locate`, { targets: input.targets });
|
|
1389
|
+
if (!res.ok) return errorResult("browser_locate", res.data, input.session_id);
|
|
1390
|
+
return structuredResult({
|
|
1391
|
+
ok: true,
|
|
1392
|
+
tool: "browser_locate",
|
|
1393
|
+
session_id: input.session_id,
|
|
1394
|
+
url: res.data?.url ?? null,
|
|
1395
|
+
title: res.data?.title ?? null,
|
|
1396
|
+
viewport: res.data?.viewport ?? null,
|
|
1397
|
+
replay: res.data?.replay ?? null,
|
|
1398
|
+
targets: Array.isArray(res.data?.targets) ? res.data.targets : [],
|
|
1399
|
+
raw: res.data
|
|
1400
|
+
});
|
|
1401
|
+
}
|
|
1402
|
+
);
|
|
1403
|
+
server.registerTool(
|
|
1404
|
+
"browser_goto",
|
|
1405
|
+
{
|
|
1406
|
+
title: "Navigate To URL",
|
|
1407
|
+
description: "Navigate an existing browser session to a URL when the user asks you to go to another page or continue browsing in the same session. Use browser_open first if no session exists. Follow with browser_screenshot to see redirects, login walls, CAPTCHA/Cloudflare state, or the loaded page.",
|
|
1408
|
+
inputSchema: BrowserGotoInputSchema,
|
|
1409
|
+
outputSchema: BrowserActionOutputSchema,
|
|
1410
|
+
annotations: annotations("Navigate To URL")
|
|
1411
|
+
},
|
|
1412
|
+
async (input) => {
|
|
1413
|
+
if (localBrowserModeEnabled()) {
|
|
1414
|
+
try {
|
|
1415
|
+
const res2 = await localBrowser.goto(input.session_id, input.url);
|
|
1416
|
+
return actionResult("browser_goto", input.session_id, true, res2, "browser_screenshot");
|
|
1417
|
+
} catch (err) {
|
|
1418
|
+
return errorResult("browser_goto", { error: err instanceof Error ? err.message : String(err), mode: "local" }, input.session_id);
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/goto`, { url: input.url });
|
|
1422
|
+
return actionResult("browser_goto", input.session_id, res.ok, res.data, "browser_screenshot");
|
|
1423
|
+
}
|
|
1424
|
+
);
|
|
1425
|
+
server.registerTool(
|
|
1426
|
+
"browser_click",
|
|
1427
|
+
{
|
|
1428
|
+
title: "Click",
|
|
1429
|
+
description: "Click a visible page target using screenshot pixel coordinates. Use this when the user asks you to press a button, open a menu, choose a result, or activate a visible UI element. Use x/y only from the latest browser_screenshot, browser_read, or browser_locate result; do not guess coordinates. Follow with browser_screenshot when the click may change the page.",
|
|
1430
|
+
inputSchema: BrowserClickInputSchema,
|
|
1431
|
+
outputSchema: BrowserActionOutputSchema,
|
|
1432
|
+
annotations: annotations("Click")
|
|
1433
|
+
},
|
|
1434
|
+
async (input) => {
|
|
1435
|
+
if (localBrowserModeEnabled()) {
|
|
1436
|
+
try {
|
|
1437
|
+
const res2 = await localBrowser.click(input.session_id, input);
|
|
1438
|
+
return actionResult("browser_click", input.session_id, true, res2, "browser_screenshot");
|
|
1439
|
+
} catch (err) {
|
|
1440
|
+
return errorResult("browser_click", { error: err instanceof Error ? err.message : String(err), mode: "local" }, input.session_id);
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/click`, {
|
|
1444
|
+
x: input.x,
|
|
1445
|
+
y: input.y,
|
|
1446
|
+
button: input.button,
|
|
1447
|
+
num_clicks: input.num_clicks
|
|
1448
|
+
});
|
|
1449
|
+
return actionResult("browser_click", input.session_id, res.ok, res.data, "browser_screenshot");
|
|
1450
|
+
}
|
|
1451
|
+
);
|
|
1452
|
+
server.registerTool(
|
|
1453
|
+
"browser_type",
|
|
1454
|
+
{
|
|
1455
|
+
title: "Type Text",
|
|
1456
|
+
description: 'Type text into the currently focused browser field. Use this when the user asks you to fill a search box, form field, login field, or editable text area. Click or Tab to the field first if focus is uncertain. Use browser_press with ["Return"] to submit, and browser_screenshot afterward when page state matters.',
|
|
1457
|
+
inputSchema: BrowserTypeInputSchema,
|
|
1458
|
+
outputSchema: BrowserActionOutputSchema,
|
|
1459
|
+
annotations: annotations("Type Text")
|
|
1460
|
+
},
|
|
1461
|
+
async (input) => {
|
|
1462
|
+
if (localBrowserModeEnabled()) {
|
|
1463
|
+
try {
|
|
1464
|
+
const res2 = await localBrowser.type(input.session_id, input);
|
|
1465
|
+
return actionResult("browser_type", input.session_id, true, res2, "browser_screenshot");
|
|
1466
|
+
} catch (err) {
|
|
1467
|
+
return errorResult("browser_type", { error: err instanceof Error ? err.message : String(err), mode: "local" }, input.session_id);
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/type`, { text: input.text, delay: input.delay });
|
|
1471
|
+
return actionResult("browser_type", input.session_id, res.ok, res.data, "browser_screenshot");
|
|
1472
|
+
}
|
|
1473
|
+
);
|
|
1474
|
+
server.registerTool(
|
|
1475
|
+
"browser_scroll",
|
|
1476
|
+
{
|
|
1477
|
+
title: "Scroll",
|
|
1478
|
+
description: "Scroll the page to reveal more content before reading, clicking, or locating elements. Positive delta_y scrolls down; negative delta_y scrolls up. Follow with browser_screenshot or browser_read to inspect newly revealed content.",
|
|
1479
|
+
inputSchema: BrowserScrollInputSchema,
|
|
1480
|
+
outputSchema: BrowserActionOutputSchema,
|
|
1481
|
+
annotations: annotations("Scroll")
|
|
1482
|
+
},
|
|
1483
|
+
async (input) => {
|
|
1484
|
+
if (localBrowserModeEnabled()) {
|
|
1485
|
+
try {
|
|
1486
|
+
const res2 = await localBrowser.scroll(input.session_id, input);
|
|
1487
|
+
return actionResult("browser_scroll", input.session_id, true, res2, "browser_screenshot");
|
|
1488
|
+
} catch (err) {
|
|
1489
|
+
return errorResult("browser_scroll", { error: err instanceof Error ? err.message : String(err), mode: "local" }, input.session_id);
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/scroll`, {
|
|
1493
|
+
delta_y: input.delta_y,
|
|
1494
|
+
delta_x: input.delta_x,
|
|
1495
|
+
x: input.x,
|
|
1496
|
+
y: input.y
|
|
1497
|
+
});
|
|
1498
|
+
return actionResult("browser_scroll", input.session_id, res.ok, res.data, "browser_screenshot");
|
|
1499
|
+
}
|
|
1500
|
+
);
|
|
1501
|
+
server.registerTool(
|
|
1502
|
+
"browser_press",
|
|
1503
|
+
{
|
|
1504
|
+
title: "Press Keys",
|
|
1505
|
+
description: 'Press keyboard keys or combinations in the active browser session. Use this for submit, Escape, Tab navigation, select-all, or keyboard shortcuts. Examples: ["Return"], ["Escape"], ["Tab"], ["Ctrl+a"], ["Ctrl+Shift+Tab"]. Use browser_type for text entry and browser_screenshot after keypresses that may change the page.',
|
|
1506
|
+
inputSchema: BrowserPressInputSchema,
|
|
1507
|
+
outputSchema: BrowserActionOutputSchema,
|
|
1508
|
+
annotations: annotations("Press Keys")
|
|
1509
|
+
},
|
|
1510
|
+
async (input) => {
|
|
1511
|
+
if (localBrowserModeEnabled()) {
|
|
1512
|
+
try {
|
|
1513
|
+
const res2 = await localBrowser.press(input.session_id, input);
|
|
1514
|
+
return actionResult("browser_press", input.session_id, true, res2, "browser_screenshot");
|
|
1515
|
+
} catch (err) {
|
|
1516
|
+
return errorResult("browser_press", { error: err instanceof Error ? err.message : String(err), mode: "local" }, input.session_id);
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/press`, { keys: input.keys });
|
|
1520
|
+
return actionResult("browser_press", input.session_id, res.ok, res.data, "browser_screenshot");
|
|
1521
|
+
}
|
|
1522
|
+
);
|
|
1523
|
+
server.registerTool(
|
|
1524
|
+
"browser_replay_start",
|
|
1525
|
+
{
|
|
1526
|
+
title: "Start Recording",
|
|
1527
|
+
description: "Start recording an MP4 replay of the session. Returns replay_id, view_url when available, and a download_url. Use to capture a task for later review; stop with browser_replay_stop.",
|
|
1528
|
+
inputSchema: BrowserSessionInputSchema,
|
|
1529
|
+
outputSchema: BrowserReplayStartOutputSchema,
|
|
1530
|
+
annotations: annotations("Start Recording")
|
|
1531
|
+
},
|
|
1532
|
+
async (input) => {
|
|
1533
|
+
if (localBrowserModeEnabled()) return localReplayUnsupported("browser_replay_start", input.session_id);
|
|
1534
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/replay/start`);
|
|
1535
|
+
if (!res.ok) return errorResult("browser_replay_start", res.data, input.session_id);
|
|
1536
|
+
return structuredResult({
|
|
1537
|
+
ok: true,
|
|
1538
|
+
tool: "browser_replay_start",
|
|
1539
|
+
session_id: input.session_id,
|
|
1540
|
+
replay_id: res.data?.replay_id ?? res.data?.replayId ?? null,
|
|
1541
|
+
view_url: res.data?.view_url ?? res.data?.viewUrl ?? null,
|
|
1542
|
+
download_url: res.data?.download_url ?? res.data?.downloadUrl ?? null,
|
|
1543
|
+
raw: res.data
|
|
1544
|
+
});
|
|
1545
|
+
}
|
|
1546
|
+
);
|
|
1547
|
+
server.registerTool(
|
|
1548
|
+
"browser_replay_stop",
|
|
1549
|
+
{
|
|
1550
|
+
title: "Stop Recording",
|
|
1551
|
+
description: "Stop a replay recording and expose the final view_url and download_url. Use browser_replay_download to save the MP4 locally.",
|
|
1552
|
+
inputSchema: BrowserReplayStopInputSchema,
|
|
1553
|
+
outputSchema: BrowserReplayStopOutputSchema,
|
|
1554
|
+
annotations: annotations("Stop Recording")
|
|
1555
|
+
},
|
|
1556
|
+
async (input) => {
|
|
1557
|
+
if (localBrowserModeEnabled()) return localReplayUnsupported("browser_replay_stop", input.session_id, input.replay_id);
|
|
1558
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/replay/stop`, { replay_id: input.replay_id });
|
|
1559
|
+
if (!res.ok) return errorResult("browser_replay_stop", res.data, input.session_id, input.replay_id);
|
|
1560
|
+
return structuredResult({
|
|
1561
|
+
ok: true,
|
|
1562
|
+
tool: "browser_replay_stop",
|
|
1563
|
+
session_id: input.session_id,
|
|
1564
|
+
replay_id: res.data?.replay_id ?? res.data?.replayId ?? input.replay_id,
|
|
1565
|
+
view_url: res.data?.view_url ?? res.data?.viewUrl ?? null,
|
|
1566
|
+
download_url: res.data?.download_url ?? res.data?.downloadUrl ?? null,
|
|
1567
|
+
raw: res.data
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1570
|
+
);
|
|
1571
|
+
server.registerTool(
|
|
1572
|
+
"browser_list_replays",
|
|
1573
|
+
{
|
|
1574
|
+
title: "List Replay Videos",
|
|
1575
|
+
description: "List replay recordings for a browser session, including final view_url and authenticated download_url values when available.",
|
|
1576
|
+
inputSchema: BrowserSessionInputSchema,
|
|
1577
|
+
outputSchema: BrowserListReplaysOutputSchema,
|
|
1578
|
+
annotations: annotations("List Replay Videos", true)
|
|
1579
|
+
},
|
|
1580
|
+
async (input) => {
|
|
1581
|
+
if (localBrowserModeEnabled()) return localReplayUnsupported("browser_list_replays", input.session_id);
|
|
1582
|
+
const res = await req("GET", `/agent/sessions/${input.session_id}/replays`);
|
|
1583
|
+
if (!res.ok) return errorResult("browser_list_replays", res.data, input.session_id);
|
|
1584
|
+
const replays = Array.isArray(res.data?.replays) ? res.data.replays : [];
|
|
1585
|
+
return structuredResult({
|
|
1586
|
+
ok: true,
|
|
1587
|
+
tool: "browser_list_replays",
|
|
1588
|
+
session_id: input.session_id,
|
|
1589
|
+
replays,
|
|
1590
|
+
count: replays.length
|
|
1591
|
+
});
|
|
1592
|
+
}
|
|
1593
|
+
);
|
|
1594
|
+
server.registerTool(
|
|
1595
|
+
"browser_replay_download",
|
|
1596
|
+
{
|
|
1597
|
+
title: "Download Replay MP4",
|
|
1598
|
+
description: "Download a replay recording through MCP Scraper and save the MP4 locally under MCP_SCRAPER_OUTPUT_DIR/browser-replays. Use after browser_replay_stop or browser_list_replays.",
|
|
1599
|
+
inputSchema: BrowserReplayDownloadInputSchema,
|
|
1600
|
+
outputSchema: BrowserReplayDownloadOutputSchema,
|
|
1601
|
+
annotations: annotations("Download Replay MP4")
|
|
1602
|
+
},
|
|
1603
|
+
async (input) => {
|
|
1604
|
+
if (localBrowserModeEnabled()) return localReplayUnsupported("browser_replay_download", input.session_id, input.replay_id);
|
|
1605
|
+
const res = await downloadReplay(input.session_id, input.replay_id, input.filename);
|
|
1606
|
+
if (!res.ok) return errorResult("browser_replay_download", res.data, input.session_id, input.replay_id);
|
|
1607
|
+
return structuredResult({
|
|
1608
|
+
ok: true,
|
|
1609
|
+
tool: "browser_replay_download",
|
|
1610
|
+
session_id: input.session_id,
|
|
1611
|
+
replay_id: input.replay_id,
|
|
1612
|
+
file_path: res.data?.file_path ?? null,
|
|
1613
|
+
bytes: typeof res.data?.bytes === "number" ? res.data.bytes : null,
|
|
1614
|
+
mime_type: res.data?.mime_type ?? null,
|
|
1615
|
+
download_url: res.data?.download_url ?? null
|
|
1616
|
+
});
|
|
1617
|
+
}
|
|
1618
|
+
);
|
|
1619
|
+
server.registerTool(
|
|
1620
|
+
"browser_replay_mark",
|
|
1621
|
+
{
|
|
1622
|
+
title: "Mark Replay Annotation",
|
|
1623
|
+
description: "While a replay is actively recording, locate one exact DOM target and return a ready-to-use annotation object with DOM bounds and replay-relative timing. Use this instead of guessing start_seconds or drawing rough rectangles. Workflow: start browser_replay_start, navigate until the target is visible and stable, call browser_replay_mark for each callout, then stop the replay and pass the returned annotations to browser_replay_annotate.",
|
|
1624
|
+
inputSchema: BrowserReplayMarkInputSchema,
|
|
1625
|
+
outputSchema: BrowserReplayMarkOutputSchema,
|
|
1626
|
+
annotations: annotations("Mark Replay Annotation", true)
|
|
1627
|
+
},
|
|
1628
|
+
async (input) => {
|
|
1629
|
+
if (localBrowserModeEnabled()) return localReplayUnsupported("browser_replay_mark", input.session_id);
|
|
1630
|
+
const res = await req("POST", `/agent/sessions/${input.session_id}/locate`, { targets: [input.target] });
|
|
1631
|
+
if (!res.ok) return errorResult("browser_replay_mark", res.data, input.session_id);
|
|
1632
|
+
const target = res.data?.targets?.[0];
|
|
1633
|
+
const element = target?.element;
|
|
1634
|
+
const elapsed = res.data?.replay?.replay_elapsed_seconds;
|
|
1635
|
+
if (!target?.found || !element) {
|
|
1636
|
+
return errorResult("browser_replay_mark", { error: target?.error ?? "target not found in current viewport", target }, input.session_id);
|
|
1637
|
+
}
|
|
1638
|
+
if (!finiteNumber2(elapsed)) {
|
|
1639
|
+
return errorResult("browser_replay_mark", { error: "no active replay clock found; call browser_replay_start before browser_replay_mark" }, input.session_id);
|
|
1640
|
+
}
|
|
1641
|
+
const padded = expandElementBounds(element, res.data?.viewport, input.padding ?? 8);
|
|
1642
|
+
const start = Math.max(0, elapsed + (input.start_offset_seconds ?? -0.25));
|
|
1643
|
+
const duration = input.duration_seconds ?? 4;
|
|
1644
|
+
const annotation = {
|
|
1645
|
+
type: input.type ?? "box",
|
|
1646
|
+
start_seconds: Number(start.toFixed(3)),
|
|
1647
|
+
end_seconds: Number((start + duration).toFixed(3)),
|
|
1648
|
+
left: Math.round(padded.left),
|
|
1649
|
+
top: Math.round(padded.top),
|
|
1650
|
+
width: Math.round(padded.width),
|
|
1651
|
+
height: Math.round(padded.height),
|
|
1652
|
+
...input.label ? { label: input.label } : {},
|
|
1653
|
+
...input.color ? { color: input.color } : {},
|
|
1654
|
+
...input.thickness ? { thickness: input.thickness } : {}
|
|
1655
|
+
};
|
|
1656
|
+
return structuredResult({
|
|
1657
|
+
ok: true,
|
|
1658
|
+
tool: "browser_replay_mark",
|
|
1659
|
+
session_id: input.session_id,
|
|
1660
|
+
replay_id: res.data?.replay?.replay_id ?? res.data?.replay?.replayId ?? null,
|
|
1661
|
+
annotation,
|
|
1662
|
+
source_width: padded.sourceWidth,
|
|
1663
|
+
source_height: padded.sourceHeight,
|
|
1664
|
+
replay: res.data.replay,
|
|
1665
|
+
target,
|
|
1666
|
+
hint: "Append annotation to your annotations array. Use source_width/source_height with browser_replay_annotate."
|
|
1667
|
+
});
|
|
1668
|
+
}
|
|
1669
|
+
);
|
|
1670
|
+
server.registerTool(
|
|
1671
|
+
"browser_replay_annotate",
|
|
1672
|
+
{
|
|
1673
|
+
title: "Annotate Replay MP4",
|
|
1674
|
+
description: "Download a browser replay MP4, render visual annotations over it, and save a new annotated MP4 locally. Use this after browser_replay_stop when the user wants proof videos with circles, boxes, arrows, underlines, or labels. For accurate timing and placement, prefer annotations returned by browser_replay_mark while the replay is recording; otherwise use exact left/top/width/height bounds from browser_locate. If the replay video size differs from the screenshot coordinate space, pass source_width and source_height.",
|
|
1675
|
+
inputSchema: BrowserReplayAnnotateInputSchema,
|
|
1676
|
+
outputSchema: BrowserReplayAnnotateOutputSchema,
|
|
1677
|
+
annotations: annotations("Annotate Replay MP4")
|
|
1678
|
+
},
|
|
1679
|
+
async (input) => {
|
|
1680
|
+
if (localBrowserModeEnabled()) return localReplayUnsupported("browser_replay_annotate", input.session_id, input.replay_id);
|
|
1681
|
+
const sourceName = input.filename ? `${input.filename}-source` : void 0;
|
|
1682
|
+
const downloaded = await downloadReplay(input.session_id, input.replay_id, sourceName);
|
|
1683
|
+
if (!downloaded.ok) return errorResult("browser_replay_annotate", downloaded.data, input.session_id, input.replay_id);
|
|
1684
|
+
try {
|
|
1685
|
+
const sourcePath = String(downloaded.data.file_path);
|
|
1686
|
+
const outputPath = annotatedReplayFilePath(input.session_id, input.replay_id, input.filename);
|
|
1687
|
+
mkdirSync(join2(outputBaseDir(), "browser-replays"), { recursive: true });
|
|
1688
|
+
const result = await annotateReplayVideo(sourcePath, outputPath, {
|
|
1689
|
+
annotations: input.annotations,
|
|
1690
|
+
sourceWidth: input.source_width,
|
|
1691
|
+
sourceHeight: input.source_height,
|
|
1692
|
+
sourceLeftOffset: input.source_left_offset,
|
|
1693
|
+
sourceTopOffset: input.source_top_offset
|
|
1694
|
+
});
|
|
1695
|
+
return structuredResult({
|
|
1696
|
+
ok: true,
|
|
1697
|
+
tool: "browser_replay_annotate",
|
|
1698
|
+
session_id: input.session_id,
|
|
1699
|
+
replay_id: input.replay_id,
|
|
1700
|
+
source_file_path: sourcePath,
|
|
1701
|
+
annotated_file_path: result.filePath,
|
|
1702
|
+
bytes: result.bytes,
|
|
1703
|
+
width: result.width,
|
|
1704
|
+
height: result.height,
|
|
1705
|
+
annotation_count: result.annotationCount,
|
|
1706
|
+
mime_type: "video/mp4"
|
|
1707
|
+
});
|
|
1708
|
+
} catch (err) {
|
|
1709
|
+
return errorResult("browser_replay_annotate", { error: err instanceof Error ? err.message : String(err) }, input.session_id, input.replay_id);
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
);
|
|
1713
|
+
server.registerTool(
|
|
1714
|
+
"browser_close",
|
|
1715
|
+
{
|
|
1716
|
+
title: "Close Browser Session",
|
|
1717
|
+
description: "Close and release a browser session when the task is done, when the user asks to stop the browser, or when active browser billing should end. Use browser_list_sessions first if you need to recover the session_id.",
|
|
1718
|
+
inputSchema: BrowserSessionInputSchema,
|
|
1719
|
+
outputSchema: BrowserCloseOutputSchema,
|
|
1720
|
+
annotations: { title: "Close Browser Session", readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false }
|
|
1721
|
+
},
|
|
1722
|
+
async (input) => {
|
|
1723
|
+
if (localBrowserModeEnabled()) {
|
|
1724
|
+
try {
|
|
1725
|
+
const res2 = await localBrowser.close(input.session_id);
|
|
1726
|
+
return structuredResult({
|
|
1727
|
+
ok: true,
|
|
1728
|
+
tool: "browser_close",
|
|
1729
|
+
session_id: input.session_id,
|
|
1730
|
+
closed: true,
|
|
1731
|
+
raw: res2
|
|
1732
|
+
});
|
|
1733
|
+
} catch (err) {
|
|
1734
|
+
return errorResult("browser_close", { error: err instanceof Error ? err.message : String(err), mode: "local" }, input.session_id);
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
const res = await req("DELETE", `/agent/sessions/${input.session_id}`);
|
|
1738
|
+
if (!res.ok) return errorResult("browser_close", res.data, input.session_id);
|
|
1739
|
+
return structuredResult({
|
|
1740
|
+
ok: true,
|
|
1741
|
+
tool: "browser_close",
|
|
1742
|
+
session_id: input.session_id,
|
|
1743
|
+
closed: true,
|
|
1744
|
+
raw: res.data
|
|
1745
|
+
});
|
|
1746
|
+
}
|
|
1747
|
+
);
|
|
1748
|
+
server.registerTool(
|
|
1749
|
+
"browser_list_sessions",
|
|
1750
|
+
{
|
|
1751
|
+
title: "List Browser Sessions",
|
|
1752
|
+
description: "List browser sessions and their status, with a watch_url for each. Use this to recover a session_id, find an active browser, or decide which session to close. Use browser_open to create a new session.",
|
|
1753
|
+
inputSchema: BrowserListInputSchema,
|
|
1754
|
+
outputSchema: BrowserListSessionsOutputSchema,
|
|
1755
|
+
annotations: annotations("List Browser Sessions", true)
|
|
1756
|
+
},
|
|
1757
|
+
async (input) => {
|
|
1758
|
+
if (localBrowserModeEnabled()) {
|
|
1759
|
+
const sessions2 = localBrowser.list(input.include_closed);
|
|
1760
|
+
return structuredResult({
|
|
1761
|
+
ok: true,
|
|
1762
|
+
tool: "browser_list_sessions",
|
|
1763
|
+
session_id: null,
|
|
1764
|
+
sessions: sessions2,
|
|
1765
|
+
count: sessions2.length
|
|
1766
|
+
});
|
|
1767
|
+
}
|
|
1768
|
+
const res = await req("GET", `/agent/sessions${input.include_closed ? "?all=1" : ""}`);
|
|
1769
|
+
if (!res.ok) return errorResult("browser_list_sessions", res.data);
|
|
1770
|
+
const sessions = (res.data.sessions ?? []).map((s) => ({ ...s, watch_url: `${consoleBase}/console/${s.session_id}` }));
|
|
1771
|
+
return structuredResult({
|
|
1772
|
+
ok: true,
|
|
1773
|
+
tool: "browser_list_sessions",
|
|
1774
|
+
session_id: null,
|
|
1775
|
+
sessions,
|
|
1776
|
+
count: sessions.length
|
|
1777
|
+
});
|
|
1778
|
+
}
|
|
1779
|
+
);
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
export {
|
|
1783
|
+
buildBrowserAgentMcpServer,
|
|
1784
|
+
registerBrowserAgentMcpTools
|
|
1785
|
+
};
|
|
1786
|
+
//# sourceMappingURL=chunk-HS6OKELW.js.map
|