@playwright/mcp 0.0.34 → 0.0.35
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 +29 -38
- package/lib/browserContextFactory.js +10 -12
- package/lib/browserServerBackend.js +3 -6
- package/lib/context.js +5 -5
- package/lib/extension/cdpRelay.js +12 -6
- package/lib/extension/extensionContextFactory.js +5 -7
- package/lib/index.js +2 -1
- package/lib/loopTools/context.js +3 -2
- package/lib/loopTools/main.js +8 -5
- package/lib/mcp/{transport.js → http.js} +51 -37
- package/lib/mcp/mdb.js +198 -0
- package/lib/mcp/proxyBackend.js +5 -7
- package/lib/mcp/server.js +39 -12
- package/lib/mcp/tool.js +3 -0
- package/lib/program.js +44 -23
- package/lib/tab.js +1 -1
- package/lib/tools/form.js +57 -0
- package/lib/tools/navigate.js +0 -16
- package/lib/tools/tabs.js +31 -59
- package/lib/tools.js +2 -0
- package/lib/vscode/host.js +128 -0
- package/lib/vscode/main.js +62 -0
- package/package.json +1 -1
- package/lib/utils/httpServer.js +0 -39
- /package/lib/{utils → mcp}/manualPromise.js +0 -0
|
@@ -0,0 +1,128 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
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]);
|
package/package.json
CHANGED
package/lib/utils/httpServer.js
DELETED
|
@@ -1,39 +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 assert from 'assert';
|
|
17
|
-
import http from 'http';
|
|
18
|
-
export async function startHttpServer(config) {
|
|
19
|
-
const { host, port } = config;
|
|
20
|
-
const httpServer = http.createServer();
|
|
21
|
-
await new Promise((resolve, reject) => {
|
|
22
|
-
httpServer.on('error', reject);
|
|
23
|
-
httpServer.listen(port, host, () => {
|
|
24
|
-
resolve();
|
|
25
|
-
httpServer.removeListener('error', reject);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
return httpServer;
|
|
29
|
-
}
|
|
30
|
-
export function httpAddressToString(address) {
|
|
31
|
-
assert(address, 'Could not bind server socket');
|
|
32
|
-
if (typeof address === 'string')
|
|
33
|
-
return address;
|
|
34
|
-
const resolvedPort = address.port;
|
|
35
|
-
let resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;
|
|
36
|
-
if (resolvedHost === '0.0.0.0' || resolvedHost === '[::]')
|
|
37
|
-
resolvedHost = 'localhost';
|
|
38
|
-
return `http://${resolvedHost}:${resolvedPort}`;
|
|
39
|
-
}
|
|
File without changes
|