@@ -566,9 +566,32 @@ const runDaemonSession = (input: {
566566// Stdio MCP session
567567// ---------------------------------------------------------------------------
568568
569+ const withStdoutReroutedToStderr = async < A > ( body : ( ) => Promise < A > ) : Promise < A > => {
570+ const originalWrite = process . stdout . write ;
571+ const originalLog = console . log ;
572+ const originalInfo = console . info ;
573+ const originalDebug = console . debug ;
574+ const stderrWrite = process . stderr . write . bind ( process . stderr ) ;
575+
576+ process . stdout . write = ( ( ...args : Parameters < typeof process . stdout . write > ) =>
577+ stderrWrite ( ...args ) ) as typeof process . stdout . write ;
578+ console . log = console . error . bind ( console ) ;
579+ console . info = console . error . bind ( console ) ;
580+ console . debug = console . error . bind ( console ) ;
581+
582+ try {
583+ return await body ( ) ;
584+ } finally {
585+ process . stdout . write = originalWrite ;
586+ console . log = originalLog ;
587+ console . info = originalInfo ;
588+ console . debug = originalDebug ;
589+ }
590+ } ;
591+
569592const runStdioMcpSession = ( ) =>
570593 Effect . gen ( function * ( ) {
571- const executor = yield * Effect . promise ( ( ) => getExecutor ( ) ) ;
594+ const executor = yield * Effect . promise ( ( ) => withStdoutReroutedToStderr ( ( ) => getExecutor ( ) ) ) ;
572595 yield * Effect . promise ( ( ) =>
573596 runMcpStdioServer ( { executor, codeExecutor : makeQuickJsExecutor ( ) } ) ,
574597 ) ;
0 commit comments