Skip to main content
Glama

playwright_custom_user_agent

Set a custom User Agent for Playwright browser automation to simulate different browsers or devices, enhancing web testing and interaction scenarios.

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 with execute method: validates current browser user agent matches the requested one after setting during browser launch.
    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 schema definition: 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"], }, },
  • Dispatch in handleToolCall switch statement calling the tool's execute method.
    case "playwright_custom_user_agent": return await customUserAgentTool.execute(args, context);
  • Instantiation of CustomUserAgentTool instance.
    if (!customUserAgentTool) customUserAgentTool = new CustomUserAgentTool(server);
  • Special handling in ensureBrowser to set userAgent only when this tool is called.
    userAgent: name === "playwright_custom_user_agent" ? args.userAgent : undefined, headless: args.headless,

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/BhanuTJ93/MCP'

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