Skip to main content
Glama

close_tab

Close browser tabs in Puppeteer automation workflows. 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

  • Core handler function that closes the specified browser tab (or active tab if no tabId) using Puppeteer Page.close(), updates internal tab state, and handles errors gracefully.
    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); } }
  • Registers the 'close_tab' MCP tool with the server, including description, input schema, and a thin wrapper handler that delegates to the core closeTab function and enriches the response with closed and new active tab IDs.
    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); } );
  • Zod schema defining the input for close_tab tool: optional tabId string (uses active tab if omitted). tabIdSchema is defined earlier as z.string().optional().
    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