browser_tab_close
Close the currently active browser tab.
Instructions
browser tab close
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/tools/browser_tools.js:951-1008 (handler)The main handler function 'browserTabClose' that closes a browser tab. If no index is specified, it closes the current tab (using the module-level `page` variable). If an index is given, it validates the index and closes the tab at that index from `context.pages()`. It also handles switching to another tab if the closed tab was the current one. Returns success/error messages.
async function browserTabClose(index) { try { const browser = await getBrowser(); const pages = context ? context.pages() : []; // If no index specified, close current tab if (index === undefined) { if (page && !page.isClosed()) { await page.close(); // Switch to another tab if available const remainingPages = context.pages(); if (remainingPages.length > 0) { page = remainingPages[remainingPages.length - 1]; } else { page = null; } return { success: true, message: 'Current tab closed' }; } } else { // Close tab by index if (index < 0 || index >= pages.length) { return { success: false, message: `Invalid tab index: ${index}` }; } await pages[index].close(); // If we closed the current page, switch to another if (pages[index] === page) { const remainingPages = context.pages(); if (remainingPages.length > 0) { page = remainingPages[Math.min(index, remainingPages.length - 1)]; } else { page = null; } } return { success: true, message: `Tab ${index} closed` }; } } catch (error) { logger.error(`Error closing tab: ${error.message}`); return { success: false, message: error.message }; } } - src/mcp/server.js:127-136 (registration)Registration of 'browser_tab_close' as an MCP tool. It is added to the 'browserExtras' array with its name, and registered with a simple input schema (empty properties) during the 'tools/list' handler.
const browserExtras = [ { n:'browser_navigate_back' }, { n:'browser_navigate_forward' }, { n:'browser_hover' }, { n:'browser_drag' }, { n:'browser_select_option' }, { n:'browser_press_key' }, { n:'browser_snapshot' }, { n:'browser_console_messages' }, { n:'browser_network_requests' }, { n:'browser_tab_list' }, { n:'browser_tab_new' }, { n:'browser_tab_select' }, { n:'browser_tab_close' }, { n:'browser_file_upload' }, { n:'browser_wait' }, { n:'browser_wait_for' }, { n:'browser_resize' }, { n:'browser_handle_dialog' } ]; for (const b of browserExtras) { tools.push({ name: b.n, description: b.n.replace(/_/g,' '), inputSchema: { type:'object', properties:{} } }); } - src/mcp/server.js:295-295 (handler)Dispatch call in the 'tools/call' handler that invokes browserTools.browserTabClose(args.index) when the tool name is 'browser_tab_close'.
case 'browser_tab_close': data = await browserTools.browserTabClose(args.index); break; - src/tools/browser_tools.js:1146-1150 (helper)Export of the browserTabClose function as part of the module's exports, under the 'Tab management' section.
// Tab management browserTabList, browserTabNew, browserTabSelect, browserTabClose,