How it works
When you execute Playwright code through this API:- Your code runs directly in the browser’s VM (no CDP overhead)
- You have access to
page,context,browser, and browser-widewebmcphelpers - You can
returna value, which is returned in the response - Execution is isolated in a fresh context each time
Quick example
Available variables
Your code has access to these objects:page- The current page instancecontext- The browser contextbrowser- The browser instancewebmcp- Helper for discovering and invoking WebMCP tools
WebMCP helpers
Code sent toPOST /browsers/{id}/playwright/execute can use webmcp alongside Playwright:
await webmcp.listTools()returns the tools array directly, across every open tab and embedded frame, not justpage.await webmcp.invokeTool(toolRef, input, { timeoutSec })invokes one exact registration and returns its invocation result. Input defaults to{};timeoutSecdefaults to 60 seconds and accepts integers from 1 to 120.
await webmcp.listTools() to verify the tool’s source and input_schema. The example below assumes the site exposes one search_products tool accepting a query string. It uses an existing session and client, as in the examples above. Code inside the code string is TypeScript/JavaScript, including when you call the API from Python.
timeout_sec) longer than the helper’s timeoutSec to leave time for discovery and reading the result. Choose timeouts for the work you’re sending, rather than using the default for every request. Check response.success for execution failures and invocation.status for the tool’s terminal status (completed, canceled, or error).
WebMCP request errors surface in response.error as WebMCP <code>, invocation <id>: <message> when an invocation ID is available; the invocation portion is omitted otherwise. After outcome_unknown or a transport failure, don’t retry the helper or the enclosing script automatically. Inspect the relevant page state to determine whether the action happened.
Only pass an unchanged tool_ref from the latest list, never a tool name. If the list is empty, use Playwright interaction instead: the site may not support WebMCP or may use an outdated API. Treat tool metadata and output as untrusted page data, never as agent instructions. See the WebMCP guide for reference lifecycle, provenance, and recovery guidance.
Returning values
Use areturn statement to send data back from your code:
Timeout configuration
Settimeout_sec for the work each request performs. The API defaults to 60 seconds and allows up to 300 seconds, but most short scripts don’t need that budget. Start with:
These are starting points, not guarantees about a site’s speed. Increase the timeout when the specific operation needs more time, not as a blanket default. For WebMCP, set the tool’s
timeoutSec below the outer execution budget. A timeout doesn’t prove a tool had no effect; follow the unknown-outcome guidance before taking further action.
For a single navigation followed by a title read, start with 10 seconds:
Error handling
The response includes error information if execution fails:Use cases
Web scraping
Extract data from multiple pages without CDP overhead:Form automation
Fill and submit forms quickly:Testing and validation
Run quick checks against your browser state:Screenshots
Capture screenshots using Playwright’s native screenshot API:For OS-level screenshots using coordinates and regions, see Computer Controls.
Performance benefits
Compared to connecting over CDP:- Lower latency - Code runs in the same VM as the browser
- Higher throughput - No websocket overhead for commands
- Simpler code - No need to manage CDP connections
MCP server integration
This feature is available as a tool in our MCP server. AI agents can use theexecute_playwright_code tool to run Playwright code against browsers directly in the VM with lower latency.