From eb62f6223aa685ae4b56da6c5d2f6f4a0ecce52f Mon Sep 17 00:00:00 2001 From: "Tommy D. Rossi" Date: Sun, 28 Dec 2025 19:01:08 +0100 Subject: [PATCH] allow changing port and host via options and env --- README.md | 14 +++++++++++++- playwriter/src/cli.ts | 24 ++++++++++++++++++------ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index bde38bb..d8cc51f 100644 --- a/README.md +++ b/README.md @@ -185,18 +185,30 @@ Run agents in isolated environments (devcontainers, VMs, SSH) while controlling ```bash npx playwriter serve --token +# or use environment variable: +PLAYWRITER_TOKEN= npx playwriter serve ``` **In container/VM (where agent runs):** +Using environment variables: + ```bash -export PLAYWRITER_URL="ws://host.docker.internal:19988?token=" +export PLAYWRITER_HOST="host.docker.internal" +export PLAYWRITER_TOKEN="" +``` + +Or using CLI options: + +```bash +npx playwriter --host host.docker.internal --token ``` Use `host.docker.internal` for devcontainers, or your host's IP for VMs/SSH. ## Known Issues +- **Do not use `bunx` to run the MCP.** Bun has incomplete support for the `ws` library's WebSocket events, which breaks playwright-core's CDP connection. Use `npx` instead. See bun issues: [#5951](https://github.com/oven-sh/bun/issues/5951), [#9911](https://github.com/oven-sh/bun/issues/9911) - If all pages urls return `about:blank` in every MCP session restart your Chrome browser. This seems to be a Chrome bug that sometimes happen. It is some hidden state in `chrome.debugger` Extensions API. Restarting the extension worker does not fix it. - When connecting the MCP to a page, the browser may switch to light mode. This happens because Playwright, via CDP, automatically sends an "emulate media" command on start. If you'd like to see this behavior changed, you can upvote the related issue [here](https://github.com/microsoft/playwright/issues/37627). diff --git a/playwriter/src/cli.ts b/playwriter/src/cli.ts index 6d6fe64..5fa5f58 100644 --- a/playwriter/src/cli.ts +++ b/playwriter/src/cli.ts @@ -11,16 +11,28 @@ const cli = cac('playwriter') cli .command('', 'Start the MCP server (default)') - .action(async () => { + .option('--host ', 'Remote relay server host to connect to (or use PLAYWRITER_HOST env var)') + .option('--token ', 'Authentication token (or use PLAYWRITER_TOKEN env var)') + .action(async (options: { host?: string; token?: string }) => { const { startMcp } = await import('./mcp.js') - await startMcp() + await startMcp({ + host: options.host, + token: options.token, + }) }) cli .command('serve', 'Start the CDP relay server for remote MCP connections') .option('--host ', 'Host to bind to', { default: '0.0.0.0' }) - .option('--token ', 'Authentication token for /cdp/* endpoints') + .option('--token ', 'Authentication token (or use PLAYWRITER_TOKEN env var)') .action(async (options: { host: string; token?: string }) => { + const token = options.token || process.env.PLAYWRITER_TOKEN + if (!token) { + console.error('Error: Authentication token is required.') + console.error('Provide --token or set PLAYWRITER_TOKEN environment variable.') + process.exit(1) + } + const logger = createFileLogger() process.title = 'playwriter-serve' @@ -38,19 +50,19 @@ cli const server = await startPlayWriterCDPRelayServer({ port: RELAY_PORT, host: options.host, - token: options.token, + token, logger, }) console.log('Playwriter CDP relay server started') console.log(` Host: ${options.host}`) console.log(` Port: ${RELAY_PORT}`) - console.log(` Token: ${options.token ? '(configured)' : '(none)'}`) + console.log(` Token: (configured)`) console.log(` Logs: ${logger.logFilePath}`) console.log('') console.log('Endpoints:') console.log(` Extension: ws://${options.host}:${RELAY_PORT}/extension`) - console.log(` CDP: ws://${options.host}:${RELAY_PORT}/cdp/${options.token ? '?token=' : ''}`) + console.log(` CDP: ws://${options.host}:${RELAY_PORT}/cdp/?token=`) console.log('') console.log('Press Ctrl+C to stop.')