set_cookie
Sets a browser cookie on the active session to inject session tokens or feature flags without requiring login flows.
Instructions
Sets a browser cookie on the active session. The browser must already be on the target domain — cookies cannot be set cross-domain. Use to inject session tokens or feature flags without login flows.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | Cookie name | |
| value | Yes | Cookie value | |
| domain | No | Cookie domain (defaults to current domain) | |
| path | No | Cookie path (defaults to "/") | |
| expiry | No | Expiry date as Unix timestamp in seconds | |
| httpOnly | No | HttpOnly flag | |
| secure | No | Secure flag | |
| sameSite | No | SameSite attribute |
Implementation Reference
- src/tools/cookies.tool.ts:24-49 (handler)The setCookieTool function is the handler for the 'set_cookie' tool. It extracts cookie parameters (name, value, domain, path, expiry, httpOnly, secure, sameSite), calls getBrowser() to get the active browser session, and uses browser.setCookies() to set the cookie. Returns success message or error.
export const setCookieTool: ToolCallback = async ({ name, value, domain, path = '/', expiry, httpOnly, secure, sameSite, }: Cookie): Promise<CallToolResult> => { try { const cookie: Cookie = { name, value, path, domain, expiry, httpOnly, secure, sameSite }; const { getBrowser } = await import('../session/state'); const browser = getBrowser(); await browser.setCookies(cookie); return { content: [{ type: 'text', text: `Cookie "${name}" set successfully` }], }; } catch (e) { return { isError: true, content: [{ type: 'text', text: `Error setting cookie: ${e}` }], }; } }; - src/tools/cookies.tool.ts:8-22 (schema)The setCookieToolDefinition defines the 'set_cookie' tool schema with name, description, annotations, and inputSchema (Zod validation) specifying parameters: name (string), value (string), domain (optional string), path (optional string), expiry (optional number), httpOnly (optional coerced boolean), secure (optional coerced boolean), sameSite (optional enum: strict/lax/none).
export const setCookieToolDefinition: ToolDefinition = { name: 'set_cookie', description: 'Sets a browser cookie on the active session. The browser must already be on the target domain — cookies cannot be set cross-domain. Use to inject session tokens or feature flags without login flows.', annotations: { title: 'Set Cookie', destructiveHint: false, idempotentHint: true }, inputSchema: { name: z.string().describe('Cookie name'), value: z.string().describe('Cookie value'), domain: z.string().optional().describe('Cookie domain (defaults to current domain)'), path: z.string().optional().describe('Cookie path (defaults to "/")'), expiry: z.number().optional().describe('Expiry date as Unix timestamp in seconds'), httpOnly: coerceBoolean.optional().describe('HttpOnly flag'), secure: coerceBoolean.optional().describe('Secure flag'), sameSite: z.enum(['strict', 'lax', 'none']).optional().describe('SameSite attribute'), }, }; - src/server.ts:137-137 (registration)Registration of the set_cookie tool in the MCP server: registerTool(setCookieToolDefinition, setCookieTool) at line 137, imported from ./tools/cookies.tool on lines 16-21.
registerTool(setCookieToolDefinition, setCookieTool); - src/utils/zod-helpers.ts:3-11 (helper)The coerceBoolean helper used in the schema to accept boolean, string 'true'/'false'/'0'/'1', or truthy values and convert them to proper booleans via Zod preprocessing.
export const coerceBoolean = z.preprocess((val) => { if (typeof val === 'boolean') return val; if (typeof val === 'string') { if (val === 'false' || val === '0') return false; if (val === 'true' || val === '1') return true; return Boolean(val); } return val; }, z.boolean()); - src/session/state.ts:18-24 (helper)The getBrowser() helper retrieves the active browser session from the state map, used by setCookieTool to obtain the browser instance for calling setCookies().
export function getBrowser(): WebdriverIO.Browser { const browser = state.browsers.get(state.currentSession); if (!browser) { throw new Error('No active browser session'); } return browser; }