-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathMcpPage.ts
More file actions
430 lines (384 loc) · 12.5 KB
/
McpPage.ts
File metadata and controls
430 lines (384 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {logger} from './logger.js';
import {TextSnapshot} from './TextSnapshot.js';
import type {
Dialog,
ElementHandle,
Page,
Viewport,
WebMCPTool,
} from './third_party/index.js';
import type {ToolGroup, ToolDefinition} from './tools/inPage.js';
import {takeSnapshot} from './tools/snapshot.js';
import type {
ContextPage,
DevToolsData,
Response,
} from './tools/ToolDefinition.js';
import type {
EmulationSettings,
GeolocationOptions,
TextSnapshotNode,
} from './types.js';
import {
getNetworkMultiplierFromString,
WaitForHelper,
} from './WaitForHelper.js';
/**
* Per-page state wrapper. Consolidates dialog, snapshot, emulation,
* and metadata that were previously scattered across Maps in McpContext.
*
* Internal class consumed only by McpContext. Fields are public for direct
* read/write access. The dialog field is private because it requires an
* event listener lifecycle managed by the constructor/dispose pair.
*/
export class McpPage implements ContextPage {
readonly pptrPage: Page;
readonly id: number;
// Snapshot
textSnapshot: TextSnapshot | null = null;
uniqueBackendNodeIdToMcpId = new Map<string, string>();
extraHandles: ElementHandle[] = [];
// Emulation
emulationSettings: EmulationSettings = {};
// Metadata
isolatedContextName?: string;
devToolsPage?: Page;
// Dialog
#dialog?: Dialog;
#dialogHandler: (dialog: Dialog) => void;
inPageTools: ToolGroup<ToolDefinition> | undefined;
constructor(page: Page, id: number) {
this.pptrPage = page;
this.id = id;
this.#dialogHandler = (dialog: Dialog): void => {
this.#dialog = dialog;
};
page.on('dialog', this.#dialogHandler);
}
get dialog(): Dialog | undefined {
return this.#dialog;
}
getDialog(): Dialog | undefined {
return this.dialog;
}
clearDialog(): void {
this.#dialog = undefined;
}
throwIfDialogOpen(): void {
if (this.#dialog) {
throw new Error(
`A dialog is open (${this.#dialog.type()}: ${this.#dialog.message()}).`,
);
}
}
getInPageTools(): ToolGroup<ToolDefinition> | undefined {
return this.inPageTools;
}
getWebMcpTools(): WebMCPTool[] {
return this.pptrPage.webmcp.tools();
}
get networkConditions(): string | null {
return this.emulationSettings.networkConditions ?? null;
}
get cpuThrottlingRate(): number {
return this.emulationSettings.cpuThrottlingRate ?? 1;
}
get geolocation(): GeolocationOptions | null {
return this.emulationSettings.geolocation ?? null;
}
get viewport(): Viewport | null {
return this.emulationSettings.viewport ?? null;
}
get userAgent(): string | null {
return this.emulationSettings.userAgent ?? null;
}
get colorScheme(): 'dark' | 'light' | null {
return this.emulationSettings.colorScheme ?? null;
}
// Public for testability: tests spy on this method to verify throttle multipliers.
createWaitForHelper(
cpuMultiplier: number,
networkMultiplier: number,
): WaitForHelper {
return new WaitForHelper(this.pptrPage, cpuMultiplier, networkMultiplier);
}
async waitForEventsAfterAction(
action: () => Promise<unknown>,
options?: {timeout?: number; handleDialog?: 'accept' | 'dismiss' | string},
): Promise<{navigatedToUrl?: string}> {
const urlBefore = this.pptrPage.url();
const helper = this.createWaitForHelper(
this.cpuThrottlingRate,
getNetworkMultiplierFromString(this.networkConditions),
);
await helper.waitForEventsAfterAction(action, options);
const urlAfter = this.pptrPage.url();
return urlAfter === urlBefore ? {} : {navigatedToUrl: urlAfter};
}
dispose(): void {
this.pptrPage.off('dialog', this.#dialogHandler);
}
async executeInPageTool(
toolName: string,
params: Record<string, unknown>,
response: Response,
): Promise<void> {
// Creates array of ElementHandles from the UIDs in the params.
// We do not replace the uids with the ElementsHandles yet, because
// the `evaluate` function only turns them into DOM elements if they
// are passed as non-nested arguments.
const handles: ElementHandle[] = [];
for (const value of Object.values(params)) {
if (
value instanceof Object &&
'uid' in value &&
typeof value.uid === 'string' &&
Object.keys(value).length === 1
) {
handles.push(await this.getElementByUid(value.uid));
}
}
const result = await this.pptrPage.evaluate(
async (name, args, ...elements) => {
// Replace the UIDs with DOM elements.
for (const [key, value] of Object.entries(args)) {
if (
value instanceof Object &&
'uid' in value &&
typeof value.uid === 'string' &&
Object.keys(value).length === 1
) {
args[key] = elements.shift();
}
}
if (!window.__dtmcp?.executeTool) {
throw new Error('No tools found on the page');
}
const toolResult = await window.__dtmcp.executeTool(name, args);
const stashDOMElement = (el: Element) => {
if (!window.__dtmcp) {
window.__dtmcp = {};
}
if (window.__dtmcp.stashedElements === undefined) {
window.__dtmcp.stashedElements = [];
}
window.__dtmcp.stashedElements.push(el);
return {
stashedId: `stashed-${window.__dtmcp.stashedElements.length - 1}`,
};
};
const ancestors: unknown[] = [];
// Recursively walks the tool result:
// - Replaces DOM elements with an ID and stashes the DOM element on the window object
// - Replaces non-plain objects with a string representation of the object
// - Replaces circular references with the string '<Circular reference>'
// - Replaces functions with the string '<Function object>'
const processToolResult = (
data: unknown,
parentEl?: unknown,
): unknown => {
// 1. Handle DOM Elements
if (data instanceof Element) {
return stashDOMElement(data);
}
// 2. Handle Arrays
if (Array.isArray(data)) {
return data.map((item: unknown) =>
processToolResult(item, parentEl),
);
}
// 3. Handle Objects
if (data !== null && typeof data === 'object') {
while (ancestors.length > 0 && ancestors.at(-1) !== parentEl) {
ancestors.pop();
}
if (ancestors.includes(data)) {
return '<Circular reference>';
}
ancestors.push(data);
// If not a plain object, return a string representation of the object
if (Object.getPrototypeOf(data) !== Object.prototype) {
return `<${data.constructor.name} instance>`;
}
const processedObj: Record<string, unknown> = {};
for (const [key, value] of Object.entries(data)) {
processedObj[key] = processToolResult(value, data);
}
return processedObj;
}
// 4. Handle Functions
if (typeof data === 'function') {
return '<Function object>';
}
// 5. Return primitives (strings, numbers, booleans) as-is
return data;
};
return {
result: processToolResult(toolResult),
stashed: window.__dtmcp?.stashedElements?.length ?? 0,
};
},
toolName,
params,
...handles,
);
const elementHandles: ElementHandle[] = [];
for (let i = 0; i < (result.stashed ?? 0); i++) {
const elementHandle = await this.pptrPage.evaluateHandle(index => {
const el = window.__dtmcp?.stashedElements?.[index];
if (!el) {
throw new Error(`Stashed element at index ${index} not found`);
}
return el;
}, i);
elementHandles.push(elementHandle);
}
if (elementHandles.length) {
const oldHandles = [...this.extraHandles];
this.textSnapshot = await TextSnapshot.create(this, {
extraHandles: elementHandles,
});
response.includeSnapshot();
for (const handle of oldHandles) {
await handle
.dispose()
.catch(e => logger('Failed to dispose old handle', e));
}
}
const cdpElementIds = await Promise.all(
elementHandles.map(async (elementHandle, index) => {
const backendNodeId = await elementHandle.backendNodeId();
if (!backendNodeId) {
logger(
`No backendNodeId for stashed DOM element with index ${index}`,
);
return `stashed-${index}`;
}
const cdpElementId = this.resolveCdpElementId(backendNodeId);
if (!cdpElementId) {
logger(
`Could not get cdpElementId for backend node ${backendNodeId}`,
);
return `stashed-${index}`;
}
return cdpElementId;
}),
);
const recursivelyReplaceStashedElements = (node: unknown): unknown => {
if (Array.isArray(node)) {
return node.map(x => recursivelyReplaceStashedElements(x));
}
if (node !== null && typeof node === 'object') {
if (
'stashedId' in node &&
typeof node.stashedId === 'string' &&
node.stashedId.startsWith('stashed-') &&
Object.keys(node).length === 1
) {
const index = parseInt(node.stashedId.split('-')[1]);
return {uid: cdpElementIds[index]};
}
const resultObj: Record<string, unknown> = {};
for (const [key, value] of Object.entries(node)) {
resultObj[key] = recursivelyReplaceStashedElements(value);
}
return resultObj;
}
return node;
};
const resultWithUids = recursivelyReplaceStashedElements(result.result);
response.appendResponseLine(JSON.stringify(resultWithUids, null, 2));
}
async getElementByUid(uid: string): Promise<ElementHandle<Element>> {
if (!this.textSnapshot) {
throw new Error(
`No snapshot found for page ${this.id ?? '?'}. Use ${takeSnapshot.name} to capture one.`,
);
}
const node = this.textSnapshot.idToNode.get(uid);
if (!node) {
throw new Error(`Element uid "${uid}" not found on page ${this.id}.`);
}
return this.#resolveElementHandle(node, uid);
}
async #resolveElementHandle(
node: TextSnapshotNode,
uid: string,
): Promise<ElementHandle<Element>> {
const message = `Element with uid ${uid} no longer exists on the page.`;
try {
const handle = await node.elementHandle();
if (!handle) {
throw new Error(message);
}
return handle;
} catch (error) {
throw new Error(message, {
cause: error,
});
}
}
getAXNodeByUid(uid: string) {
return this.textSnapshot?.idToNode.get(uid);
}
resolveCdpElementId(cdpBackendNodeId: number): string | undefined {
if (!cdpBackendNodeId) {
logger('no cdpBackendNodeId');
return;
}
const snapshot = this.textSnapshot;
if (!snapshot) {
logger('no text snapshot');
return;
}
// TODO: index by backendNodeId instead.
const queue = [snapshot.root];
while (queue.length) {
const current = queue.pop()!;
if (current.backendNodeId === cdpBackendNodeId) {
return current.id;
}
for (const child of current.children) {
queue.push(child);
}
}
return;
}
async getDevToolsData(): Promise<DevToolsData> {
try {
logger('Getting DevTools UI data');
const devtoolsPage = this.devToolsPage;
if (!devtoolsPage) {
logger('No DevTools page detected');
return {};
}
const {cdpRequestId, cdpBackendNodeId} = await devtoolsPage.evaluate(
async () => {
// @ts-expect-error no types
const UI = await import('/bundled/ui/legacy/legacy.js');
// @ts-expect-error no types
const SDK = await import('/bundled/core/sdk/sdk.js');
const request = UI.Context.Context.instance().flavor(
SDK.NetworkRequest.NetworkRequest,
);
const node = UI.Context.Context.instance().flavor(
SDK.DOMModel.DOMNode,
);
return {
cdpRequestId: request?.requestId(),
cdpBackendNodeId: node?.backendNodeId(),
};
},
);
return {cdpBackendNodeId, cdpRequestId};
} catch (err) {
logger('error getting devtools data', err);
}
return {};
}
}