browser_switch_window_by_index
Switch between multiple browser windows or tabs using numerical indexing to manage web automation tasks across different pages.
Instructions
Switch to a window by its index
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | Yes | The index of the window to switch to |
Implementation Reference
- src/tools/browserTools.ts:332-361 (handler)Handler function that switches the browser window by the given index using Selenium WebDriver. Validates index range, retrieves window handles, switches to the specified handle, and returns success or error message.async ({ index }) => { try { const driver = stateManager.getDriver(); const windowHandles = await driver.getAllWindowHandles(); if (index < 0 || index >= windowHandles.length) { return { content: [{ type: 'text', text: `Invalid window index: ${index}` }], }; } const handle = windowHandles[index]; if (!handle) { return { content: [{ type: 'text', text: `No window handle found at index: ${index}` }], }; } await driver.switchTo().window(handle); return { content: [{ type: 'text', text: `Switched to window at index: ${index}` }], }; } catch (e) { return { content: [ { type: 'text', text: `Error switching to window by index: ${(e as Error).message}`, }, ], }; } }
- src/tools/browserTools.ts:329-331 (schema)Zod schema defining the input parameter 'index' as a number for the tool.{ index: z.number().describe('The index of the window to switch to'), },
- src/tools/browserTools.ts:327-362 (registration)Registers the 'browser_switch_window_by_index' tool on the MCP server with name, description, input schema, and inline handler function.'browser_switch_window_by_index', 'Switch to a window by its index', { index: z.number().describe('The index of the window to switch to'), }, async ({ index }) => { try { const driver = stateManager.getDriver(); const windowHandles = await driver.getAllWindowHandles(); if (index < 0 || index >= windowHandles.length) { return { content: [{ type: 'text', text: `Invalid window index: ${index}` }], }; } const handle = windowHandles[index]; if (!handle) { return { content: [{ type: 'text', text: `No window handle found at index: ${index}` }], }; } await driver.switchTo().window(handle); return { content: [{ type: 'text', text: `Switched to window at index: ${index}` }], }; } catch (e) { return { content: [ { type: 'text', text: `Error switching to window by index: ${(e as Error).message}`, }, ], }; } } );