Playwright MCP Server
The Playwright MCP Server provides an integration with Cursor IDE to enable AI-powered generation of Playwright tests. Operating as a custom Model Context Protocol (MCP) server, it analyzes a project's site context, existing test patterns, and authentication configurations to generate new tests based on user prompts. The server is currently at version 1.0.0, suggesting its initial stable release. Its release cadence is not explicitly stated but is likely tied to its primary use case with the Cursor IDE. Key differentiators include its deep integration with Cursor's chat interface, intelligent code reuse from existing tests, and sophisticated project structure analysis to ensure generated tests adhere to established conventions, making it a specialized tool for accelerating Playwright test development within the Cursor ecosystem.
Common errors
-
ERROR: OPENAI_API_KEY environment variable is not set.
cause The required `OPENAI_API_KEY` environment variable is missing from the server's environment.fixSet the environment variable: `export OPENAI_API_KEY="your-key-here"` in your terminal or ensure it's loaded via an `.env` file when starting the server. -
Error: Cannot find module 'playwright-mcp-server'
cause The `playwright-mcp-server` package is not installed or the `npx` command cannot resolve its location.fixRun `npm install` in the package's directory. If using `npx`, ensure the package is globally linked (`npm link`) or specify the full path to `src/index.js` in `.cursor/settings.json`. -
Error: ENOENT: no such file or directory, stat '/path/to/project/site-context.json'
cause The `site-context.json` file, which defines your site's structure, is missing or located in an unexpected path.fixCreate `site-context.json` in one of the specified locations (e.g., project root) and ensure it's correctly formatted according to the schema. -
Failed to authenticate. Check username, password, or selectors in .playwright-mcp-auth.json
cause The authentication configuration in `.playwright-mcp-auth.json` is incorrect, preventing successful login to protected pages.fixReview `.playwright-mcp-auth.json` for correct `username`, `password`, `loginUrl`, and selector values (`usernameSelector`, `passwordSelector`, `submitSelector`). Ensure they match your site's login form. -
MCP server 'playwright' not found or failed to start.
cause Cursor IDE could not start or establish a connection with the configured MCP server process.fixVerify the `mcpServers` configuration for the `playwright` entry in `.cursor/settings.json`. Check the `command` and `args` paths. Ensure the `playwright-mcp-server` is runnable from your terminal and check its console logs for any startup errors.
Warnings
- gotcha The server requires an `OPENAI_API_KEY` environment variable to function. Failure to set this will prevent the AI test generation functionality from working, leading to errors or non-responsive behavior.
- gotcha The `site-context.json` file is crucial for the server to understand your website structure. Incorrect paths or malformed JSON will lead to the server failing to identify pages or elements correctly.
- gotcha Authentication configuration in `.playwright-mcp-auth.json` is critical for testing pages behind a login. Misconfigurations, incorrect selectors, or invalid credentials will result in authentication failures during test generation.
- gotcha Proper configuration of `.cursor/settings.json` is essential for Cursor to locate and communicate with the MCP server. Incorrect `command` or `args` paths will prevent Cursor from invoking the server.
- gotcha This package is designed as a standalone server, primarily interacted with via the command line and integrated through the Cursor IDE. Attempting to import and use its runtime components programmatically as a library is not its intended use case and may lead to unexpected behavior.
Install
-
npm install playwright-mcp-server -
yarn add playwright-mcp-server -
pnpm add playwright-mcp-server
Imports
- SiteContext
import type { SiteContext } from 'playwright-mcp-server' - AuthConfiguration
import type { AuthConfiguration } from 'playwright-mcp-server' - McpServerConfig
import type { McpServerConfig } from 'playwright-mcp-server'
Quickstart
/* quickstart.ts: Setup and Configuration for Playwright MCP Server */
// 1. Ensure your OpenAI API Key is set as an environment variable.
// This is crucial for the server to generate tests using AI models.
// Example (run in your shell or .env file):
// export OPENAI_API_KEY="sk-your-actual-api-key"
// For demonstration purposes in a script, you might access it like:
const openaiApiKey = process.env.OPENAI_API_KEY ?? '';
if (!openaiApiKey) {
console.warn("WARNING: OPENAI_API_KEY environment variable is not set. The MCP server will fail to generate tests.\nPlease set it before starting the server.");
}
console.log("\n2. Install dependencies for the Playwright MCP Server:");
console.log(" $ npm install");
console.log("\n3. Start the Playwright MCP Server (typically in a separate terminal):");
console.log(" $ npm start");
console.log(" (or using npx if globally available: $ npx playwright-mcp-server)");
console.log("\n4. Configure Cursor IDE to use this MCP server. Create or update your project's `.cursor/settings.json` file:");
console.log(JSON.stringify({
"mcpServers": {
"playwright": {
"command": "npx",
"args": [
"playwright-mcp-server"
]
}
}
}, null, 2));
console.log("\nOnce the server is running and Cursor is configured, you can interact with it via Cursor's chat interface, e.g.:");
console.log(" /mcp playwright Create a test on the login page that validates user authentication.");
// Note: This TypeScript quickstart primarily outlines the setup and command execution,
// as the package is a server rather than a library for direct programmatic import.