Skip to main content
Glama
andytango
by andytango

switch_tab

Switch between browser tabs to make a specific tab active for automated interactions like navigation, content extraction, or form filling.

Instructions

Switch to a different tab, making it the active tab for subsequent operations.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tabIdYesTab ID to switch to

Implementation Reference

  • Core implementation of switching to the specified tab: retrieves the page from state, sets it as active tab ID, brings the page to front, returns tab info on success or tabNotFound error.
    export async function switchTab(tabId) {
        const page = state.tabs.get(tabId);
        if (!page) {
            return err(tabNotFound(tabId));
        }
        state.activeTabId = tabId;
        try {
            // Bring page to front
            await page.bringToFront();
            return ok({
                id: tabId,
                url: page.url(),
                title: await page.title(),
                isActive: true,
            });
        }
        catch (error) {
            return err(tabNotFound(tabId));
        }
    }
  • Zod schema defining the input for the switch_tab tool: requires a tabId string.
    export const switchTabSchema = z.object({
        tabId: z.string().describe('Tab ID to switch to'),
    });
  • Registers the 'switch_tab' MCP tool on the server, using the switchTabSchema for input validation and an inline async handler that delegates to switchTab and wraps the result with handleResult.
    server.tool('switch_tab', 'Switch to a different tab, making it the active tab for subsequent operations.', switchTabSchema.shape, async ({ tabId }) => {
        const result = await switchTab(tabId);
        return handleResult(result);
    });

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/andytango/puppeteer-mcp'

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