@mcp-b/chrome-devtools-mcp 0.12.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +554 -0
- package/build/src/DevToolsConnectionAdapter.js +69 -0
- package/build/src/DevtoolsUtils.js +206 -0
- package/build/src/McpContext.js +499 -0
- package/build/src/McpResponse.js +396 -0
- package/build/src/Mutex.js +37 -0
- package/build/src/PageCollector.js +283 -0
- package/build/src/WaitForHelper.js +139 -0
- package/build/src/browser.js +134 -0
- package/build/src/cli.js +213 -0
- package/build/src/formatters/consoleFormatter.js +121 -0
- package/build/src/formatters/networkFormatter.js +77 -0
- package/build/src/formatters/snapshotFormatter.js +73 -0
- package/build/src/index.js +21 -0
- package/build/src/issue-descriptions.js +39 -0
- package/build/src/logger.js +27 -0
- package/build/src/main.js +130 -0
- package/build/src/polyfill.js +7 -0
- package/build/src/third_party/index.js +16 -0
- package/build/src/tools/ToolDefinition.js +20 -0
- package/build/src/tools/categories.js +24 -0
- package/build/src/tools/console.js +85 -0
- package/build/src/tools/emulation.js +87 -0
- package/build/src/tools/input.js +268 -0
- package/build/src/tools/network.js +106 -0
- package/build/src/tools/pages.js +237 -0
- package/build/src/tools/performance.js +147 -0
- package/build/src/tools/screenshot.js +84 -0
- package/build/src/tools/script.js +71 -0
- package/build/src/tools/snapshot.js +52 -0
- package/build/src/tools/tools.js +31 -0
- package/build/src/tools/webmcp.js +233 -0
- package/build/src/trace-processing/parse.js +84 -0
- package/build/src/transports/WebMCPBridgeScript.js +196 -0
- package/build/src/transports/WebMCPClientTransport.js +276 -0
- package/build/src/transports/index.js +7 -0
- package/build/src/utils/keyboard.js +296 -0
- package/build/src/utils/pagination.js +49 -0
- package/build/src/utils/types.js +6 -0
- package/package.json +87 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import './polyfill.js';
|
|
7
|
+
import { ensureBrowserConnected, ensureBrowserLaunched } from './browser.js';
|
|
8
|
+
import { parseArguments } from './cli.js';
|
|
9
|
+
import { loadIssueDescriptions } from './issue-descriptions.js';
|
|
10
|
+
import { logger, saveLogsToFile } from './logger.js';
|
|
11
|
+
import { McpContext } from './McpContext.js';
|
|
12
|
+
import { McpResponse } from './McpResponse.js';
|
|
13
|
+
import { Mutex } from './Mutex.js';
|
|
14
|
+
import { McpServer, StdioServerTransport, SetLevelRequestSchema, } from './third_party/index.js';
|
|
15
|
+
import { ToolCategory } from './tools/categories.js';
|
|
16
|
+
import { tools } from './tools/tools.js';
|
|
17
|
+
// If moved update release-please config
|
|
18
|
+
// x-release-please-start-version
|
|
19
|
+
const VERSION = '0.11.0';
|
|
20
|
+
// x-release-please-end
|
|
21
|
+
export const args = parseArguments(VERSION);
|
|
22
|
+
const logFile = args.logFile ? saveLogsToFile(args.logFile) : undefined;
|
|
23
|
+
logger(`Starting Chrome DevTools MCP Server v${VERSION}`);
|
|
24
|
+
const server = new McpServer({
|
|
25
|
+
name: 'chrome_devtools',
|
|
26
|
+
title: 'Chrome DevTools MCP server',
|
|
27
|
+
version: VERSION,
|
|
28
|
+
}, { capabilities: { logging: {} } });
|
|
29
|
+
server.server.setRequestHandler(SetLevelRequestSchema, () => {
|
|
30
|
+
return {};
|
|
31
|
+
});
|
|
32
|
+
let context;
|
|
33
|
+
async function getContext() {
|
|
34
|
+
const extraArgs = (args.chromeArg ?? []).map(String);
|
|
35
|
+
if (args.proxyServer) {
|
|
36
|
+
extraArgs.push(`--proxy-server=${args.proxyServer}`);
|
|
37
|
+
}
|
|
38
|
+
const devtools = args.experimentalDevtools ?? false;
|
|
39
|
+
const browser = args.browserUrl || args.wsEndpoint
|
|
40
|
+
? await ensureBrowserConnected({
|
|
41
|
+
browserURL: args.browserUrl,
|
|
42
|
+
wsEndpoint: args.wsEndpoint,
|
|
43
|
+
wsHeaders: args.wsHeaders,
|
|
44
|
+
devtools,
|
|
45
|
+
})
|
|
46
|
+
: await ensureBrowserLaunched({
|
|
47
|
+
headless: args.headless,
|
|
48
|
+
executablePath: args.executablePath,
|
|
49
|
+
channel: args.channel,
|
|
50
|
+
isolated: args.isolated ?? false,
|
|
51
|
+
userDataDir: args.userDataDir,
|
|
52
|
+
logFile,
|
|
53
|
+
viewport: args.viewport,
|
|
54
|
+
args: extraArgs,
|
|
55
|
+
acceptInsecureCerts: args.acceptInsecureCerts,
|
|
56
|
+
devtools,
|
|
57
|
+
});
|
|
58
|
+
if (context?.browser !== browser) {
|
|
59
|
+
context = await McpContext.from(browser, logger, {
|
|
60
|
+
experimentalDevToolsDebugging: devtools,
|
|
61
|
+
experimentalIncludeAllPages: args.experimentalIncludeAllPages,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return context;
|
|
65
|
+
}
|
|
66
|
+
const logDisclaimers = () => {
|
|
67
|
+
console.error(`chrome-devtools-mcp exposes content of the browser instance to the MCP clients allowing them to inspect,
|
|
68
|
+
debug, and modify any data in the browser or DevTools.
|
|
69
|
+
Avoid sharing sensitive or personal information that you do not want to share with MCP clients.`);
|
|
70
|
+
};
|
|
71
|
+
const toolMutex = new Mutex();
|
|
72
|
+
function registerTool(tool) {
|
|
73
|
+
if (tool.annotations.category === ToolCategory.EMULATION &&
|
|
74
|
+
args.categoryEmulation === false) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (tool.annotations.category === ToolCategory.PERFORMANCE &&
|
|
78
|
+
args.categoryPerformance === false) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (tool.annotations.category === ToolCategory.NETWORK &&
|
|
82
|
+
args.categoryNetwork === false) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
server.registerTool(tool.name, {
|
|
86
|
+
description: tool.description,
|
|
87
|
+
inputSchema: tool.schema,
|
|
88
|
+
annotations: tool.annotations,
|
|
89
|
+
}, async (params) => {
|
|
90
|
+
const guard = await toolMutex.acquire();
|
|
91
|
+
try {
|
|
92
|
+
logger(`${tool.name} request: ${JSON.stringify(params, null, ' ')}`);
|
|
93
|
+
const context = await getContext();
|
|
94
|
+
logger(`${tool.name} context: resolved`);
|
|
95
|
+
await context.detectOpenDevToolsWindows();
|
|
96
|
+
const response = new McpResponse();
|
|
97
|
+
await tool.handler({
|
|
98
|
+
params,
|
|
99
|
+
}, response, context);
|
|
100
|
+
const content = await response.handle(tool.name, context);
|
|
101
|
+
return {
|
|
102
|
+
content,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
logger(`${tool.name} error:`, err, err?.stack);
|
|
107
|
+
const errorText = err && 'message' in err ? err.message : String(err);
|
|
108
|
+
return {
|
|
109
|
+
content: [
|
|
110
|
+
{
|
|
111
|
+
type: 'text',
|
|
112
|
+
text: errorText,
|
|
113
|
+
},
|
|
114
|
+
],
|
|
115
|
+
isError: true,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
guard.dispose();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
for (const tool of tools) {
|
|
124
|
+
registerTool(tool);
|
|
125
|
+
}
|
|
126
|
+
await loadIssueDescriptions();
|
|
127
|
+
const transport = new StdioServerTransport();
|
|
128
|
+
await server.connect(transport);
|
|
129
|
+
logger('Chrome DevTools MCP Server connected');
|
|
130
|
+
logDisclaimers();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import 'core-js/modules/es.promise.with-resolvers.js';
|
|
7
|
+
import 'core-js/proposals/iterator-helpers.js';
|
|
8
|
+
export { default as yargs } from 'yargs';
|
|
9
|
+
export { hideBin } from 'yargs/helpers';
|
|
10
|
+
export { default as debug } from 'debug';
|
|
11
|
+
export { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
12
|
+
export { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
13
|
+
export { SetLevelRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
|
|
14
|
+
export { z as zod } from 'zod';
|
|
15
|
+
export { Locator, PredefinedNetworkConditions, CDPSessionEvent, } from 'puppeteer-core';
|
|
16
|
+
export { default as puppeteer } from 'puppeteer-core';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { zod } from '../third_party/index.js';
|
|
7
|
+
export function defineTool(definition) {
|
|
8
|
+
return definition;
|
|
9
|
+
}
|
|
10
|
+
export const CLOSE_PAGE_ERROR = 'The last open page cannot be closed. It is fine to keep it open.';
|
|
11
|
+
export const timeoutSchema = {
|
|
12
|
+
timeout: zod
|
|
13
|
+
.number()
|
|
14
|
+
.int()
|
|
15
|
+
.optional()
|
|
16
|
+
.describe(`Maximum wait time in milliseconds. If set to 0, the default timeout will be used.`)
|
|
17
|
+
.transform(value => {
|
|
18
|
+
return value && value <= 0 ? undefined : value;
|
|
19
|
+
}),
|
|
20
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
export var ToolCategory;
|
|
7
|
+
(function (ToolCategory) {
|
|
8
|
+
ToolCategory["INPUT"] = "input";
|
|
9
|
+
ToolCategory["NAVIGATION"] = "navigation";
|
|
10
|
+
ToolCategory["EMULATION"] = "emulation";
|
|
11
|
+
ToolCategory["PERFORMANCE"] = "performance";
|
|
12
|
+
ToolCategory["NETWORK"] = "network";
|
|
13
|
+
ToolCategory["DEBUGGING"] = "debugging";
|
|
14
|
+
ToolCategory["WEBMCP"] = "webmcp";
|
|
15
|
+
})(ToolCategory || (ToolCategory = {}));
|
|
16
|
+
export const labels = {
|
|
17
|
+
[ToolCategory.INPUT]: 'Input automation',
|
|
18
|
+
[ToolCategory.NAVIGATION]: 'Navigation automation',
|
|
19
|
+
[ToolCategory.EMULATION]: 'Emulation',
|
|
20
|
+
[ToolCategory.PERFORMANCE]: 'Performance',
|
|
21
|
+
[ToolCategory.NETWORK]: 'Network',
|
|
22
|
+
[ToolCategory.DEBUGGING]: 'Debugging',
|
|
23
|
+
[ToolCategory.WEBMCP]: 'Website MCP Tools',
|
|
24
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { zod } from '../third_party/index.js';
|
|
7
|
+
import { ToolCategory } from './categories.js';
|
|
8
|
+
import { defineTool } from './ToolDefinition.js';
|
|
9
|
+
const FILTERABLE_MESSAGE_TYPES = [
|
|
10
|
+
'log',
|
|
11
|
+
'debug',
|
|
12
|
+
'info',
|
|
13
|
+
'error',
|
|
14
|
+
'warn',
|
|
15
|
+
'dir',
|
|
16
|
+
'dirxml',
|
|
17
|
+
'table',
|
|
18
|
+
'trace',
|
|
19
|
+
'clear',
|
|
20
|
+
'startGroup',
|
|
21
|
+
'startGroupCollapsed',
|
|
22
|
+
'endGroup',
|
|
23
|
+
'assert',
|
|
24
|
+
'profile',
|
|
25
|
+
'profileEnd',
|
|
26
|
+
'count',
|
|
27
|
+
'timeEnd',
|
|
28
|
+
'verbose',
|
|
29
|
+
'issue',
|
|
30
|
+
];
|
|
31
|
+
export const listConsoleMessages = defineTool({
|
|
32
|
+
name: 'list_console_messages',
|
|
33
|
+
description: 'List all console messages for the currently selected page since the last navigation.',
|
|
34
|
+
annotations: {
|
|
35
|
+
category: ToolCategory.DEBUGGING,
|
|
36
|
+
readOnlyHint: true,
|
|
37
|
+
},
|
|
38
|
+
schema: {
|
|
39
|
+
pageSize: zod
|
|
40
|
+
.number()
|
|
41
|
+
.int()
|
|
42
|
+
.positive()
|
|
43
|
+
.optional()
|
|
44
|
+
.describe('Maximum number of messages to return. When omitted, returns all requests.'),
|
|
45
|
+
pageIdx: zod
|
|
46
|
+
.number()
|
|
47
|
+
.int()
|
|
48
|
+
.min(0)
|
|
49
|
+
.optional()
|
|
50
|
+
.describe('Page number to return (0-based). When omitted, returns the first page.'),
|
|
51
|
+
types: zod
|
|
52
|
+
.array(zod.enum(FILTERABLE_MESSAGE_TYPES))
|
|
53
|
+
.optional()
|
|
54
|
+
.describe('Filter messages to only return messages of the specified resource types. When omitted or empty, returns all messages.'),
|
|
55
|
+
includePreservedMessages: zod
|
|
56
|
+
.boolean()
|
|
57
|
+
.default(false)
|
|
58
|
+
.optional()
|
|
59
|
+
.describe('Set to true to return the preserved messages over the last 3 navigations.'),
|
|
60
|
+
},
|
|
61
|
+
handler: async (request, response) => {
|
|
62
|
+
response.setIncludeConsoleData(true, {
|
|
63
|
+
pageSize: request.params.pageSize,
|
|
64
|
+
pageIdx: request.params.pageIdx,
|
|
65
|
+
types: request.params.types,
|
|
66
|
+
includePreservedMessages: request.params.includePreservedMessages,
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
export const getConsoleMessage = defineTool({
|
|
71
|
+
name: 'get_console_message',
|
|
72
|
+
description: `Gets a console message by its ID. You can get all messages by calling ${listConsoleMessages.name}.`,
|
|
73
|
+
annotations: {
|
|
74
|
+
category: ToolCategory.DEBUGGING,
|
|
75
|
+
readOnlyHint: true,
|
|
76
|
+
},
|
|
77
|
+
schema: {
|
|
78
|
+
msgid: zod
|
|
79
|
+
.number()
|
|
80
|
+
.describe('The msgid of a console message on the page from the listed console messages'),
|
|
81
|
+
},
|
|
82
|
+
handler: async (request, response) => {
|
|
83
|
+
response.attachConsoleMessage(request.params.msgid);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { zod, PredefinedNetworkConditions } from '../third_party/index.js';
|
|
7
|
+
import { ToolCategory } from './categories.js';
|
|
8
|
+
import { defineTool } from './ToolDefinition.js';
|
|
9
|
+
const throttlingOptions = [
|
|
10
|
+
'No emulation',
|
|
11
|
+
'Offline',
|
|
12
|
+
...Object.keys(PredefinedNetworkConditions),
|
|
13
|
+
];
|
|
14
|
+
export const emulate = defineTool({
|
|
15
|
+
name: 'emulate',
|
|
16
|
+
description: `Emulates various features on the selected page.`,
|
|
17
|
+
annotations: {
|
|
18
|
+
category: ToolCategory.EMULATION,
|
|
19
|
+
readOnlyHint: false,
|
|
20
|
+
},
|
|
21
|
+
schema: {
|
|
22
|
+
networkConditions: zod
|
|
23
|
+
.enum(throttlingOptions)
|
|
24
|
+
.optional()
|
|
25
|
+
.describe(`Throttle network. Set to "No emulation" to disable. If omitted, conditions remain unchanged.`),
|
|
26
|
+
cpuThrottlingRate: zod
|
|
27
|
+
.number()
|
|
28
|
+
.min(1)
|
|
29
|
+
.max(20)
|
|
30
|
+
.optional()
|
|
31
|
+
.describe('Represents the CPU slowdown factor. Set the rate to 1 to disable throttling. If omitted, throttling remains unchanged.'),
|
|
32
|
+
geolocation: zod
|
|
33
|
+
.object({
|
|
34
|
+
latitude: zod
|
|
35
|
+
.number()
|
|
36
|
+
.min(-90)
|
|
37
|
+
.max(90)
|
|
38
|
+
.describe('Latitude between -90 and 90.'),
|
|
39
|
+
longitude: zod
|
|
40
|
+
.number()
|
|
41
|
+
.min(-180)
|
|
42
|
+
.max(180)
|
|
43
|
+
.describe('Longitude between -180 and 180.'),
|
|
44
|
+
})
|
|
45
|
+
.nullable()
|
|
46
|
+
.optional()
|
|
47
|
+
.describe('Geolocation to emulate. Set to null to clear the geolocation override.'),
|
|
48
|
+
},
|
|
49
|
+
handler: async (request, _response, context) => {
|
|
50
|
+
const page = context.getSelectedPage();
|
|
51
|
+
const { networkConditions, cpuThrottlingRate, geolocation } = request.params;
|
|
52
|
+
if (networkConditions) {
|
|
53
|
+
if (networkConditions === 'No emulation') {
|
|
54
|
+
await page.emulateNetworkConditions(null);
|
|
55
|
+
context.setNetworkConditions(null);
|
|
56
|
+
}
|
|
57
|
+
else if (networkConditions === 'Offline') {
|
|
58
|
+
await page.emulateNetworkConditions({
|
|
59
|
+
offline: true,
|
|
60
|
+
download: 0,
|
|
61
|
+
upload: 0,
|
|
62
|
+
latency: 0,
|
|
63
|
+
});
|
|
64
|
+
context.setNetworkConditions('Offline');
|
|
65
|
+
}
|
|
66
|
+
else if (networkConditions in PredefinedNetworkConditions) {
|
|
67
|
+
const networkCondition = PredefinedNetworkConditions[networkConditions];
|
|
68
|
+
await page.emulateNetworkConditions(networkCondition);
|
|
69
|
+
context.setNetworkConditions(networkConditions);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (cpuThrottlingRate) {
|
|
73
|
+
await page.emulateCPUThrottling(cpuThrottlingRate);
|
|
74
|
+
context.setCpuThrottlingRate(cpuThrottlingRate);
|
|
75
|
+
}
|
|
76
|
+
if (geolocation !== undefined) {
|
|
77
|
+
if (geolocation === null) {
|
|
78
|
+
await page.setGeolocation({ latitude: 0, longitude: 0 });
|
|
79
|
+
context.setGeolocation(null);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
await page.setGeolocation(geolocation);
|
|
83
|
+
context.setGeolocation(geolocation);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
});
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { zod } from '../third_party/index.js';
|
|
7
|
+
import { parseKey } from '../utils/keyboard.js';
|
|
8
|
+
import { ToolCategory } from './categories.js';
|
|
9
|
+
import { defineTool } from './ToolDefinition.js';
|
|
10
|
+
export const click = defineTool({
|
|
11
|
+
name: 'click',
|
|
12
|
+
description: `Clicks on the provided element`,
|
|
13
|
+
annotations: {
|
|
14
|
+
category: ToolCategory.INPUT,
|
|
15
|
+
readOnlyHint: false,
|
|
16
|
+
},
|
|
17
|
+
schema: {
|
|
18
|
+
uid: zod
|
|
19
|
+
.string()
|
|
20
|
+
.describe('The uid of an element on the page from the page content snapshot'),
|
|
21
|
+
dblClick: zod
|
|
22
|
+
.boolean()
|
|
23
|
+
.optional()
|
|
24
|
+
.describe('Set to true for double clicks. Default is false.'),
|
|
25
|
+
},
|
|
26
|
+
handler: async (request, response, context) => {
|
|
27
|
+
const uid = request.params.uid;
|
|
28
|
+
const handle = await context.getElementByUid(uid);
|
|
29
|
+
try {
|
|
30
|
+
await context.waitForEventsAfterAction(async () => {
|
|
31
|
+
await handle.asLocator().click({
|
|
32
|
+
count: request.params.dblClick ? 2 : 1,
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
response.appendResponseLine(request.params.dblClick
|
|
36
|
+
? `Successfully double clicked on the element`
|
|
37
|
+
: `Successfully clicked on the element`);
|
|
38
|
+
response.includeSnapshot();
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
void handle.dispose();
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
export const hover = defineTool({
|
|
46
|
+
name: 'hover',
|
|
47
|
+
description: `Hover over the provided element`,
|
|
48
|
+
annotations: {
|
|
49
|
+
category: ToolCategory.INPUT,
|
|
50
|
+
readOnlyHint: false,
|
|
51
|
+
},
|
|
52
|
+
schema: {
|
|
53
|
+
uid: zod
|
|
54
|
+
.string()
|
|
55
|
+
.describe('The uid of an element on the page from the page content snapshot'),
|
|
56
|
+
},
|
|
57
|
+
handler: async (request, response, context) => {
|
|
58
|
+
const uid = request.params.uid;
|
|
59
|
+
const handle = await context.getElementByUid(uid);
|
|
60
|
+
try {
|
|
61
|
+
await context.waitForEventsAfterAction(async () => {
|
|
62
|
+
await handle.asLocator().hover();
|
|
63
|
+
});
|
|
64
|
+
response.appendResponseLine(`Successfully hovered over the element`);
|
|
65
|
+
response.includeSnapshot();
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
void handle.dispose();
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
// The AXNode for an option doesn't contain its `value`. We set text content of the option as value.
|
|
73
|
+
// If the form is a combobox, we need to find the correct option by its text value.
|
|
74
|
+
// To do that, loop through the children while checking which child's text matches the requested value (requested value is actually the text content).
|
|
75
|
+
// When the correct option is found, use the element handle to get the real value.
|
|
76
|
+
async function selectOption(handle, aXNode, value) {
|
|
77
|
+
let optionFound = false;
|
|
78
|
+
for (const child of aXNode.children) {
|
|
79
|
+
if (child.role === 'option' && child.name === value && child.value) {
|
|
80
|
+
optionFound = true;
|
|
81
|
+
const childHandle = await child.elementHandle();
|
|
82
|
+
if (childHandle) {
|
|
83
|
+
try {
|
|
84
|
+
const childValueHandle = await childHandle.getProperty('value');
|
|
85
|
+
try {
|
|
86
|
+
const childValue = await childValueHandle.jsonValue();
|
|
87
|
+
if (childValue) {
|
|
88
|
+
await handle.asLocator().fill(childValue.toString());
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
void childValueHandle.dispose();
|
|
93
|
+
}
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
void childHandle.dispose();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!optionFound) {
|
|
103
|
+
throw new Error(`Could not find option with text "${value}"`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function fillFormElement(uid, value, context) {
|
|
107
|
+
const handle = await context.getElementByUid(uid);
|
|
108
|
+
try {
|
|
109
|
+
const aXNode = context.getAXNodeByUid(uid);
|
|
110
|
+
if (aXNode && aXNode.role === 'combobox') {
|
|
111
|
+
await selectOption(handle, aXNode, value);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
await handle.asLocator().fill(value);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
finally {
|
|
118
|
+
void handle.dispose();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
export const fill = defineTool({
|
|
122
|
+
name: 'fill',
|
|
123
|
+
description: `Type text into a input, text area or select an option from a <select> element.`,
|
|
124
|
+
annotations: {
|
|
125
|
+
category: ToolCategory.INPUT,
|
|
126
|
+
readOnlyHint: false,
|
|
127
|
+
},
|
|
128
|
+
schema: {
|
|
129
|
+
uid: zod
|
|
130
|
+
.string()
|
|
131
|
+
.describe('The uid of an element on the page from the page content snapshot'),
|
|
132
|
+
value: zod.string().describe('The value to fill in'),
|
|
133
|
+
},
|
|
134
|
+
handler: async (request, response, context) => {
|
|
135
|
+
await context.waitForEventsAfterAction(async () => {
|
|
136
|
+
await fillFormElement(request.params.uid, request.params.value, context);
|
|
137
|
+
});
|
|
138
|
+
response.appendResponseLine(`Successfully filled out the element`);
|
|
139
|
+
response.includeSnapshot();
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
export const drag = defineTool({
|
|
143
|
+
name: 'drag',
|
|
144
|
+
description: `Drag an element onto another element`,
|
|
145
|
+
annotations: {
|
|
146
|
+
category: ToolCategory.INPUT,
|
|
147
|
+
readOnlyHint: false,
|
|
148
|
+
},
|
|
149
|
+
schema: {
|
|
150
|
+
from_uid: zod.string().describe('The uid of the element to drag'),
|
|
151
|
+
to_uid: zod.string().describe('The uid of the element to drop into'),
|
|
152
|
+
},
|
|
153
|
+
handler: async (request, response, context) => {
|
|
154
|
+
const fromHandle = await context.getElementByUid(request.params.from_uid);
|
|
155
|
+
const toHandle = await context.getElementByUid(request.params.to_uid);
|
|
156
|
+
try {
|
|
157
|
+
await context.waitForEventsAfterAction(async () => {
|
|
158
|
+
await fromHandle.drag(toHandle);
|
|
159
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
160
|
+
await toHandle.drop(fromHandle);
|
|
161
|
+
});
|
|
162
|
+
response.appendResponseLine(`Successfully dragged an element`);
|
|
163
|
+
response.includeSnapshot();
|
|
164
|
+
}
|
|
165
|
+
finally {
|
|
166
|
+
void fromHandle.dispose();
|
|
167
|
+
void toHandle.dispose();
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
export const fillForm = defineTool({
|
|
172
|
+
name: 'fill_form',
|
|
173
|
+
description: `Fill out multiple form elements at once`,
|
|
174
|
+
annotations: {
|
|
175
|
+
category: ToolCategory.INPUT,
|
|
176
|
+
readOnlyHint: false,
|
|
177
|
+
},
|
|
178
|
+
schema: {
|
|
179
|
+
elements: zod
|
|
180
|
+
.array(zod.object({
|
|
181
|
+
uid: zod.string().describe('The uid of the element to fill out'),
|
|
182
|
+
value: zod.string().describe('Value for the element'),
|
|
183
|
+
}))
|
|
184
|
+
.describe('Elements from snapshot to fill out.'),
|
|
185
|
+
},
|
|
186
|
+
handler: async (request, response, context) => {
|
|
187
|
+
for (const element of request.params.elements) {
|
|
188
|
+
await context.waitForEventsAfterAction(async () => {
|
|
189
|
+
await fillFormElement(element.uid, element.value, context);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
response.appendResponseLine(`Successfully filled out the form`);
|
|
193
|
+
response.includeSnapshot();
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
export const uploadFile = defineTool({
|
|
197
|
+
name: 'upload_file',
|
|
198
|
+
description: 'Upload a file through a provided element.',
|
|
199
|
+
annotations: {
|
|
200
|
+
category: ToolCategory.INPUT,
|
|
201
|
+
readOnlyHint: false,
|
|
202
|
+
},
|
|
203
|
+
schema: {
|
|
204
|
+
uid: zod
|
|
205
|
+
.string()
|
|
206
|
+
.describe('The uid of the file input element or an element that will open file chooser on the page from the page content snapshot'),
|
|
207
|
+
filePath: zod.string().describe('The local path of the file to upload'),
|
|
208
|
+
},
|
|
209
|
+
handler: async (request, response, context) => {
|
|
210
|
+
const { uid, filePath } = request.params;
|
|
211
|
+
const handle = (await context.getElementByUid(uid));
|
|
212
|
+
try {
|
|
213
|
+
try {
|
|
214
|
+
await handle.uploadFile(filePath);
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
// Some sites use a proxy element to trigger file upload instead of
|
|
218
|
+
// a type=file element. In this case, we want to default to
|
|
219
|
+
// Page.waitForFileChooser() and upload the file this way.
|
|
220
|
+
try {
|
|
221
|
+
const page = context.getSelectedPage();
|
|
222
|
+
const [fileChooser] = await Promise.all([
|
|
223
|
+
page.waitForFileChooser({ timeout: 3000 }),
|
|
224
|
+
handle.asLocator().click(),
|
|
225
|
+
]);
|
|
226
|
+
await fileChooser.accept([filePath]);
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
throw new Error(`Failed to upload file. The element could not accept the file directly, and clicking it did not trigger a file chooser.`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
response.includeSnapshot();
|
|
233
|
+
response.appendResponseLine(`File uploaded from ${filePath}.`);
|
|
234
|
+
}
|
|
235
|
+
finally {
|
|
236
|
+
void handle.dispose();
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
export const pressKey = defineTool({
|
|
241
|
+
name: 'press_key',
|
|
242
|
+
description: `Press a key or key combination. Use this when other input methods like fill() cannot be used (e.g., keyboard shortcuts, navigation keys, or special key combinations).`,
|
|
243
|
+
annotations: {
|
|
244
|
+
category: ToolCategory.INPUT,
|
|
245
|
+
readOnlyHint: false,
|
|
246
|
+
},
|
|
247
|
+
schema: {
|
|
248
|
+
key: zod
|
|
249
|
+
.string()
|
|
250
|
+
.describe('A key or a combination (e.g., "Enter", "Control+A", "Control++", "Control+Shift+R"). Modifiers: Control, Shift, Alt, Meta'),
|
|
251
|
+
},
|
|
252
|
+
handler: async (request, response, context) => {
|
|
253
|
+
const page = context.getSelectedPage();
|
|
254
|
+
const tokens = parseKey(request.params.key);
|
|
255
|
+
const [key, ...modifiers] = tokens;
|
|
256
|
+
await context.waitForEventsAfterAction(async () => {
|
|
257
|
+
for (const modifier of modifiers) {
|
|
258
|
+
await page.keyboard.down(modifier);
|
|
259
|
+
}
|
|
260
|
+
await page.keyboard.press(key);
|
|
261
|
+
for (const modifier of modifiers.toReversed()) {
|
|
262
|
+
await page.keyboard.up(modifier);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
response.appendResponseLine(`Successfully pressed key: ${request.params.key}`);
|
|
266
|
+
response.includeSnapshot();
|
|
267
|
+
},
|
|
268
|
+
});
|