Skip to main content
The Playwright MCP server provides a programmatic API for integrating browser automation capabilities directly into your Node.js applications. This allows you to embed MCP server functionality without relying on the CLI.

When to Use the API

Use the programmatic API when you need to:
  • Integrate Playwright MCP into custom Node.js services
  • Create HTTP servers with SSE transport for MCP
  • Provide your own browser context management
  • Embed browser automation in existing applications
  • Deploy Playwright MCP as a standalone service

When to Use CLI Instead

Use the CLI approach when:
  • Connecting to MCP clients like Claude Desktop, VS Code, or Cursor
  • Running browser automation through standard MCP configuration
  • You don’t need custom browser context management
  • You want the simplest setup process

Basic Usage Pattern

The programmatic API centers around the createConnection function, which creates an MCP server instance:
import { createConnection } from '@playwright/mcp';
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';

// Create connection with default configuration
const server = await createConnection();

// Create connection with custom configuration
const server = await createConnection({
  browser: {
    launchOptions: { headless: true }
  }
});

Core Components

createConnection Function

The main entry point for creating an MCP server instance. See createConnection for detailed documentation.

Configuration Schema

The Config object allows you to customize browser behavior, server settings, timeouts, and capabilities. See Configuration Schema for all available options.

Tool Capabilities

Control which automation features are enabled by specifying capabilities. See Tool Capabilities for details on available capability groups.

Integration Example

Here’s how to integrate Playwright MCP into an HTTP server with SSE transport:
import http from 'http';
import { createConnection } from '@playwright/mcp';
import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';

http.createServer(async (req, res) => {
  // Create a headless Playwright MCP server with SSE transport
  const connection = await createConnection({
    browser: { launchOptions: { headless: true } }
  });
  const transport = new SSEServerTransport('/messages', res);
  await connection.connect(transport);
}).listen(8931);

Next Steps