google-gemini/gemini-cli · error · Error

Failed to initialize chrome-devtools MCP client

Error message

Failed to initialize chrome-devtools MCP client

What it means

Defensive guard in getRawMcpClient(): after awaiting ensureConnection(), rawMcpClient is still undefined. Normally connectMcp() (called via connectWithRetry) assigns this.rawMcpClient; reaching this throw means connection setup returned without ever setting the client — typically a race with close() or an internal logic gap.

Source

Thrown at packages/core/src/agents/browser/browserManager.ts:298

  constructor(private config: Config) {
    const browserConfig = config.getBrowserAgentConfig();
    this.shouldInjectOverlay = !browserConfig?.customConfig?.headless;
    this.shouldDisableInput = config.shouldDisableBrowserUserInput();
    this.maxActionsPerTask =
      browserConfig?.customConfig.maxActionsPerTask ?? 100;
  }

  /**
   * Gets the raw MCP SDK Client for direct tool calls.
   * This client is ISOLATED from the main tool registry.
   */
  async getRawMcpClient(): Promise<Client> {
    if (this.rawMcpClient) {
      return this.rawMcpClient;
    }
    await this.ensureConnection();
    if (!this.rawMcpClient) {
      throw new Error('Failed to initialize chrome-devtools MCP client');
    }
    return this.rawMcpClient;
  }

  /**
   * Gets the tool definitions discovered from the MCP server.
   * These are dynamically fetched from chrome-devtools-mcp.
   */
  async getDiscoveredTools(): Promise<McpTool[]> {
    await this.ensureConnection();
    return this.discoveredTools;
  }

  /**
   * Calls a tool on the MCP server.
   *
   * @param toolName The name of the tool to call
   * @param args Arguments to pass to the tool

View on GitHub (pinned to 5024443c72)

Solutions

  1. Avoid issuing browser tool calls while the session/CLI is being torn down; drain in-flight calls before close().
  2. If seen persistently, check that connectMcp() is not swallowing the assignment — ensure this.rawMcpClient is set before any awaited step that can throw.
  3. Retry the call after the connection settles; ensureConnection() will re-run connectWithRetry.
  4. Report as an internal bug if reproducible, including the chrome-devtools-mcp stderr from debug logs.
Defensive patterns

Strategy: retry

Validate before calling

// Ensure connection is healthy before relying on the client.
async function safeGetClient(bm) {
  if (!bm.isConnected()) await bm.ensureConnection();
  return bm.getRawMcpClient();
}

Try / catch

try {
  const client = await bm.getRawMcpClient();
} catch (e) {
  if (e instanceof Error && /Failed to initialize chrome-devtools MCP client/.test(e.message)) {
    // connection raced with close(); reconnect and retry once
    await bm.ensureConnection();
    return bm.getRawMcpClient();
  }
  throw e;
}

Prevention

When it happens

Trigger: getRawMcpClient() is called when isConnected() is false, ensureConnection() runs, but during the connect sequence close() was invoked concurrently (setting rawMcpClient=undefined), or connectMcp threw in a way that left state inconsistent yet connectWithRetry resolved.

Common situations: BrowserManager.close() racing a pending tool call (e.g. /clear or CLI exit during an in-flight browser action); a reconnect cycle interrupted by teardown; very rare under normal use — usually indicates a lifecycle bug or concurrent shutdown.

Related errors


AI-assisted analysis of google-gemini/gemini-cli@5024443c72 (2026-08-12). Data as JSON: /api/errors/62a1341542b03d53. Report an issue: GitHub.