switch_tab
Switch between open browser tabs to interact with content in different tabs. Use when links open new tabs or you need to access multiple pages during web automation sessions.
Instructions
Switch the active browser tab when multiple tabs are open in the session. Common scenario: clicking a link that opens a chat in a new tab requires switching to that tab to interact with it. Use get_current_page_info first to see all available tabs and their indices.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sessionId | Yes | Session ID obtained from initialize_session | |
| tabIndex | Yes | Zero-based index of the tab to switch to (0 = first tab, 1 = second tab, etc.). Use get_current_page_info to see available tabs. |
Implementation Reference
- src/tools/navigation.js:36-61 (handler)The core handler function that executes the switch_tab tool: switches to the specified tab index in the browser session using Playwright context.pages() and updates the session's active page.export async function switchTab(sessionId, tabIndex) { const session = getSession(sessionId); const { context } = session; const pages = context.pages(); if (tabIndex < 0 || tabIndex >= pages.length) { throw new Error( `Tab index ${tabIndex} out of range. Available tabs: 0-${ pages.length - 1 }` ); } const newPage = pages[tabIndex]; session.page = newPage; return { success: true, sessionId, currentTabIndex: tabIndex, totalTabs: pages.length, currentUrl: newPage.url(), title: await newPage.title(), message: `Switched to tab ${tabIndex}`, }; }
- src/index.js:201-220 (schema)The tool schema definition including name, description, and inputSchema for 'switch_tab' as registered in the MCP ListTools response.{ name: "switch_tab", description: "Switch the active browser tab when multiple tabs are open in the session. Common scenario: clicking a link that opens a chat in a new tab requires switching to that tab to interact with it. Use get_current_page_info first to see all available tabs and their indices.", inputSchema: { type: "object", properties: { sessionId: { type: "string", description: "Session ID obtained from initialize_session", }, tabIndex: { type: "number", description: "Zero-based index of the tab to switch to (0 = first tab, 1 = second tab, etc.). Use get_current_page_info to see available tabs.", }, }, required: ["sessionId", "tabIndex"], }, },
- src/index.js:477-493 (registration)Registration in the CallToolRequestSchema switch statement: validates parameters and calls the switchTab handler for 'switch_tab' tool invocations.case "switch_tab": { const { sessionId, tabIndex } = args; if (!sessionId) { throw new McpError( ErrorCode.InvalidParams, "sessionId parameter is required" ); } if (typeof tabIndex !== "number") { throw new McpError( ErrorCode.InvalidParams, "tabIndex parameter must be a number" ); } result = await switchTab(sessionId, tabIndex); break; }
- src/index.js:12-26 (registration)Import statement that brings the switchTab function into the main server file from the tools aggregator.reverseEngineerChat, takeScreenshot, clickElement, fillForm, switchTab, waitForElement, navigateToUrl, getCurrentPageInfo, initializeSession, closeSession, startNetworkCapture, stopNetworkCapture, getNetworkCaptureStatus, clearNetworkCapture, } from "./tools/reverseEngineer.js";
- src/tools/reverseEngineer.js:19-19 (helper)Re-export of the switchTab function from its implementation module to the central tools export file.export { navigateToUrl, switchTab } from "./navigation.js";