connect-browser-with-ws
Establish a WebSocket connection to an AdsPower browser instance using its WebSocket URL, enabling remote control and automation of browser profiles.
Instructions
Connect the browser with the ws url
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| userId | No | The browser id of the browser to connect | |
| serialNumber | No | The serial number of the browser to connect | |
| wsUrl | Yes | The ws url of the browser, get from the open-browser tool content `ws.puppeteer` |
Implementation Reference
- src/handlers/automation.ts:9-16 (handler)The main handler function for the 'connect-browser-with-ws' tool. It receives the wsUrl parameter and delegates the connection to the browserBase instance, handling success/error responses.
async connectBrowserWithWs({ wsUrl }: CreateAutomationParams) { try { await browser.connectBrowserWithWs(wsUrl); return `Browser connected successfully with: ${wsUrl}`; } catch (error) { return `Failed to connect browser with: ${error?.toString()}`; } }, - src/types/schemas.ts:167-171 (schema)Zod schema defining input parameters for the tool: optional userId/serialNumber and required wsUrl.
createAutomationSchema: z.object({ userId: z.string().optional().describe('The browser id of the browser to connect'), serialNumber: z.string().optional().describe('The serial number of the browser to connect'), wsUrl: z.string().describe('The ws url of the browser, get from the open-browser tool content `ws.puppeteer`') }).strict(), - src/utils/toolRegister.ts:50-51 (registration)Registers the tool with the MCP server, specifying name, description, input schema, and the wrapped handler function.
server.tool('connect-browser-with-ws', 'Connect the browser with the ws url', schemas.createAutomationSchema.shape, wrapHandler(automationHandlers.connectBrowserWithWs)); - src/utils/browserBase.ts:43-50 (helper)Supporting method in BrowserBase class that performs the actual WebSocket connection to an existing browser using Playwright's connectOverCDP.
async connectBrowserWithWs(wsUrl: string) { this.browser = await chromium.connectOverCDP(wsUrl); const defaultContext = this.browser.contexts()[0]; this.page = defaultContext.pages()[0]; await this.page.bringToFront().catch((error) => { console.error('Failed to bring page to front', error); }); }