fix(cli): genkit mcp runs first-run analytics prompt on stdin/stdout unless --non-interactive is set
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:
console.log(ANALYTICS_NOTIFICATION)writes"Genkit CLI and Developer UI use cookies..."tostdout.readline.question(...)consumes the MCP client's{"jsonrpc":"2.0","id":1,"method":"initialize",...}frame fromstdinas the user's "Enter" acknowledgement, and thenmcp.tsstarts up and logs"Starting MCP server in: /workspace\nGenkit MCP Server running on stdio"tostderr.- 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 capturedstderralongside thestdoutparse 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:
- Exclude
'mcp'fromnotifyAnalyticsIfFirstRun()in thepreActionhook (just like'config'), sincegenkit mcpis a JSON-RPCstdioserver and cannot interactively prompt onstdin/stdout:if ( !process.argv.includes('--non-interactive') && commandName !== 'config' && commandName !== 'mcp' ) { await notifyAnalyticsIfFirstRun(); } - Skip
showUpdateNotification()(or callforceStderr()before it) when running themcpsubcommand sostdoutis strictly reserved forStdioServerTransportJSON-RPC messages.
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()ingenkit-tools/cli/src/cli.tsto skipnotifyAnalyticsIfFirstRun()andshowUpdateNotification()when running themcpcommand. - Add a unit test in
genkit-tools/cli/tests/cli_test.tsand runpnpm --dir genkit-tools/cli testto verifygenkit mcpdoes not trigger first-run analytics prompts or update notifications without--non-interactive.
Details
genkit-tools/cli/src/commands/mcp.tscallsforceStderr()inside.action(), which executes aftershowUpdateNotification()(lines 146-156 ofgenkit-tools/cli/src/cli.ts) and thepreActionhook (lines 93-141 ofgenkit-tools/cli/src/cli.ts).genkit-tools/common/src/utils/analytics.ts(notifyAnalyticsIfFirstRun, lines 183-202) callsconsole.log(ANALYTICS_NOTIFICATION)and opens areadlineprompt onprocess.stdinandprocess.stdoutwhenanalytics_notificationis not yet set inconfigstore.warnIfLegacyGenkitContextingenkit-tools/cli/src/cli.ts(lines 120-127) already checkscommandName !== 'mcp'andprocess.argv[2] !== SERVER_HARNESS_COMMANDto avoid polluting non-interactive streams.
Opened #7256 (run 75f4cf53-e6f1-4eaa-a130-d8ffef959b9f, Cloud Run logs)