Skip to main content
Glama

playwright_custom_user_agent

Set a custom User Agent string for browser automation to simulate different devices or browsers during testing and web scraping.

Instructions

Set a custom User Agent for the browser

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
userAgentYesCustom User Agent for the Playwright browser instance

Implementation Reference

  • Handler function that validates the browser's current User-Agent matches the provided argument by evaluating navigator.userAgent on the page.
    async execute(args: CustomUserAgentArgs, context: ToolContext): Promise<ToolResponse> {
      return this.safeExecute(context, async (page) => {
        if (!args.userAgent) {
          return createErrorResponse("Missing required parameter: userAgent must be provided");
        }
    
        try {
          const currentUserAgent = await page.evaluate(() => navigator.userAgent);
    
          if (currentUserAgent !== args.userAgent) {
            const messages = [
              "Page was already initialized with a different User Agent.",
              `Requested: ${args.userAgent}`,
              `Current: ${currentUserAgent}`,
            ];
            return createErrorResponse(messages.join("\n"));
          }
    
          return createSuccessResponse("User Agent validation successful");
        } catch (error) {
          return createErrorResponse(`Failed to validate User Agent: ${(error as Error).message}`);
        }
      });
    }
  • Tool definition with input schema specifying the required 'userAgent' string parameter.
      name: "playwright_custom_user_agent",
      description: "Set a custom User Agent for the browser",
      inputSchema: {
        type: "object",
        properties: {
          userAgent: { type: "string", description: "Custom User Agent for the Playwright browser instance" },
        },
        required: ["userAgent"],
      },
    },
  • Switch case in main tool handler that routes calls to the CustomUserAgentTool instance.
    case "playwright_custom_user_agent":
      return await customUserAgentTool.execute(args, context);
  • Conditional logic that passes the userAgent argument to browser launch settings specifically for this tool.
    userAgent: name === "playwright_custom_user_agent" ? args.userAgent : undefined,
  • src/tools.ts:507-507 (registration)
    Inclusion in BROWSER_TOOLS array which triggers browser initialization for this tool.
    "playwright_custom_user_agent",

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/aakashH242/mcp-playwright'

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