Skip to main content
Glama

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

Implementation Reference

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

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