Skip to main content
Glama

playwright_custom_user_agent

Set a custom User Agent in Playwright browser automation to simulate different devices or browsers, enabling tailored web interactions during testing or scraping tasks.

Instructions

Set a custom User Agent for the browser

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
userAgentYesCustom User Agent for the Playwright browser instance

Implementation Reference

  • CustomUserAgentTool class implementing the tool's execute method, which validates the current user agent matches the provided one.
    export class CustomUserAgentTool extends BrowserToolBase {
      /**
       * Execute the custom user agent tool
       */
      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 in createToolDefinitions(), including name, description, and inputSchema requiring 'userAgent' string.
    {
      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"],
      },
    },
  • Registration and dispatch in handleToolCall switch statement, calling CustomUserAgentTool.execute
    case "playwright_custom_user_agent":
      return await customUserAgentTool.execute(args, context);
  • Special handling in ensureBrowser to set custom userAgent in browser context when this tool is called.
    const browserSettings = {
      viewport: {
        width: args.width,
        height: args.height
      },
      userAgent: name === "playwright_custom_user_agent" ? args.userAgent : undefined,
      headless: args.headless,
      browserType: args.browserType || 'chromium'
  • Code generation helper method to produce Playwright code for setting user agent in test files.
    private generateCustomUserAgentStep(parameters: Record<string, unknown>): string {
      const { userAgent } = parameters;
      return `
      // Set custom user agent
      await context.setUserAgent('${userAgent}');`;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden for behavioral disclosure. While 'Set' implies a configuration change, it doesn't describe whether this persists across browser sessions, affects all subsequent requests, requires specific permissions, or has side effects on other browser settings. For a tool that modifies browser behavior with no annotation coverage, this is insufficient transparency.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, clear sentence that directly states the tool's function without any fluff or redundancy. It's front-loaded with the essential action and target, making it immediately scannable and efficient. Every word earns its place in this minimal but complete statement.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool modifies browser configuration with no annotations and no output schema, the description is incomplete. It doesn't explain what happens after setting the User Agent, whether the change is immediate or requires a page reload, what format the User Agent should follow, or potential limitations. For a configuration tool in a complex browser automation context, this leaves significant gaps in understanding.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with the single parameter 'userAgent' clearly documented in the schema. The description adds no additional parameter semantics beyond implying this sets the User Agent string. Since the schema already fully describes the parameter, the baseline score of 3 is appropriate - the description doesn't add value but doesn't need to compensate for gaps.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Set') and target ('custom User Agent for the browser'), making the purpose immediately understandable. It distinguishes itself from siblings by focusing specifically on User Agent configuration rather than navigation, interaction, or session management. However, it doesn't explicitly contrast with other User Agent-related tools (none exist in siblings), so it's not a perfect 5.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites (e.g., requires an active browser session), when it's appropriate (e.g., for testing different browser versions or bypassing detection), or what happens if used incorrectly. With many sibling tools for browser interaction, this lack of context leaves the agent guessing about proper application.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

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

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