@playwright/mcp 0.0.24 → 0.0.26
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 +354 -179
- package/config.d.ts +10 -0
- package/lib/config.js +63 -12
- package/lib/connection.js +2 -2
- package/lib/context.js +69 -50
- package/lib/index.js +3 -1
- package/lib/pageSnapshot.js +6 -2
- package/lib/program.js +34 -15
- package/lib/tab.js +9 -2
- package/lib/tools/common.js +1 -41
- package/lib/tools/screenshot.js +77 -0
- package/lib/tools/snapshot.js +1 -59
- package/lib/tools/utils.js +14 -10
- package/lib/tools/wait.js +59 -0
- package/lib/tools.js +8 -3
- package/lib/transport.js +1 -1
- package/package.json +1 -1
- /package/lib/tools/{screen.js → vision.js} +0 -0
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
import * as javascript from '../javascript.js';
|
|
19
|
+
import { outputFile } from '../config.js';
|
|
20
|
+
import { generateLocator } from './utils.js';
|
|
21
|
+
const screenshotSchema = z.object({
|
|
22
|
+
raw: z.boolean().optional().describe('Whether to return without compression (in PNG format). Default is false, which returns a JPEG image.'),
|
|
23
|
+
filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
|
|
24
|
+
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.'),
|
|
25
|
+
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.'),
|
|
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
|
+
});
|
|
32
|
+
const screenshot = defineTool({
|
|
33
|
+
capability: 'core',
|
|
34
|
+
schema: {
|
|
35
|
+
name: 'browser_take_screenshot',
|
|
36
|
+
title: 'Take a screenshot',
|
|
37
|
+
description: `Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.`,
|
|
38
|
+
inputSchema: screenshotSchema,
|
|
39
|
+
type: 'readOnly',
|
|
40
|
+
},
|
|
41
|
+
handle: async (context, params) => {
|
|
42
|
+
const tab = context.currentTabOrDie();
|
|
43
|
+
const snapshot = tab.snapshotOrDie();
|
|
44
|
+
const fileType = params.raw ? 'png' : 'jpeg';
|
|
45
|
+
const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
|
|
46
|
+
const options = { type: fileType, quality: fileType === 'png' ? undefined : 50, scale: 'css', path: fileName };
|
|
47
|
+
const isElementScreenshot = params.element && params.ref;
|
|
48
|
+
const code = [
|
|
49
|
+
`// Screenshot ${isElementScreenshot ? params.element : 'viewport'} and save it as ${fileName}`,
|
|
50
|
+
];
|
|
51
|
+
const locator = params.ref ? snapshot.refLocator(params.ref) : null;
|
|
52
|
+
if (locator)
|
|
53
|
+
code.push(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
|
|
54
|
+
else
|
|
55
|
+
code.push(`await page.screenshot(${javascript.formatObject(options)});`);
|
|
56
|
+
const includeBase64 = !context.config.noImageResponses;
|
|
57
|
+
const action = async () => {
|
|
58
|
+
const screenshot = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
|
|
59
|
+
return {
|
|
60
|
+
content: includeBase64 ? [{
|
|
61
|
+
type: 'image',
|
|
62
|
+
data: screenshot.toString('base64'),
|
|
63
|
+
mimeType: fileType === 'png' ? 'image/png' : 'image/jpeg',
|
|
64
|
+
}] : []
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
code,
|
|
69
|
+
action,
|
|
70
|
+
captureSnapshot: true,
|
|
71
|
+
waitForNetwork: false,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
export default [
|
|
76
|
+
screenshot,
|
|
77
|
+
];
|
package/lib/tools/snapshot.js
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
import { z } from 'zod';
|
|
17
17
|
import { defineTool } from './tool.js';
|
|
18
18
|
import * as javascript from '../javascript.js';
|
|
19
|
-
import {
|
|
19
|
+
import { generateLocator } from './utils.js';
|
|
20
20
|
const snapshot = defineTool({
|
|
21
21
|
capability: 'core',
|
|
22
22
|
schema: {
|
|
@@ -186,63 +186,6 @@ const selectOption = defineTool({
|
|
|
186
186
|
};
|
|
187
187
|
},
|
|
188
188
|
});
|
|
189
|
-
const screenshotSchema = z.object({
|
|
190
|
-
raw: z.boolean().optional().describe('Whether to return without compression (in PNG format). Default is false, which returns a JPEG image.'),
|
|
191
|
-
filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
|
|
192
|
-
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.'),
|
|
193
|
-
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.'),
|
|
194
|
-
}).refine(data => {
|
|
195
|
-
return !!data.element === !!data.ref;
|
|
196
|
-
}, {
|
|
197
|
-
message: 'Both element and ref must be provided or neither.',
|
|
198
|
-
path: ['ref', 'element']
|
|
199
|
-
});
|
|
200
|
-
const screenshot = defineTool({
|
|
201
|
-
capability: 'core',
|
|
202
|
-
schema: {
|
|
203
|
-
name: 'browser_take_screenshot',
|
|
204
|
-
title: 'Take a screenshot',
|
|
205
|
-
description: `Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.`,
|
|
206
|
-
inputSchema: screenshotSchema,
|
|
207
|
-
type: 'readOnly',
|
|
208
|
-
},
|
|
209
|
-
handle: async (context, params) => {
|
|
210
|
-
const tab = context.currentTabOrDie();
|
|
211
|
-
const snapshot = tab.snapshotOrDie();
|
|
212
|
-
const fileType = params.raw ? 'png' : 'jpeg';
|
|
213
|
-
const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
|
|
214
|
-
const options = { type: fileType, quality: fileType === 'png' ? undefined : 50, scale: 'css', path: fileName };
|
|
215
|
-
const isElementScreenshot = params.element && params.ref;
|
|
216
|
-
const code = [
|
|
217
|
-
`// Screenshot ${isElementScreenshot ? params.element : 'viewport'} and save it as ${fileName}`,
|
|
218
|
-
];
|
|
219
|
-
const locator = params.ref ? snapshot.refLocator(params.ref) : null;
|
|
220
|
-
if (locator)
|
|
221
|
-
code.push(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`);
|
|
222
|
-
else
|
|
223
|
-
code.push(`await page.screenshot(${javascript.formatObject(options)});`);
|
|
224
|
-
const includeBase64 = !context.config.noImageResponses;
|
|
225
|
-
const action = async () => {
|
|
226
|
-
const screenshot = locator ? await locator.screenshot(options) : await tab.page.screenshot(options);
|
|
227
|
-
return {
|
|
228
|
-
content: includeBase64 ? [{
|
|
229
|
-
type: 'image',
|
|
230
|
-
data: screenshot.toString('base64'),
|
|
231
|
-
mimeType: fileType === 'png' ? 'image/png' : 'image/jpeg',
|
|
232
|
-
}] : []
|
|
233
|
-
};
|
|
234
|
-
};
|
|
235
|
-
return {
|
|
236
|
-
code,
|
|
237
|
-
action,
|
|
238
|
-
captureSnapshot: true,
|
|
239
|
-
waitForNetwork: false,
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
export async function generateLocator(locator) {
|
|
244
|
-
return locator._generateLocatorString();
|
|
245
|
-
}
|
|
246
189
|
export default [
|
|
247
190
|
snapshot,
|
|
248
191
|
click,
|
|
@@ -250,5 +193,4 @@ export default [
|
|
|
250
193
|
hover,
|
|
251
194
|
type,
|
|
252
195
|
selectOption,
|
|
253
|
-
screenshot,
|
|
254
196
|
];
|
package/lib/tools/utils.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
export async function waitForCompletion(context,
|
|
16
|
+
export async function waitForCompletion(context, tab, callback) {
|
|
17
17
|
const requests = new Set();
|
|
18
18
|
let frameNavigated = false;
|
|
19
19
|
let waitCallback = () => { };
|
|
@@ -30,22 +30,20 @@ export async function waitForCompletion(context, page, callback) {
|
|
|
30
30
|
frameNavigated = true;
|
|
31
31
|
dispose();
|
|
32
32
|
clearTimeout(timeout);
|
|
33
|
-
void
|
|
34
|
-
waitCallback();
|
|
35
|
-
});
|
|
33
|
+
void tab.waitForLoadState('load').then(waitCallback);
|
|
36
34
|
};
|
|
37
35
|
const onTimeout = () => {
|
|
38
36
|
dispose();
|
|
39
37
|
waitCallback();
|
|
40
38
|
};
|
|
41
|
-
page.on('request', requestListener);
|
|
42
|
-
page.on('requestfinished', requestFinishedListener);
|
|
43
|
-
page.on('framenavigated', frameNavigateListener);
|
|
39
|
+
tab.page.on('request', requestListener);
|
|
40
|
+
tab.page.on('requestfinished', requestFinishedListener);
|
|
41
|
+
tab.page.on('framenavigated', frameNavigateListener);
|
|
44
42
|
const timeout = setTimeout(onTimeout, 10000);
|
|
45
43
|
const dispose = () => {
|
|
46
|
-
page.off('request', requestListener);
|
|
47
|
-
page.off('requestfinished', requestFinishedListener);
|
|
48
|
-
page.off('framenavigated', frameNavigateListener);
|
|
44
|
+
tab.page.off('request', requestListener);
|
|
45
|
+
tab.page.off('requestfinished', requestFinishedListener);
|
|
46
|
+
tab.page.off('framenavigated', frameNavigateListener);
|
|
49
47
|
clearTimeout(timeout);
|
|
50
48
|
};
|
|
51
49
|
try {
|
|
@@ -67,3 +65,9 @@ export function sanitizeForFilePath(s) {
|
|
|
67
65
|
return sanitize(s);
|
|
68
66
|
return sanitize(s.substring(0, separator)) + '.' + sanitize(s.substring(separator + 1));
|
|
69
67
|
}
|
|
68
|
+
export async function generateLocator(locator) {
|
|
69
|
+
return locator._frame._wrapApiCall(() => locator._generateLocatorString(), true);
|
|
70
|
+
}
|
|
71
|
+
export async function callOnPageNoTrace(page, callback) {
|
|
72
|
+
return await page._wrapApiCall(() => callback(page), true);
|
|
73
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
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 wait = captureSnapshot => defineTool({
|
|
19
|
+
capability: 'wait',
|
|
20
|
+
schema: {
|
|
21
|
+
name: 'browser_wait_for',
|
|
22
|
+
title: 'Wait for',
|
|
23
|
+
description: 'Wait for text to appear or disappear or a specified time to pass',
|
|
24
|
+
inputSchema: z.object({
|
|
25
|
+
time: z.number().optional().describe('The time to wait in seconds'),
|
|
26
|
+
text: z.string().optional().describe('The text to wait for'),
|
|
27
|
+
textGone: z.string().optional().describe('The text to wait for to disappear'),
|
|
28
|
+
}),
|
|
29
|
+
type: 'readOnly',
|
|
30
|
+
},
|
|
31
|
+
handle: async (context, params) => {
|
|
32
|
+
if (!params.text && !params.textGone && !params.time)
|
|
33
|
+
throw new Error('Either time, text or textGone must be provided');
|
|
34
|
+
const code = [];
|
|
35
|
+
if (params.time) {
|
|
36
|
+
code.push(`await new Promise(f => setTimeout(f, ${params.time} * 1000));`);
|
|
37
|
+
await new Promise(f => setTimeout(f, Math.min(10000, params.time * 1000)));
|
|
38
|
+
}
|
|
39
|
+
const tab = context.currentTabOrDie();
|
|
40
|
+
const locator = params.text ? tab.page.getByText(params.text).first() : undefined;
|
|
41
|
+
const goneLocator = params.textGone ? tab.page.getByText(params.textGone).first() : undefined;
|
|
42
|
+
if (goneLocator) {
|
|
43
|
+
code.push(`await page.getByText(${JSON.stringify(params.textGone)}).first().waitFor({ state: 'hidden' });`);
|
|
44
|
+
await goneLocator.waitFor({ state: 'hidden' });
|
|
45
|
+
}
|
|
46
|
+
if (locator) {
|
|
47
|
+
code.push(`await page.getByText(${JSON.stringify(params.text)}).first().waitFor({ state: 'visible' });`);
|
|
48
|
+
await locator.waitFor({ state: 'visible' });
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
code,
|
|
52
|
+
captureSnapshot,
|
|
53
|
+
waitForNetwork: false,
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
export default (captureSnapshot) => [
|
|
58
|
+
wait(captureSnapshot),
|
|
59
|
+
];
|
package/lib/tools.js
CHANGED
|
@@ -24,8 +24,10 @@ import network from './tools/network.js';
|
|
|
24
24
|
import pdf from './tools/pdf.js';
|
|
25
25
|
import snapshot from './tools/snapshot.js';
|
|
26
26
|
import tabs from './tools/tabs.js';
|
|
27
|
-
import
|
|
27
|
+
import screenshot from './tools/screenshot.js';
|
|
28
28
|
import testing from './tools/testing.js';
|
|
29
|
+
import vision from './tools/vision.js';
|
|
30
|
+
import wait from './tools/wait.js';
|
|
29
31
|
export const snapshotTools = [
|
|
30
32
|
...common(true),
|
|
31
33
|
...console,
|
|
@@ -36,11 +38,13 @@ export const snapshotTools = [
|
|
|
36
38
|
...navigate(true),
|
|
37
39
|
...network,
|
|
38
40
|
...pdf,
|
|
41
|
+
...screenshot,
|
|
39
42
|
...snapshot,
|
|
40
43
|
...tabs(true),
|
|
41
44
|
...testing,
|
|
45
|
+
...wait(true),
|
|
42
46
|
];
|
|
43
|
-
export const
|
|
47
|
+
export const visionTools = [
|
|
44
48
|
...common(false),
|
|
45
49
|
...console,
|
|
46
50
|
...dialogs(false),
|
|
@@ -50,7 +54,8 @@ export const screenshotTools = [
|
|
|
50
54
|
...navigate(false),
|
|
51
55
|
...network,
|
|
52
56
|
...pdf,
|
|
53
|
-
...screen,
|
|
54
57
|
...tabs(false),
|
|
55
58
|
...testing,
|
|
59
|
+
...vision,
|
|
60
|
+
...wait(false),
|
|
56
61
|
];
|
package/lib/transport.js
CHANGED
|
@@ -127,6 +127,6 @@ export function startHttpTransport(config, port, hostname, connectionList) {
|
|
|
127
127
|
'If your client supports streamable HTTP, you can use the /mcp endpoint instead.',
|
|
128
128
|
].join('\n');
|
|
129
129
|
// eslint-disable-next-line no-console
|
|
130
|
-
console.
|
|
130
|
+
console.error(message);
|
|
131
131
|
});
|
|
132
132
|
}
|
package/package.json
CHANGED
|
File without changes
|