browser_set_cookie_object
Set browser cookies programmatically to manage user sessions, authentication states, or testing scenarios during web automation.
Instructions
Set a cookie in the browser
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cookie | Yes | Cookie string to set, e.g. 'name=value; Path=/; HttpOnly' |
Implementation Reference
- src/tools/cookieTools.ts:49-63 (registration)Registers the 'browser_set_cookie_object' MCP tool with input schema and handler function that instantiates CookieService and calls setCookie.server.tool( 'browser_set_cookie_object', 'Set a cookie in the browser', { cookie: z.string().min(1).max(4096).describe("Cookie string to set, e.g. 'name=value; Path=/; HttpOnly'"), }, async ({ cookie }) => { const driver = stateManager.getDriver(); const cookieService = new CookieService(driver); await cookieService.setCookie(cookie); return { content: [{ type: 'text', text: `Set cookie: ${cookie}` }], }; } );
- src/services/cookieService.ts:20-62 (helper)Implements the core logic for setting a cookie by parsing the string format into a cookie object and adding it via Selenium WebDriver.async setCookie(cookie: string): Promise<void> { // Parse cookie string into an object const [nameValue, ...attributes] = cookie.split(';').map(part => part.trim()); let name = ''; let value = ''; if (nameValue) { const parts = nameValue.split('='); name = parts[0] ?? ''; value = parts[1] ?? ''; } const cookieObj: any = { name, value }; attributes.forEach(attr => { const parts = attr.split('='); const attrName = parts[0]; const attrValue = parts[1]; if (!attrName) return; switch (attrName.toLowerCase()) { case 'name': cookieObj.name = attrValue; break; case 'domain': cookieObj.domain = attrValue; break; case 'path': cookieObj.path = attrValue; break; case 'expires': if (attrValue !== undefined) { cookieObj.expiry = Math.floor(new Date(attrValue).getTime() / 1000); } break; case 'secure': cookieObj.secure = true; break; case 'httponly': cookieObj.httpOnly = true; break; } }); await this.driver.manage().addCookie(cookieObj); }