switch_tab
Switch active browser tab using a window handle or 0-based index. Subsequent commands execute on the selected tab.
Instructions
Focuses a browser tab by window handle or 0-based index. All subsequent tool calls operate on the active tab. Provide handle OR index — use get_tabs to find them. Browser-only; use switch_context for mobile webviews.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| handle | No | Window handle to switch to | |
| index | No | 0-based tab index to switch to |
Implementation Reference
- src/tools/tabs.tool.ts:17-35 (handler)The switchTabTool callback: the main handler that executes the switch_tab logic. Accepts handle or index, uses getBrowser() to call switchToWindow or resolves index via getWindowHandles.
export const switchTabTool: ToolCallback = async ({ handle, index }: { handle?: string; index?: number }): Promise<CallToolResult> => { try { const browser = getBrowser(); if (handle) { await browser.switchToWindow(handle); return { content: [{ type: 'text', text: `Switched to tab: ${handle}` }] }; } else if (index !== undefined) { const handles = await browser.getWindowHandles(); if (index >= handles.length) { return { isError: true, content: [{ type: 'text', text: `Error: index ${index} out of range (${handles.length} tabs)` }] }; } await browser.switchToWindow(handles[index]); return { content: [{ type: 'text', text: `Switched to tab ${index}: ${handles[index]}` }] }; } return { isError: true, content: [{ type: 'text', text: 'Error: Must provide either handle or index' }] }; } catch (e) { return { isError: true, content: [{ type: 'text', text: `Error switching tab: ${e}` }] }; } }; - src/tools/tabs.tool.ts:7-15 (schema)The switchTabToolDefinition: defines the 'switch_tab' tool name, description, annotations, and inputSchema (optional handle string and optional index number).
export const switchTabToolDefinition: ToolDefinition = { name: 'switch_tab', description: 'Focuses a browser tab by window handle or 0-based index. All subsequent tool calls operate on the active tab. Provide handle OR index — use get_tabs to find them. Browser-only; use switch_context for mobile webviews.', annotations: { title: 'Switch Browser Tab', destructiveHint: false, idempotentHint: true }, inputSchema: { handle: z.string().optional().describe('Window handle to switch to'), index: z.number().int().min(0).optional().describe('0-based tab index to switch to'), }, }; - src/server.ts:129-129 (registration)Registration of switch_tab tool via registerTool(switchTabToolDefinition, switchTabTool) at line 129.
registerTool(switchTabToolDefinition, switchTabTool); - src/server.ts:68-68 (registration)Import of switchTabTool and switchTabToolDefinition from tools/tabs.tool.ts into the server entry point.
import { switchTabTool, switchTabToolDefinition } from './tools/tabs.tool'; - src/tools/tabs.tool.ts:5-5 (helper)Import of getBrowser() helper from session/state which retrieves the current browser instance used by the handler.
import { getBrowser } from '../session/state';