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";
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It describes the core behavior (switching tabs) and mentions prerequisites (multiple tabs must be open), but doesn't disclose potential side effects like whether this affects page state, if it waits for page load, or error conditions. For a mutation tool with zero annotation coverage, this leaves some behavioral gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is efficiently structured with two sentences: the first states the purpose and context, the second provides usage guidance with a concrete example and references to another tool. Every sentence adds clear value without redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (changing browser state), no annotations, and no output schema, the description does well by explaining purpose, usage context, and parameter relationships. However, it could be more complete by mentioning what happens after switching (e.g., whether the agent should wait for page load) or potential errors.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents both parameters completely. The description adds value by explaining why tabIndex matters ('Use get_current_page_info to see available tabs') and providing a real-world context for its use, though it doesn't add syntax or format details beyond what the schema provides.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Switch the active browser tab') and resource ('when multiple tabs are open in the session'), distinguishing it from siblings like get_current_page_info (which lists tabs) or navigate_to_url (which loads new pages). It provides concrete context about why this tool is needed in a multi-tab scenario.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description explicitly states when to use this tool ('when multiple tabs are open') and provides a clear alternative ('Use get_current_page_info first to see all available tabs and their indices'), naming the sibling tool. It also gives a practical example scenario ('clicking a link that opens a chat in a new tab requires switching to that tab'), making usage context very clear.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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