|
| 1 | +import { remote } from 'webdriverio'; |
| 2 | +import type { ToolCallback } from '@modelcontextprotocol/sdk/server/mcp'; |
| 3 | +import type { CallToolResult } from '@modelcontextprotocol/sdk/types'; |
| 4 | +import type { ToolDefinition } from '../types/tool'; |
| 5 | +import { z } from 'zod'; |
| 6 | +import { getBrowser } from './browser.tool'; |
| 7 | + |
| 8 | +export const attachBrowserToolDefinition: ToolDefinition = { |
| 9 | + name: 'attach_browser', |
| 10 | + description: `Attach to a Chrome instance already running with --remote-debugging-port. |
| 11 | +
|
| 12 | +Start Chrome first (quit any running Chrome instance before launching): |
| 13 | +
|
| 14 | + macOS — with real profile (preserves extensions, cookies, logins): |
| 15 | + pkill -x "Google Chrome" && sleep 1 |
| 16 | + /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --remote-debugging-port=9222 --user-data-dir="$HOME/Library/Application Support/Google/Chrome" --profile-directory=Default & |
| 17 | +
|
| 18 | + macOS — with fresh profile (lightweight, no extensions): |
| 19 | + pkill -x "Google Chrome" && sleep 1 |
| 20 | + /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-debug & |
| 21 | +
|
| 22 | + Linux — with real profile: |
| 23 | + google-chrome --remote-debugging-port=9222 --user-data-dir="$HOME/.config/google-chrome" --profile-directory=Default & |
| 24 | +
|
| 25 | + Linux — with fresh profile: |
| 26 | + google-chrome --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-debug & |
| 27 | +
|
| 28 | +Verify Chrome is ready: curl http://localhost:9222/json/version |
| 29 | +
|
| 30 | +Then call attach_browser() to hand control to the AI. All other tools (navigate, click, get_visible_elements, etc.) will work on the attached session. Use close_session() to detach without closing Chrome.`, |
| 31 | + inputSchema: { |
| 32 | + port: z.number().default(9222).describe('Chrome remote debugging port (default: 9222)'), |
| 33 | + host: z.string().default('localhost').describe('Host where Chrome is running (default: localhost)'), |
| 34 | + userDataDir: z.string().default('/tmp/chrome-debug').describe('Chrome user data directory — must match the --user-data-dir used when launching Chrome. Use your real profile path (e.g. "$HOME/Library/Application Support/Google/Chrome") to preserve extensions and logins, or /tmp/chrome-debug for a fresh profile (default: /tmp/chrome-debug)'), |
| 35 | + navigationUrl: z.string().optional().describe('URL to navigate to immediately after attaching'), |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +async function getActiveTabUrl(host: string, port: number): Promise<string | null> { |
| 40 | + try { |
| 41 | + const res = await fetch(`http://${host}:${port}/json`); |
| 42 | + const tabs = await res.json() as { type: string; url: string }[]; |
| 43 | + const page = tabs.find((t) => t.type === 'page' && t.url && !t.url.startsWith('devtools://')); |
| 44 | + return page?.url ?? null; |
| 45 | + } catch { |
| 46 | + return null; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export const attachBrowserTool: ToolCallback = async ({ |
| 51 | + port = 9222, |
| 52 | + host = 'localhost', |
| 53 | + userDataDir = '/tmp/chrome-debug', |
| 54 | + navigationUrl, |
| 55 | +}: { |
| 56 | + port?: number; |
| 57 | + host?: string; |
| 58 | + userDataDir?: string; |
| 59 | + navigationUrl?: string; |
| 60 | +}): Promise<CallToolResult> => { |
| 61 | + try { |
| 62 | + const state = (getBrowser as any).__state; |
| 63 | + |
| 64 | + // Capture the active tab URL before WebDriver blanks it |
| 65 | + const activeUrl = navigationUrl ?? await getActiveTabUrl(host, port); |
| 66 | + |
| 67 | + const browser = await remote({ |
| 68 | + capabilities: { |
| 69 | + browserName: 'chrome', |
| 70 | + 'goog:chromeOptions': { |
| 71 | + debuggerAddress: `${host}:${port}`, |
| 72 | + args: [`--user-data-dir=${userDataDir}`], |
| 73 | + }, |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + const { sessionId } = browser; |
| 78 | + state.browsers.set(sessionId, browser); |
| 79 | + state.currentSession = sessionId; |
| 80 | + state.sessionMetadata.set(sessionId, { |
| 81 | + type: 'browser', |
| 82 | + capabilities: browser.capabilities, |
| 83 | + isAttached: true, |
| 84 | + }); |
| 85 | + |
| 86 | + if (activeUrl) { |
| 87 | + await browser.url(activeUrl); |
| 88 | + } |
| 89 | + |
| 90 | + const title = await browser.getTitle(); |
| 91 | + const url = await browser.getUrl(); |
| 92 | + |
| 93 | + return { |
| 94 | + content: [{ |
| 95 | + type: 'text', |
| 96 | + text: `Attached to Chrome on ${host}:${port}\nSession ID: ${sessionId}\nCurrent page: "${title}" (${url})`, |
| 97 | + }], |
| 98 | + }; |
| 99 | + } catch (e) { |
| 100 | + return { |
| 101 | + content: [{ type: 'text', text: `Error attaching to browser: ${e}` }], |
| 102 | + }; |
| 103 | + } |
| 104 | +}; |
0 commit comments