set_cookies
Configure browser cookies to manage authentication, sessions, and user preferences during automated web interactions.
Instructions
Set cookies in the browser
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cookies | Yes | ||
| tabId | No | Tab ID to operate on (uses active tab if not specified) |
Implementation Reference
- src/tools/cookies.ts:56-88 (handler)Handler function that implements the set_cookies tool logic: gets the page, defaults cookie domain/path from current URL, normalizes cookies, calls page.setCookie(), handles errors, returns success with count and names.async ({ cookies, tabId }) => { const pageResult = await getPageForOperation(tabId); if (!pageResult.success) { return handleResult(pageResult); } const page = pageResult.data; try { // Get the current page URL for domain defaulting const pageUrl = new URL(page.url()); const cookiesToSet = cookies.map((cookie: CookieParam) => ({ name: cookie.name, value: cookie.value, domain: cookie.domain ?? pageUrl.hostname, path: cookie.path ?? '/', expires: cookie.expires, httpOnly: cookie.httpOnly, secure: cookie.secure, sameSite: cookie.sameSite as 'Strict' | 'Lax' | 'None' | undefined, })); await page.setCookie(...cookiesToSet); return handleResult(ok({ set: cookies.length, cookies: cookiesToSet.map((c) => c.name), })); } catch (error) { return handleResult(err(normalizeError(error))); } }
- src/schemas.ts:177-189 (schema)Zod schema defining the input parameters for the set_cookies tool, including required cookies array and optional tabId.export const setCookiesSchema = z.object({ cookies: z.array(z.object({ name: z.string(), value: z.string(), domain: z.string().optional(), path: z.string().optional().default('/'), expires: z.number().optional().describe('Unix timestamp when cookie expires'), httpOnly: z.boolean().optional(), secure: z.boolean().optional(), sameSite: z.enum(['Strict', 'Lax', 'None']).optional(), })).min(1), tabId: tabIdSchema, });
- src/tools/cookies.ts:52-89 (registration)Registration of the set_cookies tool on the MCP server within registerCookiesTools, specifying name, description, input schema shape, and handler function.server.tool( 'set_cookies', 'Set cookies in the browser', setCookiesSchema.shape, async ({ cookies, tabId }) => { const pageResult = await getPageForOperation(tabId); if (!pageResult.success) { return handleResult(pageResult); } const page = pageResult.data; try { // Get the current page URL for domain defaulting const pageUrl = new URL(page.url()); const cookiesToSet = cookies.map((cookie: CookieParam) => ({ name: cookie.name, value: cookie.value, domain: cookie.domain ?? pageUrl.hostname, path: cookie.path ?? '/', expires: cookie.expires, httpOnly: cookie.httpOnly, secure: cookie.secure, sameSite: cookie.sameSite as 'Strict' | 'Lax' | 'None' | undefined, })); await page.setCookie(...cookiesToSet); return handleResult(ok({ set: cookies.length, cookies: cookiesToSet.map((c) => c.name), })); } catch (error) { return handleResult(err(normalizeError(error))); } } );