@playwright/mcp 0.0.36 → 0.0.37-alpha-2025-09-09
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 +82 -47
- package/cli.js +7 -1
- package/config.d.ts +24 -0
- package/index.d.ts +1 -1
- package/index.js +2 -2
- package/package.json +14 -40
- package/lib/browserContextFactory.js +0 -211
- package/lib/browserServerBackend.js +0 -77
- package/lib/config.js +0 -246
- package/lib/context.js +0 -226
- package/lib/extension/cdpRelay.js +0 -358
- package/lib/extension/extensionContextFactory.js +0 -56
- package/lib/extension/protocol.js +0 -18
- package/lib/index.js +0 -40
- package/lib/loop/loop.js +0 -69
- package/lib/loop/loopClaude.js +0 -152
- package/lib/loop/loopOpenAI.js +0 -141
- package/lib/loop/main.js +0 -60
- package/lib/loopTools/context.js +0 -67
- package/lib/loopTools/main.js +0 -54
- package/lib/loopTools/perform.js +0 -32
- package/lib/loopTools/snapshot.js +0 -29
- package/lib/loopTools/tool.js +0 -18
- package/lib/mcp/http.js +0 -135
- package/lib/mcp/inProcessTransport.js +0 -72
- package/lib/mcp/manualPromise.js +0 -111
- package/lib/mcp/mdb.js +0 -198
- package/lib/mcp/proxyBackend.js +0 -104
- package/lib/mcp/server.js +0 -123
- package/lib/mcp/tool.js +0 -32
- package/lib/program.js +0 -132
- package/lib/response.js +0 -165
- package/lib/sessionLog.js +0 -121
- package/lib/tab.js +0 -249
- package/lib/tools/common.js +0 -55
- package/lib/tools/console.js +0 -33
- package/lib/tools/dialogs.js +0 -47
- package/lib/tools/evaluate.js +0 -53
- package/lib/tools/files.js +0 -44
- package/lib/tools/form.js +0 -57
- package/lib/tools/install.js +0 -53
- package/lib/tools/keyboard.js +0 -78
- package/lib/tools/mouse.js +0 -99
- package/lib/tools/navigate.js +0 -54
- package/lib/tools/network.js +0 -41
- package/lib/tools/pdf.js +0 -40
- package/lib/tools/screenshot.js +0 -79
- package/lib/tools/snapshot.js +0 -139
- package/lib/tools/tabs.js +0 -59
- package/lib/tools/tool.js +0 -33
- package/lib/tools/utils.js +0 -74
- package/lib/tools/verify.js +0 -137
- package/lib/tools/wait.js +0 -55
- package/lib/tools.js +0 -54
- package/lib/utils/codegen.js +0 -49
- package/lib/utils/fileUtils.js +0 -36
- package/lib/utils/guid.js +0 -22
- package/lib/utils/log.js +0 -21
- package/lib/utils/package.js +0 -20
- package/lib/vscode/host.js +0 -128
- package/lib/vscode/main.js +0 -62
package/lib/tools/verify.js
DELETED
|
@@ -1,137 +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 { defineTabTool } from './tool.js';
|
|
18
|
-
import * as javascript from '../utils/codegen.js';
|
|
19
|
-
import { generateLocator } from './utils.js';
|
|
20
|
-
const verifyElement = defineTabTool({
|
|
21
|
-
capability: 'verify',
|
|
22
|
-
schema: {
|
|
23
|
-
name: 'browser_verify_element_visible',
|
|
24
|
-
title: 'Verify element visible',
|
|
25
|
-
description: 'Verify element is visible on the page',
|
|
26
|
-
inputSchema: z.object({
|
|
27
|
-
role: z.string().describe('ROLE of the element. Can be found in the snapshot like this: \`- {ROLE} "Accessible Name":\`'),
|
|
28
|
-
accessibleName: z.string().describe('ACCESSIBLE_NAME of the element. Can be found in the snapshot like this: \`- role "{ACCESSIBLE_NAME}"\`'),
|
|
29
|
-
}),
|
|
30
|
-
type: 'readOnly',
|
|
31
|
-
},
|
|
32
|
-
handle: async (tab, params, response) => {
|
|
33
|
-
const locator = tab.page.getByRole(params.role, { name: params.accessibleName });
|
|
34
|
-
if (await locator.count() === 0) {
|
|
35
|
-
response.addError(`Element with role "${params.role}" and accessible name "${params.accessibleName}" not found`);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
response.addCode(`await expect(page.getByRole(${javascript.escapeWithQuotes(params.role)}, { name: ${javascript.escapeWithQuotes(params.accessibleName)} })).toBeVisible();`);
|
|
39
|
-
response.addResult('Done');
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
const verifyText = defineTabTool({
|
|
43
|
-
capability: 'verify',
|
|
44
|
-
schema: {
|
|
45
|
-
name: 'browser_verify_text_visible',
|
|
46
|
-
title: 'Verify text visible',
|
|
47
|
-
description: `Verify text is visible on the page. Prefer ${verifyElement.schema.name} if possible.`,
|
|
48
|
-
inputSchema: z.object({
|
|
49
|
-
text: z.string().describe('TEXT to verify. Can be found in the snapshot like this: \`- role "Accessible Name": {TEXT}\` or like this: \`- text: {TEXT}\`'),
|
|
50
|
-
}),
|
|
51
|
-
type: 'readOnly',
|
|
52
|
-
},
|
|
53
|
-
handle: async (tab, params, response) => {
|
|
54
|
-
const locator = tab.page.getByText(params.text).filter({ visible: true });
|
|
55
|
-
if (await locator.count() === 0) {
|
|
56
|
-
response.addError('Text not found');
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
response.addCode(`await expect(page.getByText(${javascript.escapeWithQuotes(params.text)})).toBeVisible();`);
|
|
60
|
-
response.addResult('Done');
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
const verifyList = defineTabTool({
|
|
64
|
-
capability: 'verify',
|
|
65
|
-
schema: {
|
|
66
|
-
name: 'browser_verify_list_visible',
|
|
67
|
-
title: 'Verify list visible',
|
|
68
|
-
description: 'Verify list is visible on the page',
|
|
69
|
-
inputSchema: z.object({
|
|
70
|
-
element: z.string().describe('Human-readable list description'),
|
|
71
|
-
ref: z.string().describe('Exact target element reference that points to the list'),
|
|
72
|
-
items: z.array(z.string()).describe('Items to verify'),
|
|
73
|
-
}),
|
|
74
|
-
type: 'readOnly',
|
|
75
|
-
},
|
|
76
|
-
handle: async (tab, params, response) => {
|
|
77
|
-
const locator = await tab.refLocator({ ref: params.ref, element: params.element });
|
|
78
|
-
const itemTexts = [];
|
|
79
|
-
for (const item of params.items) {
|
|
80
|
-
const itemLocator = locator.getByText(item);
|
|
81
|
-
if (await itemLocator.count() === 0) {
|
|
82
|
-
response.addError(`Item "${item}" not found`);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
itemTexts.push((await itemLocator.textContent()));
|
|
86
|
-
}
|
|
87
|
-
const ariaSnapshot = `\`
|
|
88
|
-
- list:
|
|
89
|
-
${itemTexts.map(t => ` - listitem: ${javascript.escapeWithQuotes(t, '"')}`).join('\n')}
|
|
90
|
-
\``;
|
|
91
|
-
response.addCode(`await expect(page.locator('body')).toMatchAriaSnapshot(${ariaSnapshot});`);
|
|
92
|
-
response.addResult('Done');
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
const verifyValue = defineTabTool({
|
|
96
|
-
capability: 'verify',
|
|
97
|
-
schema: {
|
|
98
|
-
name: 'browser_verify_value',
|
|
99
|
-
title: 'Verify value',
|
|
100
|
-
description: 'Verify element value',
|
|
101
|
-
inputSchema: z.object({
|
|
102
|
-
type: z.enum(['textbox', 'checkbox', 'radio', 'combobox', 'slider']).describe('Type of the element'),
|
|
103
|
-
element: z.string().describe('Human-readable element description'),
|
|
104
|
-
ref: z.string().describe('Exact target element reference that points to the element'),
|
|
105
|
-
value: z.string().describe('Value to verify. For checkbox, use "true" or "false".'),
|
|
106
|
-
}),
|
|
107
|
-
type: 'readOnly',
|
|
108
|
-
},
|
|
109
|
-
handle: async (tab, params, response) => {
|
|
110
|
-
const locator = await tab.refLocator({ ref: params.ref, element: params.element });
|
|
111
|
-
const locatorSource = `page.${await generateLocator(locator)}`;
|
|
112
|
-
if (params.type === 'textbox' || params.type === 'slider' || params.type === 'combobox') {
|
|
113
|
-
const value = await locator.inputValue();
|
|
114
|
-
if (value !== params.value) {
|
|
115
|
-
response.addError(`Expected value "${params.value}", but got "${value}"`);
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
response.addCode(`await expect(${locatorSource}).toHaveValue(${javascript.quote(params.value)});`);
|
|
119
|
-
}
|
|
120
|
-
else if (params.type === 'checkbox' || params.type === 'radio') {
|
|
121
|
-
const value = await locator.isChecked();
|
|
122
|
-
if (value !== (params.value === 'true')) {
|
|
123
|
-
response.addError(`Expected value "${params.value}", but got "${value}"`);
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
const matcher = value ? 'toBeChecked' : 'not.toBeChecked';
|
|
127
|
-
response.addCode(`await expect(${locatorSource}).${matcher}();`);
|
|
128
|
-
}
|
|
129
|
-
response.addResult('Done');
|
|
130
|
-
},
|
|
131
|
-
});
|
|
132
|
-
export default [
|
|
133
|
-
verifyElement,
|
|
134
|
-
verifyText,
|
|
135
|
-
verifyList,
|
|
136
|
-
verifyValue,
|
|
137
|
-
];
|
package/lib/tools/wait.js
DELETED
|
@@ -1,55 +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 = defineTool({
|
|
19
|
-
capability: 'core',
|
|
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, response) => {
|
|
32
|
-
if (!params.text && !params.textGone && !params.time)
|
|
33
|
-
throw new Error('Either time, text or textGone must be provided');
|
|
34
|
-
if (params.time) {
|
|
35
|
-
response.addCode(`await new Promise(f => setTimeout(f, ${params.time} * 1000));`);
|
|
36
|
-
await new Promise(f => setTimeout(f, Math.min(30000, params.time * 1000)));
|
|
37
|
-
}
|
|
38
|
-
const tab = context.currentTabOrDie();
|
|
39
|
-
const locator = params.text ? tab.page.getByText(params.text).first() : undefined;
|
|
40
|
-
const goneLocator = params.textGone ? tab.page.getByText(params.textGone).first() : undefined;
|
|
41
|
-
if (goneLocator) {
|
|
42
|
-
response.addCode(`await page.getByText(${JSON.stringify(params.textGone)}).first().waitFor({ state: 'hidden' });`);
|
|
43
|
-
await goneLocator.waitFor({ state: 'hidden' });
|
|
44
|
-
}
|
|
45
|
-
if (locator) {
|
|
46
|
-
response.addCode(`await page.getByText(${JSON.stringify(params.text)}).first().waitFor({ state: 'visible' });`);
|
|
47
|
-
await locator.waitFor({ state: 'visible' });
|
|
48
|
-
}
|
|
49
|
-
response.addResult(`Waited for ${params.text || params.textGone || params.time}`);
|
|
50
|
-
response.setIncludeSnapshot();
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
export default [
|
|
54
|
-
wait,
|
|
55
|
-
];
|
package/lib/tools.js
DELETED
|
@@ -1,54 +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 evaluate from './tools/evaluate.js';
|
|
20
|
-
import files from './tools/files.js';
|
|
21
|
-
import form from './tools/form.js';
|
|
22
|
-
import install from './tools/install.js';
|
|
23
|
-
import keyboard from './tools/keyboard.js';
|
|
24
|
-
import mouse from './tools/mouse.js';
|
|
25
|
-
import navigate from './tools/navigate.js';
|
|
26
|
-
import network from './tools/network.js';
|
|
27
|
-
import pdf from './tools/pdf.js';
|
|
28
|
-
import snapshot from './tools/snapshot.js';
|
|
29
|
-
import tabs from './tools/tabs.js';
|
|
30
|
-
import screenshot from './tools/screenshot.js';
|
|
31
|
-
import wait from './tools/wait.js';
|
|
32
|
-
import verify from './tools/verify.js';
|
|
33
|
-
export const allTools = [
|
|
34
|
-
...common,
|
|
35
|
-
...console,
|
|
36
|
-
...dialogs,
|
|
37
|
-
...evaluate,
|
|
38
|
-
...files,
|
|
39
|
-
...form,
|
|
40
|
-
...install,
|
|
41
|
-
...keyboard,
|
|
42
|
-
...navigate,
|
|
43
|
-
...network,
|
|
44
|
-
...mouse,
|
|
45
|
-
...pdf,
|
|
46
|
-
...screenshot,
|
|
47
|
-
...snapshot,
|
|
48
|
-
...tabs,
|
|
49
|
-
...wait,
|
|
50
|
-
...verify,
|
|
51
|
-
];
|
|
52
|
-
export function filteredTools(config) {
|
|
53
|
-
return allTools.filter(tool => tool.capability.startsWith('core') || config.capabilities?.includes(tool.capability));
|
|
54
|
-
}
|
package/lib/utils/codegen.js
DELETED
|
@@ -1,49 +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
|
-
// adapted from:
|
|
17
|
-
// - https://github.com/microsoft/playwright/blob/76ee48dc9d4034536e3ec5b2c7ce8be3b79418a8/packages/playwright-core/src/utils/isomorphic/stringUtils.ts
|
|
18
|
-
// - https://github.com/microsoft/playwright/blob/76ee48dc9d4034536e3ec5b2c7ce8be3b79418a8/packages/playwright-core/src/server/codegen/javascript.ts
|
|
19
|
-
// NOTE: this function should not be used to escape any selectors.
|
|
20
|
-
export function escapeWithQuotes(text, char = '\'') {
|
|
21
|
-
const stringified = JSON.stringify(text);
|
|
22
|
-
const escapedText = stringified.substring(1, stringified.length - 1).replace(/\\"/g, '"');
|
|
23
|
-
if (char === '\'')
|
|
24
|
-
return char + escapedText.replace(/[']/g, '\\\'') + char;
|
|
25
|
-
if (char === '"')
|
|
26
|
-
return char + escapedText.replace(/["]/g, '\\"') + char;
|
|
27
|
-
if (char === '`')
|
|
28
|
-
return char + escapedText.replace(/[`]/g, '\\`') + char;
|
|
29
|
-
throw new Error('Invalid escape char');
|
|
30
|
-
}
|
|
31
|
-
export function quote(text) {
|
|
32
|
-
return escapeWithQuotes(text, '\'');
|
|
33
|
-
}
|
|
34
|
-
export function formatObject(value, indent = ' ') {
|
|
35
|
-
if (typeof value === 'string')
|
|
36
|
-
return quote(value);
|
|
37
|
-
if (Array.isArray(value))
|
|
38
|
-
return `[${value.map(o => formatObject(o)).join(', ')}]`;
|
|
39
|
-
if (typeof value === 'object') {
|
|
40
|
-
const keys = Object.keys(value).filter(key => value[key] !== undefined).sort();
|
|
41
|
-
if (!keys.length)
|
|
42
|
-
return '{}';
|
|
43
|
-
const tokens = [];
|
|
44
|
-
for (const key of keys)
|
|
45
|
-
tokens.push(`${key}: ${formatObject(value[key])}`);
|
|
46
|
-
return `{\n${indent}${tokens.join(`,\n${indent}`)}\n}`;
|
|
47
|
-
}
|
|
48
|
-
return String(value);
|
|
49
|
-
}
|
package/lib/utils/fileUtils.js
DELETED
|
@@ -1,36 +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 os from 'node:os';
|
|
17
|
-
import path from 'node:path';
|
|
18
|
-
export function cacheDir() {
|
|
19
|
-
let cacheDirectory;
|
|
20
|
-
if (process.platform === 'linux')
|
|
21
|
-
cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
|
|
22
|
-
else if (process.platform === 'darwin')
|
|
23
|
-
cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
|
|
24
|
-
else if (process.platform === 'win32')
|
|
25
|
-
cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
|
|
26
|
-
else
|
|
27
|
-
throw new Error('Unsupported platform: ' + process.platform);
|
|
28
|
-
return path.join(cacheDirectory, 'ms-playwright');
|
|
29
|
-
}
|
|
30
|
-
export function sanitizeForFilePath(s) {
|
|
31
|
-
const sanitize = (s) => s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
|
|
32
|
-
const separator = s.lastIndexOf('.');
|
|
33
|
-
if (separator === -1)
|
|
34
|
-
return sanitize(s);
|
|
35
|
-
return sanitize(s.substring(0, separator)) + '.' + sanitize(s.substring(separator + 1));
|
|
36
|
-
}
|
package/lib/utils/guid.js
DELETED
|
@@ -1,22 +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 crypto from 'crypto';
|
|
17
|
-
export function createGuid() {
|
|
18
|
-
return crypto.randomBytes(16).toString('hex');
|
|
19
|
-
}
|
|
20
|
-
export function createHash(data) {
|
|
21
|
-
return crypto.createHash('sha256').update(data).digest('hex').slice(0, 7);
|
|
22
|
-
}
|
package/lib/utils/log.js
DELETED
|
@@ -1,21 +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 debug from 'debug';
|
|
17
|
-
const errorsDebug = debug('pw:mcp:errors');
|
|
18
|
-
export function logUnhandledError(error) {
|
|
19
|
-
errorsDebug(error);
|
|
20
|
-
}
|
|
21
|
-
export const testDebug = debug('pw:mcp:test');
|
package/lib/utils/package.js
DELETED
|
@@ -1,20 +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 fs from 'fs';
|
|
17
|
-
import path from 'path';
|
|
18
|
-
import url from 'url';
|
|
19
|
-
const __filename = url.fileURLToPath(import.meta.url);
|
|
20
|
-
export const packageJSON = JSON.parse(fs.readFileSync(path.join(path.dirname(__filename), '..', '..', 'package.json'), 'utf8'));
|
package/lib/vscode/host.js
DELETED
|
@@ -1,128 +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 { fileURLToPath } from 'url';
|
|
17
|
-
import path from 'path';
|
|
18
|
-
import { z } from 'zod';
|
|
19
|
-
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
20
|
-
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
21
|
-
import { ListRootsRequestSchema, PingRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
|
22
|
-
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
23
|
-
import * as mcpServer from '../mcp/server.js';
|
|
24
|
-
import { logUnhandledError } from '../utils/log.js';
|
|
25
|
-
import { packageJSON } from '../utils/package.js';
|
|
26
|
-
import { BrowserServerBackend } from '../browserServerBackend.js';
|
|
27
|
-
import { contextFactory } from '../browserContextFactory.js';
|
|
28
|
-
const contextSwitchOptions = z.object({
|
|
29
|
-
connectionString: z.string().optional().describe('The connection string to use to connect to the browser'),
|
|
30
|
-
lib: z.string().optional().describe('The library to use for the connection'),
|
|
31
|
-
});
|
|
32
|
-
class VSCodeProxyBackend {
|
|
33
|
-
_config;
|
|
34
|
-
_defaultTransportFactory;
|
|
35
|
-
name = 'Playwright MCP Client Switcher';
|
|
36
|
-
version = packageJSON.version;
|
|
37
|
-
_currentClient;
|
|
38
|
-
_contextSwitchTool;
|
|
39
|
-
_roots = [];
|
|
40
|
-
_clientVersion;
|
|
41
|
-
constructor(_config, _defaultTransportFactory) {
|
|
42
|
-
this._config = _config;
|
|
43
|
-
this._defaultTransportFactory = _defaultTransportFactory;
|
|
44
|
-
this._contextSwitchTool = this._defineContextSwitchTool();
|
|
45
|
-
}
|
|
46
|
-
async initialize(server, clientVersion, roots) {
|
|
47
|
-
this._clientVersion = clientVersion;
|
|
48
|
-
this._roots = roots;
|
|
49
|
-
const transport = await this._defaultTransportFactory();
|
|
50
|
-
await this._setCurrentClient(transport);
|
|
51
|
-
}
|
|
52
|
-
async listTools() {
|
|
53
|
-
const response = await this._currentClient.listTools();
|
|
54
|
-
return [
|
|
55
|
-
...response.tools,
|
|
56
|
-
this._contextSwitchTool,
|
|
57
|
-
];
|
|
58
|
-
}
|
|
59
|
-
async callTool(name, args) {
|
|
60
|
-
if (name === this._contextSwitchTool.name)
|
|
61
|
-
return this._callContextSwitchTool(args);
|
|
62
|
-
return await this._currentClient.callTool({
|
|
63
|
-
name,
|
|
64
|
-
arguments: args,
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
serverClosed(server) {
|
|
68
|
-
void this._currentClient?.close().catch(logUnhandledError);
|
|
69
|
-
}
|
|
70
|
-
async _callContextSwitchTool(params) {
|
|
71
|
-
if (!params.connectionString || !params.lib) {
|
|
72
|
-
const transport = await this._defaultTransportFactory();
|
|
73
|
-
await this._setCurrentClient(transport);
|
|
74
|
-
return {
|
|
75
|
-
content: [{ type: 'text', text: '### Result\nSuccessfully disconnected.\n' }],
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
await this._setCurrentClient(new StdioClientTransport({
|
|
79
|
-
command: process.execPath,
|
|
80
|
-
cwd: process.cwd(),
|
|
81
|
-
args: [
|
|
82
|
-
path.join(fileURLToPath(import.meta.url), '..', 'main.js'),
|
|
83
|
-
JSON.stringify(this._config),
|
|
84
|
-
params.connectionString,
|
|
85
|
-
params.lib,
|
|
86
|
-
],
|
|
87
|
-
}));
|
|
88
|
-
return {
|
|
89
|
-
content: [{ type: 'text', text: '### Result\nSuccessfully connected.\n' }],
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
_defineContextSwitchTool() {
|
|
93
|
-
return {
|
|
94
|
-
name: 'browser_connect',
|
|
95
|
-
description: 'Do not call, this tool is used in the integration with the Playwright VS Code Extension and meant for programmatic usage only.',
|
|
96
|
-
inputSchema: zodToJsonSchema(contextSwitchOptions, { strictUnions: true }),
|
|
97
|
-
annotations: {
|
|
98
|
-
title: 'Connect to a browser running in VS Code.',
|
|
99
|
-
readOnlyHint: true,
|
|
100
|
-
openWorldHint: false,
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
async _setCurrentClient(transport) {
|
|
105
|
-
await this._currentClient?.close();
|
|
106
|
-
this._currentClient = undefined;
|
|
107
|
-
const client = new Client(this._clientVersion);
|
|
108
|
-
client.registerCapabilities({
|
|
109
|
-
roots: {
|
|
110
|
-
listRoots: true,
|
|
111
|
-
},
|
|
112
|
-
});
|
|
113
|
-
client.setRequestHandler(ListRootsRequestSchema, () => ({ roots: this._roots }));
|
|
114
|
-
client.setRequestHandler(PingRequestSchema, () => ({}));
|
|
115
|
-
await client.connect(transport);
|
|
116
|
-
this._currentClient = client;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
export async function runVSCodeTools(config) {
|
|
120
|
-
const serverBackendFactory = {
|
|
121
|
-
name: 'Playwright w/ vscode',
|
|
122
|
-
nameInConfig: 'playwright-vscode',
|
|
123
|
-
version: packageJSON.version,
|
|
124
|
-
create: () => new VSCodeProxyBackend(config, () => mcpServer.wrapInProcess(new BrowserServerBackend(config, contextFactory(config))))
|
|
125
|
-
};
|
|
126
|
-
await mcpServer.start(serverBackendFactory, config.server);
|
|
127
|
-
return;
|
|
128
|
-
}
|
package/lib/vscode/main.js
DELETED
|
@@ -1,62 +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 { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
17
|
-
import * as mcpServer from '../mcp/server.js';
|
|
18
|
-
import { BrowserServerBackend } from '../browserServerBackend.js';
|
|
19
|
-
class VSCodeBrowserContextFactory {
|
|
20
|
-
_config;
|
|
21
|
-
_playwright;
|
|
22
|
-
_connectionString;
|
|
23
|
-
name = 'vscode';
|
|
24
|
-
description = 'Connect to a browser running in the Playwright VS Code extension';
|
|
25
|
-
constructor(_config, _playwright, _connectionString) {
|
|
26
|
-
this._config = _config;
|
|
27
|
-
this._playwright = _playwright;
|
|
28
|
-
this._connectionString = _connectionString;
|
|
29
|
-
}
|
|
30
|
-
async createContext(clientInfo, abortSignal) {
|
|
31
|
-
let launchOptions = this._config.browser.launchOptions;
|
|
32
|
-
if (this._config.browser.userDataDir) {
|
|
33
|
-
launchOptions = {
|
|
34
|
-
...launchOptions,
|
|
35
|
-
...this._config.browser.contextOptions,
|
|
36
|
-
userDataDir: this._config.browser.userDataDir,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
const connectionString = new URL(this._connectionString);
|
|
40
|
-
connectionString.searchParams.set('launch-options', JSON.stringify(launchOptions));
|
|
41
|
-
const browserType = this._playwright.chromium; // it could also be firefox or webkit, we just need some browser type to call `connect` on
|
|
42
|
-
const browser = await browserType.connect(connectionString.toString());
|
|
43
|
-
const context = browser.contexts()[0] ?? await browser.newContext(this._config.browser.contextOptions);
|
|
44
|
-
return {
|
|
45
|
-
browserContext: context,
|
|
46
|
-
close: async () => {
|
|
47
|
-
await browser.close();
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
async function main(config, connectionString, lib) {
|
|
53
|
-
const playwright = await import(lib).then(mod => mod.default ?? mod);
|
|
54
|
-
const factory = new VSCodeBrowserContextFactory(config, playwright, connectionString);
|
|
55
|
-
await mcpServer.connect({
|
|
56
|
-
name: 'Playwright MCP',
|
|
57
|
-
nameInConfig: 'playwright-vscode',
|
|
58
|
-
create: () => new BrowserServerBackend(config, factory),
|
|
59
|
-
version: 'unused'
|
|
60
|
-
}, new StdioServerTransport(), false);
|
|
61
|
-
}
|
|
62
|
-
await main(JSON.parse(process.argv[2]), process.argv[3], process.argv[4]);
|