Always consider the destination and lifecycle of output streams to prevent protocol interference, data loss, and unexpected behavior. Choose stdout for application output and stderr for debugging/logging. Avoid accidentally closing streams and be mindful of output pollution in protocol-sensitive contexts.
Always consider the destination and lifecycle of output streams to prevent protocol interference, data loss, and unexpected behavior. Choose stdout for application output and stderr for debugging/logging. Avoid accidentally closing streams and be mindful of output pollution in protocol-sensitive contexts.
Key practices:
{ end: false }
option to prevent closing the target streamprocess.stderr.write
over console.warn
in tests to avoid mock interferenceconsole.error.bind(console)
instead of custom implementations to preserve formattingExample:
// Good: Prevent stdout closure when piping
fileStream.pipe(process.stdout, { end: false })
// Good: Redirect console in protocol contexts
if (process.argv.includes('mcp')) {
console.log = console.error.bind(console)
}
// Good: Use stderr for debug output
console.warn(`${namespace} ${format}`, ...rest)
Enter the URL of a public GitHub repository