Skip to main content
Glama

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
NameRequiredDescriptionDefault
indexYesThe index of the window to switch to

Implementation Reference

  • 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}`,
            },
          ],
        };
      }
    }
  • 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'),
    },
  • 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}`,
              },
            ],
          };
        }
      }
    );
Behavior2/5

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

With no annotations provided, the description carries full burden but offers minimal behavioral insight. It doesn't disclose what 'switch' entails (e.g., focus change, potential visual updates), error conditions (e.g., invalid index), side effects, or how indices are determined (e.g., order of opening). This leaves critical operational details unclear for a mutation tool.

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 a single, efficient sentence with zero wasted words. It's front-loaded with the core action and parameter, making it immediately understandable without unnecessary elaboration.

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

Completeness2/5

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

For a mutation tool with no annotations and no output schema, the description is incomplete. It lacks details on behavior (e.g., what happens on success/failure), index semantics (e.g., zero-based), and relationship to sibling tools. Given the complexity of browser control, more context is needed for safe and effective use.

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

Parameters3/5

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

Schema description coverage is 100%, with the parameter 'index' clearly documented in the schema. The description adds no additional meaning beyond implying the parameter is used for selection, matching the baseline score when the schema does the heavy lifting.

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

Purpose4/5

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

The description clearly states the action ('Switch to') and target ('a window by its index'), providing a specific verb+resource combination. It distinguishes this tool from other window-switching siblings like 'browser_switch_to_window', 'browser_switch_to_window_by_title', and 'browser_switch_to_window_by_url' by specifying the index-based selection method, though it doesn't explicitly contrast them.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives like 'browser_switch_to_window' (which likely uses handles) or 'browser_switch_to_window_by_title'. It lacks context about prerequisites (e.g., needing open windows), exclusions, or typical scenarios for index-based switching.

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/pshivapr/selenium-mcp'

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