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