Playwright Smart MCP
# Playwright Smart MCP
A **token-efficient** Playwright MCP server that exposes **4 smart tools instead of 22**, reducing context window usage by ~85%.
## ✨ New in v2.1
- **🔧 Code Execution**: New `browser_execute_code` tool for complex UI interactions
- **🖥️ Headed Mode**: See the browser window, watch automation happen
- **💾 Session Persistence**: Log in once, stay logged in across sessions
- **🔐 Auto-Save**: Cookies & login state saved automatically after every action
- **⚡ Interactive Workflows**: You log in, AI takes over
See [FEATURES.md](FEATURES.md) for full documentation on headed mode and session persistence.
## The Problem
The standard Playwright MCP server exposes 22 tools, each with full JSON schemas. This consumes **~5,000 tokens** of your context window before you even do anything.
## The Solution
This server consolidates all functionality into 4 high-level tools:
| Tool | What It Does | Replaces |
|------|--------------|----------|
| `browser_screenshot` | Navigate + capture screenshot | navigate, wait, screenshot |
| `browser_extract` | Navigate + get page content | navigate, snapshot, evaluate |
| `browser_interact` | Click, type, fill, scroll, etc. | 15+ interaction tools |
| `browser_execute_code` | Execute JavaScript in browser | evaluate, complex DOM manipulation |
**Result:** ~900 tokens instead of ~5,000 (**82% reduction**)
## Installation
### Option 1: Clone and Use Locally
```bash
git clone https://github.com/jaystarz1/playwright-smart-mcp.git
cd playwright-smart-mcp
npm install
```
### Option 2: npx (coming soon)
```bash
npx playwright-smart-mcp
```
## Configuration
### Claude Code (Global - All Projects)
Add to `~/.claude/settings.json`:
```json
{
"mcpServers": {
"playwright-smart": {
"type": "stdio",
"command": "node",
"args": ["/path/to/playwright-smart-mcp/server.js"]
}
}
}
```
### Claude Code (Project-Specific)
Add to your project's `.claude.json` or via CLI:
```bash
claude mcp add playwright-smart node /path/to/playwright-smart-mcp/server.js
```
### Claude Desktop
Add to your Claude Desktop config:
```json
{
"mcpServers": {
"playwright-smart": {
"command": "node",
"args": ["/path/to/playwright-smart-mcp/server.js"]
}
}
}
```
## Usage
### browser_screenshot
Take a screenshot of any URL:
```json
{
"url": "https://example.com",
"fullPage": true,
"waitFor": ".main-content"
}
```
### browser_extract
Get page content and links:
```json
{
"url": "https://example.com",
"selector": ".article-body",
"includeLinks": true
}
```
### browser_interact
Execute a sequence of actions:
```json
{
"url": "https://example.com/login",
"actions": [
{"type": "fill", "selector": "#email", "value": "user@example.com"},
{"type": "fill", "selector": "#password", "value": "secret"},
{"type": "click", "selector": "button[type=submit]"},
{"type": "wait", "selector": ".dashboard"}
],
"returnScreenshot": true
}
```
**Available action types:**
- `click` - Click an element
- `type` - Type text (keystroke by keystroke)
- `fill` - Fill a form field (faster than type)
- `select` - Select dropdown option
- `wait` - Wait for element or timeout
- `scroll` - Scroll the page
- `press` - Press a keyboard key (Enter, Tab, etc.)
### browser_execute_code
Execute JavaScript in the browser context when standard interactions fail:
```json
{
"code": "document.querySelector('[placeholder*=\"Add people\"]').value = 'user@example.com'; return 'Email entered';"
}
```
**Use cases:**
- Complex React/Vue components that don't respond to standard selectors
- Dynamic UI elements that require custom event dispatching
- DOM manipulation that's simpler with JavaScript than chained interactions
- Extracting data in custom formats
**Example: Sharing Google Drive folder**
```json
{
"code": "const input = document.querySelector('[placeholder*=\"Add people\"]'); if (input) { input.value = 'user@example.com'; input.dispatchEvent(new Event('input', { bubbles: true })); return 'Email added'; } return 'Input not found';"
}
```
## Example: Form Submission
Instead of 6+ tool calls with standard Playwright MCP:
```
browser_navigate → form page
browser_type → email field
browser_type → password field
browser_click → submit button
browser_wait_for → success message
browser_screenshot → capture result
```
Do it in **one call**:
```json
{
"tool": "browser_interact",
"arguments": {
"url": "https://example.com/signup",
"actions": [
{"type": "fill", "selector": "#email", "value": "user@example.com"},
{"type": "fill", "selector": "#password", "value": "secret123"},
{"type": "click", "selector": "button[type=submit]"},
{"type": "wait", "selector": ".welcome-message"}
],
"returnScreenshot": true
}
}
```
## Why This Approach?
Based on [Anthropic's engineering blog post on code execution with MCP](https://www.anthropic.com/engineering/code-execution-with-mcp), which advocates for **progressive disclosure** of tool capabilities and **code execution as an escape hatch**.
> "Models are great at navigating filesystems. Presenting tools as code on a filesystem allows models to read tool definitions on-demand, rather than reading them all up-front."
This server takes that principle and applies it to browser automation:
- **High-level tools** (`browser_interact`) handle 95% of use cases
- **Code execution** (`browser_execute_code`) handles the edge cases when selectors fail
- **Result**: Scalpel approach vs sledgehammer - fewer tools, smarter usage
## Token Comparison
| Server | Tools | Est. Tokens | Use Case |
|--------|-------|-------------|----------|
| Playwright MCP | 22 | ~5,000 | Full control needed |
| **Playwright Smart** | 4 | ~900 | Most automation tasks + edge cases |
## License
MIT
## Contributing
PRs welcome! Ideas for improvement:
- [ ] Add `browser_pdf` tool for PDF generation
- [ ] Add `browser_record` for session recording
- [ ] Support for multiple tabs/contexts
- [ ] Pre-defined workflows (login, form submit, scrape)
## Credits
- Built on [Playwright](https://playwright.dev/)
- Inspired by [Anthropic's MCP documentation](https://www.anthropic.com/engineering/code-execution-with-mcp)
- Created by Jay Tarzwell / The Chatbot Genius
TDQS
Scored across 4 tools
Each tool targets a distinct function: capture (screenshot), read (extract), act (interact), and fall back to raw code (execute_code). The overlap between browser_interact and browser_execute_code is deliberate but could cause misselection since both can perform clicks/typing, with the latter described as a fallback. All tools start with navigation, so an agent must infer whether state persists across calls.
All names use a consistent browser_ prefix and mostly verb or verb_noun forms (extract, interact, execute_code). browser_screenshot is a noun where the others are verbs, which is a minor deviation but still readable and predictable.
Four tools is well-scoped for a browser automation server; each covers a distinct capability layer (visual, textual, interactive, programmatic) and none feels redundant or missing a sibling.
The surface lacks explicit tools for waiting/asserting, reading console or network logs, and inspecting the current page without re-navigating, which are common browser automation needs. browser_execute_code can work around some of these, but the lifecycle is somewhat thin.