pilot_import_cookies
Import browser cookies from Chrome, Edge, Brave, Arc, or Comet into headless sessions for automated testing and authentication.
Instructions
Import cookies from a real Chromium browser (Chrome, Arc, Brave, Edge, Comet). Decrypts from browser cookie database and adds to the headless browser session.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| browser | No | Browser name (chrome, arc, brave, edge, comet). Auto-detects if omitted. | |
| domains | Yes | Cookie domains to import (e.g. [".github.com", ".google.com"]) | |
| profile | No | Browser profile name (default: "Default") | |
| list_browsers | No | List installed browsers instead of importing | |
| list_profiles | No | List available profiles for the specified browser | |
| list_domains | No | List cookie domains available in the browser |
Implementation Reference
- src/tools/settings.ts:69-114 (handler)The tool handler for pilot_import_cookies in src/tools/settings.ts. It handles listing browsers, profiles, domains, and the actual cookie import.
async ({ browser, domains, profile, list_browsers, list_profiles, list_domains: listDom }) => { try { if (list_browsers) { const installed = findInstalledBrowsers(); if (installed.length === 0) { return { content: [{ type: 'text' as const, text: `No Chromium browsers found. Supported: ${listSupportedBrowserNames().join(', ')}` }] }; } return { content: [{ type: 'text' as const, text: `Installed browsers:\n${installed.map(b => ` - ${b.name}`).join('\n')}` }] }; } if (list_profiles && browser) { const profiles = listProfiles(browser); if (profiles.length === 0) { return { content: [{ type: 'text' as const, text: `No profiles found for ${browser}` }] }; } return { content: [{ type: 'text' as const, text: `Profiles for ${browser}:\n${profiles.map(p => ` - ${p.name} (${p.displayName})`).join('\n')}` }] }; } if (listDom && browser) { const result = listDomains(browser, profile || 'Default'); const top = result.domains.slice(0, 50); return { content: [{ type: 'text' as const, text: `Cookie domains in ${result.browser} (top ${top.length}):\n${top.map(d => ` ${d.domain} (${d.count} cookies)`).join('\n')}` }] }; } // Import mode await bm.ensureBrowser(); const browserName = browser || 'chrome'; const result = await importCookies(browserName, domains, profile || 'Default'); if (result.cookies.length > 0) { await bm.getContext().addCookies(result.cookies as any); } const msg = [`Imported ${result.count} cookies from ${browserName}`]; if (result.failed > 0) msg.push(`(${result.failed} failed to decrypt)`); if (Object.keys(result.domainCounts).length > 0) { msg.push('\nPer domain:'); for (const [domain, count] of Object.entries(result.domainCounts)) { msg.push(` ${domain}: ${count}`); } } return { content: [{ type: 'text' as const, text: msg.join('\n') }] }; } catch (err) { return { content: [{ type: 'text' as const, text: wrapError(err) }], isError: true }; } } - src/cookie-import.ts:174-218 (handler)The core logic function importCookies in src/cookie-import.ts, which performs the database query, decryption, and formatting of cookies for Playwright.
export async function importCookies( browserName: string, domains: string[], profile = 'Default', ): Promise<ImportResult> { if (domains.length === 0) return { cookies: [], count: 0, failed: 0, domainCounts: {} }; const browser = resolveBrowser(browserName); const match = getBrowserMatch(browser, profile); const derivedKeys = await getDerivedKeys(match); const db = openDb(match.dbPath, browser.name); try { const now = chromiumNow(); const placeholders = domains.map(() => '?').join(','); const stmt = db.prepare( `SELECT host_key, name, value, encrypted_value, path, expires_utc, is_secure, is_httponly, has_expires, samesite FROM cookies WHERE host_key IN (${placeholders}) AND (has_expires = 0 OR expires_utc > ?) ORDER BY host_key, name` ); const rows = stmt.all(...domains, now.toString()) as RawCookie[]; const cookies: PlaywrightCookie[] = []; let failed = 0; const domainCounts: Record<string, number> = {}; for (const row of rows) { try { const value = decryptCookieValue(row, derivedKeys); const cookie = toPlaywrightCookie(row, value); cookies.push(cookie); domainCounts[row.host_key] = (domainCounts[row.host_key] || 0) + 1; } catch { failed++; } } return { cookies, count: cookies.length, failed, domainCounts }; } finally { db.close(); } } - src/tools/settings.ts:58-115 (registration)Registration of the 'pilot_import_cookies' tool with its schema definition in src/tools/settings.ts.
server.tool( 'pilot_import_cookies', 'Import cookies from a real Chromium browser (Chrome, Arc, Brave, Edge, Comet). Decrypts from browser cookie database and adds to the headless browser session.', { browser: z.string().optional().describe('Browser name (chrome, arc, brave, edge, comet). Auto-detects if omitted.'), domains: z.array(z.string()).describe('Cookie domains to import (e.g. [".github.com", ".google.com"])'), profile: z.string().optional().describe('Browser profile name (default: "Default")'), list_browsers: z.boolean().optional().describe('List installed browsers instead of importing'), list_profiles: z.boolean().optional().describe('List available profiles for the specified browser'), list_domains: z.boolean().optional().describe('List cookie domains available in the browser'), }, async ({ browser, domains, profile, list_browsers, list_profiles, list_domains: listDom }) => { try { if (list_browsers) { const installed = findInstalledBrowsers(); if (installed.length === 0) { return { content: [{ type: 'text' as const, text: `No Chromium browsers found. Supported: ${listSupportedBrowserNames().join(', ')}` }] }; } return { content: [{ type: 'text' as const, text: `Installed browsers:\n${installed.map(b => ` - ${b.name}`).join('\n')}` }] }; } if (list_profiles && browser) { const profiles = listProfiles(browser); if (profiles.length === 0) { return { content: [{ type: 'text' as const, text: `No profiles found for ${browser}` }] }; } return { content: [{ type: 'text' as const, text: `Profiles for ${browser}:\n${profiles.map(p => ` - ${p.name} (${p.displayName})`).join('\n')}` }] }; } if (listDom && browser) { const result = listDomains(browser, profile || 'Default'); const top = result.domains.slice(0, 50); return { content: [{ type: 'text' as const, text: `Cookie domains in ${result.browser} (top ${top.length}):\n${top.map(d => ` ${d.domain} (${d.count} cookies)`).join('\n')}` }] }; } // Import mode await bm.ensureBrowser(); const browserName = browser || 'chrome'; const result = await importCookies(browserName, domains, profile || 'Default'); if (result.cookies.length > 0) { await bm.getContext().addCookies(result.cookies as any); } const msg = [`Imported ${result.count} cookies from ${browserName}`]; if (result.failed > 0) msg.push(`(${result.failed} failed to decrypt)`); if (Object.keys(result.domainCounts).length > 0) { msg.push('\nPer domain:'); for (const [domain, count] of Object.entries(result.domainCounts)) { msg.push(` ${domain}: ${count}`); } } return { content: [{ type: 'text' as const, text: msg.join('\n') }] }; } catch (err) { return { content: [{ type: 'text' as const, text: wrapError(err) }], isError: true }; } } );