@szymonrybczak/playwright-mcp 0.0.2 → 0.0.5
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/index.d.ts +20 -8
- package/lib/cjs/_virtual/_commonjsHelpers.js +9 -0
- package/lib/cjs/_virtual/browser.js +11 -0
- package/lib/cjs/_virtual/browser2.js +7 -0
- package/lib/cjs/cloudflare/package.json.js +10 -0
- package/lib/cjs/index.js +27 -0
- package/lib/cjs/node_modules/debug/src/browser.js +290 -0
- package/lib/cjs/node_modules/debug/src/common.js +307 -0
- package/lib/cjs/node_modules/ms/index.js +176 -0
- package/lib/cjs/package.js +9 -0
- package/lib/cjs/src/browserContextFactory.js +240 -0
- package/lib/cjs/src/config.js +77 -0
- package/lib/cjs/src/connection.js +74 -0
- package/lib/cjs/src/context.js +281 -0
- package/lib/cjs/src/fileUtils.js +25 -0
- package/lib/cjs/src/index.js +15 -0
- package/lib/cjs/src/javascript.js +41 -0
- package/lib/cjs/src/manualPromise.js +39 -0
- package/lib/cjs/src/pageSnapshot.js +35 -0
- package/lib/cjs/src/tab.js +91 -0
- package/lib/cjs/src/tools/common.js +60 -0
- package/lib/cjs/src/tools/console.js +36 -0
- package/lib/cjs/src/tools/dialogs.js +44 -0
- package/lib/cjs/src/tools/files.js +43 -0
- package/lib/cjs/src/tools/install.js +49 -0
- package/lib/cjs/src/tools/keyboard.js +38 -0
- package/lib/cjs/src/tools/navigate.js +85 -0
- package/lib/cjs/src/tools/network.js +43 -0
- package/lib/cjs/src/tools/pdf.js +42 -0
- package/lib/cjs/src/tools/screenshot.js +69 -0
- package/lib/cjs/src/tools/snapshot.js +195 -0
- package/lib/cjs/src/tools/tabs.js +110 -0
- package/lib/cjs/src/tools/testing.js +52 -0
- package/lib/cjs/src/tools/tool.js +9 -0
- package/lib/cjs/src/tools/utils.js +75 -0
- package/lib/cjs/src/tools/vision.js +181 -0
- package/lib/cjs/src/tools/wait.js +51 -0
- package/lib/cjs/src/tools.js +54 -0
- package/lib/esm/_virtual/_commonjsHelpers.js +5 -0
- package/lib/esm/_virtual/browser.js +7 -0
- package/lib/esm/_virtual/browser2.js +3 -0
- package/lib/esm/cloudflare/package.json.js +5 -0
- package/lib/esm/index.js +23 -0
- package/lib/esm/node_modules/debug/src/browser.js +286 -0
- package/lib/esm/node_modules/debug/src/common.js +303 -0
- package/lib/esm/node_modules/ms/index.js +172 -0
- package/lib/esm/package.js +5 -0
- package/lib/esm/src/browserContextFactory.js +216 -0
- package/lib/esm/src/config.js +72 -0
- package/lib/esm/src/connection.js +69 -0
- package/lib/esm/src/context.js +277 -0
- package/lib/esm/src/fileUtils.js +20 -0
- package/lib/esm/src/index.js +11 -0
- package/lib/esm/src/javascript.js +35 -0
- package/lib/esm/src/manualPromise.js +35 -0
- package/lib/esm/src/pageSnapshot.js +31 -0
- package/lib/esm/src/tab.js +87 -0
- package/lib/esm/src/tools/common.js +56 -0
- package/lib/esm/src/tools/console.js +32 -0
- package/lib/esm/src/tools/dialogs.js +40 -0
- package/lib/esm/src/tools/files.js +39 -0
- package/lib/esm/src/tools/install.js +45 -0
- package/lib/esm/src/tools/keyboard.js +34 -0
- package/lib/esm/src/tools/navigate.js +81 -0
- package/lib/esm/src/tools/network.js +39 -0
- package/lib/esm/src/tools/pdf.js +38 -0
- package/lib/esm/src/tools/screenshot.js +65 -0
- package/lib/esm/src/tools/snapshot.js +191 -0
- package/lib/esm/src/tools/tabs.js +106 -0
- package/lib/esm/src/tools/testing.js +48 -0
- package/lib/esm/src/tools/tool.js +5 -0
- package/lib/esm/src/tools/utils.js +68 -0
- package/lib/esm/src/tools/vision.js +177 -0
- package/lib/esm/src/tools/wait.js +47 -0
- package/lib/esm/src/tools.js +49 -0
- package/package.json +15 -44
- package/LICENSE +0 -202
- package/README.md +0 -508
- package/cli.js +0 -18
- package/config.d.ts +0 -128
- package/index.js +0 -19
- package/lib/browserContextFactory.js +0 -227
- package/lib/browserServer.js +0 -151
- package/lib/config.js +0 -189
- package/lib/connection.js +0 -82
- package/lib/context.js +0 -291
- package/lib/fileUtils.js +0 -32
- package/lib/httpServer.js +0 -201
- package/lib/index.js +0 -36
- package/lib/javascript.js +0 -49
- package/lib/manualPromise.js +0 -111
- package/lib/package.js +0 -20
- package/lib/pageSnapshot.js +0 -43
- package/lib/program.js +0 -72
- package/lib/server.js +0 -48
- package/lib/tab.js +0 -101
- package/lib/tools/common.js +0 -68
- package/lib/tools/console.js +0 -44
- package/lib/tools/dialogs.js +0 -52
- package/lib/tools/files.js +0 -51
- package/lib/tools/install.js +0 -57
- package/lib/tools/keyboard.js +0 -46
- package/lib/tools/navigate.js +0 -93
- package/lib/tools/network.js +0 -51
- package/lib/tools/pdf.js +0 -49
- package/lib/tools/screenshot.js +0 -77
- package/lib/tools/snapshot.js +0 -204
- package/lib/tools/tabs.js +0 -118
- package/lib/tools/testing.js +0 -60
- package/lib/tools/tool.js +0 -18
- package/lib/tools/utils.js +0 -80
- package/lib/tools/vision.js +0 -189
- package/lib/tools/wait.js +0 -59
- package/lib/tools.js +0 -61
- package/lib/transport.js +0 -133
package/lib/tools/testing.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
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 generateTestSchema = z.object({
|
|
19
|
-
name: z.string().describe('The name of the test'),
|
|
20
|
-
description: z.string().describe('The description of the test'),
|
|
21
|
-
steps: z.array(z.string()).describe('The steps of the test'),
|
|
22
|
-
});
|
|
23
|
-
const generateTest = defineTool({
|
|
24
|
-
capability: 'testing',
|
|
25
|
-
schema: {
|
|
26
|
-
name: 'browser_generate_playwright_test',
|
|
27
|
-
title: 'Generate a Playwright test',
|
|
28
|
-
description: 'Generate a Playwright test for given scenario',
|
|
29
|
-
inputSchema: generateTestSchema,
|
|
30
|
-
type: 'readOnly',
|
|
31
|
-
},
|
|
32
|
-
handle: async (context, params) => {
|
|
33
|
-
return {
|
|
34
|
-
resultOverride: {
|
|
35
|
-
content: [{
|
|
36
|
-
type: 'text',
|
|
37
|
-
text: instructions(params),
|
|
38
|
-
}],
|
|
39
|
-
},
|
|
40
|
-
code: [],
|
|
41
|
-
captureSnapshot: false,
|
|
42
|
-
waitForNetwork: false,
|
|
43
|
-
};
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
const instructions = (params) => [
|
|
47
|
-
`## Instructions`,
|
|
48
|
-
`- You are a playwright test generator.`,
|
|
49
|
-
`- You are given a scenario and you need to generate a playwright test for it.`,
|
|
50
|
-
'- DO NOT generate test code based on the scenario alone. DO run steps one by one using the tools provided instead.',
|
|
51
|
-
'- Only after all steps are completed, emit a Playwright TypeScript test that uses @playwright/test based on message history',
|
|
52
|
-
'- Save generated test file in the tests directory',
|
|
53
|
-
`Test name: ${params.name}`,
|
|
54
|
-
`Description: ${params.description}`,
|
|
55
|
-
`Steps:`,
|
|
56
|
-
...params.steps.map((step, index) => `- ${index + 1}. ${step}`),
|
|
57
|
-
].join('\n');
|
|
58
|
-
export default [
|
|
59
|
-
generateTest,
|
|
60
|
-
];
|
package/lib/tools/tool.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
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
|
-
}
|
package/lib/tools/utils.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
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 async function waitForCompletion(context, tab, callback) {
|
|
17
|
-
const requests = new Set();
|
|
18
|
-
let frameNavigated = false;
|
|
19
|
-
let waitCallback = () => { };
|
|
20
|
-
const waitBarrier = new Promise(f => { waitCallback = f; });
|
|
21
|
-
const requestListener = (request) => requests.add(request);
|
|
22
|
-
const requestFinishedListener = (request) => {
|
|
23
|
-
requests.delete(request);
|
|
24
|
-
if (!requests.size)
|
|
25
|
-
waitCallback();
|
|
26
|
-
};
|
|
27
|
-
const frameNavigateListener = (frame) => {
|
|
28
|
-
if (frame.parentFrame())
|
|
29
|
-
return;
|
|
30
|
-
frameNavigated = true;
|
|
31
|
-
dispose();
|
|
32
|
-
clearTimeout(timeout);
|
|
33
|
-
void tab.waitForLoadState('load').then(waitCallback);
|
|
34
|
-
};
|
|
35
|
-
const onTimeout = () => {
|
|
36
|
-
dispose();
|
|
37
|
-
waitCallback();
|
|
38
|
-
};
|
|
39
|
-
tab.page.on('request', requestListener);
|
|
40
|
-
tab.page.on('requestfinished', requestFinishedListener);
|
|
41
|
-
tab.page.on('framenavigated', frameNavigateListener);
|
|
42
|
-
const timeout = setTimeout(onTimeout, 10000);
|
|
43
|
-
const dispose = () => {
|
|
44
|
-
tab.page.off('request', requestListener);
|
|
45
|
-
tab.page.off('requestfinished', requestFinishedListener);
|
|
46
|
-
tab.page.off('framenavigated', frameNavigateListener);
|
|
47
|
-
clearTimeout(timeout);
|
|
48
|
-
};
|
|
49
|
-
try {
|
|
50
|
-
const result = await callback();
|
|
51
|
-
if (!requests.size && !frameNavigated)
|
|
52
|
-
waitCallback();
|
|
53
|
-
await waitBarrier;
|
|
54
|
-
await context.waitForTimeout(1000);
|
|
55
|
-
return result;
|
|
56
|
-
}
|
|
57
|
-
finally {
|
|
58
|
-
dispose();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
export function sanitizeForFilePath(s) {
|
|
62
|
-
const sanitize = (s) => s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
|
|
63
|
-
const separator = s.lastIndexOf('.');
|
|
64
|
-
if (separator === -1)
|
|
65
|
-
return sanitize(s);
|
|
66
|
-
return sanitize(s.substring(0, separator)) + '.' + sanitize(s.substring(separator + 1));
|
|
67
|
-
}
|
|
68
|
-
export async function generateLocator(locator) {
|
|
69
|
-
try {
|
|
70
|
-
return await locator._generateLocatorString();
|
|
71
|
-
}
|
|
72
|
-
catch (e) {
|
|
73
|
-
if (e instanceof Error && /locator._generateLocatorString: No element matching locator/.test(e.message))
|
|
74
|
-
throw new Error('Ref not found, likely because element was removed. Use browser_snapshot to see what elements are currently on the page.');
|
|
75
|
-
throw e;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
export async function callOnPageNoTrace(page, callback) {
|
|
79
|
-
return await page._wrapApiCall(() => callback(page), { internal: true });
|
|
80
|
-
}
|
package/lib/tools/vision.js
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
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
|
-
const elementSchema = z.object({
|
|
20
|
-
element: z.string().describe('Human-readable element description used to obtain permission to interact with the element'),
|
|
21
|
-
});
|
|
22
|
-
const screenshot = defineTool({
|
|
23
|
-
capability: 'core',
|
|
24
|
-
schema: {
|
|
25
|
-
name: 'browser_screen_capture',
|
|
26
|
-
title: 'Take a screenshot',
|
|
27
|
-
description: 'Take a screenshot of the current page',
|
|
28
|
-
inputSchema: z.object({}),
|
|
29
|
-
type: 'readOnly',
|
|
30
|
-
},
|
|
31
|
-
handle: async (context) => {
|
|
32
|
-
const tab = await context.ensureTab();
|
|
33
|
-
const options = { type: 'jpeg', quality: 50, scale: 'css' };
|
|
34
|
-
const code = [
|
|
35
|
-
`// Take a screenshot of the current page`,
|
|
36
|
-
`await page.screenshot(${javascript.formatObject(options)});`,
|
|
37
|
-
];
|
|
38
|
-
const action = () => tab.page.screenshot(options).then(buffer => {
|
|
39
|
-
return {
|
|
40
|
-
content: [{ type: 'image', data: buffer.toString('base64'), mimeType: 'image/jpeg' }],
|
|
41
|
-
};
|
|
42
|
-
});
|
|
43
|
-
return {
|
|
44
|
-
code,
|
|
45
|
-
action,
|
|
46
|
-
captureSnapshot: false,
|
|
47
|
-
waitForNetwork: false
|
|
48
|
-
};
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
const moveMouse = defineTool({
|
|
52
|
-
capability: 'core',
|
|
53
|
-
schema: {
|
|
54
|
-
name: 'browser_screen_move_mouse',
|
|
55
|
-
title: 'Move mouse',
|
|
56
|
-
description: 'Move mouse to a given position',
|
|
57
|
-
inputSchema: elementSchema.extend({
|
|
58
|
-
x: z.coerce.number().describe('X coordinate'),
|
|
59
|
-
y: z.coerce.number().describe('Y coordinate'),
|
|
60
|
-
}),
|
|
61
|
-
type: 'readOnly',
|
|
62
|
-
},
|
|
63
|
-
handle: async (context, params) => {
|
|
64
|
-
const tab = context.currentTabOrDie();
|
|
65
|
-
const code = [
|
|
66
|
-
`// Move mouse to (${params.x}, ${params.y})`,
|
|
67
|
-
`await page.mouse.move(${params.x}, ${params.y});`,
|
|
68
|
-
];
|
|
69
|
-
const action = () => tab.page.mouse.move(params.x, params.y);
|
|
70
|
-
return {
|
|
71
|
-
code,
|
|
72
|
-
action,
|
|
73
|
-
captureSnapshot: false,
|
|
74
|
-
waitForNetwork: false
|
|
75
|
-
};
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
const click = defineTool({
|
|
79
|
-
capability: 'core',
|
|
80
|
-
schema: {
|
|
81
|
-
name: 'browser_screen_click',
|
|
82
|
-
title: 'Click',
|
|
83
|
-
description: 'Click left mouse button',
|
|
84
|
-
inputSchema: elementSchema.extend({
|
|
85
|
-
x: z.coerce.number().describe('X coordinate'),
|
|
86
|
-
y: z.coerce.number().describe('Y coordinate'),
|
|
87
|
-
}),
|
|
88
|
-
type: 'destructive',
|
|
89
|
-
},
|
|
90
|
-
handle: async (context, params) => {
|
|
91
|
-
const tab = context.currentTabOrDie();
|
|
92
|
-
const code = [
|
|
93
|
-
`// Click mouse at coordinates (${params.x}, ${params.y})`,
|
|
94
|
-
`await page.mouse.move(${params.x}, ${params.y});`,
|
|
95
|
-
`await page.mouse.down();`,
|
|
96
|
-
`await page.mouse.up();`,
|
|
97
|
-
];
|
|
98
|
-
const action = async () => {
|
|
99
|
-
await tab.page.mouse.move(params.x, params.y);
|
|
100
|
-
await tab.page.mouse.down();
|
|
101
|
-
await tab.page.mouse.up();
|
|
102
|
-
};
|
|
103
|
-
return {
|
|
104
|
-
code,
|
|
105
|
-
action,
|
|
106
|
-
captureSnapshot: false,
|
|
107
|
-
waitForNetwork: true,
|
|
108
|
-
};
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
const drag = defineTool({
|
|
112
|
-
capability: 'core',
|
|
113
|
-
schema: {
|
|
114
|
-
name: 'browser_screen_drag',
|
|
115
|
-
title: 'Drag mouse',
|
|
116
|
-
description: 'Drag left mouse button',
|
|
117
|
-
inputSchema: elementSchema.extend({
|
|
118
|
-
startX: z.coerce.number().describe('Start X coordinate'),
|
|
119
|
-
startY: z.coerce.number().describe('Start Y coordinate'),
|
|
120
|
-
endX: z.coerce.number().describe('End X coordinate'),
|
|
121
|
-
endY: z.coerce.number().describe('End Y coordinate'),
|
|
122
|
-
}),
|
|
123
|
-
type: 'destructive',
|
|
124
|
-
},
|
|
125
|
-
handle: async (context, params) => {
|
|
126
|
-
const tab = context.currentTabOrDie();
|
|
127
|
-
const code = [
|
|
128
|
-
`// Drag mouse from (${params.startX}, ${params.startY}) to (${params.endX}, ${params.endY})`,
|
|
129
|
-
`await page.mouse.move(${params.startX}, ${params.startY});`,
|
|
130
|
-
`await page.mouse.down();`,
|
|
131
|
-
`await page.mouse.move(${params.endX}, ${params.endY});`,
|
|
132
|
-
`await page.mouse.up();`,
|
|
133
|
-
];
|
|
134
|
-
const action = async () => {
|
|
135
|
-
await tab.page.mouse.move(params.startX, params.startY);
|
|
136
|
-
await tab.page.mouse.down();
|
|
137
|
-
await tab.page.mouse.move(params.endX, params.endY);
|
|
138
|
-
await tab.page.mouse.up();
|
|
139
|
-
};
|
|
140
|
-
return {
|
|
141
|
-
code,
|
|
142
|
-
action,
|
|
143
|
-
captureSnapshot: false,
|
|
144
|
-
waitForNetwork: true,
|
|
145
|
-
};
|
|
146
|
-
},
|
|
147
|
-
});
|
|
148
|
-
const type = defineTool({
|
|
149
|
-
capability: 'core',
|
|
150
|
-
schema: {
|
|
151
|
-
name: 'browser_screen_type',
|
|
152
|
-
title: 'Type text',
|
|
153
|
-
description: 'Type text',
|
|
154
|
-
inputSchema: z.object({
|
|
155
|
-
text: z.string().describe('Text to type into the element'),
|
|
156
|
-
submit: z.coerce.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
|
|
157
|
-
}),
|
|
158
|
-
type: 'destructive',
|
|
159
|
-
},
|
|
160
|
-
handle: async (context, params) => {
|
|
161
|
-
const tab = context.currentTabOrDie();
|
|
162
|
-
const code = [
|
|
163
|
-
`// Type ${params.text}`,
|
|
164
|
-
`await page.keyboard.type('${params.text}');`,
|
|
165
|
-
];
|
|
166
|
-
const action = async () => {
|
|
167
|
-
await tab.page.keyboard.type(params.text);
|
|
168
|
-
if (params.submit)
|
|
169
|
-
await tab.page.keyboard.press('Enter');
|
|
170
|
-
};
|
|
171
|
-
if (params.submit) {
|
|
172
|
-
code.push(`// Submit text`);
|
|
173
|
-
code.push(`await page.keyboard.press('Enter');`);
|
|
174
|
-
}
|
|
175
|
-
return {
|
|
176
|
-
code,
|
|
177
|
-
action,
|
|
178
|
-
captureSnapshot: false,
|
|
179
|
-
waitForNetwork: true,
|
|
180
|
-
};
|
|
181
|
-
},
|
|
182
|
-
});
|
|
183
|
-
export default [
|
|
184
|
-
screenshot,
|
|
185
|
-
moveMouse,
|
|
186
|
-
click,
|
|
187
|
-
drag,
|
|
188
|
-
type,
|
|
189
|
-
];
|
package/lib/tools/wait.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
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.coerce.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
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
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 common from './tools/common.js';
|
|
17
|
-
import console from './tools/console.js';
|
|
18
|
-
import dialogs from './tools/dialogs.js';
|
|
19
|
-
import files from './tools/files.js';
|
|
20
|
-
import install from './tools/install.js';
|
|
21
|
-
import keyboard from './tools/keyboard.js';
|
|
22
|
-
import navigate from './tools/navigate.js';
|
|
23
|
-
import network from './tools/network.js';
|
|
24
|
-
import pdf from './tools/pdf.js';
|
|
25
|
-
import snapshot from './tools/snapshot.js';
|
|
26
|
-
import tabs from './tools/tabs.js';
|
|
27
|
-
import screenshot from './tools/screenshot.js';
|
|
28
|
-
import testing from './tools/testing.js';
|
|
29
|
-
import vision from './tools/vision.js';
|
|
30
|
-
import wait from './tools/wait.js';
|
|
31
|
-
export const snapshotTools = [
|
|
32
|
-
...common(true),
|
|
33
|
-
...console,
|
|
34
|
-
...dialogs(true),
|
|
35
|
-
...files(true),
|
|
36
|
-
...install,
|
|
37
|
-
...keyboard(true),
|
|
38
|
-
...navigate(true),
|
|
39
|
-
...network,
|
|
40
|
-
...pdf,
|
|
41
|
-
...screenshot,
|
|
42
|
-
...snapshot,
|
|
43
|
-
...tabs(true),
|
|
44
|
-
...testing,
|
|
45
|
-
...wait(true),
|
|
46
|
-
];
|
|
47
|
-
export const visionTools = [
|
|
48
|
-
...common(false),
|
|
49
|
-
...console,
|
|
50
|
-
...dialogs(false),
|
|
51
|
-
...files(false),
|
|
52
|
-
...install,
|
|
53
|
-
...keyboard(false),
|
|
54
|
-
...navigate(false),
|
|
55
|
-
...network,
|
|
56
|
-
...pdf,
|
|
57
|
-
...tabs(false),
|
|
58
|
-
...testing,
|
|
59
|
-
...vision,
|
|
60
|
-
...wait(false),
|
|
61
|
-
];
|
package/lib/transport.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
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 http from 'node:http';
|
|
17
|
-
import assert from 'node:assert';
|
|
18
|
-
import crypto from 'node:crypto';
|
|
19
|
-
import debug from 'debug';
|
|
20
|
-
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|
21
|
-
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
22
|
-
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
23
|
-
export async function startStdioTransport(server) {
|
|
24
|
-
await server.createConnection(new StdioServerTransport());
|
|
25
|
-
}
|
|
26
|
-
const testDebug = debug('pw:mcp:test');
|
|
27
|
-
async function handleSSE(server, req, res, url, sessions) {
|
|
28
|
-
if (req.method === 'POST') {
|
|
29
|
-
const sessionId = url.searchParams.get('sessionId');
|
|
30
|
-
if (!sessionId) {
|
|
31
|
-
res.statusCode = 400;
|
|
32
|
-
return res.end('Missing sessionId');
|
|
33
|
-
}
|
|
34
|
-
const transport = sessions.get(sessionId);
|
|
35
|
-
if (!transport) {
|
|
36
|
-
res.statusCode = 404;
|
|
37
|
-
return res.end('Session not found');
|
|
38
|
-
}
|
|
39
|
-
return await transport.handlePostMessage(req, res);
|
|
40
|
-
}
|
|
41
|
-
else if (req.method === 'GET') {
|
|
42
|
-
const transport = new SSEServerTransport('/sse', res);
|
|
43
|
-
sessions.set(transport.sessionId, transport);
|
|
44
|
-
testDebug(`create SSE session: ${transport.sessionId}`);
|
|
45
|
-
const connection = await server.createConnection(transport);
|
|
46
|
-
res.on('close', () => {
|
|
47
|
-
testDebug(`delete SSE session: ${transport.sessionId}`);
|
|
48
|
-
sessions.delete(transport.sessionId);
|
|
49
|
-
// eslint-disable-next-line no-console
|
|
50
|
-
void connection.close().catch(e => console.error(e));
|
|
51
|
-
});
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
res.statusCode = 405;
|
|
55
|
-
res.end('Method not allowed');
|
|
56
|
-
}
|
|
57
|
-
async function handleStreamable(server, req, res, sessions) {
|
|
58
|
-
const sessionId = req.headers['mcp-session-id'];
|
|
59
|
-
if (sessionId) {
|
|
60
|
-
const transport = sessions.get(sessionId);
|
|
61
|
-
if (!transport) {
|
|
62
|
-
res.statusCode = 404;
|
|
63
|
-
res.end('Session not found');
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
return await transport.handleRequest(req, res);
|
|
67
|
-
}
|
|
68
|
-
if (req.method === 'POST') {
|
|
69
|
-
const transport = new StreamableHTTPServerTransport({
|
|
70
|
-
sessionIdGenerator: () => crypto.randomUUID(),
|
|
71
|
-
onsessioninitialized: sessionId => {
|
|
72
|
-
sessions.set(sessionId, transport);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
transport.onclose = () => {
|
|
76
|
-
if (transport.sessionId)
|
|
77
|
-
sessions.delete(transport.sessionId);
|
|
78
|
-
};
|
|
79
|
-
await server.createConnection(transport);
|
|
80
|
-
await transport.handleRequest(req, res);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
res.statusCode = 400;
|
|
84
|
-
res.end('Invalid request');
|
|
85
|
-
}
|
|
86
|
-
export async function startHttpServer(config) {
|
|
87
|
-
const { host, port } = config;
|
|
88
|
-
const httpServer = http.createServer();
|
|
89
|
-
await new Promise((resolve, reject) => {
|
|
90
|
-
httpServer.on('error', reject);
|
|
91
|
-
httpServer.listen(port, host, () => {
|
|
92
|
-
resolve();
|
|
93
|
-
httpServer.removeListener('error', reject);
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
return httpServer;
|
|
97
|
-
}
|
|
98
|
-
export function startHttpTransport(httpServer, mcpServer) {
|
|
99
|
-
const sseSessions = new Map();
|
|
100
|
-
const streamableSessions = new Map();
|
|
101
|
-
httpServer.on('request', async (req, res) => {
|
|
102
|
-
const url = new URL(`http://localhost${req.url}`);
|
|
103
|
-
if (url.pathname.startsWith('/mcp'))
|
|
104
|
-
await handleStreamable(mcpServer, req, res, streamableSessions);
|
|
105
|
-
else
|
|
106
|
-
await handleSSE(mcpServer, req, res, url, sseSessions);
|
|
107
|
-
});
|
|
108
|
-
const url = httpAddressToString(httpServer.address());
|
|
109
|
-
const message = [
|
|
110
|
-
`Listening on ${url}`,
|
|
111
|
-
'Put this in your client config:',
|
|
112
|
-
JSON.stringify({
|
|
113
|
-
'mcpServers': {
|
|
114
|
-
'playwright': {
|
|
115
|
-
'url': `${url}/sse`
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}, undefined, 2),
|
|
119
|
-
'If your client supports streamable HTTP, you can use the /mcp endpoint instead.',
|
|
120
|
-
].join('\n');
|
|
121
|
-
// eslint-disable-next-line no-console
|
|
122
|
-
console.error(message);
|
|
123
|
-
}
|
|
124
|
-
export function httpAddressToString(address) {
|
|
125
|
-
assert(address, 'Could not bind server socket');
|
|
126
|
-
if (typeof address === 'string')
|
|
127
|
-
return address;
|
|
128
|
-
const resolvedPort = address.port;
|
|
129
|
-
let resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;
|
|
130
|
-
if (resolvedHost === '0.0.0.0' || resolvedHost === '[::]')
|
|
131
|
-
resolvedHost = 'localhost';
|
|
132
|
-
return `http://${resolvedHost}:${resolvedPort}`;
|
|
133
|
-
}
|