close-browser
Close a specific browser instance by its ID and remove it from the active browsers list, ensuring clean resource management in MCP Fetch's browser automation tasks.
Instructions
Close a browser instance and remove it from the active browsers list
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| browserId | Yes | The ID of the browser instance to close |
Implementation Reference
- Core handler function that closes the specified browser instance, handles cleanup of associated pages, and returns success/error JSON.export async function closeBrowser(browserId: string) { // Check if browser exists const browsers = getBrowsers() const browserInstance = browsers[browserId] if (!browserInstance) { return JSON.stringify({ success: false, error: `Browser with ID '${browserId}' not found`, }, null, 2) } const { browser } = browserInstance // Close the browser try { if (browser.connected) { await browser.close() } } catch { // do nothing } // Clean up any pages associated with this browser const pages = getPages() const pageIds = Object.keys(pages) let cleanedPagesCount = 0 for (const pageId of pageIds) { if (pages[pageId].browserId === browserId) { delete pages[pageId] cleanedPagesCount++ } } // Remove from global object delete browsers[browserId] return JSON.stringify({ success: true, message: `Browser ${browserId} closed successfully`, cleanedPagesCount, }, null, 2) }
- src/tools/puppeteer.ts:37-40 (schema)Zod schema defining the input parameters for the 'close-browser' action within the puppeteer tool.z.object({ type: z.literal("close-browser"), browserId: z.string().describe("The ID of the browser instance to close"), }),
- src/tools/puppeteer.ts:107-108 (handler)Dispatch handler in the main puppeteer tool function that routes 'close-browser' actions to the closeBrowser implementation.case "close-browser": return await closeBrowser(action.browserId)