browser_set_cookie_object
Set browser cookies programmatically to manage user sessions, authentication states, and website preferences during automated web testing and browser automation workflows.
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)Primary registration of the 'browser_set_cookie_object' tool using McpServer.tool(), including description, Zod input schema, and inline handler function.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/tools/cookieTools.ts:55-62 (handler)The inline handler function for the tool, which retrieves the WebDriver, instantiates CookieService, calls setCookie, and returns a success message.async ({ cookie }) => { const driver = stateManager.getDriver(); const cookieService = new CookieService(driver); await cookieService.setCookie(cookie); return { content: [{ type: 'text', text: `Set cookie: ${cookie}` }], }; }
- src/tools/cookieTools.ts:52-54 (schema)Zod schema for the tool input parameter 'cookie', validating it as a non-empty string up to 4096 chars with description.{ cookie: z.string().min(1).max(4096).describe("Cookie string to set, e.g. 'name=value; Path=/; HttpOnly'"), },
- src/services/cookieService.ts:20-62 (helper)Core helper method in CookieService that parses the cookie string (name=value; attributes), constructs a cookie object, and adds it to the 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); }
- src/tools/index.ts:12-12 (registration)Secondary registration: calls registerCookieTools in the main registerAllTools function, which includes the browser_set_cookie_object tool.registerCookieTools(server, stateManager);