playwright_custom_user_agent
Set a custom User Agent for browser automation within the Playwright MCP Server, enabling tailored web interactions for scraping, testing, or simulating specific devices.
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:1-40 (handler)Implementation of CustomUserAgentTool: validates that the browser context was launched with the specified userAgent by checking navigator.userAgent.import { BrowserToolBase } from './base.js'; import type { ToolContext, ToolResponse } from '../common/types.js'; import { createSuccessResponse, createErrorResponse } from '../common/types.js'; interface CustomUserAgentArgs { userAgent: string; } /** * Tool for validating custom User Agent settings */ 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 schema: defines 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:514-515 (registration)Tool dispatch/registration in main handleToolCall switch statement.case "playwright_custom_user_agent": return await customUserAgentTool.execute(args, context);
- src/toolHandler.ts:429-436 (helper)Special handling: passes userAgent from args to browser launch 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'
- src/tools.ts:464-464 (registration)Added to BROWSER_TOOLS array, triggering browser launch before execution."playwright_custom_user_agent",