playwright_custom_user_agent
Set a custom User Agent string for browser automation to simulate different devices or browsers, bypass restrictions, and test website compatibility.
Instructions
Set a custom User Agent for the browser
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| userAgent | Yes | Custom User Agent for the Playwright browser instance |
Implementation Reference
- src/tools/browser/useragent.ts:12-40 (handler)The CustomUserAgentTool class containing the execute method that validates the custom user agent set on the browser page.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}`); } }); } }
- src/tools.ts:331-341 (schema)Tool definition including name, description, and input schema for validation.{ 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"], }, },
- src/toolHandler.ts:514-515 (registration)Switch case in main tool handler that dispatches to the CustomUserAgentTool execute method.case "playwright_custom_user_agent": return await customUserAgentTool.execute(args, context);
- src/toolHandler.ts:331-331 (registration)Instantiation of the CustomUserAgentTool instance.if (!customUserAgentTool) customUserAgentTool = new CustomUserAgentTool(server);
- src/toolHandler.ts:434-435 (helper)Passes the userAgent argument to browser launch settings specifically for this tool.userAgent: name === "playwright_custom_user_agent" ? args.userAgent : undefined, headless: args.headless,