ScreenshotNeo

BlogGuides

Playwright MCP Server in Java

Use Playwright MCP with Java projects: configure the Node server, add Playwright Java, generate Java code, and troubleshoot real deployments.

By the ScreenshotNeo team1 October 20267 min read

Short answer: there is no separately documented Java implementation of the Playwright MCP server. The official server runs as the Node.js package @playwright/mcp. A Java developer normally runs that MCP server from an MCP client, while using the Playwright Java Maven library for application and test code. The MCP server can inspect pages through structured accessibility snapshots and can generate Java snippets with --codegen java.

What “Playwright MCP in Java” means

Playwright MCP exposes browser automation through the Model Context Protocol (MCP), allowing an AI client to inspect roles, names and text in structured accessibility snapshots rather than relying only on screenshots. The server process is Node.js; your maintained automation code can still be Java.

Concern Recommended choice
MCP server runtime Node.js 20 or newer with npx @playwright/mcp@latest
Java automation code Maven dependency com.microsoft.playwright:playwright
AI connection stdio for a local client, or standalone HTTP at /mcp
Browser mode Headless for CI and workers; headed for local debugging
Code ownership Use generated Java as a starting point, then maintain it in your project

See the official Playwright MCP documentation, the Playwright Java guide, and the Playwright MCP repository for current options. Package versions change; the Java documentation displayed 1.63.0 on 2026-09-29, so verify the current version before publishing a build.

Prerequisites

  1. Install Node.js 20 or newer on the machine that will host MCP.
  2. Install a compatible MCP client such as Codex, VS Code, Cursor, Claude Code or another client with MCP support.
  3. Install Java and Maven for your project.
  4. Decide whether the browser runs headed or headless and which engine you need: Chromium, Chrome, Firefox, WebKit or Microsoft Edge.

Configure the MCP server over stdio

Most desktop MCP clients start the server on demand. Add this entry to the client’s MCP configuration:

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest"]
    }
  }
}

For CI or a remote worker, add --headless. To select a browser explicitly, include one of the supported browser options in the argument list, for example:

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest", "--headless", "--browser", "firefox"]
    }
  }
}

Use the client’s documented configuration location; the JSON shape is consistent, but file locations differ between clients.

Run Playwright MCP as a standalone HTTP server

When the AI client is separate from the machine that launches the browser, start the server on a port:

npx @playwright/mcp@latest --port 8931

Configure the client to connect to:

http://localhost:8931/mcp

Restrict network access to trusted clients. The Playwright MCP repository warns that MCP is not a security boundary; enforce permissions, allowed hosts, secrets handling and file access at the client and deployment layers.

Add Playwright Java to your Maven project

Add the Playwright Java module to pom.xml. Pin a version that you have checked in the current documentation or Maven repository:

<dependency>
  <groupId>com.microsoft.playwright</groupId>
  <artifactId>playwright</artifactId>
  <version>1.63.0</version>
</dependency>

The version above is the value shown in the reviewed documentation on 2026-09-29 and is mutable. Update it deliberately and keep the browser binaries aligned with the library version.

Complete Java example

This program launches Chromium, opens a page, reads its title and writes a screenshot. It is independent of the MCP server and is suitable for a test or application module.

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class Main {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch(
          new BrowserType.LaunchOptions().setHeadless(true));
      Page page = browser.newPage();
      page.navigate("https://example.com");
      System.out.println(page.title());
      page.screenshot(new Page.ScreenshotOptions()
          .setPath(java.nio.file.Paths.get("example.png"))
          .setFullPage(true));
      browser.close();
    }
  }
}

Run it with your normal Maven compile and execution workflow. In CI, install the browser binaries required by the Playwright Java release and cache them between jobs when your runner permits it.

Generate Java code from MCP-assisted exploration

The MCP repository supports Java code generation. Add the codegen option to the server arguments:

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest", "--codegen", "java"]
    }
  }
}

Generated code captures the interaction sequence you explore through the AI client. Treat it as scaffolding: replace brittle selectors, add assertions, isolate test data and set explicit timeouts before committing it.

How the Java and MCP pieces fit together

  1. The MCP client starts or connects to the Node.js server.
  2. The server launches a browser and exposes tools to the model.
  3. The model uses accessibility snapshots and browser actions to inspect or manipulate a page.
  4. You ask for Java code or copy a generated flow into the Maven project.
  5. Your Java process runs the maintained flow directly through com.microsoft.playwright.

Configuration choices that affect reliability

  • Headless: use --headless for workers and CI. Use headed mode locally when diagnosing layout, authentication or timing problems.
  • Browser: choose Chromium, Chrome, Firefox, WebKit or Edge explicitly when cross-browser behavior matters.
  • Transport: stdio has the smallest local setup; HTTP is useful when the client and browser host are separate.
  • Selectors: prefer accessible roles, labels and stable test IDs. Avoid generated CSS classes and positional selectors.
  • Timeouts: set navigation and action timeouts appropriate to your site. Waiting for a fixed delay alone makes runs slower and less deterministic.
  • Isolation: create a fresh browser context for independent tests, and keep credentials in the client or deployment secret store.

Troubleshooting

Symptom Likely cause Fix
npx cannot find the package Node.js is missing, too old or unavailable on PATH Install Node.js 20+, restart the client and run node --version.
Client shows no Playwright tools Malformed MCP JSON or a server process that exits immediately Validate the JSON, run npx @playwright/mcp@latest in a terminal and inspect the client log.
HTTP client cannot connect Server is not listening on port 8931, or a firewall blocks it Start with --port 8931, use the exact /mcp path and allow only the required network route.
Browser executable is missing Playwright browser binaries were not installed or are from another release Install the browsers required by your Playwright version and keep the Java dependency and browser cache aligned.
Actions time out Page is still loading, a selector is unstable, or the target is inside a different frame Use a role or test ID, wait for the intended state, handle frames explicitly and capture a trace or screenshot while debugging.
Java compilation fails on Playwright imports Maven dependency is absent or version resolution failed Check the coordinates, run Maven with dependency updates enabled and confirm the resolved version.
Generated Java is flaky Codegen recorded incidental timing or unstable selectors Refactor selectors, add assertions and condition-based waits, and remove unnecessary sleeps.

Performance, reliability and cost

The MCP server adds a Node process and an AI interaction layer; direct Java Playwright runs are usually the simpler path for repeatable, high-volume tests. Use MCP for exploration, diagnosis and natural-language browser control, then move stable workflows into versioned Java code. Headless mode reduces desktop dependencies. Browser startup and page navigation dominate many short runs, so reuse a browser where isolation rules allow it and create separate contexts for test data.

There is no independent performance, reliability or adoption figure in the official material reviewed for this guide. Measure your own pages, browser engine and CI runner before setting timeouts or capacity targets.

Or skip the browser setup

If your goal is a clean image or PDF rather than an interactive browser session, ScreenshotNeo provides a one-request screenshot API and an MCP server. It removes cookie banners, newsletter popups and chat widgets before capture. Bot checks, blank pages, failed loads, timeouts and cache hits are not billed, and the response identifies the result with X-Page-Verdict and X-Billed headers. Its MCP tools include take_screenshot, get_page_info and capture_pdf, so AI agents can request captures directly.

See the ScreenshotNeo API documentation for all options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

ScreenshotNeo includes full-page and element capture, device presets, custom viewports, retina scale, dark mode, PDF controls, custom CSS and JavaScript, waits, request blocking, headers, cookies, user agents, geolocation, caching, signed links, async webhooks, bulk capture and a usage API. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. Create a free ScreenshotNeo account.

FAQ

Can I run the MCP server entirely inside a Java process?

The official setup documents a Node.js server. Run it as a separate stdio or HTTP process and keep Java as the automation client when you need maintained Java code.

Does MCP replace Playwright Java?

No. MCP is an AI-facing control interface; Playwright Java is the library your Java code imports and executes.

Can MCP work without screenshots?

Yes. Its structured accessibility snapshots let the model reason over roles, names and text. Screenshots remain useful for visual verification and debugging.

Which transport should a team choose?

Choose stdio for a local desktop client. Choose standalone HTTP when the browser host and MCP client are separate and you can secure the network path.