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
| Name | Required | Description | Default |
|---|---|---|---|
| tabId | No | Tab ID to operate on (uses active tab if not specified) |
Implementation Reference
- src/tabs.ts:133-158 (handler)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); } }
- src/tools/tab-tools.ts:33-50 (registration)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); } );
- src/schemas.ts:16-18 (schema)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, });