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
| Name | Required | Description | Default |
|---|---|---|---|
| tabId | No | Tab ID to operate on (uses active tab if not specified) |
Implementation Reference
- src/tools/tab-tools.ts:33-50 (registration)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); } );
- src/tabs.ts:133-158 (handler)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); } }
- src/schemas.ts:16-18 (schema)Zod schema defining the input for close_tab tool: optional tabId string.export const closeTabSchema = z.object({ tabId: tabIdSchema, });