Chrome DevTools for coding agents
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import '../utils/polyfill.js';
import process from 'node:process';
import {BrowserManager} from '../BrowserManager.js';
import {McpServer} from '../index.js';
import {ClearcutLogger} from '../telemetry/ClearcutLogger.js';
import {computeFlagUsage} from '../telemetry/flagUtils.js';
import {StdioServerTransport} from '../third_party/index.js';
import {checkForUpdates} from '../utils/check-for-updates.js';
import {logDisclaimers} from '../utils/disclaimers.js';
import {logger, saveLogsToFile} from '../utils/logger.js';
import {setupUnhandledRejectionHandler} from '../utils/errorHandling.js';
import {VERSION} from '../version.js';
import {mcpOptions} from '../config/mcp-options.js';
import {ConfigParser} from '../config/ConfigParser.js';
await checkForUpdates(
'Run `npm install chrome-devtools-mcp@latest` to update.',
);
const configParser = new ConfigParser(VERSION);
const serverConfig = configParser.parseConfig();
const args = serverConfig.resolved;
const logFile = args.logFile ? saveLogsToFile(args.logFile) : undefined;
setupUnhandledRejectionHandler(() => {
process.exit(1);
});
logger?.(`Starting Chrome DevTools MCP Server v${VERSION}`);
const browserManager = new BrowserManager(serverConfig, {
logFile,
});
const serverPromise = McpServer.from(serverConfig, {
browserManager,
});
// Shutdown on stdin EOF (stdio MCP convention — the client closes the
// transport to signal exit) and on standard termination signals. Without
// this, an active Chrome subprocess keeps the Node event loop ref'd after
// stdin closes and the server hangs until something else kills it.
let shuttingDown = false;
async function shutdown(reason: string): Promise<void> {
if (shuttingDown) {
return;
}
shuttingDown = true;
logger?.(`Shutting down (${reason})`);
// Backstop in case browser teardown hangs (e.g. unresponsive Chrome,
// slow beforeunload handlers, many tabs). Exits 0 because we still
// honored the shutdown request; the log line preserves observability.
// Unref'd so it doesn't keep the loop alive on the clean path.
setTimeout(() => {
logger?.('Shutdown timeout exceeded, forcing exit');
process.exit(0);
}, 5000).unref();
const server = await serverPromise;
await server.close();
process.exit(0);
}
process.stdin.on('end', () => {
void shutdown('stdin end');
});
process.stdin.on('close', () => {
void shutdown('stdin close');
});
process.on('SIGTERM', () => {
void shutdown('SIGTERM');
});
process.on('SIGINT', () => {
void shutdown('SIGINT');
});
process.on('SIGHUP', () => {
void shutdown('SIGHUP');
});
const server = await serverPromise;
const transport = new StdioServerTransport();
await server.connect(transport);
logger?.('Chrome DevTools MCP Server connected');
logDisclaimers(args);
void ClearcutLogger.get()?.logDailyActiveIfNeeded();
void ClearcutLogger.get()?.logServerStart(computeFlagUsage(args, mcpOptions));