forked from ChromeDevTools/chrome-devtools-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.ts
More file actions
284 lines (270 loc) · 8.52 KB
/
input.ts
File metadata and controls
284 lines (270 loc) · 8.52 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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {ElementHandle} from 'puppeteer-core';
import z from 'zod';
import {ToolCategories} from './categories.js';
import {defineTool} from './ToolDefinition.js';
export const click = defineTool({
name: 'click',
description: `Clicks on the provided element`,
annotations: {
category: ToolCategories.INPUT_AUTOMATION,
readOnlyHint: false,
},
schema: {
uid: z
.string()
.describe(
'The uid of an element on the page from the page content snapshot',
),
dblClick: z
.boolean()
.optional()
.describe('Set to true for double clicks. Default is false.'),
},
handler: async (request, response, context) => {
const uid = request.params.uid;
const handle = await context.getElementByUid(uid);
try {
await context.waitForEventsAfterAction(async () => {
await handle.asLocator().click({
count: request.params.dblClick ? 2 : 1,
});
});
response.appendResponseLine(
request.params.dblClick
? `Successfully double clicked on the element`
: `Successfully clicked on the element`,
);
response.setIncludeSnapshot(true);
} finally {
void handle.dispose();
}
},
});
export const hover = defineTool({
name: 'hover',
description: `Hover over the provided element`,
annotations: {
category: ToolCategories.INPUT_AUTOMATION,
readOnlyHint: false,
},
schema: {
uid: z
.string()
.describe(
'The uid of an element on the page from the page content snapshot',
),
},
handler: async (request, response, context) => {
const uid = request.params.uid;
const handle = await context.getElementByUid(uid);
try {
await context.waitForEventsAfterAction(async () => {
await handle.asLocator().hover();
});
response.appendResponseLine(`Successfully hovered over the element`);
response.setIncludeSnapshot(true);
} finally {
void handle.dispose();
}
},
});
export const fill = defineTool({
name: 'fill',
description: `Type text into a input, text area or select an option from a <select> element.`,
annotations: {
category: ToolCategories.INPUT_AUTOMATION,
readOnlyHint: false,
},
schema: {
uid: z
.string()
.describe(
'The uid of an element on the page from the page content snapshot',
),
value: z.string().describe('The value to fill in'),
},
handler: async (request, response, context) => {
const handle = await context.getElementByUid(request.params.uid);
try {
await context.waitForEventsAfterAction(async () => {
await handle.asLocator().fill(request.params.value);
});
response.appendResponseLine(`Successfully filled out the element`);
response.setIncludeSnapshot(true);
} finally {
void handle.dispose();
}
},
});
export const drag = defineTool({
name: 'drag',
description: `Drag an element onto another element`,
annotations: {
category: ToolCategories.INPUT_AUTOMATION,
readOnlyHint: false,
},
schema: {
from_uid: z.string().describe('The uid of the element to drag'),
to_uid: z.string().describe('The uid of the element to drop into'),
},
handler: async (request, response, context) => {
const fromHandle = await context.getElementByUid(request.params.from_uid);
const toHandle = await context.getElementByUid(request.params.to_uid);
try {
await context.waitForEventsAfterAction(async () => {
await fromHandle.drag(toHandle);
await new Promise(resolve => setTimeout(resolve, 50));
await toHandle.drop(fromHandle);
});
response.appendResponseLine(`Successfully dragged an element`);
response.setIncludeSnapshot(true);
} finally {
void fromHandle.dispose();
void toHandle.dispose();
}
},
});
export const fillForm = defineTool({
name: 'fill_form',
description: `Fill out multiple form elements at once`,
annotations: {
category: ToolCategories.INPUT_AUTOMATION,
readOnlyHint: false,
},
schema: {
elements: z
.array(
z.object({
uid: z.string().describe('The uid of the element to fill out'),
value: z.string().describe('Value for the element'),
}),
)
.describe('Elements from snapshot to fill out.'),
},
handler: async (request, response, context) => {
for (const element of request.params.elements) {
const handle = await context.getElementByUid(element.uid);
try {
await context.waitForEventsAfterAction(async () => {
await handle.asLocator().fill(element.value);
});
} finally {
void handle.dispose();
}
}
response.appendResponseLine(`Successfully filled out the form`);
response.setIncludeSnapshot(true);
},
});
export const uploadFile = defineTool({
name: 'upload_file',
description: 'Upload a file through a provided element.',
annotations: {
category: ToolCategories.INPUT_AUTOMATION,
readOnlyHint: false,
},
schema: {
uid: z
.string()
.describe(
'The uid of the file input element or an element that will open file chooser on the page from the page content snapshot',
),
filePath: z.string().describe('The local path of the file to upload'),
},
handler: async (request, response, context) => {
const {uid, filePath} = request.params;
const handle = (await context.getElementByUid(
uid,
)) as ElementHandle<HTMLInputElement>;
try {
try {
await handle.uploadFile(filePath);
} catch {
// Some sites use a proxy element to trigger file upload instead of
// a type=file element. In this case, we want to default to
// Page.waitForFileChooser() and upload the file this way.
try {
const page = context.getSelectedPage();
const [fileChooser] = await Promise.all([
page.waitForFileChooser({timeout: 3000}),
handle.asLocator().click(),
]);
await fileChooser.accept([filePath]);
} catch {
throw new Error(
`Failed to upload file. The element could not accept the file directly, and clicking it did not trigger a file chooser.`,
);
}
}
response.setIncludeSnapshot(true);
response.appendResponseLine(`File uploaded from ${filePath}.`);
} finally {
void handle.dispose();
}
},
});
/**
* Split a key combination string into individual keys.
* Handles combinations like "Control+A" and special cases like "Control++".
* Based on Playwright's implementation.
*/
function splitKeyCombo(keyString: string): string[] {
const keys: string[] = [];
let building = '';
for (const char of keyString) {
if (char === '+' && building) {
// Only split if there's text before +
keys.push(building);
building = '';
} else {
building += char;
}
}
keys.push(building);
return keys;
}
export const pressKey = defineTool({
name: 'press_key',
description: `Press a key or key combination on the keyboard. Supports modifier keys and combinations.`,
annotations: {
category: ToolCategories.INPUT_AUTOMATION,
readOnlyHint: false,
},
schema: {
key: z
.string()
.describe(
'Key to press. Can be a single key (e.g., "Enter", "Escape", "a") or a combination with modifiers (e.g., "Control+A", "Control+Shift+T", "Control++"). Modifier keys: Control, Shift, Alt, Meta.',
),
},
handler: async (request, response, context) => {
const page = context.getSelectedPage();
const tokens = splitKeyCombo(request.params.key);
const key = tokens[tokens.length - 1];
const modifiers = tokens.slice(0, -1);
await context.waitForEventsAfterAction(async () => {
// Press down modifiers
for (const modifier of modifiers) {
// @ts-expect-error - Puppeteer KeyInput type is too restrictive for dynamic input
await page.keyboard.down(modifier);
}
// Press the key
// @ts-expect-error - Puppeteer KeyInput type is too restrictive for dynamic input
await page.keyboard.press(key);
// Release modifiers in reverse order
for (let i = modifiers.length - 1; i >= 0; i--) {
// @ts-expect-error - Puppeteer KeyInput type is too restrictive for dynamic input
await page.keyboard.up(modifiers[i]);
}
});
response.appendResponseLine(
`Successfully pressed key: ${request.params.key}`,
);
response.setIncludeSnapshot(true);
},
});