@nodesandbox-ai/plugin-playwright
Playwright
Let an agent see what the sandbox's dev server serves: open localhost, read the page as Playwright's accessibility snapshot, click and type, read the console, and take screenshots. Claude Code, OpenCode and Pi get it as an MCP server.
npm i @nodesandbox-ai/client @nodesandbox-ai/plugin-playwrightAdd Playwright
import { Sandbox } from '@nodesandbox-ai/client';
import { claudeCodePlugin } from '@nodesandbox-ai/plugin-claude';
import { playwrightPlugin } from '@nodesandbox-ai/plugin-playwright';
const sandbox = await Sandbox.create({
apiKey: 'ns_live_…',
source: { type: 'files', files: {} },
plugins: [claudeCodePlugin(), playwrightPlugin()],
});The plugin adds the playwright command, with playwright-cli as an alias. Nothing downloads: there is no browser to install, because pages open in the tab that runs the sandbox. It works on its own, and with an agent plugin beside it the agent gets Playwright's tools over MCP.
The CLI
npm run dev &
playwright-cli open http://localhost:3000
playwright-cli snapshot # the page, with refs: - button "Save" [ref=e7]
playwright-cli fill e5 "Ada" --submit
playwright-cli click e7
playwright-cli console error
playwright-cli screenshot # .playwright-cli/page-<time>.pngThe commands are those of Microsoft's agent CLI, playwright-cli 0.1.24, with its arguments and its output: ### Ran Playwright code, ### Page, and snapshots and console logs written to .playwright-cli/. playwright cli … is the same. playwright --version, playwright screenshot <url> <file> and playwright install answer as Playwright's own CLI does. Run playwright-cli --help for the full list.
Targets are a ref from the last snapshot (e7), a locator as a test would write it (getByRole('button', { name: 'Save' })), or a selector (CSS, text=). Matching is strict, as in Playwright: a target that matches two elements is an error that lists them.
MCP for the agents
playwright mcp serves Playwright's MCP tools over stdio, as npx @playwright/mcp does. With the plugin installed, Claude Code, OpenCode and Pi have it as their playwright MCP server, with no setup: the plugin declares it, and each agent plugin adds it to the agent's own configuration before it starts.
playwrightPlugin(); // playwright mcp
playwrightPlugin({ mcp: { caps: ['testing', 'vision'] } }); // playwright mcp --caps=testing,vision
playwrightPlugin({ mcp: false }); // the commands onlyClaude Code~/.claude.json, as a user-scope stdio server. claude mcp list shows it.OpenCode~/.config/opencode/opencode.json, as a local server with a 60s request timeout (OpenCode's default is 5s). /mcps in the TUI shows it.Pi~/.pi/agent/mcp.json, with a description of what it is for. pi mcp list shows it.CodexNot configured. Codex runs its commands with stdin closed, and an MCP stdio server needs its input.Entries written this way carry NODESANDBOX_PLUGIN_MCP in their environment. They are updated on the next run, and removed once the plugin is no longer in the sandbox. A server you configured yourself under the same name is never overwritten; to take over a written one, remove that variable from it. Agents write nothing when no plugin offers a server.
The tools are Playwright's own: names, descriptions, input schemas and annotations come from playwright-core 1.65.1, and caps chooses them as upstream does (every core tool, plus storage, testing, vision and the rest when named). Answers have upstream's sections, with snapshots and console logs in .playwright-mcp/. Tools this tab cannot carry out are left out of the tool list and explain why when called. The package's tests run an MCP client against Playwright's own server and against this one, and require the same answers.
Any plugin can offer MCP servers the same way; see MCP servers.
With Elements
Add playwrightPlugin() to the same plugins as your agents. It needs no sidenav button: each agent tab the sidenav opens (render: <SandboxTerminal command="claude" />) starts with Playwright among its MCP servers. Ask the agent to open the dev server and check its work; the pages it drives are separate from the preview, so the preview is left as it was. See Sidenav and agents.
const plugins = useMemo(() => [
opencodePlugin(),
claudeCodePlugin(),
piPlugin(),
playwrightPlugin(),
], []);How it works
A real Playwright launches a browser and drives it over its protocol, and a browser tab can do neither. Here the runtime opens each page as a hidden frame of the sandbox's own origin, served by the Service Worker exactly as the preview is, and the page runs Playwright's own injected script. Snapshots, refs, selector engines, strict mode and generated locators are therefore Playwright's: on the same page, snapshot prints what Playwright prints. What Playwright's server does over the protocol (waiting until an element is visible, enabled and not covered, then sending the pointer and keyboard) the plugin does with the page's DOM events.
The pages need a runtime host with the page driver. The commands run in your app, like every plugin's; see the execution model.
What differs
Which pagesThe sandbox's servers only, on localhost:<port> or 127.0.0.1. The internet and other origins are refused by name; curl reaches them.InputThe page's own events: isTrusted is false, and CSS :hover does not follow hover. Typing goes through the editor, so React and other frameworks see ordinary input events.ScreenshotsThe DOM drawn again, not the compositor's pixels: canvas and WebGL content, cross-origin images and nested frames may be missing.BrowserOne, the tab's own. No Firefox or WebKit, no devices beyond a mobile-sized viewport (open --mobile), no media emulation.StorageThe tab's origin, so localStorage also lists the app's own keys. Cookies are what document.cookie shows: no httpOnly cookies.Not availablePDF, file upload, drag and drop, dialogs, network routing and logs, storage-state files, tracing, video, run-code, codegen and playwright test. Each answers why when called.