Enables browser automation and testing through Playwright, supporting multi-browser control including Firefox for web navigation, interaction, assertions, screenshots, and accessibility testing.
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
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
Configuration
Create a .env file in the project root (optional):
Running the Server
Project Structure
Available 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
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
Before committing: Run npm run lint && npm run type-check && npm run build
Coding Standards
Tool Registration Pattern
Action Module Pattern
Error Handling
Testing
Tests use @playwright/test framework. Configuration is in playwright.config.ts.
Test 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