mcp-playwright-test-runner
# mcp-playwright-test-runner
[](https://github.com/AmrAsaad1025/mcp-playwright-test-runner/actions/workflows/ci.yml)
An MCP server that lets an AI client (Claude Code, Cursor, any MCP host) list, run and read your Playwright tests.
When an agent writes or fixes tests it needs three things: which tests exist, what happened when they ran, and why one failed. Normally that means copying terminal output back into the chat. This server exposes those three as MCP tools over stdio, so the agent can call them itself and keep working.
## Tools
| Tool | What it does | What it returns |
| --- | --- | --- |
| `list_tests` | Lists the tests Playwright can see, without running them | count, title, file, line, project |
| `run_tests` | Runs the suite, or a filtered part of it (`file`, `grep`, `project`) | `run_id`, status, summary, one line of error per failure |
| `get_results` | Reads a finished run back by `run_id` (latest by default) | per-test status, duration, retries, error line |
| `get_failure_details` | Explains a failure | full error, stack, location, captured stdout/stderr, screenshot and trace paths |
Everything is plain JSON in a text content block, so any MCP client can read it.
## Quickstart
```bash
git clone https://github.com/AmrAsaad1025/mcp-playwright-test-runner.git
cd mcp-playwright-test-runner
npm install
npx playwright install chromium
npm run build
```
Point it at your own Playwright project with Claude Code:
```bash
claude mcp add playwright-tests -- node "$(pwd)/dist/index.js" --project-dir /path/to/your/playwright/project
```
Or drop it into `.mcp.json`:
```json
{
"mcpServers": {
"playwright-tests": {
"command": "node",
"args": ["/absolute/path/dist/index.js", "--project-dir", "/absolute/path/to/project"],
"env": { "PW_TIMEOUT_MS": "600000" }
}
}
}
```
Then ask the agent something like "run the login tests and tell me what broke".
## Try it without your own project
`examples/demo-project` is a small Playwright suite (a local login + cart page, 4 tests, no network). The demo drives the server over stdio exactly like a client would:
```bash
npm run demo
```
```
→ tools/list
list_tests List the tests Playwright can see in the configured project without running them.
run_tests Run the Playwright suite, or a filtered part of it, and return a compact summary plus a run_id.
get_results Read back a finished run: per-test status, duration, retry count and the first line of any error.
get_failure_details Explain why tests failed: full error message, stack, location, captured stdout/stderr and the screenshot or trace paths Playwright saved.
→ list_tests {}
4 tests
- Cart › adds a product to the cart (cart.spec.ts:12)
- Cart › filters products by name (cart.spec.ts:20)
- Login › shows the shop after a valid login (login.spec.ts:4)
- Login › rejects a wrong password (login.spec.ts:14)
→ run_tests {}
run_id run-001 status passed 3.7s
total 4 passed 4 failed 0 skipped 0 flaky 0
→ get_results { run_id: "run-001" }
passed 517ms Cart › adds a product to the cart
passed 443ms Cart › filters products by name
passed 342ms Login › shows the shop after a valid login
passed 297ms Login › rejects a wrong password
→ run_tests { file: "tests/zz-demo-failure.spec.ts" } (a broken test, written for this demo)
run_id run-002 status failed
order total includes the T-Shirt: Error: expect(locator).toHaveText(expected) failed
→ get_failure_details { run_id: "run-002" }
order total includes the T-Shirt (tests/zz-demo-failure.spec.ts:12:40)
| Error: expect(locator).toHaveText(expected) failed
|
| Locator: locator('#total')
| Expected: "25.00"
| Received: "10.00"
| Timeout: 5000ms
captured output: cart total after one add: 10.00
attachment: screenshot -> test-results/zz-demo-failure-order-total-includes-the-T-Shirt-chromium/test-failed-1.png
attachment: error-context -> test-results/zz-demo-failure-order-total-includes-the-T-Shirt-chromium/error-context.md
attachment: trace -> test-results/zz-demo-failure-order-total-includes-the-T-Shirt-chromium/trace.zip
```
## How it works
- The server spawns the Playwright CLI in the target project with `--reporter=json` and `PLAYWRIGHT_JSON_OUTPUT_NAME` pointing at a temp file. The report comes from that file, so anything a test prints to stdout cannot corrupt it.
- `src/playwright/report.ts` flattens the nested reporter JSON into one row per test, keeping describe titles, retry counts, attachments and stdout.
- Each run is stored in memory under a `run_id` (last 20 by default), so later tool calls read the detail without running the suite again.
- A run that goes past `PW_TIMEOUT_MS` is killed and reported as `timed_out`.
- A non-zero exit that collected no tests is reported as `error` with the tail of stderr, because "the suite never started" and "the suite failed" need different reactions.
- Error text is stripped of ANSI colour codes and truncated per field, so it reads properly in a chat.
### Configuration
| Setting | Flag or env | Default |
| --- | --- | --- |
| Project to test | `--project-dir`, `PW_PROJECT_DIR` | current directory |
| Playwright command | `PLAYWRIGHT_CMD` | `npx playwright` |
| Run timeout | `PW_TIMEOUT_MS` | 600000 (10 min) |
| Runs kept in memory | `MAX_STORED_RUNS` | 20 |
## Project layout
```
src/index.ts entry point, stdio transport
src/server.ts registers the four tools
src/tools/ one file per tool, plain functions over injected deps
src/playwright/report.ts JSON reporter parser
src/playwright/runner.ts child process, output capture, timeout
src/playwright/runs.ts in-memory run history
examples/demo-project/ small Playwright suite used by the demo and the e2e test
scripts/demo.ts MCP client that walks through the whole flow
test/ unit, protocol and end-to-end tests
```
## Tests
```bash
npm test # 17 tests, no browser needed (the Playwright CLI is stubbed)
REAL_PW=1 npm test # also runs a real Chromium run against examples/demo-project
```
| File | What it covers |
| --- | --- |
| `test/report.test.ts` | reporter parsing: passes, failures, flaky retries, skips, global errors, bad JSON |
| `test/tools.test.ts` | each tool's output, the timeout path, run history, unknown run ids |
| `test/mcp-stdio.test.ts` | the real server over stdio: `tools/list`, `run_tests` then `get_results`, bad run id |
| `test/demo-project.test.ts` | a real Playwright run through the protocol (needs a browser) |
CI runs the typecheck, the tests with `REAL_PW=1`, the build, and the demo on every push.
## Limits
- stdio transport only; no HTTP/SSE server yet.
- One project per server process. Run a second server for a second project.
- The run history is memory-only, so restarting the server forgets earlier runs.
- The environment running the server needs a Playwright browser installed.
- It is a way for an agent to read a run it triggered, not a replacement for a CI dashboard.
## License
MIT
TDQS
Scored across 4 tools
Each tool maps to a distinct phase of the workflow: discovery, execution, result summarization, and failure diagnosis. The boundary between get_results and get_failure_details is explicitly clarified as summary versus detailed diagnostics.
All tools use a consistent snake_case verb_noun pattern: list_tests, run_tests, get_results, get_failure_details. The convention is predictable throughout.
Four tools exactly cover the core test-runner workflow without filler. Each tool earns its place and the surface is well-scoped.
The set covers test discovery, execution, result retrieval, and failure diagnosis. Minor gaps exist around explicitly canceling a run or listing run history, though timeout handling and run_id/default-latest behavior mitigate these.