Enables browser automation and testing through Playwright, supporting multi-browser control including Firefox for web navigation, interaction, assertions, screenshots, and accessibility testing.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@MCP Playwright Servertake a screenshot of the homepage and run an accessibility audit"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
MCP Playwright Server
A comprehensive Model Context Protocol (MCP) server for browser automation using Playwright. This server enables AI assistants to control browsers via the MCP protocol, providing powerful tools for web testing, accessibility audits, and browser automation.
Features
π Multi-Browser Support - Chromium, Firefox, and WebKit
π§ 50+ Browser Automation Tools - Navigation, interaction, assertions, screenshots
βΏ Accessibility Testing - Built-in axe-core integration with WCAG compliance checks
π Session Management - Isolated browser contexts with rate limiting
πΈ Visual Testing - Screenshots, video recording, and tracing
π§ͺ AI Test Agents - Automated test planning, generation, and healing
Technology Stack
Category | Technologies |
Runtime | Node.js 18+ |
Language | TypeScript 5.8 |
Browser Automation | Playwright 1.52+ |
Protocol | Model Context Protocol (MCP) SDK |
Validation | Zod |
Accessibility | @axe-core/playwright |
Logging | Winston |
Testing | @playwright/test |
Linting | ESLint 9, Prettier |
Architecture
AI Client β MCP Protocol β Tool Handler β BrowserManager β Action Module β Playwright API
β
SessionManager (lifecycle, rate limiting)Layer Responsibilities
Layer | Location | Purpose |
Entry |
| Bootstrap, graceful shutdown |
MCP Server |
| Tool/resource registration, session cleanup |
Handlers |
| Tool definitions grouped by category |
Browser Manager |
| Orchestrates action modules |
Actions |
| Domain-specific Playwright operations |
Session Manager |
| Session/page lifecycle, rate limiting |
Getting Started
Prerequisites
Node.js 18 or higher
npm or yarn
Installation
# Clone the repository
git clone https://github.com/j0hanz/playwright-mcp.git
cd playwright-mcp
# Install dependencies
npm install
# Install Playwright browsers
npm run install:browsersConfiguration
Create a .env file in the project root (optional):
LOG_LEVEL=info # debug, info, warn, error
DEFAULT_BROWSER=chromium # chromium, firefox, webkit
HEADLESS=true # Run headless mode
MAX_SESSIONS=5 # Concurrent sessions (1-20)
SESSION_TIMEOUT=1800000 # Session expiry in ms (30 min)
TIMEOUT_ACTION=20000 # Element action timeout in ms
TIMEOUT_NAVIGATION=30000 # Page navigation timeout in msRunning the Server
# Development mode (with hot reload)
npm run dev
# Production build and run
npm run build
npm startProject Structure
βββ src/
β βββ index.ts # Application entry point
β βββ config/
β β βββ server-config.ts # Environment configuration
β β βββ types.ts # TypeScript type definitions
β βββ server/
β β βββ mcp-server.ts # MCP server implementation
β β βββ handlers/ # Tool handler categories
β β βββ browser-tools.ts # Browser lifecycle tools
β β βββ navigation-tools.ts # Navigation tools
β β βββ interaction-tools.ts# Click, fill, hover tools
β β βββ assertion-tools.ts # Web-first assertions
β β βββ page-tools.ts # Screenshots, content, a11y
β β βββ test-tools.ts # Test file management
β β βββ advanced-tools.ts # Network, tracing, dialogs
β β βββ schemas.ts # Zod validation schemas
β βββ playwright/
β β βββ browser-manager.ts # Central browser orchestration
β β βββ session-manager.ts # Session lifecycle
β β βββ browser-launcher.ts # Browser launch logic
β β βββ actions/ # Domain-specific actions
β β βββ assertion-actions.ts
β β βββ interaction-actions.ts
β β βββ navigation-actions.ts
β β βββ ...
β βββ utils/
β βββ error-handler.ts # Centralized error handling
β βββ logger.ts # Winston logger
βββ tests/ # Playwright test files
βββ specs/ # Human-readable test plans
βββ .github/
β βββ agents/ # AI agent definitions
β βββ prompts/ # Agent prompts
β βββ copilot-instructions.md # Development guidelines
βββ playwright.config.ts # Playwright test configurationAvailable Tools
Browser Lifecycle
Tool | Description |
| Launch browser (Chromium, Firefox, WebKit) with optional auth state |
| Close browser session |
| List, create, close, or select browser tabs |
| List all active browser sessions |
| Save cookies/localStorage for auth reuse |
| Clear session data for test isolation |
Navigation
Tool | Description |
| Navigate to URL |
| Go back/forward in history |
| Reload current page |
| Accept/dismiss browser dialogs |
Interaction
Tool | Description |
| Click by role, text, testid, or selector |
| Fill inputs by label, placeholder, or selector |
| Hover over elements |
| Select dropdown options |
| Press keys (Enter, Tab, shortcuts) |
| Type text character by character |
| Check/uncheck checkboxes |
| Upload files |
| Drag and drop elements |
Assertions
Tool | Description |
| Assert state (visible, hidden, enabled, disabled) |
| Assert element text content |
| Assert input value |
| Assert page URL |
| Assert page title |
| Assert element attribute |
| Assert CSS property |
| Assert checkbox state |
| Assert element count |
Page Operations
Tool | Description |
| Capture screenshots (full page, element, region) |
| Generate PDF from page (Chromium only) |
| Get HTML and text content |
| Execute JavaScript (read-only) |
| Wait for elements |
| Wait for page load |
| Run axe-core accessibility audit |
| Get accessibility tree snapshot |
Cookie Management
Tool | Description |
| Retrieve cookies from browser context |
| Add cookies (auth tokens, sessions) |
| Clear all or specific cookies |
Advanced
Tool | Description |
| Mock network responses |
| Remove network mocks |
| Record execution traces |
| Capture console messages |
| Record HTTP archive |
| Control time in tests |
| Get video recording path |
Best Practices for Stable Tests
Following these practices will ensure your tests are resilient, maintainable, and less prone to flakiness. See the full Best Practices Guide for detailed examples.
Core Principles
Use Semantic, User-Facing Locators
Role-based locators are most reliable:
getByRole('button', { name: 'Submit' })Avoid CSS selectors and XPath β these break when styling changes
Priority: Role β Label β Placeholder β Text β TestId β CSS (last resort)
Use Locator Chaining and Filtering
Chain locators to narrow searches:
page.getByRole('listitem').filter({ hasText: 'Product 2' })Filter by text or other locators for dynamic content
This reduces strict mode violations and increases clarity
Always Use Web-First Assertions
Use
expect()assertions which auto-wait:await expect(page.getByText('Success')).toBeVisible()Don't use direct checks like
isVisible()without expectAssertions wait up to 5 seconds (configurable) before failing
Avoid Common Pitfalls
β
waitForTimeout()β use specific waits insteadβ
waitForLoadState('networkidle')β use'domcontentloaded'or wait for elementsβ CSS class selectors β use role/label/text locators
β Screenshots as selectors β use
browser_snapshotfor finding elementsβ
test.only()ortest.skip()β remove before committing
Example: Good Test Structure
test('Add todo and verify', async ({ page }) => {
// Navigate
await page.goto('/');
// Get accessibility snapshot to understand page structure
const snapshot = await page.accessibility.snapshot();
// Interact using semantic locators (role > label > text)
await page.getByPlaceholder('What needs to be done?').fill('Buy groceries');
await page.getByRole('button', { name: 'Add' }).click();
// Verify using web-first assertions (auto-wait)
await expect(page.getByText('Buy groceries')).toBeVisible();
await expect(page.getByRole('listitem')).toHaveCount(1);
});Locator Priority
When interacting with elements, prefer user-facing locators (most reliable first):
Role β -
element_click(locatorType: 'role', role: 'button', name: 'Submit')Label β -
element_fill(locatorType: 'label', value: 'Email', text: '...')Text -
element_click(locatorType: 'text', value: 'Learn more')Placeholder -
element_fill(locatorType: 'placeholder', value: 'Search...')TestId -
element_click(locatorType: 'testid', value: 'submit-btn')Selector - CSS selector (last resort only)
Development Workflow
# Watch mode with hot reload
npm run dev
# Build TypeScript to dist/
npm run build
# Run ESLint
npm run lint
npm run lint:fix
# Type check without emit
npm run type-check
# Format with Prettier
npm run format
# Run tests
npm test
npm run test:ui # Interactive UI
npm run test:headed # Visible browser
npm run test:debug # Debug modeBefore committing: Run npm run lint && npm run type-check && npm run build
Coding Standards
Tool Registration Pattern
server.registerTool(
'tool_name',
{
title: 'Human Title',
description: 'What this tool does',
inputSchema: {
/* Zod schemas */
},
outputSchema: {
/* Result shape */
},
},
createToolHandler(async (input) => {
const result = await browserManager.someMethod(input);
return {
content: [{ type: 'text', text: 'Human readable' }],
structuredContent: result, // Machine readable
};
}, 'Error prefix message')
);Action Module Pattern
export class MyActions extends BaseAction {
async myOperation(sessionId: string, pageId: string, options: Options) {
return this.executePageOperation(
sessionId,
pageId,
'My operation',
async (page) => {
// Playwright operations
return { success: true, data: '...' };
}
);
}
}Error Handling
import {
ErrorCode,
ErrorHandler,
validateUUID,
} from '../utils/error-handler.js';
validateUUID(sessionId, 'sessionId'); // Throws on invalid
throw ErrorHandler.sessionNotFound(id); // Factory methods
throw ErrorHandler.handlePlaywrightError(e); // Maps Playwright errorsTesting
Tests use @playwright/test framework. Configuration is in playwright.config.ts.
npm test # Run all tests
npm run test:ui # Interactive test UI
npm run test:headed # With visible browser
npm run test:debug # Debug mode with inspector
npm run test:trace # Record traces
npm run test:report # Show HTML reportTest Configuration
Timeout: 30 seconds per test
Retries: 2 on CI, 0 locally
Browsers: Chromium, Firefox, WebKit, Mobile Chrome, Mobile Safari
Viewport: 1366x900
Test ID Attribute:
data-testid
AI Test Agents
Three AI agents for automated test workflows:
Agent | Input | Output |
Planner | App URL + seed test |
|
Generator | Test plan |
|
Healer | Failing test | Fixed test file |
Usage
Planner: Explore app and create test plans in
specs/Generator: Transform plans into Playwright tests
Healer: Debug and fix failing tests
Agent definitions are in .github/agents/ with prompts in .github/prompts/.
Security
URL validation: Only
http://andhttps://protocols allowedUUID validation: All session/page IDs validated
Rate limiting: Configurable
MAX_SESSIONS_PER_MINUTESession isolation: Each browser context is isolated
Script restrictions: Only safe, read-only JavaScript evaluation
Contributing
Fork the repository
Create a feature branch (
git checkout -b feature/amazing-feature)Follow the coding standards in
.github/copilot-instructions.mdRun linting and type checking (
npm run lint && npm run type-check)Ensure tests pass (
npm test)Commit your changes (
git commit -m 'Add amazing feature')Push to the branch (
git push origin feature/amazing-feature)Open a Pull Request
Adding a New Tool
Add method to action class in
src/playwright/actions/Register in handler file in
src/server/handlers/Add schemas to
schemas.tsif new input shapes neededAdd tests for the new functionality
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
Playwright - Browser automation framework
Model Context Protocol - AI assistant protocol
axe-core - Accessibility testing engine