browser_tab_close
Automatically close a specific browser tab by its index or the current tab using Cloudflare Playwright MCP, enabling precise control during automated browser testing.
Instructions
Close a tab
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | No | The index of the tab to close. Closes current tab if not provided. |
Implementation Reference
- src/tools/tabs.ts:116-126 (handler)Handler function that executes the browser_tab_close tool: closes the tab via context.closeTab, generates internal code comment, and sets execution flags.handle: async (context, params) => { await context.closeTab(params.index); const code = [ `// <internal code to close tab ${params.index}>`, ]; return { code, captureSnapshot, waitForNetwork: false }; },
- src/tools/tabs.ts:106-114 (schema)Schema definition for the browser_tab_close tool, including name, title, description, input schema for optional tab index, and type as destructive.schema: { name: 'browser_tab_close', title: 'Close a tab', description: 'Close a tab', inputSchema: z.object({ index: z.coerce.number().optional().describe('The index of the tab to close. Closes current tab if not provided.'), }), type: 'destructive', },
- src/tools.ts:27-66 (registration)Global registration of tabs tools (including browser_tab_close) into snapshotTools and visionTools arrays by spreading the tabs export with captureSnapshot true/false.import tabs from './tools/tabs.js'; import screenshot from './tools/screenshot.js'; import testing from './tools/testing.js'; import vision from './tools/vision.js'; import wait from './tools/wait.js'; import type { Tool } from './tools/tool.js'; export const snapshotTools: Tool<any>[] = [ ...common(true), ...console, ...dialogs(true), ...files(true), ...install, ...keyboard(true), ...navigate(true), ...network, ...pdf, ...screenshot, ...snapshot, ...tabs(true), ...testing, ...wait(true), ]; export const visionTools: Tool<any>[] = [ ...common(false), ...console, ...dialogs(false), ...files(false), ...install, ...keyboard(false), ...navigate(false), ...network, ...pdf, ...tabs(false), ...testing, ...vision, ...wait(false), ];
- src/context.ts:129-133 (helper)Core helper method on Context class that implements tab closing logic using Playwright's page.close() and returns updated tabs list.async closeTab(index: number | undefined) { const tab = index === undefined ? this._currentTab : this._tabs[index - 1]; await tab?.page.close(); return await this.listTabsMarkdown(); }
- src/tools/tabs.ts:129-134 (registration)Local registration of the closeTab tool factory in the exported array of tab tools.export default (captureSnapshot: boolean) => [ listTabs, newTab(captureSnapshot), selectTab(captureSnapshot), closeTab(captureSnapshot), ];