launch-browser
Initiate a new browser instance with customizable settings like window size and headless mode for web automation, testing, or scraping tasks using MCP Fetch.
Instructions
Launch a new browser instance and store it for later use
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| headless | No | Whether to run browser in headless mode. Defaults to false | |
| height | No | Browser window height in pixels. Defaults to 720 | |
| width | No | Browser window width in pixels. Defaults to 1280 |
Implementation Reference
- Core handler function that launches a Puppeteer browser with given dimensions, headless mode, and optional initial URL navigation. Manages browser instance tracking and returns browserId.export async function launchBrowser(headless: boolean, width: number, height: number, url?: string) { // Generate unique ID for this browser instance const id = generateBrowserId() // Launch browser with specified options const browser = await puppeteer.launch({ headless, defaultViewport: { width, height, }, args: [ `--window-size=${width},${height}`, ], }) // Store browser instance in global object const browserInstance: BrowserInstance = { id, browser, createdAt: new Date(), } const browsers = getBrowsers() browsers[id] = browserInstance // Navigate to URL if provided let initialUrl = undefined let initialPageId = undefined if (url) { try { // Get the default page (first tab) const browserPages = await browser.pages() if (browserPages.length > 0) { const defaultPage = browserPages[0] await defaultPage.goto(url, { waitUntil: 'networkidle2' }) initialUrl = defaultPage.url() // Register this page in the page tracking system const pageId = generatePageId() const pages = getPages() const pageInstance: PageInstance = { id: pageId, page: defaultPage, browserId: id, createdAt: new Date(), } pages[pageId] = pageInstance initialPageId = pageId } } catch (error) { // If navigation fails, continue but include a warning return JSON.stringify({ success: true, browserId: id, message: `Browser launched successfully with ID: ${id}`, warning: `Failed to navigate to ${url}: ${error instanceof Error ? error.message : 'Unknown error'}`, config: { headless, width, height, }, }, null, 2) } } return JSON.stringify({ success: true, browserId: id, message: `Browser launched successfully with ID: ${id}`, config: { headless, width, height, }, ...(initialUrl && { url: initialUrl }), ...(initialPageId && { pageId: initialPageId }), }, null, 2) }
- src/tools/puppeteer.ts:19-36 (schema)Zod schema defining the input parameters for the 'launch-browser' action within the puppeteer tool.z.object({ type: z.literal("launch-browser"), headless: z.boolean() .optional() .default(false) .describe("Whether to run browser in headless mode. Defaults to false"), width: z.number() .optional() .default(1280) .describe("Browser window width in pixels. Defaults to 1280"), height: z.number() .optional() .default(720) .describe("Browser window height in pixels. Defaults to 720"), url: z.string() .optional() .describe("Optional URL to navigate to after launching the browser"), }),
- src/tools/puppeteer.ts:97-140 (handler)Main puppeteer tool handler with switch statement dispatching to launchBrowser for the 'launch-browser' action type.export default async function puppeteer({ action }: InferSchema<typeof schema>) { try { switch (action.type) { // Browser actions case "list-browsers": return await listBrowsers() case "launch-browser": return await launchBrowser(action.headless, action.width, action.height, action.url) case "close-browser": return await closeBrowser(action.browserId) // Page actions case "list-pages": return await listPages() case "open-page": return await openPage(action.browserId, action.url) case "close-page": return await closePage(action.pageId) // Exec action case "exec-page": return await execPage(action.pageId, action.source) // Screenshot action case "take-screenshot": return await takeScreenshot(action.pageId, action.fullPage, action.format, action.quality) default: return JSON.stringify({ success: false, error: "Invalid action type", }, null, 2) } } catch (error) { return JSON.stringify({ success: false, error: error instanceof Error ? error.message : "Unknown error occurred", }, null, 2) } }
- src/tools/puppeteer.ts:85-94 (registration)Tool metadata registration for the 'puppeteer' tool which encompasses the 'launch-browser' action.export const metadata: ToolMetadata = { name: "puppeteer", description: "Control browsers and pages with Puppeteer - launch/close browsers, open/close pages, execute JavaScript, take screenshots, and more", annotations: { title: "Puppeteer Control", readOnlyHint: false, destructiveHint: false, idempotentHint: false, }, }