Skip to main content
Glama

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
NameRequiredDescriptionDefault
cookieYesCookie string to set, e.g. 'name=value; Path=/; HttpOnly'

Implementation Reference

  • 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}` }], }; } );
  • 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}` }], }; }
  • 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'"), },
  • 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); }
  • Secondary registration: calls registerCookieTools in the main registerAllTools function, which includes the browser_set_cookie_object tool.
    registerCookieTools(server, stateManager);

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/pshivapr/selenium-mcp'

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