@roxybrowser/playwright-mcp 0.0.5 → 0.0.6-beta.7
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 +832 -742
- package/dist/cli.mjs +250 -0
- package/dist/cli.mjs.LICENSE.txt +42 -0
- package/dist/index.mjs +250 -0
- package/dist/index.mjs.LICENSE.txt +42 -0
- package/dist/index.mjs.map +1 -0
- package/index.d.ts +86 -23
- package/package.json +27 -41
- package/cli.js +0 -18
- package/config.d.ts +0 -119
- package/index.js +0 -19
- package/lib/browserContextFactory.js +0 -264
- package/lib/browserServerBackend.js +0 -77
- package/lib/config.js +0 -246
- package/lib/context.js +0 -242
- package/lib/extension/cdpRelay.js +0 -355
- package/lib/extension/extensionContextFactory.js +0 -54
- 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 -120
- package/lib/mcp/inProcessTransport.js +0 -72
- package/lib/mcp/proxyBackend.js +0 -104
- package/lib/mcp/server.js +0 -123
- package/lib/mcp/tool.js +0 -29
- package/lib/program.js +0 -145
- 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/install.js +0 -53
- package/lib/tools/keyboard.js +0 -78
- package/lib/tools/mouse.js +0 -99
- package/lib/tools/navigate.js +0 -70
- package/lib/tools/network.js +0 -41
- package/lib/tools/pdf.js +0 -40
- package/lib/tools/roxy.js +0 -50
- package/lib/tools/screenshot.js +0 -79
- package/lib/tools/snapshot.js +0 -139
- package/lib/tools/tabs.js +0 -87
- package/lib/tools/tool.js +0 -33
- package/lib/tools/utils.js +0 -74
- package/lib/tools/wait.js +0 -55
- package/lib/tools.js +0 -52
- 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/manualPromise.js +0 -111
- package/lib/utils/package.js +0 -20
- package/lib/vscode/host.js +0 -128
- package/lib/vscode/main.js +0 -62
package/index.d.ts
CHANGED
|
@@ -1,23 +1,86 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @roxybrowser/playwright-mcp
|
|
3
|
+
* 类型声明:CLI + 程序化 createConnection/startServer + 可扩展 Backend/工具
|
|
4
|
+
*/
|
|
5
|
+
import type { BrowserContext } from 'playwright';
|
|
6
|
+
|
|
7
|
+
/** 与 Playwright MCP 一致的配置(部分常用字段) */
|
|
8
|
+
export interface McpConfig {
|
|
9
|
+
browser?: {
|
|
10
|
+
browserName?: string;
|
|
11
|
+
isolated?: boolean;
|
|
12
|
+
userDataDir?: string;
|
|
13
|
+
launchOptions?: Record<string, unknown>;
|
|
14
|
+
contextOptions?: Record<string, unknown>;
|
|
15
|
+
cdpEndpoint?: string;
|
|
16
|
+
};
|
|
17
|
+
server?: { port?: number; host?: string };
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 创建 MCP 服务端实例(未连接 transport)。
|
|
23
|
+
* 使用 CustomBackend(官方工具 + browser_connect_roxy)。需再 server.connect(transport)。
|
|
24
|
+
*/
|
|
25
|
+
export function createConnection(
|
|
26
|
+
userConfig?: McpConfig,
|
|
27
|
+
contextGetter?: () => Promise<BrowserContext>
|
|
28
|
+
): Promise<import('@modelcontextprotocol/sdk').Server>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 以 HTTP/SSE 启动 MCP 服务(Studio 协议),返回可连接的 URL。
|
|
32
|
+
* 进程会持续运行直到退出。
|
|
33
|
+
*/
|
|
34
|
+
export function startServer(options: {
|
|
35
|
+
port: number;
|
|
36
|
+
host?: string;
|
|
37
|
+
config?: McpConfig;
|
|
38
|
+
}): Promise<{ url: string }>;
|
|
39
|
+
|
|
40
|
+
/** 自定义 Backend,继承 Playwright BrowserServerBackend 并增加 Roxy 等工具 */
|
|
41
|
+
export class CustomBackend {
|
|
42
|
+
constructor(config: McpConfig, factory: unknown);
|
|
43
|
+
listTools(): Promise<unknown[]>;
|
|
44
|
+
callTool(name: string, rawArguments: unknown, progress?: (p?: unknown) => void): Promise<unknown>;
|
|
45
|
+
serverClosed?(server?: unknown): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 支持 reconnectToCDP 的浏览器上下文工厂,用于 Roxy 连接 */
|
|
49
|
+
export class DynamicCdpContextFactory {
|
|
50
|
+
constructor(config: McpConfig, initialCdpEndpoint?: string);
|
|
51
|
+
reconnectToCDP(cdpEndpoint: string): void;
|
|
52
|
+
createContext(
|
|
53
|
+
clientInfo: unknown,
|
|
54
|
+
abortSignal?: AbortSignal,
|
|
55
|
+
options?: Record<string, unknown>
|
|
56
|
+
): Promise<{ browserContext: BrowserContext; close: () => Promise<void> }>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 定义额外工具的 schema + handle 的辅助函数 */
|
|
60
|
+
export function defineExtraTool<TArgs>(
|
|
61
|
+
schema: {
|
|
62
|
+
name: string;
|
|
63
|
+
title: string;
|
|
64
|
+
description: string;
|
|
65
|
+
inputSchema: import('zod').ZodType<TArgs>;
|
|
66
|
+
type?: 'action' | 'readOnly' | 'destructive';
|
|
67
|
+
},
|
|
68
|
+
handle: (context: unknown, args: TArgs, progress?: (params?: unknown) => void) => Promise<{
|
|
69
|
+
content: Array<{ type: 'text'; text: string }>;
|
|
70
|
+
isError?: boolean;
|
|
71
|
+
}>
|
|
72
|
+
): { schema: typeof schema; handle: typeof handle };
|
|
73
|
+
|
|
74
|
+
/** 将自定义 tool 的 schema 转为 MCP listTools 返回格式 */
|
|
75
|
+
export function extraToolToMcp(schema: {
|
|
76
|
+
name: string;
|
|
77
|
+
description: string;
|
|
78
|
+
title: string;
|
|
79
|
+
inputSchema: unknown;
|
|
80
|
+
type?: string;
|
|
81
|
+
}): {
|
|
82
|
+
name: string;
|
|
83
|
+
description: string;
|
|
84
|
+
inputSchema: object;
|
|
85
|
+
annotations: object;
|
|
86
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@roxybrowser/playwright-mcp",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6-beta.7",
|
|
4
4
|
"description": "Playwright Tools for MCP",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -16,66 +16,52 @@
|
|
|
16
16
|
},
|
|
17
17
|
"license": "Apache-2.0",
|
|
18
18
|
"scripts": {
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"ctest": "playwright test --project=chrome",
|
|
27
|
-
"ftest": "playwright test --project=firefox",
|
|
28
|
-
"wtest": "playwright test --project=webkit",
|
|
29
|
-
"run-server": "node lib/browserServer.js",
|
|
30
|
-
"clean": "npx rimraf lib",
|
|
19
|
+
"start": "node src/cli.js",
|
|
20
|
+
"dev": "node --inspect src/cli.js",
|
|
21
|
+
"dev:http": "node --inspect src/cli.js --port 9324",
|
|
22
|
+
"build:esbuild": "node build.mjs",
|
|
23
|
+
"build:webpack": "npm run clean && webpack --config webpack.config.mjs",
|
|
24
|
+
"build": "npm run build:webpack",
|
|
25
|
+
"clean": "npx rimraf dist",
|
|
31
26
|
"npm-publish": "npm run clean && npm run build && npm publish",
|
|
32
27
|
"publish:release": "npm run clean && npm version patch --no-git-tag-version && npm run build && npm publish",
|
|
33
|
-
"publish:beta": "npm version prerelease --preid=beta --no-git-tag-version && npm
|
|
28
|
+
"publish:beta": "npm version prerelease --preid=beta --no-git-tag-version && npm publish --tag beta"
|
|
34
29
|
},
|
|
35
30
|
"publishConfig": {
|
|
36
31
|
"access": "public",
|
|
37
32
|
"registry": "https://registry.npmjs.org/"
|
|
38
33
|
},
|
|
34
|
+
"main": "dist/index.mjs",
|
|
35
|
+
"files": [
|
|
36
|
+
"package.json",
|
|
37
|
+
"dist",
|
|
38
|
+
"index.d.ts"
|
|
39
|
+
],
|
|
39
40
|
"exports": {
|
|
40
41
|
"./package.json": "./package.json",
|
|
41
42
|
".": {
|
|
42
43
|
"types": "./index.d.ts",
|
|
43
|
-
"default": "./index.
|
|
44
|
+
"default": "./dist/index.mjs"
|
|
44
45
|
}
|
|
45
46
|
},
|
|
46
47
|
"dependencies": {
|
|
47
|
-
"@modelcontextprotocol/sdk": "^1.18.0",
|
|
48
48
|
"commander": "^13.1.0",
|
|
49
|
-
"debug": "^4.4.1",
|
|
50
|
-
"dotenv": "^17.2.0",
|
|
51
|
-
"mime": "^4.0.7",
|
|
52
|
-
"playwright": "1.55.0",
|
|
53
|
-
"playwright-core": "1.55.0",
|
|
54
|
-
"ws": "^8.18.1",
|
|
55
49
|
"zod": "^3.24.1",
|
|
56
50
|
"zod-to-json-schema": "^3.24.4"
|
|
57
51
|
},
|
|
58
52
|
"devDependencies": {
|
|
59
|
-
"@anthropic-ai/sdk": "^0.57.0",
|
|
60
|
-
"@eslint/eslintrc": "^3.2.0",
|
|
61
|
-
"@eslint/js": "^9.19.0",
|
|
62
|
-
"@playwright/test": "1.55.0",
|
|
63
|
-
"@stylistic/eslint-plugin": "^3.0.1",
|
|
64
|
-
"@types/debug": "^4.1.12",
|
|
65
|
-
"@types/node": "^22.13.10",
|
|
66
|
-
"@types/ws": "^8.18.1",
|
|
67
|
-
"@typescript-eslint/eslint-plugin": "^8.26.1",
|
|
68
|
-
"@typescript-eslint/parser": "^8.26.1",
|
|
69
|
-
"@typescript-eslint/utils": "^8.26.1",
|
|
70
53
|
"esbuild": "^0.20.1",
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"rimraf": "^6.0.1"
|
|
54
|
+
"playwright": "1.58.2",
|
|
55
|
+
"playwright-core": "1.58.2",
|
|
56
|
+
"rimraf": "^6.0.1",
|
|
57
|
+
"webpack": "^5.105.4",
|
|
58
|
+
"webpack-cli": "^5.1.4"
|
|
77
59
|
},
|
|
78
|
-
"bin":
|
|
79
|
-
|
|
60
|
+
"bin": "dist/cli.mjs",
|
|
61
|
+
"pnpm": {
|
|
62
|
+
"patchedDependencies": {
|
|
63
|
+
"playwright@1.58.2": "patches/playwright@1.58.2.patch",
|
|
64
|
+
"playwright-core@1.58.2": "patches/playwright-core@1.58.2.patch"
|
|
65
|
+
}
|
|
80
66
|
}
|
|
81
67
|
}
|
package/cli.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) Microsoft Corporation.
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import './lib/program.js';
|
package/config.d.ts
DELETED
|
@@ -1,119 +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
|
-
|
|
17
|
-
import type * as playwright from 'playwright';
|
|
18
|
-
|
|
19
|
-
export type ToolCapability = 'core' | 'core-tabs' | 'core-install' | 'vision' | 'pdf';
|
|
20
|
-
|
|
21
|
-
export type Config = {
|
|
22
|
-
/**
|
|
23
|
-
* The browser to use.
|
|
24
|
-
*/
|
|
25
|
-
browser?: {
|
|
26
|
-
/**
|
|
27
|
-
* The type of browser to use.
|
|
28
|
-
*/
|
|
29
|
-
browserName?: 'chromium' | 'firefox' | 'webkit';
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Keep the browser profile in memory, do not save it to disk.
|
|
33
|
-
*/
|
|
34
|
-
isolated?: boolean;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Path to a user data directory for browser profile persistence.
|
|
38
|
-
* Temporary directory is created by default.
|
|
39
|
-
*/
|
|
40
|
-
userDataDir?: string;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Launch options passed to
|
|
44
|
-
* @see https://playwright.dev/docs/api/class-browsertype#browser-type-launch-persistent-context
|
|
45
|
-
*
|
|
46
|
-
* This is useful for settings options like `channel`, `headless`, `executablePath`, etc.
|
|
47
|
-
*/
|
|
48
|
-
launchOptions?: playwright.LaunchOptions;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Context options for the browser context.
|
|
52
|
-
*
|
|
53
|
-
* This is useful for settings options like `viewport`.
|
|
54
|
-
*/
|
|
55
|
-
contextOptions?: playwright.BrowserContextOptions;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Chrome DevTools Protocol endpoint to connect to an existing browser instance in case of Chromium family browsers.
|
|
59
|
-
*/
|
|
60
|
-
cdpEndpoint?: string;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Remote endpoint to connect to an existing Playwright server.
|
|
64
|
-
*/
|
|
65
|
-
remoteEndpoint?: string;
|
|
66
|
-
},
|
|
67
|
-
|
|
68
|
-
server?: {
|
|
69
|
-
/**
|
|
70
|
-
* The port to listen on for SSE or MCP transport.
|
|
71
|
-
*/
|
|
72
|
-
port?: number;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* The host to bind the server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.
|
|
76
|
-
*/
|
|
77
|
-
host?: string;
|
|
78
|
-
},
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* List of enabled tool capabilities. Possible values:
|
|
82
|
-
* - 'core': Core browser automation features.
|
|
83
|
-
* - 'pdf': PDF generation and manipulation.
|
|
84
|
-
* - 'vision': Coordinate-based interactions.
|
|
85
|
-
*/
|
|
86
|
-
capabilities?: ToolCapability[];
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Whether to save the Playwright session into the output directory.
|
|
90
|
-
*/
|
|
91
|
-
saveSession?: boolean;
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Whether to save the Playwright trace of the session into the output directory.
|
|
95
|
-
*/
|
|
96
|
-
saveTrace?: boolean;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* The directory to save output files.
|
|
100
|
-
*/
|
|
101
|
-
outputDir?: string;
|
|
102
|
-
|
|
103
|
-
network?: {
|
|
104
|
-
/**
|
|
105
|
-
* List of origins to allow the browser to request. Default is to allow all. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
|
|
106
|
-
*/
|
|
107
|
-
allowedOrigins?: string[];
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* List of origins to block the browser to request. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
|
|
111
|
-
*/
|
|
112
|
-
blockedOrigins?: string[];
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Whether to send image responses to the client. Can be "allow", "omit", or "auto". Defaults to "auto", which sends images if the client can display them.
|
|
117
|
-
*/
|
|
118
|
-
imageResponses?: 'allow' | 'omit';
|
|
119
|
-
};
|
package/index.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) Microsoft Corporation.
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { createConnection } from './lib/index.js';
|
|
19
|
-
export { createConnection };
|
|
@@ -1,264 +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 net from 'net';
|
|
18
|
-
import path from 'path';
|
|
19
|
-
import * as playwright from 'playwright';
|
|
20
|
-
// @ts-ignore
|
|
21
|
-
import { registryDirectory } from 'playwright-core/lib/server/registry/index';
|
|
22
|
-
// @ts-ignore
|
|
23
|
-
import { startTraceViewerServer } from 'playwright-core/lib/server';
|
|
24
|
-
import { logUnhandledError, testDebug } from './utils/log.js';
|
|
25
|
-
import { createHash } from './utils/guid.js';
|
|
26
|
-
import { outputFile } from './config.js';
|
|
27
|
-
export function contextFactory(config) {
|
|
28
|
-
if (config.browser.remoteEndpoint)
|
|
29
|
-
return new RemoteContextFactory(config);
|
|
30
|
-
if (config.browser.cdpEndpoint)
|
|
31
|
-
return new CdpContextFactory(config);
|
|
32
|
-
if (config.browser.isolated)
|
|
33
|
-
return new IsolatedContextFactory(config);
|
|
34
|
-
return new PersistentContextFactory(config);
|
|
35
|
-
}
|
|
36
|
-
export function createDynamicCdpContextFactory(config) {
|
|
37
|
-
return new DynamicCdpContextFactory(config);
|
|
38
|
-
}
|
|
39
|
-
class BaseContextFactory {
|
|
40
|
-
config;
|
|
41
|
-
_logName;
|
|
42
|
-
_browserPromise;
|
|
43
|
-
constructor(name, config) {
|
|
44
|
-
this._logName = name;
|
|
45
|
-
this.config = config;
|
|
46
|
-
}
|
|
47
|
-
async _obtainBrowser(clientInfo) {
|
|
48
|
-
if (this._browserPromise)
|
|
49
|
-
return this._browserPromise;
|
|
50
|
-
testDebug(`obtain browser (${this._logName})`);
|
|
51
|
-
this._browserPromise = this._doObtainBrowser(clientInfo);
|
|
52
|
-
void this._browserPromise.then(browser => {
|
|
53
|
-
browser.on('disconnected', () => {
|
|
54
|
-
this._browserPromise = undefined;
|
|
55
|
-
});
|
|
56
|
-
}).catch(() => {
|
|
57
|
-
this._browserPromise = undefined;
|
|
58
|
-
});
|
|
59
|
-
return this._browserPromise;
|
|
60
|
-
}
|
|
61
|
-
async _doObtainBrowser(clientInfo) {
|
|
62
|
-
throw new Error('Not implemented');
|
|
63
|
-
}
|
|
64
|
-
async createContext(clientInfo) {
|
|
65
|
-
testDebug(`create browser context (${this._logName})`);
|
|
66
|
-
const browser = await this._obtainBrowser(clientInfo);
|
|
67
|
-
const browserContext = await this._doCreateContext(browser);
|
|
68
|
-
return { browserContext, close: () => this._closeBrowserContext(browserContext, browser) };
|
|
69
|
-
}
|
|
70
|
-
async _doCreateContext(browser) {
|
|
71
|
-
throw new Error('Not implemented');
|
|
72
|
-
}
|
|
73
|
-
async _closeBrowserContext(browserContext, browser) {
|
|
74
|
-
testDebug(`close browser context (${this._logName})`);
|
|
75
|
-
if (browser.contexts().length === 1)
|
|
76
|
-
this._browserPromise = undefined;
|
|
77
|
-
await browserContext.close().catch(logUnhandledError);
|
|
78
|
-
if (browser.contexts().length === 0) {
|
|
79
|
-
testDebug(`close browser (${this._logName})`);
|
|
80
|
-
await browser.close().catch(logUnhandledError);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
class IsolatedContextFactory extends BaseContextFactory {
|
|
85
|
-
constructor(config) {
|
|
86
|
-
super('isolated', config);
|
|
87
|
-
}
|
|
88
|
-
async _doObtainBrowser(clientInfo) {
|
|
89
|
-
await injectCdpPort(this.config.browser);
|
|
90
|
-
const browserType = playwright[this.config.browser.browserName];
|
|
91
|
-
return browserType.launch({
|
|
92
|
-
tracesDir: await startTraceServer(this.config, clientInfo.rootPath),
|
|
93
|
-
...this.config.browser.launchOptions,
|
|
94
|
-
handleSIGINT: false,
|
|
95
|
-
handleSIGTERM: false,
|
|
96
|
-
}).catch(error => {
|
|
97
|
-
if (error.message.includes('Executable doesn\'t exist'))
|
|
98
|
-
throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
|
|
99
|
-
throw error;
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
async _doCreateContext(browser) {
|
|
103
|
-
return browser.newContext(this.config.browser.contextOptions);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
class CdpContextFactory extends BaseContextFactory {
|
|
107
|
-
constructor(config) {
|
|
108
|
-
super('cdp', config);
|
|
109
|
-
}
|
|
110
|
-
async _doObtainBrowser() {
|
|
111
|
-
return playwright.chromium.connectOverCDP(this.config.browser.cdpEndpoint);
|
|
112
|
-
}
|
|
113
|
-
async _doCreateContext(browser) {
|
|
114
|
-
return this.config.browser.isolated ? await browser.newContext() : browser.contexts()[0];
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
export class DynamicCdpContextFactory extends BaseContextFactory {
|
|
118
|
-
_currentCdpEndpoint;
|
|
119
|
-
_reconnecting = false;
|
|
120
|
-
constructor(config) {
|
|
121
|
-
super('dynamic-cdp', config);
|
|
122
|
-
}
|
|
123
|
-
async reconnectToCDP(cdpEndpoint) {
|
|
124
|
-
if (this._currentCdpEndpoint === cdpEndpoint && this._browserPromise) {
|
|
125
|
-
testDebug(`Already connected to CDP endpoint: ${cdpEndpoint}`);
|
|
126
|
-
return; // Already connected to this endpoint
|
|
127
|
-
}
|
|
128
|
-
testDebug(`Reconnecting to CDP endpoint: ${cdpEndpoint}`);
|
|
129
|
-
this._reconnecting = true;
|
|
130
|
-
try {
|
|
131
|
-
// Set the endpoint first to avoid race conditions
|
|
132
|
-
this._currentCdpEndpoint = cdpEndpoint;
|
|
133
|
-
// Force browser reconnection by clearing the promise
|
|
134
|
-
this._browserPromise = undefined;
|
|
135
|
-
}
|
|
136
|
-
finally {
|
|
137
|
-
this._reconnecting = false;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
async _doObtainBrowser() {
|
|
141
|
-
const currentEndpoint = this._currentCdpEndpoint;
|
|
142
|
-
if (!currentEndpoint) {
|
|
143
|
-
testDebug(`No CDP endpoint set (reconnecting: ${this._reconnecting})`);
|
|
144
|
-
throw new Error('No CDP endpoint set. Use the browser_connect_roxy tool to connect to RoxyBrowser first. ' +
|
|
145
|
-
'Example: {"name": "browser_connect_roxy", "arguments": {"cdpEndpoint": "ws://127.0.0.1:PORT/devtools/browser/ID"}}');
|
|
146
|
-
}
|
|
147
|
-
testDebug(`Connecting to CDP endpoint: ${currentEndpoint} (reconnecting: ${this._reconnecting})`);
|
|
148
|
-
try {
|
|
149
|
-
const browser = await playwright.chromium.connectOverCDP(currentEndpoint);
|
|
150
|
-
testDebug(`Successfully connected to browser via CDP`);
|
|
151
|
-
return browser;
|
|
152
|
-
}
|
|
153
|
-
catch (error) {
|
|
154
|
-
testDebug(`Failed to connect to CDP endpoint: ${error.message}`);
|
|
155
|
-
throw new Error(`Failed to connect to RoxyBrowser at ${currentEndpoint}: ${error.message}`);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
async _doCreateContext(browser) {
|
|
159
|
-
const context = this.config.browser.isolated ? await browser.newContext() : browser.contexts()[0];
|
|
160
|
-
testDebug(`Created browser context (isolated: ${this.config.browser.isolated})`);
|
|
161
|
-
return context;
|
|
162
|
-
}
|
|
163
|
-
getCurrentCdpEndpoint() {
|
|
164
|
-
return this._currentCdpEndpoint;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
class RemoteContextFactory extends BaseContextFactory {
|
|
168
|
-
constructor(config) {
|
|
169
|
-
super('remote', config);
|
|
170
|
-
}
|
|
171
|
-
async _doObtainBrowser() {
|
|
172
|
-
const url = new URL(this.config.browser.remoteEndpoint);
|
|
173
|
-
url.searchParams.set('browser', this.config.browser.browserName);
|
|
174
|
-
if (this.config.browser.launchOptions)
|
|
175
|
-
url.searchParams.set('launch-options', JSON.stringify(this.config.browser.launchOptions));
|
|
176
|
-
return playwright[this.config.browser.browserName].connect(String(url));
|
|
177
|
-
}
|
|
178
|
-
async _doCreateContext(browser) {
|
|
179
|
-
return browser.newContext();
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
class PersistentContextFactory {
|
|
183
|
-
config;
|
|
184
|
-
name = 'persistent';
|
|
185
|
-
description = 'Create a new persistent browser context';
|
|
186
|
-
_userDataDirs = new Set();
|
|
187
|
-
constructor(config) {
|
|
188
|
-
this.config = config;
|
|
189
|
-
}
|
|
190
|
-
async createContext(clientInfo) {
|
|
191
|
-
await injectCdpPort(this.config.browser);
|
|
192
|
-
testDebug('create browser context (persistent)');
|
|
193
|
-
const userDataDir = this.config.browser.userDataDir ?? await this._createUserDataDir(clientInfo.rootPath);
|
|
194
|
-
const tracesDir = await startTraceServer(this.config, clientInfo.rootPath);
|
|
195
|
-
this._userDataDirs.add(userDataDir);
|
|
196
|
-
testDebug('lock user data dir', userDataDir);
|
|
197
|
-
const browserType = playwright[this.config.browser.browserName];
|
|
198
|
-
for (let i = 0; i < 5; i++) {
|
|
199
|
-
try {
|
|
200
|
-
const browserContext = await browserType.launchPersistentContext(userDataDir, {
|
|
201
|
-
tracesDir,
|
|
202
|
-
...this.config.browser.launchOptions,
|
|
203
|
-
...this.config.browser.contextOptions,
|
|
204
|
-
handleSIGINT: false,
|
|
205
|
-
handleSIGTERM: false,
|
|
206
|
-
});
|
|
207
|
-
const close = () => this._closeBrowserContext(browserContext, userDataDir);
|
|
208
|
-
return { browserContext, close };
|
|
209
|
-
}
|
|
210
|
-
catch (error) {
|
|
211
|
-
if (error.message.includes('Executable doesn\'t exist'))
|
|
212
|
-
throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
|
|
213
|
-
if (error.message.includes('ProcessSingleton') || error.message.includes('Invalid URL')) {
|
|
214
|
-
// User data directory is already in use, try again.
|
|
215
|
-
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
216
|
-
continue;
|
|
217
|
-
}
|
|
218
|
-
throw error;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
throw new Error(`Browser is already in use for ${userDataDir}, use --isolated to run multiple instances of the same browser`);
|
|
222
|
-
}
|
|
223
|
-
async _closeBrowserContext(browserContext, userDataDir) {
|
|
224
|
-
testDebug('close browser context (persistent)');
|
|
225
|
-
testDebug('release user data dir', userDataDir);
|
|
226
|
-
await browserContext.close().catch(() => { });
|
|
227
|
-
this._userDataDirs.delete(userDataDir);
|
|
228
|
-
testDebug('close browser context complete (persistent)');
|
|
229
|
-
}
|
|
230
|
-
async _createUserDataDir(rootPath) {
|
|
231
|
-
const dir = process.env.PWMCP_PROFILES_DIR_FOR_TEST ?? registryDirectory;
|
|
232
|
-
const browserToken = this.config.browser.launchOptions?.channel ?? this.config.browser?.browserName;
|
|
233
|
-
// Hesitant putting hundreds of files into the user's workspace, so using it for hashing instead.
|
|
234
|
-
const rootPathToken = rootPath ? `-${createHash(rootPath)}` : '';
|
|
235
|
-
const result = path.join(dir, `mcp-${browserToken}${rootPathToken}`);
|
|
236
|
-
await fs.promises.mkdir(result, { recursive: true });
|
|
237
|
-
return result;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
async function injectCdpPort(browserConfig) {
|
|
241
|
-
if (browserConfig.browserName === 'chromium')
|
|
242
|
-
browserConfig.launchOptions.cdpPort = await findFreePort();
|
|
243
|
-
}
|
|
244
|
-
async function findFreePort() {
|
|
245
|
-
return new Promise((resolve, reject) => {
|
|
246
|
-
const server = net.createServer();
|
|
247
|
-
server.listen(0, () => {
|
|
248
|
-
const { port } = server.address();
|
|
249
|
-
server.close(() => resolve(port));
|
|
250
|
-
});
|
|
251
|
-
server.on('error', reject);
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
async function startTraceServer(config, rootPath) {
|
|
255
|
-
if (!config.saveTrace)
|
|
256
|
-
return undefined;
|
|
257
|
-
const tracesDir = await outputFile(config, rootPath, `traces-${Date.now()}`);
|
|
258
|
-
const server = await startTraceViewerServer();
|
|
259
|
-
const urlPrefix = server.urlPrefix('human-readable');
|
|
260
|
-
const url = urlPrefix + '/trace/index.html?trace=' + tracesDir + '/trace.json';
|
|
261
|
-
// eslint-disable-next-line no-console
|
|
262
|
-
console.error('\nTrace viewer listening on ' + url);
|
|
263
|
-
return tracesDir;
|
|
264
|
-
}
|