Skip to main content
Glama

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
NameRequiredDescriptionDefault
cookiesYes
tabIdNoTab ID to operate on (uses active tab if not specified)

Implementation Reference

  • 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))); } }
  • 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, });
  • 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))); } } );

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/andytango/puppeteer-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server