set_cookies
Configure browser cookies for web automation tasks, enabling session management and authentication during automated browsing sessions.
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)The handler function for the 'set_cookies' tool. It gets the page for the operation, normalizes cookie parameters using the current page URL for defaults, sets the cookies using Puppeteer's page.setCookie method, and returns the number of cookies set and their 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 an array of cookies with fields like name, value, domain, etc., 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-90 (registration)Registers the 'set_cookies' tool on the MCP server using server.tool, providing name, description, input schema, 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))); } } );