genkit icon indicating copy to clipboard operation
genkit copied to clipboard

fix(cli): genkit mcp runs first-run analytics prompt on stdin/stdout unless --non-interactive is set

Open MichaelDoyle opened this issue 1 month ago • 1 comments

Description

When starting the Genkit MCP server via genkit mcp in a fresh environment (such as a container, CI runner, or evaluation harness where ~/.config/configstore/genkit-tools.json does not yet have analytics_notification: true), the MCP stdio handshake (initialize) fails with:

MCP load failed for genkit: Starting MCP server in: /workspace
Genkit MCP Server running on stdio
: connection closed: calling "initialize": client is closing: invalid character 'G' looking for beginning of value

Root Cause

genkit-tools/cli/src/commands/mcp.ts already calls forceStderr() (added in #3132) inside its .action() handler so logger output (Starting MCP server in: ..., Genkit MCP Server running on stdio) is routed to stderr.

However, before mcp's .action() handler runs, startCLI() in genkit-tools/cli/src/cli.ts executes its preAction hook:

if (
  !process.argv.includes('--non-interactive') &&
  commandName !== 'config'
) {
  await notifyAnalyticsIfFirstRun();
}

In genkit-tools/common/src/utils/analytics.ts, notifyAnalyticsIfFirstRun() writes directly to process.stdout via console.log() and opens an interactive readline prompt on process.stdin / process.stdout:

console.log(ANALYTICS_NOTIFICATION); // Starts with "Genkit CLI and Developer UI use cookies..."

const readline = createInterface({
  input: process.stdin,
  output: process.stdout,
});
await readline.question('Press "Enter" to acknowledge and continue');
readline.close();

When an MCP client spawns genkit mcp and sends its newline-delimited initialize JSON-RPC request on stdin:

  1. console.log(ANALYTICS_NOTIFICATION) writes "Genkit CLI and Developer UI use cookies..." to stdout.
  2. readline.question(...) consumes the MCP client's {"jsonrpc":"2.0","id":1,"method":"initialize",...} frame from stdin as the user's "Enter" acknowledgement, and then mcp.ts starts up and logs "Starting MCP server in: /workspace\nGenkit MCP Server running on stdio" to stderr.
  3. The MCP client tries to parse the first line on stdout ("Genkit CLI and Developer UI use cookies..."), fails on the leading 'G' (invalid character 'G' looking for beginning of value), and prints the captured stderr alongside the stdout parse error.

Additionally, showUpdateNotification() in cli.ts runs before mcp's forceStderr() unless --no-update-notification is passed.

Workaround

Pass --non-interactive and --no-update-notification in the MCP server config:

{
  "command": "genkit",
  "args": ["mcp", "--non-interactive", "--no-update-notification"]
}

Proposed Fix

In genkit-tools/cli/src/cli.ts:

  1. Exclude 'mcp' from notifyAnalyticsIfFirstRun() in the preAction hook (just like 'config'), since genkit mcp is a JSON-RPC stdio server and cannot interactively prompt on stdin/stdout:
    if (
      !process.argv.includes('--non-interactive') &&
      commandName !== 'config' &&
      commandName !== 'mcp'
    ) {
      await notifyAnalyticsIfFirstRun();
    }
    
  2. Skip showUpdateNotification() (or call forceStderr() before it) when running the mcp subcommand so stdout is strictly reserved for StdioServerTransport JSON-RPC messages.

MichaelDoyle avatar Oct 21 '26 18:10 MichaelDoyle

Triage: P2 · XS · implement: startCLI in genkit-tools/cli/src/cli.ts runs notifyAnalyticsIfFirstRun and showUpdateNotification before the mcp command action, corrupting the MCP stdio JSON-RPC stream.

Area: genkit-tools

Root cause: In genkit-tools/cli/src/cli.ts, startCLI() runs showUpdateNotification() before mcp's action calls forceStderr(), and its preAction hook calls notifyAnalyticsIfFirstRun() unless --non-interactive is passed or commandName === 'config'. Because notifyAnalyticsIfFirstRun() writes to stdout and reads from stdin, it corrupts the MCP stdio JSON-RPC stream on first run.

Plan:

  • Update startCLI() in genkit-tools/cli/src/cli.ts to skip notifyAnalyticsIfFirstRun() and showUpdateNotification() when running the mcp command.
  • Add a unit test in genkit-tools/cli/tests/cli_test.ts and run pnpm --dir genkit-tools/cli test to verify genkit mcp does not trigger first-run analytics prompts or update notifications without --non-interactive.
Details
  • genkit-tools/cli/src/commands/mcp.ts calls forceStderr() inside .action(), which executes after showUpdateNotification() (lines 146-156 of genkit-tools/cli/src/cli.ts) and the preAction hook (lines 93-141 of genkit-tools/cli/src/cli.ts).
  • genkit-tools/common/src/utils/analytics.ts (notifyAnalyticsIfFirstRun, lines 183-202) calls console.log(ANALYTICS_NOTIFICATION) and opens a readline prompt on process.stdin and process.stdout when analytics_notification is not yet set in configstore.
  • warnIfLegacyGenkitContext in genkit-tools/cli/src/cli.ts (lines 120-127) already checks commandName !== 'mcp' and process.argv[2] !== SERVER_HARNESS_COMMAND to avoid polluting non-interactive streams.

Opened #7256 (run 75f4cf53-e6f1-4eaa-a130-d8ffef959b9f, Cloud Run logs)

genkit-bot avatar Nov 18 '26 15:11 genkit-bot