@roxybrowser/playwright-mcp 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +742 -0
- package/cli.js +18 -0
- package/config.d.ts +119 -0
- package/index.d.ts +23 -0
- package/index.js +19 -0
- package/lib/browserContextFactory.js +264 -0
- package/lib/browserServerBackend.js +77 -0
- package/lib/config.js +246 -0
- package/lib/context.js +242 -0
- package/lib/extension/cdpRelay.js +355 -0
- package/lib/extension/extensionContextFactory.js +54 -0
- package/lib/index.js +40 -0
- package/lib/loop/loop.js +69 -0
- package/lib/loop/loopClaude.js +152 -0
- package/lib/loop/loopOpenAI.js +141 -0
- package/lib/loop/main.js +60 -0
- package/lib/loopTools/context.js +67 -0
- package/lib/loopTools/main.js +54 -0
- package/lib/loopTools/perform.js +32 -0
- package/lib/loopTools/snapshot.js +29 -0
- package/lib/loopTools/tool.js +18 -0
- package/lib/mcp/http.js +120 -0
- package/lib/mcp/inProcessTransport.js +72 -0
- package/lib/mcp/proxyBackend.js +104 -0
- package/lib/mcp/server.js +123 -0
- package/lib/mcp/tool.js +29 -0
- package/lib/program.js +145 -0
- package/lib/response.js +165 -0
- package/lib/sessionLog.js +121 -0
- package/lib/tab.js +249 -0
- package/lib/tools/common.js +55 -0
- package/lib/tools/console.js +33 -0
- package/lib/tools/dialogs.js +47 -0
- package/lib/tools/evaluate.js +53 -0
- package/lib/tools/files.js +44 -0
- package/lib/tools/install.js +53 -0
- package/lib/tools/keyboard.js +78 -0
- package/lib/tools/mouse.js +99 -0
- package/lib/tools/navigate.js +70 -0
- package/lib/tools/network.js +41 -0
- package/lib/tools/pdf.js +40 -0
- package/lib/tools/roxy.js +50 -0
- package/lib/tools/screenshot.js +79 -0
- package/lib/tools/snapshot.js +139 -0
- package/lib/tools/tabs.js +87 -0
- package/lib/tools/tool.js +33 -0
- package/lib/tools/utils.js +74 -0
- package/lib/tools/wait.js +55 -0
- package/lib/tools.js +52 -0
- package/lib/utils/codegen.js +49 -0
- package/lib/utils/fileUtils.js +36 -0
- package/lib/utils/guid.js +22 -0
- package/lib/utils/log.js +21 -0
- package/lib/utils/manualPromise.js +111 -0
- package/lib/utils/package.js +20 -0
- package/lib/vscode/host.js +128 -0
- package/lib/vscode/main.js +62 -0
- package/package.json +79 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { defineTabTool } from './tool.js';
|
|
18
|
+
const elementSchema = z.object({
|
|
19
|
+
element: z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
|
|
20
|
+
});
|
|
21
|
+
const mouseMove = defineTabTool({
|
|
22
|
+
capability: 'vision',
|
|
23
|
+
schema: {
|
|
24
|
+
name: 'browser_mouse_move_xy',
|
|
25
|
+
title: 'Move mouse',
|
|
26
|
+
description: 'Move mouse to a given position',
|
|
27
|
+
inputSchema: elementSchema.extend({
|
|
28
|
+
x: z.number().describe('X coordinate'),
|
|
29
|
+
y: z.number().describe('Y coordinate'),
|
|
30
|
+
}),
|
|
31
|
+
type: 'readOnly',
|
|
32
|
+
},
|
|
33
|
+
handle: async (tab, params, response) => {
|
|
34
|
+
response.addCode(`// Move mouse to (${params.x}, ${params.y})`);
|
|
35
|
+
response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
|
|
36
|
+
await tab.waitForCompletion(async () => {
|
|
37
|
+
await tab.page.mouse.move(params.x, params.y);
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
const mouseClick = defineTabTool({
|
|
42
|
+
capability: 'vision',
|
|
43
|
+
schema: {
|
|
44
|
+
name: 'browser_mouse_click_xy',
|
|
45
|
+
title: 'Click',
|
|
46
|
+
description: 'Click left mouse button at a given position',
|
|
47
|
+
inputSchema: elementSchema.extend({
|
|
48
|
+
x: z.number().describe('X coordinate'),
|
|
49
|
+
y: z.number().describe('Y coordinate'),
|
|
50
|
+
}),
|
|
51
|
+
type: 'destructive',
|
|
52
|
+
},
|
|
53
|
+
handle: async (tab, params, response) => {
|
|
54
|
+
response.setIncludeSnapshot();
|
|
55
|
+
response.addCode(`// Click mouse at coordinates (${params.x}, ${params.y})`);
|
|
56
|
+
response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
|
|
57
|
+
response.addCode(`await page.mouse.down();`);
|
|
58
|
+
response.addCode(`await page.mouse.up();`);
|
|
59
|
+
await tab.waitForCompletion(async () => {
|
|
60
|
+
await tab.page.mouse.move(params.x, params.y);
|
|
61
|
+
await tab.page.mouse.down();
|
|
62
|
+
await tab.page.mouse.up();
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
const mouseDrag = defineTabTool({
|
|
67
|
+
capability: 'vision',
|
|
68
|
+
schema: {
|
|
69
|
+
name: 'browser_mouse_drag_xy',
|
|
70
|
+
title: 'Drag mouse',
|
|
71
|
+
description: 'Drag left mouse button to a given position',
|
|
72
|
+
inputSchema: elementSchema.extend({
|
|
73
|
+
startX: z.number().describe('Start X coordinate'),
|
|
74
|
+
startY: z.number().describe('Start Y coordinate'),
|
|
75
|
+
endX: z.number().describe('End X coordinate'),
|
|
76
|
+
endY: z.number().describe('End Y coordinate'),
|
|
77
|
+
}),
|
|
78
|
+
type: 'destructive',
|
|
79
|
+
},
|
|
80
|
+
handle: async (tab, params, response) => {
|
|
81
|
+
response.setIncludeSnapshot();
|
|
82
|
+
response.addCode(`// Drag mouse from (${params.startX}, ${params.startY}) to (${params.endX}, ${params.endY})`);
|
|
83
|
+
response.addCode(`await page.mouse.move(${params.startX}, ${params.startY});`);
|
|
84
|
+
response.addCode(`await page.mouse.down();`);
|
|
85
|
+
response.addCode(`await page.mouse.move(${params.endX}, ${params.endY});`);
|
|
86
|
+
response.addCode(`await page.mouse.up();`);
|
|
87
|
+
await tab.waitForCompletion(async () => {
|
|
88
|
+
await tab.page.mouse.move(params.startX, params.startY);
|
|
89
|
+
await tab.page.mouse.down();
|
|
90
|
+
await tab.page.mouse.move(params.endX, params.endY);
|
|
91
|
+
await tab.page.mouse.up();
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
export default [
|
|
96
|
+
mouseMove,
|
|
97
|
+
mouseClick,
|
|
98
|
+
mouseDrag,
|
|
99
|
+
];
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { defineTool, defineTabTool } from './tool.js';
|
|
18
|
+
const navigate = defineTool({
|
|
19
|
+
capability: 'core',
|
|
20
|
+
schema: {
|
|
21
|
+
name: 'browser_navigate',
|
|
22
|
+
title: 'Navigate to a URL',
|
|
23
|
+
description: 'Navigate to a URL',
|
|
24
|
+
inputSchema: z.object({
|
|
25
|
+
url: z.string().describe('The URL to navigate to'),
|
|
26
|
+
}),
|
|
27
|
+
type: 'destructive',
|
|
28
|
+
},
|
|
29
|
+
handle: async (context, params, response) => {
|
|
30
|
+
const tab = await context.ensureTab();
|
|
31
|
+
await tab.navigate(params.url);
|
|
32
|
+
response.setIncludeSnapshot();
|
|
33
|
+
response.addCode(`await page.goto('${params.url}');`);
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
const goBack = defineTabTool({
|
|
37
|
+
capability: 'core',
|
|
38
|
+
schema: {
|
|
39
|
+
name: 'browser_navigate_back',
|
|
40
|
+
title: 'Go back',
|
|
41
|
+
description: 'Go back to the previous page',
|
|
42
|
+
inputSchema: z.object({}),
|
|
43
|
+
type: 'readOnly',
|
|
44
|
+
},
|
|
45
|
+
handle: async (tab, params, response) => {
|
|
46
|
+
await tab.page.goBack();
|
|
47
|
+
response.setIncludeSnapshot();
|
|
48
|
+
response.addCode(`await page.goBack();`);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
const goForward = defineTabTool({
|
|
52
|
+
capability: 'core',
|
|
53
|
+
schema: {
|
|
54
|
+
name: 'browser_navigate_forward',
|
|
55
|
+
title: 'Go forward',
|
|
56
|
+
description: 'Go forward to the next page',
|
|
57
|
+
inputSchema: z.object({}),
|
|
58
|
+
type: 'readOnly',
|
|
59
|
+
},
|
|
60
|
+
handle: async (tab, params, response) => {
|
|
61
|
+
await tab.page.goForward();
|
|
62
|
+
response.setIncludeSnapshot();
|
|
63
|
+
response.addCode(`await page.goForward();`);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
export default [
|
|
67
|
+
navigate,
|
|
68
|
+
goBack,
|
|
69
|
+
goForward,
|
|
70
|
+
];
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { defineTabTool } from './tool.js';
|
|
18
|
+
const requests = defineTabTool({
|
|
19
|
+
capability: 'core',
|
|
20
|
+
schema: {
|
|
21
|
+
name: 'browser_network_requests',
|
|
22
|
+
title: 'List network requests',
|
|
23
|
+
description: 'Returns all network requests since loading the page',
|
|
24
|
+
inputSchema: z.object({}),
|
|
25
|
+
type: 'readOnly',
|
|
26
|
+
},
|
|
27
|
+
handle: async (tab, params, response) => {
|
|
28
|
+
const requests = tab.requests();
|
|
29
|
+
[...requests.entries()].forEach(([req, res]) => response.addResult(renderRequest(req, res)));
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
function renderRequest(request, response) {
|
|
33
|
+
const result = [];
|
|
34
|
+
result.push(`[${request.method().toUpperCase()}] ${request.url()}`);
|
|
35
|
+
if (response)
|
|
36
|
+
result.push(`=> [${response.status()}] ${response.statusText()}`);
|
|
37
|
+
return result.join(' ');
|
|
38
|
+
}
|
|
39
|
+
export default [
|
|
40
|
+
requests,
|
|
41
|
+
];
|
package/lib/tools/pdf.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { defineTabTool } from './tool.js';
|
|
18
|
+
import * as javascript from '../utils/codegen.js';
|
|
19
|
+
const pdfSchema = z.object({
|
|
20
|
+
filename: z.string().optional().describe('File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.'),
|
|
21
|
+
});
|
|
22
|
+
const pdf = defineTabTool({
|
|
23
|
+
capability: 'pdf',
|
|
24
|
+
schema: {
|
|
25
|
+
name: 'browser_pdf_save',
|
|
26
|
+
title: 'Save as PDF',
|
|
27
|
+
description: 'Save page as PDF',
|
|
28
|
+
inputSchema: pdfSchema,
|
|
29
|
+
type: 'readOnly',
|
|
30
|
+
},
|
|
31
|
+
handle: async (tab, params, response) => {
|
|
32
|
+
const fileName = await tab.context.outputFile(params.filename ?? `page-${new Date().toISOString()}.pdf`);
|
|
33
|
+
response.addCode(`await page.pdf(${javascript.formatObject({ path: fileName })});`);
|
|
34
|
+
response.addResult(`Saved page as ${fileName}`);
|
|
35
|
+
await tab.page.pdf({ path: fileName });
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
export default [
|
|
39
|
+
pdf,
|
|
40
|
+
];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { defineTool } from './tool.js';
|
|
18
|
+
const roxyConnect = defineTool({
|
|
19
|
+
capability: 'core',
|
|
20
|
+
schema: {
|
|
21
|
+
name: 'browser_connect_roxy',
|
|
22
|
+
title: 'Connect to RoxyBrowser',
|
|
23
|
+
description: 'Connect to RoxyBrowser using CDP WebSocket endpoint',
|
|
24
|
+
inputSchema: z.object({
|
|
25
|
+
cdpEndpoint: z.string().describe('The CDP WebSocket endpoint URL from RoxyBrowser (e.g., ws://127.0.0.1:59305/devtools/browser/4d876b0b-6adc-4e9f-b572-bb68ff02a199)'),
|
|
26
|
+
}),
|
|
27
|
+
type: 'destructive',
|
|
28
|
+
},
|
|
29
|
+
handle: async (context, params, response) => {
|
|
30
|
+
try {
|
|
31
|
+
// Validate CDP endpoint format
|
|
32
|
+
if (!params.cdpEndpoint.startsWith('ws://') && !params.cdpEndpoint.startsWith('wss://'))
|
|
33
|
+
throw new Error(`Invalid CDP endpoint format. Expected WebSocket URL starting with ws:// or wss://, got: ${params.cdpEndpoint}`);
|
|
34
|
+
// Use the reconnectToCDP method to connect to RoxyBrowser
|
|
35
|
+
await context.reconnectToCDP(params.cdpEndpoint);
|
|
36
|
+
// Verify connection by ensuring we have tabs or can create one
|
|
37
|
+
await context.ensureTab();
|
|
38
|
+
response.addResult(`Successfully connected to RoxyBrowser at ${params.cdpEndpoint}`);
|
|
39
|
+
response.addCode(`// Connected to RoxyBrowser CDP endpoint: ${params.cdpEndpoint}`);
|
|
40
|
+
// Take a snapshot to show current state
|
|
41
|
+
response.setIncludeSnapshot();
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
response.addError(`Failed to connect to RoxyBrowser: ${error.message}`);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
export default [
|
|
49
|
+
roxyConnect,
|
|
50
|
+
];
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { defineTabTool } from './tool.js';
|
|
18
|
+
import * as javascript from '../utils/codegen.js';
|
|
19
|
+
import { generateLocator } from './utils.js';
|
|
20
|
+
const screenshotSchema = z.object({
|
|
21
|
+
type: z.enum(['png', 'jpeg']).default('png').describe('Image format for the screenshot. Default is png.'),
|
|
22
|
+
filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
|
|
23
|
+
element: z.string().optional().describe('Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too.'),
|
|
24
|
+
ref: z.string().optional().describe('Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too.'),
|
|
25
|
+
fullPage: z.boolean().optional().describe('When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Cannot be used with element screenshots.'),
|
|
26
|
+
}).refine(data => {
|
|
27
|
+
return !!data.element === !!data.ref;
|
|
28
|
+
}, {
|
|
29
|
+
message: 'Both element and ref must be provided or neither.',
|
|
30
|
+
path: ['ref', 'element']
|
|
31
|
+
}).refine(data => {
|
|
32
|
+
return !(data.fullPage && (data.element || data.ref));
|
|
33
|
+
}, {
|
|
34
|
+
message: 'fullPage cannot be used with element screenshots.',
|
|
35
|
+
path: ['fullPage']
|
|
36
|
+
});
|
|
37
|
+
const screenshot = defineTabTool({
|
|
38
|
+
capability: 'core',
|
|
39
|
+
schema: {
|
|
40
|
+
name: 'browser_take_screenshot',
|
|
41
|
+
title: 'Take a screenshot',
|
|
42
|
+
description: `Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.`,
|
|
43
|
+
inputSchema: screenshotSchema,
|
|
44
|
+
type: 'readOnly',
|
|
45
|
+
},
|
|
46
|
+
handle: async (tab, params, response) => {
|
|
47
|
+
const fileType = params.type || 'png';
|
|
48
|
+
const fileName = await tab.context.outputFile(params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
|
|
49
|
+
const options = {
|
|
50
|
+
type: fileType,
|
|
51
|
+
quality: fileType === 'png' ? undefined : 90,
|
|
52
|
+
scale: 'css',
|
|
53
|
+
path: fileName,
|
|
54
|
+
...(params.fullPage !== undefined && { fullPage: params.fullPage })
|
|
55
|
+
};
|
|
56
|
+
const isElementScreenshot = params.element && params.ref;
|
|
57
|
+
const screenshotTarget = isElementScreenshot ? params.element : (params.fullPage ? 'full page' : 'viewport');
|
|
58
|
+
response.addCode(`// Screenshot ${screenshotTarget} and save it as ${fileName}`);
|
|
59
|
+
// Only get snapshot when element screenshot is needed
|
|
60
|
+
const locator = params.ref ? await tab.refLocator({ element: params.element || '', ref: params.ref }) : null;
|
|
61
|
+
if (locator)
|
|
62
|
+
response.addCode(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
|
|
63
|
+
else
|
|
64
|
+
response.addCode(`await page.screenshot(${javascript.formatObject(options)});`);
|
|
65
|
+
const buffer = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
|
|
66
|
+
response.addResult(`Took the ${screenshotTarget} screenshot and saved it as ${fileName}`);
|
|
67
|
+
// https://github.com/microsoft/playwright-mcp/issues/817
|
|
68
|
+
// Never return large images to LLM, saving them to the file system is enough.
|
|
69
|
+
if (!params.fullPage) {
|
|
70
|
+
response.addImage({
|
|
71
|
+
contentType: fileType === 'png' ? 'image/png' : 'image/jpeg',
|
|
72
|
+
data: buffer
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
export default [
|
|
78
|
+
screenshot,
|
|
79
|
+
];
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { defineTabTool, defineTool } from './tool.js';
|
|
18
|
+
import * as javascript from '../utils/codegen.js';
|
|
19
|
+
import { generateLocator } from './utils.js';
|
|
20
|
+
const snapshot = defineTool({
|
|
21
|
+
capability: 'core',
|
|
22
|
+
schema: {
|
|
23
|
+
name: 'browser_snapshot',
|
|
24
|
+
title: 'Page snapshot',
|
|
25
|
+
description: 'Capture accessibility snapshot of the current page, this is better than screenshot',
|
|
26
|
+
inputSchema: z.object({}),
|
|
27
|
+
type: 'readOnly',
|
|
28
|
+
},
|
|
29
|
+
handle: async (context, params, response) => {
|
|
30
|
+
await context.ensureTab();
|
|
31
|
+
response.setIncludeSnapshot();
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
export const elementSchema = z.object({
|
|
35
|
+
element: z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
|
|
36
|
+
ref: z.string().describe('Exact target element reference from the page snapshot'),
|
|
37
|
+
});
|
|
38
|
+
const clickSchema = elementSchema.extend({
|
|
39
|
+
doubleClick: z.boolean().optional().describe('Whether to perform a double click instead of a single click'),
|
|
40
|
+
button: z.enum(['left', 'right', 'middle']).optional().describe('Button to click, defaults to left'),
|
|
41
|
+
});
|
|
42
|
+
const click = defineTabTool({
|
|
43
|
+
capability: 'core',
|
|
44
|
+
schema: {
|
|
45
|
+
name: 'browser_click',
|
|
46
|
+
title: 'Click',
|
|
47
|
+
description: 'Perform click on a web page',
|
|
48
|
+
inputSchema: clickSchema,
|
|
49
|
+
type: 'destructive',
|
|
50
|
+
},
|
|
51
|
+
handle: async (tab, params, response) => {
|
|
52
|
+
response.setIncludeSnapshot();
|
|
53
|
+
const locator = await tab.refLocator(params);
|
|
54
|
+
const button = params.button;
|
|
55
|
+
const buttonAttr = button ? `{ button: '${button}' }` : '';
|
|
56
|
+
if (params.doubleClick)
|
|
57
|
+
response.addCode(`await page.${await generateLocator(locator)}.dblclick(${buttonAttr});`);
|
|
58
|
+
else
|
|
59
|
+
response.addCode(`await page.${await generateLocator(locator)}.click(${buttonAttr});`);
|
|
60
|
+
await tab.waitForCompletion(async () => {
|
|
61
|
+
if (params.doubleClick)
|
|
62
|
+
await locator.dblclick({ button });
|
|
63
|
+
else
|
|
64
|
+
await locator.click({ button });
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
const drag = defineTabTool({
|
|
69
|
+
capability: 'core',
|
|
70
|
+
schema: {
|
|
71
|
+
name: 'browser_drag',
|
|
72
|
+
title: 'Drag mouse',
|
|
73
|
+
description: 'Perform drag and drop between two elements',
|
|
74
|
+
inputSchema: z.object({
|
|
75
|
+
startElement: z.string().describe('Human-readable source element description used to obtain the permission to interact with the element'),
|
|
76
|
+
startRef: z.string().describe('Exact source element reference from the page snapshot'),
|
|
77
|
+
endElement: z.string().describe('Human-readable target element description used to obtain the permission to interact with the element'),
|
|
78
|
+
endRef: z.string().describe('Exact target element reference from the page snapshot'),
|
|
79
|
+
}),
|
|
80
|
+
type: 'destructive',
|
|
81
|
+
},
|
|
82
|
+
handle: async (tab, params, response) => {
|
|
83
|
+
response.setIncludeSnapshot();
|
|
84
|
+
const [startLocator, endLocator] = await tab.refLocators([
|
|
85
|
+
{ ref: params.startRef, element: params.startElement },
|
|
86
|
+
{ ref: params.endRef, element: params.endElement },
|
|
87
|
+
]);
|
|
88
|
+
await tab.waitForCompletion(async () => {
|
|
89
|
+
await startLocator.dragTo(endLocator);
|
|
90
|
+
});
|
|
91
|
+
response.addCode(`await page.${await generateLocator(startLocator)}.dragTo(page.${await generateLocator(endLocator)});`);
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
const hover = defineTabTool({
|
|
95
|
+
capability: 'core',
|
|
96
|
+
schema: {
|
|
97
|
+
name: 'browser_hover',
|
|
98
|
+
title: 'Hover mouse',
|
|
99
|
+
description: 'Hover over element on page',
|
|
100
|
+
inputSchema: elementSchema,
|
|
101
|
+
type: 'readOnly',
|
|
102
|
+
},
|
|
103
|
+
handle: async (tab, params, response) => {
|
|
104
|
+
response.setIncludeSnapshot();
|
|
105
|
+
const locator = await tab.refLocator(params);
|
|
106
|
+
response.addCode(`await page.${await generateLocator(locator)}.hover();`);
|
|
107
|
+
await tab.waitForCompletion(async () => {
|
|
108
|
+
await locator.hover();
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
const selectOptionSchema = elementSchema.extend({
|
|
113
|
+
values: z.array(z.string()).describe('Array of values to select in the dropdown. This can be a single value or multiple values.'),
|
|
114
|
+
});
|
|
115
|
+
const selectOption = defineTabTool({
|
|
116
|
+
capability: 'core',
|
|
117
|
+
schema: {
|
|
118
|
+
name: 'browser_select_option',
|
|
119
|
+
title: 'Select option',
|
|
120
|
+
description: 'Select an option in a dropdown',
|
|
121
|
+
inputSchema: selectOptionSchema,
|
|
122
|
+
type: 'destructive',
|
|
123
|
+
},
|
|
124
|
+
handle: async (tab, params, response) => {
|
|
125
|
+
response.setIncludeSnapshot();
|
|
126
|
+
const locator = await tab.refLocator(params);
|
|
127
|
+
response.addCode(`await page.${await generateLocator(locator)}.selectOption(${javascript.formatObject(params.values)});`);
|
|
128
|
+
await tab.waitForCompletion(async () => {
|
|
129
|
+
await locator.selectOption(params.values);
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
export default [
|
|
134
|
+
snapshot,
|
|
135
|
+
click,
|
|
136
|
+
drag,
|
|
137
|
+
hover,
|
|
138
|
+
selectOption,
|
|
139
|
+
];
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { defineTool } from './tool.js';
|
|
18
|
+
const listTabs = defineTool({
|
|
19
|
+
capability: 'core-tabs',
|
|
20
|
+
schema: {
|
|
21
|
+
name: 'browser_tab_list',
|
|
22
|
+
title: 'List tabs',
|
|
23
|
+
description: 'List browser tabs',
|
|
24
|
+
inputSchema: z.object({}),
|
|
25
|
+
type: 'readOnly',
|
|
26
|
+
},
|
|
27
|
+
handle: async (context, params, response) => {
|
|
28
|
+
await context.ensureTab();
|
|
29
|
+
response.setIncludeTabs();
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
const selectTab = defineTool({
|
|
33
|
+
capability: 'core-tabs',
|
|
34
|
+
schema: {
|
|
35
|
+
name: 'browser_tab_select',
|
|
36
|
+
title: 'Select a tab',
|
|
37
|
+
description: 'Select a tab by index',
|
|
38
|
+
inputSchema: z.object({
|
|
39
|
+
index: z.number().describe('The index of the tab to select'),
|
|
40
|
+
}),
|
|
41
|
+
type: 'readOnly',
|
|
42
|
+
},
|
|
43
|
+
handle: async (context, params, response) => {
|
|
44
|
+
await context.selectTab(params.index);
|
|
45
|
+
response.setIncludeSnapshot();
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
const newTab = defineTool({
|
|
49
|
+
capability: 'core-tabs',
|
|
50
|
+
schema: {
|
|
51
|
+
name: 'browser_tab_new',
|
|
52
|
+
title: 'Open a new tab',
|
|
53
|
+
description: 'Open a new tab',
|
|
54
|
+
inputSchema: z.object({
|
|
55
|
+
url: z.string().optional().describe('The URL to navigate to in the new tab. If not provided, the new tab will be blank.'),
|
|
56
|
+
}),
|
|
57
|
+
type: 'readOnly',
|
|
58
|
+
},
|
|
59
|
+
handle: async (context, params, response) => {
|
|
60
|
+
const tab = await context.newTab();
|
|
61
|
+
if (params.url)
|
|
62
|
+
await tab.navigate(params.url);
|
|
63
|
+
response.setIncludeSnapshot();
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
const closeTab = defineTool({
|
|
67
|
+
capability: 'core-tabs',
|
|
68
|
+
schema: {
|
|
69
|
+
name: 'browser_tab_close',
|
|
70
|
+
title: 'Close a tab',
|
|
71
|
+
description: 'Close a tab',
|
|
72
|
+
inputSchema: z.object({
|
|
73
|
+
index: z.number().optional().describe('The index of the tab to close. Closes current tab if not provided.'),
|
|
74
|
+
}),
|
|
75
|
+
type: 'destructive',
|
|
76
|
+
},
|
|
77
|
+
handle: async (context, params, response) => {
|
|
78
|
+
await context.closeTab(params.index);
|
|
79
|
+
response.setIncludeSnapshot();
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
export default [
|
|
83
|
+
listTabs,
|
|
84
|
+
newTab,
|
|
85
|
+
selectTab,
|
|
86
|
+
closeTab,
|
|
87
|
+
];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export function defineTool(tool) {
|
|
17
|
+
return tool;
|
|
18
|
+
}
|
|
19
|
+
export function defineTabTool(tool) {
|
|
20
|
+
return {
|
|
21
|
+
...tool,
|
|
22
|
+
handle: async (context, params, response) => {
|
|
23
|
+
const tab = context.currentTabOrDie();
|
|
24
|
+
const modalStates = tab.modalStates().map(state => state.type);
|
|
25
|
+
if (tool.clearsModalState && !modalStates.includes(tool.clearsModalState))
|
|
26
|
+
response.addError(`Error: The tool "${tool.schema.name}" can only be used when there is related modal state present.\n` + tab.modalStatesMarkdown().join('\n'));
|
|
27
|
+
else if (!tool.clearsModalState && modalStates.length)
|
|
28
|
+
response.addError(`Error: Tool "${tool.schema.name}" does not handle the modal state.\n` + tab.modalStatesMarkdown().join('\n'));
|
|
29
|
+
else
|
|
30
|
+
return tool.handle(tab, params, response);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|