Enables web automation for Amazon's shopping platform, allowing navigation, search, and interaction with product pages through semantic HTML snapshots.
Identifies and preserves semantic HTML5 tags when generating page snapshots, maintaining the structural meaning of web content while reducing token usage.
Handles JavaScript-based web interactions through Playwright, enabling automation of dynamic web applications built with JavaScript.
Supports Linux as a platform for running the MCP server, with specific file paths for operation records.
Supports macOS as a platform for running the MCP server, with specific file paths for operation records.
Built on Node.js with requirements for Node.js >= 18.0.0 for running the server components.
Built with TypeScript for type safety, with development resources for TypeScript contributors.
Includes WebGL vendor spoofing as part of its stealth features to prevent browser fingerprinting during web automation.
better-playwright-mcp
A better Playwright MCP (Model Context Protocol) server that uses a client-server architecture for browser automation.
Why Better?
Traditional browser automation tools send entire page HTML to AI assistants, which quickly exhausts token limits and makes complex web interactions impractical. better-playwright-mcp solves this with an innovative semantic snapshot algorithm that reduces page content by up to 90% while preserving all meaningful elements.
The Problem
- Full page HTML often exceeds 100K+ tokens
- Most HTML is noise: inline styles, tracking scripts, invisible elements
- AI assistants have limited context windows (even with 200K limits)
- Complex web automation becomes impossible after just a few page loads
Our Solution: Semantic Snapshots
Our core innovation is a multi-stage pruning algorithm that:
- Identifies meaningful elements - Interactive elements (buttons, inputs), semantic HTML5 tags, and text-containing elements
- Generates unique identifiers - Each element gets a hash-based
xp
attribute derived from its XPath for precise targeting - Removes invisible content - Elements with
display:none
, zero dimensions, or hidden parents are marked and removed - Unwraps useless wrappers - Eliminates divs and spans that only wrap other elements
- Strips unnecessary attributes - Keeps only essential attributes like
href
,value
,placeholder
Result: A clean, semantic representation that typically uses only 10% of the original tokens while maintaining full functionality.
Architecture
This project implements a unique two-tier architecture:
- MCP Server - Communicates with AI assistants via Model Context Protocol
- HTTP Server - Runs in the background to control the actual browser instances
This design allows the MCP server to remain lightweight while delegating browser control to a dedicated HTTP service.
Features
- 🎯 90% token reduction through semantic HTML snapshots
- 🎭 Full Playwright browser automation via MCP
- 🏗️ Client-server architecture for better separation of concerns
- 🛡️ Stealth mode to avoid detection
- 📍 Hash-based element identifiers for precise targeting
- 💾 Persistent browser profiles
- 🚀 Optimized for long-running automation tasks
- 📊 Token-aware output with automatic truncation
Installation
Usage
Default Mode (MCP)
The MCP server requires an HTTP server to be running. You need to start both:
Step 1: Start the HTTP server
Step 2: In another terminal, start the MCP server
The MCP server will:
- Start listening on stdio for MCP protocol messages
- Connect to the HTTP server on port 3102
- Route browser automation commands through the HTTP server
Options:
--snapshot-dir <path>
- Directory to save snapshots
Standalone HTTP Server Mode
You can also run the HTTP server independently (useful for debugging or custom integrations):
Options:
-p, --port <number>
- Server port (default: 3102)--host <string>
- Server host (default: localhost)--headless
- Run browser in headless mode--chromium
- Use Chromium instead of Chrome--no-user-profile
- Do not use persistent user profile--user-data-dir <path>
- User data directory--snapshot-dir <path>
- Directory to save snapshots
MCP Tools
When used with AI assistants, the following tools are available:
Page Management
createPage
- Create a new browser page with name and descriptionactivatePage
- Activate a specific page by IDclosePage
- Close a specific pagelistPages
- List all managed pages with titles and URLscloseAllPages
- Close all managed pageslistPagesWithoutId
- List unmanaged browser pagesclosePagesWithoutId
- Close all unmanaged pagesclosePageByIndex
- Close page by index
Browser Actions
browserClick
- Click an element using itsxp
identifierbrowserType
- Type text into an elementbrowserHover
- Hover over an elementbrowserSelectOption
- Select options in a dropdownbrowserPressKey
- Press keyboard keysbrowserFileUpload
- Upload files to file inputbrowserHandleDialog
- Handle browser dialogs (alert, confirm, prompt)browserNavigate
- Navigate to a URLbrowserNavigateBack
- Go back to previous pagebrowserNavigateForward
- Go forward to next pagescrollToBottom
- Scroll to bottom of page/elementscrollToTop
- Scroll to top of page/elementwaitForTimeout
- Wait for specified millisecondswaitForSelector
- Wait for element to appear
Snapshot & Utilities
getPageSnapshot
- Get semantic HTML snapshot withxp
identifiersgetScreenshot
- Take a screenshot (PNG/JPEG)getPDFSnapshot
- Generate PDF of the pagegetElementHTML
- Get HTML of specific elementdownloadImage
- Download image from URLcaptureSnapshot
- Capture full page with automatic scrolling
How It Works
Semantic Snapshots in Action
Before (original HTML):
After (semantic snapshot):
The algorithm:
- Removes unnecessary wrapper divs
- Strips inline styles and event handlers
- Adds unique identifier (
xp
attribute) - a hash of the element's XPath - Preserves only meaningful content
Diff-Based Optimization
To reduce data transfer and token usage:
- First snapshot is always complete
- Subsequent snapshots only include changes (diffs)
- Automatic caching for performance
Stealth Features
Browser instances are configured with:
- Custom user agent strings
- Disabled automation indicators
- WebGL vendor spoofing
- Canvas fingerprint protection
Examples
Creating and Navigating Pages
Interacting with Elements
Capturing Page State
Development
Prerequisites
- Node.js >= 18.0.0
- TypeScript
- Chrome or Chromium browser
Building from Source
Project Structure
Troubleshooting
Common Issues
- MCP server not connecting
- Ensure the HTTP server is accessible on port 3102
- Check firewall settings
- Try running with
DEBUG=* npx better-playwright-mcp
- Browser not launching
- Ensure Chrome or Chromium is installed
- Try using
--chromium
flag - Check system resources
- Token limit exceeded
- Snapshots are automatically truncated to 20,000 tokens
- Use targeted selectors to reduce snapshot size
- Consider using screenshot instead of snapshot for visual inspection
Debug Mode
Enable detailed logging:
Logs and Records
Operation records are saved to:
- macOS/Linux:
/tmp/playwright-records/
- Windows:
%TEMP%\playwright-records\
Each page has its own directory with timestamped operation logs.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
Tools
A client-server browser automation solution that reduces HTML token usage by up to 90% through semantic snapshots, enabling complex web interactions without exhausting AI context windows.
Related MCP Servers
- AsecurityAlicenseAqualityFacilitates browser automation with custom capabilities and agent-based interactions, integrated through the browser-use library.Last updated -1775PythonMIT License
- AsecurityFlicenseAqualityEnables AI agents to interact with web browsers using natural language, featuring automated browsing, form filling, vision-based element detection, and structured JSON responses for systematic browser control.Last updated -150Python
- -securityAlicense-qualityEnables browser automation and real-time computer vision tasks through AI-driven commands, offering zero-cost digital navigation and interaction for enhanced web experiences.Last updated -01JavaScriptMIT License
- -securityFlicense-qualityProvides browser automation capabilities through an API endpoint that interprets natural language commands to perform web tasks using OpenAI's GPT models.Last updated -Python