Skip to main content
Glama

switch_tab

Switch between open browser tabs during web automation sessions to interact with content in new tabs, such as chat interfaces opened from links.

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
NameRequiredDescriptionDefault
sessionIdYesSession ID obtained from initialize_session
tabIndexYesZero-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

  • The core handler function that switches the active page/tab in the browser session by index using Playwright.
    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}`, }; }
  • MCP tool schema definition including name, description, and input schema for switch_tab.
    { 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 and dispatching logic in the MCP CallToolRequestSchema handler that invokes the switchTab function.
    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:16-26 (registration)
    Import of the switchTab tool from reverseEngineer.js into the main index.js.
    switchTab, waitForElement, navigateToUrl, getCurrentPageInfo, initializeSession, closeSession, startNetworkCapture, stopNetworkCapture, getNetworkCaptureStatus, clearNetworkCapture, } from "./tools/reverseEngineer.js";
  • Re-export of switchTab from navigation.js to centralize tool exports.
    export { navigateToUrl, switchTab } from "./navigation.js";

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/pyscout/webscout-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server