Skip to content

Chrome DevTools for coding agents

fix: detect X server display on Linux (#1027)

Alex Rudenko1746ed9ee11c
diff --git a/src/browser.ts b/src/browser.ts
index 64db1568..3fb79a05 100644
--- a/src/browser.ts
+++ b/src/browser.ts
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
+import {execSync} from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
@@ -148,6 +149,24 @@ interface McpLaunchOptions {
enableExtensions?: boolean;
}
+export function detectDisplay(): void {
+ // Only detect display on Linux/UNIX.
+ if (os.platform() === 'win32' || os.platform() === 'darwin') {
+ return;
+ }
+ if (!process.env['DISPLAY']) {
+ try {
+ const result = execSync(
+ `ps -u $(id -u) -o pid= | xargs -I{} cat /proc/{}/environ 2>/dev/null | tr '\\0' '\\n' | grep -m1 '^DISPLAY=' | cut -d= -f2`,
+ );
+ const display = result.toString('utf8').trim();
+ process.env['DISPLAY'] = display;
+ } catch {
+ // no-op
+ }
+ }
+}
+
export async function launch(options: McpLaunchOptions): Promise<Browser> {
const {channel, executablePath, headless, isolated} = options;
const profileDirName =
@@ -189,6 +208,10 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
: 'chrome';
}
+ if (!headless) {
+ detectDisplay();
+ }
+
try {
const browser = await puppeteer.launch({
channel: puppeteerChannel,
diff --git a/tests/browser.test.ts b/tests/browser.test.ts
index aad6eec9..b0835bf9 100644
--- a/tests/browser.test.ts
+++ b/tests/browser.test.ts
@@ -11,9 +11,13 @@ import {describe, it} from 'node:test';
import {executablePath} from 'puppeteer';
-import {ensureBrowserConnected, launch} from '../src/browser.js';
+import {detectDisplay, ensureBrowserConnected, launch} from '../src/browser.js';
describe('browser', () => {
+ it('detects display does not crash', () => {
+ detectDisplay();
+ });
+
it('cannot launch multiple times with the same profile', async () => {
const tmpDir = os.tmpdir();
const folderPath = path.join(tmpDir, `temp-folder-${crypto.randomUUID()}`);