Skip to main content

Playwright MCP

Introductionโ€‹

The Playwright MCP server provides browser automation capabilities through the Model Context Protocol, enabling LLMs to interact with web pages using structured accessibility snapshots. It works with VS Code, Cursor, Windsurf, Claude Desktop, and any other MCP client โ€” no vision models required.

Prerequisitesโ€‹

Before you begin, make sure you have the following installed:

  • Node.js 18 or newer
  • An MCP client: VS Code, Cursor, Windsurf, Claude Code, Claude Desktop, or similar

Getting Startedโ€‹

Installationโ€‹

Add the Playwright MCP server to your client using the standard configuration:

{
"mcpServers": {
"playwright": {
"command": "npx",
"args": [
"@playwright/mcp@latest"
]
}
}
}

VS Codeโ€‹

Click one of the buttons below to install directly:

Install in VS Code Install in VS Code Insiders

Or install via the VS Code CLI:

code --add-mcp '{"name":"playwright","command":"npx","args":["@playwright/mcp@latest"]}'

Cursorโ€‹

Install in Cursor

Or go to Cursor Settings โ†’ MCP โ†’ Add new MCP Server and use command type with npx @playwright/mcp@latest.

Claude Codeโ€‹

claude mcp add playwright npx @playwright/mcp@latest

Claude Desktopโ€‹

Follow the MCP install guide and use the standard config above.

Other clientsโ€‹

The standard configuration works with most MCP clients, including Windsurf, Cline, Goose, Kiro, Codex, Copilot CLI, and others. Consult your client's MCP documentation for where to place the config.

First interactionโ€‹

Once the server is connected, ask your AI assistant to interact with a web page:

Navigate to https://demo.playwright.dev/todomvc and add a few todo items.

The assistant will use Playwright MCP tools to open the browser, navigate to the page, and interact with elements โ€” all through structured accessibility snapshots rather than screenshots.

Core Featuresโ€‹

Accessibility snapshotsโ€‹

Playwright MCP operates on the page's accessibility tree, not pixels. When a tool runs, it returns a structured snapshot showing the page elements, their roles, and text content. The LLM uses element references from these snapshots to interact with the page:

- heading "todos" [level=1]
- textbox "What needs to be done?" [ref=e5]
- listitem:
- checkbox "Toggle Todo" [ref=e10]
- text: "Buy groceries"

The LLM reads this snapshot and uses ref=e5 to type into the textbox or ref=e10 to check the checkbox.

Interacting with pagesโ€‹

Playwright MCP provides tools for all common browser interactions:

  • Navigation: Open URLs, go back/forward, reload pages.
  • Clicking and typing: Click elements, type text, fill forms, select dropdowns.
  • Screenshots: Capture the current page or specific elements for visual verification.
  • Keyboard and mouse: Press keys, hover, drag and drop.
  • Dialogs: Accept or dismiss browser dialogs.
  • Tabs: Create, close, and switch between browser tabs.

Running Playwright codeโ€‹

For complex interactions that go beyond individual tool calls, use the browser_run_code_unsafe tool to execute Playwright scripts directly. This tool runs arbitrary JavaScript in the Playwright server process and is RCE-equivalent โ€” only enable it for trusted MCP clients:

Run this Playwright code to verify the todo count:
async (page) => {
const count = await page.getByTestId('todo-count').textContent();
return count;
}

Network monitoring and mockingโ€‹

Inspect network traffic and mock API responses:

  • View network requests: List all requests made since page load.
  • Mock routes: Set up URL pattern matching to return custom responses.
  • Console messages: Access browser console output for debugging.

Storage stateโ€‹

Save and restore browser state including cookies and localStorage:

  • Save state: Persist authentication and session data to a file.
  • Restore state: Load previously saved state into a new session.
  • Cookie management: List, get, set, and delete individual cookies.

Configurationโ€‹

Headed modeโ€‹

By default, Playwright MCP runs the browser in headed mode so you can see what's happening. To run headless:

{
"mcpServers": {
"playwright": {
"command": "npx",
"args": [
"@playwright/mcp@latest",
"--headless"
]
}
}
}

Browser selectionโ€‹

Choose which browser to use:

{
"mcpServers": {
"playwright": {
"command": "npx",
"args": [
"@playwright/mcp@latest",
"--browser=firefox"
]
}
}
}

Supported values: chrome, firefox, webkit, msedge.

User profileโ€‹

Playwright MCP supports three profile modes:

  • Persistent (default): Login state and cookies are preserved between sessions. The profile is stored in ms-playwright/mcp-{channel}-{workspace-hash} in your platform's cache directory, so different projects get separate profiles automatically. Override with --user-data-dir.
  • Isolated: Each session starts fresh. Pass --isolated to enable. You can load initial state with --storage-state.
  • Browser extension: Connect to your existing browser tabs with the Playwright Extension. Pass --extension to enable.

Configuration fileโ€‹

For advanced configuration, use a JSON config file:

npx @playwright/mcp@latest --config path/to/config.json

The config file supports browser options, context options, network rules, timeouts, and more. See the Playwright MCP repository for the full schema.

Standalone serverโ€‹

When running a headed browser on a system without a display or from IDE worker processes, start the MCP server separately with HTTP transport:

npx @playwright/mcp@latest --port 8931

Then point your MCP client to the HTTP endpoint:

{
"mcpServers": {
"playwright": {
"url": "http://localhost:8931/mcp"
}
}
}

Quick Referenceโ€‹

ActionHow to do it
Install serverAdd standard config to your MCP client
Navigate to a pageAsk: "Go to https://example.com"
Click an elementAsk: "Click the Submit button"
Fill a formAsk: "Fill in the email field with test@example.com"
Take a screenshotAsk: "Take a screenshot of the page"
Run Playwright codeAsk: "Run this Playwright code: ..."
Mock an APIAsk: "Mock the /api/users endpoint to return ..."
Use headed modeDefault. Pass --headless to disable
Choose a browserPass --browser=firefox in args

What's Nextโ€‹