fix(mcp): redact secrets from what user code prints in the e2e mcp process
Stacked on #815. Base is oskar/fix-mcp-error-redaction; retarget to main once #815 merges.
Project tools and the config's top-level code run inside the e2e mcp process. Whatever they wrote to stdout or stderr reached the operator's stderr verbatim, configured secrets included. e2e run already redacts the same output in its workers (StreamRedactor over processSecrets in packages/e2e/src/run/worker/entry.ts), but src/cli/mcp.ts only moved stdout writes over to stderr. The protocol stream itself was never corrupted, since stdout writes already went to stderr.
claimStdio(packages/e2e/src/cli/mcp.ts) patchesprocess.stdoutandprocess.stderr, so every write goes throughMcpOutput(packages/e2e/src/mcp/output.ts): oneStreamRedactoroverprocessSecrets(the ledger #815 fills), shared by both streams because they land on one stderr. The protocol keeps the saved stdout writer, and the server's own log lines, already redacted at the source, go out as written.- Fails closed while a config loads. The config's top-level code can print a secret before the process knows it is one, so
SessionHostwraps load plusregisterStaticSecretsinwithholdOutput. Output is held until the load finishes, then released redacted. If the load fails, the config's secrets are unknown, so the held output is dropped and awithheld N byteswarning is logged in its place. - An unfinished line is released, redacted, once no tool call is in flight rather than at exit. A concurrent call may be partway through a value. Backpressure still reaches the writer:
writereturns stderr's answer, and a writer waiting on stdout getsdrainwhen stderr drains. - An uncaught error or unhandled rejection (a project tool's unawaited promise) used to go out raw on fd 2, and the crash left the session's app process running. It is now logged redacted and held like user output during a config load. The server then shuts down the same way it does on a signal: sessions close, apps stop, exit code 1.
Out of scope: a child process that inherits the server's stdio writes straight to the descriptors, which docs/reference/mcp.mdx now notes. A config's top-level output in the e2e run main process is also unredacted. That is a separate gap and its own change.
Verified
Ran it locally: yes
packages/e2e/tests/integration/mcp-user-output.test.tsdrives the builte2e mcpover a real stdio client. The project has a config that prints a secret at top level, a tool that prints it throughconsole.log,console.error, splitprocess.stdout.writes, and a write with no trailing newline, a tool that leaves a rejection unawaited, and a config that prints the secret and then throws. Both cases fail on #815's head and pass here. Mutation-checked: drop the load hold, the stderr patch, the failed-load withhold, live redaction, or the per-call release, and a case fails.tests/unit/mcp-output.test.tscovers concurrent loads where one fails, shutdown during a load, and a tail split across writes.- Ran the built CLI on the testbed through an MCP stdio client (
open_sessionon a broken config, then on a config with credentialadmin/verify-pw-42and project toolsseed_adminandstray, on port 4391), comparingmainto this branch. stderr:
| main | this branch |
|---|---|
[broken config] admin:verify-pw-42 | e2e mcp: [warning] withheld 35 bytes of output printed while a config failed to load: ... |
[config] seeding admin with verify-pw-42 | [config] seeding admin with <secret:admin.password> |
[tool] POST /seed admin:verify-pw-42 | [tool] POST /seed admin:<secret:admin.password> |
[tool] stdout admin:verify-pw-42 | [tool] stdout admin:<secret:admin.password> |
[tool] stderr admin:verify-pw-42 | [tool] stderr admin:<secret:admin.password> |
Error: login failed for admin:verify-pw-42 (Node crash, testbed app left listening on 4391) | e2e mcp: [error] uncaught: Error: login failed for admin:<secret:admin.password> then Session ... closed (server shutdown), port free |