fix(mcp): silence the daemon-attach log by default (#618) (#725)

The "Attached to shared daemon" line is benign INFO, but it was written to
stderr — and MCP hosts render all server stderr at error level (and append an
`undefined` data field), so on every session start a healthy attach showed up
as `[error] … undefined`. It is now gated behind CODEGRAPH_MCP_LOG_ATTACH=1:
silent by default, opt-in for debugging daemon attach. Both attach sites
(runProxy + connectWithHello) route through one helper. The daemon integration
tests opt the harness into the log so their attach assertions still observe a
successful attach.

Re-applies the approach from #640 by @mturac.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Colby Mchenry
2026-06-08 01:25:21 -04:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 7fd8b4c185
commit 10defecc4b
4 changed files with 65 additions and 7 deletions
+22 -6
View File
@@ -32,6 +32,26 @@ import type { MCPEngine } from './engine';
/** Default poll cadence for the PPID watchdog (same as the direct server). */
const DEFAULT_PPID_POLL_MS = 5000;
/**
* Env var that opts INTO the "attached to shared daemon" log line. Off by
* default: the line is benign INFO, but MCP hosts render any server stderr at
* error level (and append an `undefined` data field), so on every session start
* a healthy attach showed up as `[error] … undefined`. Set to `1` to surface it
* when debugging daemon attach. (#618; approach from #640 by @mturac)
*/
const LOG_ATTACH_ENV = 'CODEGRAPH_MCP_LOG_ATTACH';
/**
* Log a successful daemon attach — gated behind {@link LOG_ATTACH_ENV} so it is
* silent by default (see #618). Exported for tests.
*/
export function logAttachedDaemon(socketPath: string, hello: DaemonHello): void {
if (process.env[LOG_ATTACH_ENV] !== '1') return;
process.stderr.write(
`[CodeGraph MCP] Attached to shared daemon on ${socketPath} (pid ${hello.pid}, v${hello.codegraph}).\n`
);
}
export interface ProxyResult {
/**
* `proxied` — successfully attached to a same-version daemon and piped
@@ -89,9 +109,7 @@ export async function runProxy(
return { outcome: 'fallback-needed', reason: 'version mismatch' };
}
process.stderr.write(
`[CodeGraph MCP] Attached to shared daemon on ${socketPath} (pid ${hello.pid}, v${hello.codegraph}).\n`
);
logAttachedDaemon(socketPath, hello);
sendClientHello(socket);
startPpidWatchdog(socket);
@@ -130,9 +148,7 @@ export async function connectWithHello(
socket.destroy();
return 'version-mismatch';
}
process.stderr.write(
`[CodeGraph MCP] Attached to shared daemon on ${socketPath} (pid ${hello.pid}, v${hello.codegraph}).\n`
);
logAttachedDaemon(socketPath, hello);
sendClientHello(socket);
return socket;
}