Skip to main content
Glama
andytango
by andytango

close_tab

Close browser tabs in Puppeteer automation. Specify a tab ID or close the active tab to manage browser sessions and resources.

Instructions

Close a browser tab. If no tabId provided, closes the active tab.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tabIdNoTab ID to operate on (uses active tab if not specified)

Implementation Reference

  • Registers the 'close_tab' MCP tool with server.tool(), including description, input schema, and the handler function that determines the target tab, calls closeTab, and returns closed tab info.
    server.tool(
      'close_tab',
      'Close a browser tab. If no tabId provided, closes the active tab.',
      closeTabSchema.shape,
      async ({ tabId }) => {
        const targetId = tabId ?? getActiveTabId();
        const result = await closeTab(tabId);
    
        if (result.success) {
          return handleResult(ok({
            closedTabId: targetId,
            newActiveTabId: getActiveTabId(),
          }));
        }
    
        return handleResult(result);
      }
    );
  • Core handler logic for closing a tab: retrieves the Puppeteer Page, calls page.close(), handles errors and state updates for tabs and active tab.
    export async function closeTab(tabId?: string): Promise<Result<void>> {
      const targetId = tabId ?? state.activeTabId;
    
      if (!targetId) {
        return err(noActiveTab());
      }
    
      const page = state.tabs.get(targetId);
      if (!page) {
        return err(tabNotFound(targetId));
      }
    
      try {
        await page.close();
        // Page close handler will update state
        return ok(undefined);
      } catch (error) {
        // Page may already be closed
        state.tabs.delete(targetId);
        if (state.activeTabId === targetId) {
          const remaining = state.tabs.keys().next();
          state.activeTabId = remaining.done ? null : remaining.value;
        }
        return ok(undefined);
      }
    }
  • Zod schema defining the input for close_tab tool: optional tabId string.
    export const closeTabSchema = z.object({
      tabId: tabIdSchema,
    });

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