browser_tab_close
Close a specific or current browser tab by index during web automation with Playwright MCP, ensuring precise tab management in browser tasks.
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:99-117 (handler)The handle function for the browser_tab_close tool. It validates the input parameters using closeTabSchema, closes the specified tab (or current tab if index not provided) via context.closeTab(), handles the current tab response, and either executes a code snippet or lists remaining tabs.handle: async (context, params) => { const validatedParams = closeTabSchema.parse(params); await context.closeTab(validatedParams.index); const currentTab = context.currentTab(); if (currentTab) { return await currentTab.run(async () => { const code = [ `// <internal code to close tab ${validatedParams.index}>`, ]; return { code }; }, { captureSnapshot }); } return { content: [{ type: 'text', text: await context.listTabs(), }], }; },
- src/tools/tabs.ts:88-90 (schema)Zod schema for input validation of browser_tab_close tool, defining an optional 'index' number parameter.const closeTabSchema = z.object({ index: z.number().optional().describe('The index of the tab to close. Closes current tab if not provided.'), });
- src/tools/tabs.ts:92-118 (registration)Full definition of the browser_tab_close tool as a ToolFactory, including capability, schema with name 'browser_tab_close', and handle function.const closeTab: ToolFactory = captureSnapshot => ({ capability: 'tabs', schema: { name: 'browser_tab_close', description: 'Close a tab', inputSchema: zodToJsonSchema(closeTabSchema), }, handle: async (context, params) => { const validatedParams = closeTabSchema.parse(params); await context.closeTab(validatedParams.index); const currentTab = context.currentTab(); if (currentTab) { return await currentTab.run(async () => { const code = [ `// <internal code to close tab ${validatedParams.index}>`, ]; return { code }; }, { captureSnapshot }); } return { content: [{ type: 'text', text: await context.listTabs(), }], }; }, });
- src/tools/tabs.ts:120-125 (registration)Export of the tabs tools factory function, which instantiates and returns the closeTab tool among others.export default (captureSnapshot: boolean) => [ listTabs, newTab, selectTab(captureSnapshot), closeTab(captureSnapshot), ];