Notes
Set it up
Since Playwright one point six two, the MCP server ships with Playwright itself: the command is npx playwright mcp. You add it to your agent's MCP configuration, like any other server. Isolated keeps the browser profile in memory, so nothing leaks between sessions. Secrets we come back to. Version one point six three offers twenty four tools: navigate, snapshot, click, fill form, find, network requests and more.
{"mcpServers": {
"playwright": {
"command": "npx",
"args": ["playwright", "mcp",
"--isolated",
"--secrets", ".env.test"]
}}}
Playwright 1.63: 24 tools
pin the Playwright version The cost of tools
Tools are not free. The twenty four tool definitions, with their descriptions and schemas, came to about twenty thousand characters, roughly five thousand tokens, and the model receives them with every request. A whole session of eleven calls on ShopRung, logging in and adding a mug to the cart, returned about nine thousand three hundred characters. Turn off servers and tools you do not need.
24 tool definitions about 20,000 characters about 5,000 tokens sent with every model request one 11-call session on ShopRung: responses 9,285 characters, about 2,300 tokens
Snapshots
The agent sees the page as an accessibility snapshot: roles, accessible names and values, each with a ref such as e one seventy one, which it passes back to click. It is cheaper and more exact than a screenshot. Sizes on ShopRung: the login page about one thousand eight hundred characters, the product list with twelve products about six thousand three hundred. Browser find searches the snapshot for text, which costs less than reading it all.
- heading "Your cart" [ref=e133]
- row [ref=e145]:
- cell "Green Build Mug"
- spinbutton "Quantity of
Green Build Mug": "1"
- generic [ref=e169]: Total
- generic [ref=e170]: $19.99
- button "Place order"
[ref=e171] Real breakage
Two real breakages. Refs are not stable: after adding a mug, the cart link went from e ten to e one thirty two, and an old ref gives an error asking for a new snapshot. And tools change between versions. Our first script sent the argument ref, as older examples do; version one point six three calls it target, and every click failed validation. Pin the Playwright version, like any dependency.
Cart link: ref e10 → e132 after adding a mug old ref: "Ref e999 not found in the current page snapshot. Try capturing new snapshot." our first script sent "ref"; 1.63 wants "target": "Invalid input: expected string, received undefined → at target"
Secrets
A real finding. Without the secrets option, the password Alice one two three was in the generated code and in the snapshot, even though it is a password field. Everything in a snapshot goes to the model provider and into logs. With secrets pointing at a dot env file, the agent fills the field by the secret's name, and both the code and the snapshot show only the name. Use test accounts only, and never real credentials.
without --secrets:
code: .fill('Alice123!')
snapshot: textbox "Password"
[ref=e20]: Alice123!
with --secrets .env.test:
code: .fill(process.env
['ALICE_PASSWORD'])
snapshot: <secret>ALICE_
PASSWORD</secret> Safety
Limit what the agent can reach. Use an isolated profile, test accounts and a test environment, never production with real customers. Two tools run arbitrary code; the server itself calls one of them unsafe and equivalent to remote code execution, so approve those calls. The allowed origins option is documented as not a security boundary. And remember Part ten: any text on a page can try to inject instructions into the agent.
- --isolated: a fresh profile
- test accounts, test data, a test environment only --secrets for every password
- browser_run_code_unsafe and browser_evaluate run any code: approve each call
- --allowed-origins is not a security boundary (its docs)
- page text can inject the agent
Into a test
Every action returns the Playwright code it ran, with role based locators. Collect them, use relative URLs, and add the assertions the session never made: that Alice is logged in, and that the total is nineteen ninety nine. My own first assertion matched three table cells and failed in strict mode, so review and run everything an exploration produces. After the fix, the test passed in under a second.
session code, then reviewed:
goto('/#/login')
getByRole('textbox', {name:
'Email'}).fill(...)
+ expect(...'Hi, Alice Shah')
+ expect(...'$19.99')
my first assertion: strict mode
violation, 3 cells matched
fixed (exact: true): 1 passed Common mistakes
Mistakes. Turning on every MCP server and tool, which fills the context before any work starts. Reusing old refs instead of taking a new snapshot. Not pinning versions. Typing real passwords without secrets, so they land in snapshots and logs. Pointing the agent at production or real accounts. Auto approving the unsafe tools. And treating an agent session as the test, instead of writing a real test from it.
- every MCP server switched on
- reusing old refs
- unpinned tool versions
- real passwords, no --secrets
- production or real accounts
- unsafe tools auto-approved
- keeping the session, not writing the test
Say it like this
In the interview: I use it for exploring and drafting. The agent reads accessibility snapshots with refs and every action returns the Playwright code it ran. I pin the version, isolate the browser profile, keep passwords in a secrets file, use only test accounts, and approve the unsafe tools by hand. Then I turn the session into a reviewed test with real assertions, and run it in CI without any agent.
- "For exploring and drafting. The agent reads accessibility
- snapshots with refs and each action returns Playwright code.
- I pin the version, isolate the profile, use --secrets and test
- accounts, approve unsafe tools, and turn the session into a reviewed test with real
- assertions."