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
| Name | Required | Description | Default |
|---|---|---|---|
| userAgent | Yes | Custom User Agent for the Playwright browser instance |
Implementation Reference
- src/tools/browser/useragent.ts:12-40 (handler)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}`); } }); } }
- src/tools.ts:320-329 (schema)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"], }, },
- src/toolHandler.ts:502-503 (registration)Dispatch in handleToolCall switch statement calling the tool's execute method.case "playwright_custom_user_agent": return await customUserAgentTool.execute(args, context);
- src/toolHandler.ts:322-322 (registration)Instantiation of CustomUserAgentTool instance.if (!customUserAgentTool) customUserAgentTool = new CustomUserAgentTool(server);
- src/toolHandler.ts:425-426 (helper)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,