Skip to main content
Glama

browser_switch_to_window_by_title

Switch browser focus to a specific window by matching its title, enabling multi-window automation workflows in Selenium testing.

Instructions

Switch to a window by its title

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
titleYesThe title of the window to switch to

Implementation Reference

  • Full implementation of the 'browser_switch_to_window_by_title' tool, including inline schema and handler logic. The handler loops through all browser window handles, switches to each, retrieves the title, and remains on the window matching the provided title or reports not found.
    server.tool(
      'browser_switch_to_window_by_title',
      'Switch to a window by its title',
      {
        title: z.string().describe('The title of the window to switch to'),
      },
      async ({ title }) => {
        try {
          const driver = stateManager.getDriver();
          const windowHandles = await driver.getAllWindowHandles();
          for (const handle of windowHandles) {
            await driver.switchTo().window(handle);
            const currentTitle = await driver.getTitle();
            if (currentTitle === title) {
              return {
                content: [{ type: 'text', text: `Switched to window: ${title}` }],
              };
            }
          }
          return {
            content: [{ type: 'text', text: `Window with title '${title}' not found` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error switching to window by title: ${(e as Error).message}`,
              },
            ],
          };
        }
      }
    );
  • Zod input schema for the tool requiring a 'title' string parameter.
    {
      title: z.string().describe('The title of the window to switch to'),
    },
  • src/tools/index.ts:9-9 (registration)
    Invocation of registerBrowserTools within the top-level registerAllTools function, which registers the browser tools including 'browser_switch_to_window_by_title'.
    registerBrowserTools(server, stateManager);
  • The registerBrowserTools function that contains all browser tool registrations, including the target tool.
    export function registerBrowserTools(server: McpServer, stateManager: StateManager) {
      server.tool(
        'browser_open',
        'Open a new browser session',
        {
          browser: z.enum(['chrome', 'firefox', 'edge', 'safari']).describe('Browser to launch'),
          options: browserOptionsSchema,
        },
        async ({ browser, options = {} }) => {
          try {
            const driver = await BrowserService.createDriver(browser, {
              ...options,
              headless: typeof options.headless === 'boolean' ? options.headless : false,
              arguments: options.arguments ?? [],
            });
            const sessionId = `${browser}_${Date.now()}`;
    
            stateManager.addDriver(sessionId, driver);
            stateManager.setCurrentSession(sessionId);
    
            return {
              content: [
                {
                  type: 'text',
                  text: `Browser started with session_id: ${sessionId}`,
                },
              ],
            };
          } catch (e) {
            return {
              content: [
                {
                  type: 'text',
                  text: `Error starting browser: ${(e as Error).message}`,
                },
              ],
            };
          }
        }
      );
    
      server.tool(
        'browser_navigate',
        'Navigate to a URL',
        {
          url: z.string().describe('URL to navigate to'),
        },
        async ({ url }) => {
          try {
            const driver = stateManager.getDriver();
            await driver.get(url);
            return {
              content: [{ type: 'text', text: `Navigated to ${url}` }],
            };
          } catch (e) {
            return {
              content: [{ type: 'text', text: `Error navigating: ${(e as Error).message}` }],
            };
          }
        }
      );
    
      server.tool('browser_navigate_back', 'Navigate back in the browser', {}, async () => {
        try {
          const driver = stateManager.getDriver();
          await driver.navigate().back();
          return {
            content: [{ type: 'text', text: `Navigated back` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error navigating back: ${(e as Error).message}`,
              },
            ],
          };
        }
      });
    
      server.tool('browser_navigate_forward', 'Navigate forward in the browser', {}, async () => {
        try {
          const driver = stateManager.getDriver();
          await driver.navigate().forward();
          return {
            content: [{ type: 'text', text: `Navigated forward` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error navigating forward: ${(e as Error).message}`,
              },
            ],
          };
        }
      });
    
      server.tool('browser_title', 'Get the current page title', {}, async () => {
        try {
          const driver = stateManager.getDriver();
          const title = await driver.getTitle();
          return {
            content: [{ type: 'text', text: `Current page title is: ${title}` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error getting page title: ${(e as Error).message}`,
              },
            ],
          };
        }
      });
    
      server.tool('browser_get_url', 'Get the current page URL', {}, async () => {
        try {
          const driver = stateManager.getDriver();
          const url = await driver.getCurrentUrl();
          return {
            content: [{ type: 'text', text: `Current page URL is: ${url}` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error getting page URL: ${(e as Error).message}`,
              },
            ],
          };
        }
      });
    
      server.tool('browser_get_page_source', 'Get the current page source', {}, async () => {
        try {
          const driver = stateManager.getDriver();
          const pageSource = await driver.getPageSource();
          return {
            content: [{ type: 'text', text: `Current page source is: ${pageSource}` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error getting page source: ${(e as Error).message}`,
              },
            ],
          };
        }
      });
    
      server.tool('browser_maximize', 'Maximize the browser window', {}, async () => {
        try {
          const driver = stateManager.getDriver();
          await driver.manage().window().maximize();
          return {
            content: [{ type: 'text', text: `Browser window maximised` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error maximising browser window: ${(e as Error).message}`,
              },
            ],
          };
        }
      });
    
      server.tool(
        'browser_resize',
        'Resize the browser window',
        {
          width: z.number().describe('New width of the browser window'),
          height: z.number().describe('New height of the browser window'),
        },
        async ({ width, height }) => {
          try {
            const driver = stateManager.getDriver();
            await driver.manage().window().setRect({ width, height });
            return {
              content: [
                {
                  type: 'text',
                  text: `Browser window resized to ${width}x${height}`,
                },
              ],
            };
          } catch (e) {
            return {
              content: [
                {
                  type: 'text',
                  text: `Error resizing browser window: ${(e as Error).message}`,
                },
              ],
            };
          }
        }
      );
    
      server.tool('browser_refresh', 'Refresh the current page', {}, async () => {
        try {
          const driver = stateManager.getDriver();
          await driver.navigate().refresh();
          return {
            content: [{ type: 'text', text: `Browser refreshed` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error refreshing browser: ${(e as Error).message}`,
              },
            ],
          };
        }
      });
    
      server.tool(
        'browser_switch_to_window',
        'Switch to a different browser window',
        {
          windowHandle: z.string().describe('The handle of the window to switch to'),
        },
        async ({ windowHandle }) => {
          try {
            const driver = stateManager.getDriver();
            await driver.switchTo().window(windowHandle);
            return {
              content: [{ type: 'text', text: `Switched to window: ${windowHandle}` }],
            };
          } catch (e) {
            return {
              content: [
                {
                  type: 'text',
                  text: `Error switching window: ${(e as Error).message}`,
                },
              ],
            };
          }
        }
      );
    
      server.tool('browser_switch_to_original_window', 'Switches back to the original browser window', {}, async () => {
        try {
          const driver = stateManager.getDriver();
          const windowHandles = await driver.getAllWindowHandles();
          const originalHandle = windowHandles[0];
          if (!originalHandle) {
            return {
              content: [
                {
                  type: 'text',
                  text: `No original window handle found.`,
                },
              ],
            };
          }
          await driver.switchTo().window(originalHandle);
          return {
            content: [{ type: 'text', text: `Switched to original window` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error switching to original window: ${(e as Error).message}`,
              },
            ],
          };
        }
      });
    
      server.tool(
        'browser_switch_to_window_by_title',
        'Switch to a window by its title',
        {
          title: z.string().describe('The title of the window to switch to'),
        },
        async ({ title }) => {
          try {
            const driver = stateManager.getDriver();
            const windowHandles = await driver.getAllWindowHandles();
            for (const handle of windowHandles) {
              await driver.switchTo().window(handle);
              const currentTitle = await driver.getTitle();
              if (currentTitle === title) {
                return {
                  content: [{ type: 'text', text: `Switched to window: ${title}` }],
                };
              }
            }
            return {
              content: [{ type: 'text', text: `Window with title '${title}' not found` }],
            };
          } catch (e) {
            return {
              content: [
                {
                  type: 'text',
                  text: `Error switching to window by title: ${(e as Error).message}`,
                },
              ],
            };
          }
        }
      );
    
      server.tool(
        '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}`,
                },
              ],
            };
          }
        }
      );
    
      server.tool(
        'browser_switch_to_window_by_url',
        'Switch to a window by its URL',
        {
          url: z.string().describe('The URL of the window to switch to'),
        },
        async ({ url }) => {
          try {
            const driver = stateManager.getDriver();
            const windowHandles = await driver.getAllWindowHandles();
            for (const handle of windowHandles) {
              await driver.switchTo().window(handle);
              const currentUrl = await driver.getCurrentUrl();
              if (currentUrl === url) {
                return {
                  content: [{ type: 'text', text: `Switched to window: ${url}` }],
                };
              }
            }
            return {
              content: [{ type: 'text', text: `Window with URL '${url}' not found` }],
            };
          } catch (e) {
            return {
              content: [
                {
                  type: 'text',
                  text: `Error switching to window by URL: ${(e as Error).message}`,
                },
              ],
            };
          }
        }
      );
    
      server.tool('browser_close', 'Close the current browser session', {}, async () => {
        try {
          const driver = stateManager.getDriver();
          await driver.quit();
          const sessionId = stateManager.getCurrentSession();
    
          if (sessionId) {
            stateManager.removeDriver(sessionId);
          }
          stateManager.resetCurrentSession();
    
          return {
            content: [{ type: 'text', text: `Browser session ${sessionId} closed` }],
          };
        } catch (e) {
          return {
            content: [
              {
                type: 'text',
                text: `Error closing session: ${(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 the full burden of behavioral disclosure. It states the action but doesn't mention potential outcomes (e.g., what happens if no window matches the title, if it's a destructive operation, or if it affects browser state). This leaves gaps in understanding the tool's behavior beyond the basic action.

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, making it easy to parse and front-loaded with the core action. It earns its place by succinctly conveying the tool's purpose without unnecessary elaboration.

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

Completeness3/5

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

Given the tool's low complexity (one parameter, no output schema, no annotations), the description is minimally adequate but lacks depth. It doesn't address behavioral aspects like error handling or sibling differentiation, leaving room for improvement despite the simple context.

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?

The input schema has 100% description coverage, with the single parameter 'title' clearly documented. The description adds no additional meaning beyond what the schema provides, such as format examples or matching behavior. This meets the baseline of 3 for high schema coverage.

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 resource ('a window by its title'), making the purpose immediately understandable. It doesn't differentiate from sibling tools like 'browser_switch_to_window' or 'browser_switch_to_window_by_url', which would require a 5, but it's specific enough to avoid vagueness.

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?

No guidance is provided on when to use this tool versus alternatives such as 'browser_switch_to_window' or 'browser_switch_to_window_by_url'. The description lacks context about prerequisites (e.g., needing an open window with a matching title) or exclusions, offering only a basic statement of function.

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