forked from ChromeDevTools/chrome-devtools-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultibrowser.ts
More file actions
116 lines (112 loc) · 3.42 KB
/
multibrowser.ts
File metadata and controls
116 lines (112 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
switchActiveBrowser,
listBrowsers,
addBrowser,
} from '../browser.js';
import {zod} from '../third_party/index.js';
import {ToolCategory} from './categories.js';
import {defineTool} from './ToolDefinition.js';
export const switch_browser = defineTool({
name: 'switch_browser',
description:
'Switch the active browser instance. All subsequent tool calls will operate on the selected browser.',
annotations: {
category: ToolCategory.NAVIGATION,
readOnlyHint: false,
},
schema: {
browserId: zod
.string()
.describe(
'The ID of the browser to switch to. Call list_browsers to see available browsers.',
),
},
handler: async (request, response) => {
const {browserId} = request.params;
switchActiveBrowser(browserId);
response.appendResponseLine(
`Switched to browser '${browserId}'. All future tool calls will target this browser.`,
);
response.appendResponseLine(`Active browser: ${browserId}`);
},
});
export const list_browsers = defineTool({
name: 'list_browsers',
description:
'List all connected browser instances with their IDs and connection status.',
annotations: {
category: ToolCategory.NAVIGATION,
readOnlyHint: true,
},
schema: {},
handler: async (_request, response) => {
const browsers = listBrowsers();
if (browsers.length === 0) {
response.appendResponseLine('No browsers connected.');
return;
}
response.appendResponseLine('## Connected browsers');
for (const b of browsers) {
const marker = b.active ? ' [selected]' : '';
const status = b.connected ? 'connected' : 'disconnected';
response.appendResponseLine(`- ${b.id}: ${status}${marker}`);
}
},
});
export const add_browser = defineTool({
name: 'add_browser',
description:
'Connect to an additional Chrome browser instance on a different debugging port.',
annotations: {
category: ToolCategory.NAVIGATION,
readOnlyHint: false,
},
schema: {
browserId: zod
.string()
.describe(
'A unique ID for this browser instance (e.g. "admin", "test-user", "dev").',
),
browserUrl: zod
.string()
.describe(
'The debugging URL of the Chrome instance (e.g. "http://127.0.0.1:9224").',
),
switchTo: zod
.boolean()
.optional()
.describe(
'Whether to immediately switch to this browser after connecting. Default is true.',
),
},
handler: async (request, response) => {
const {browserId, browserUrl, switchTo = true} = request.params;
try {
await addBrowser(browserId, {browserURL: browserUrl});
response.appendResponseLine(
`Browser '${browserId}' connected successfully at ${browserUrl}.`,
);
if (switchTo) {
switchActiveBrowser(browserId);
response.appendResponseLine(`Switched to browser '${browserId}'.`);
}
const browsers = listBrowsers();
response.appendResponseLine('\n## All browsers');
for (const b of browsers) {
const marker = b.active ? ' [selected]' : '';
response.appendResponseLine(
`- ${b.id}: ${b.connected ? 'connected' : 'disconnected'}${marker}`,
);
}
} catch (err) {
throw new Error(
`Failed to connect to browser '${browserId}' at ${browserUrl}: ${(err as Error).message}`,
);
}
},
});