@@ -8,16 +8,18 @@ import (
88 "io"
99 "os"
1010 "os/exec"
11+ "strings"
1112 "time"
1213)
1314
1415// Stdio implements the Transport interface by executing a command
1516// and communicating with it via stdin/stdout using JSON-RPC.
1617type Stdio struct {
17- process * stdioProcess
18- command []string
19- nextID int
20- debug bool
18+ process * stdioProcess
19+ command []string
20+ nextID int
21+ debug bool
22+ showServerLogs bool
2123}
2224
2325// stdioProcess reflects the state of a running command.
@@ -50,6 +52,11 @@ func (t *Stdio) SetCloseAfterExecute(v bool) {
5052 }
5153}
5254
55+ // SetShowServerLogs toggles whether to print server logs.
56+ func (t * Stdio ) SetShowServerLogs (v bool ) {
57+ t .showServerLogs = v
58+ }
59+
5360// Execute implements the Transport interface by spawning a subprocess
5461// and communicating with it via JSON-RPC over stdin/stdout.
5562func (t * Stdio ) Execute (method string , params any ) (map [string ]any , error ) {
@@ -72,14 +79,13 @@ func (t *Stdio) Execute(method string, params any) (map[string]any, error) {
7279
7380 if ! process .isInitializeSent {
7481 if initErr := t .initialize (process .stdin , process .stdout ); initErr != nil {
82+ t .printStderr (process )
7583 if t .debug {
7684 fmt .Fprintf (os .Stderr , "DEBUG: Initialization failed: %v\n " , initErr )
77- if process .stderrBuf .Len () > 0 {
78- fmt .Fprintf (os .Stderr , "DEBUG: stderr during init: %s\n " , process .stderrBuf .String ())
79- }
8085 }
8186 return nil , initErr
8287 }
88+ t .printStderr (process )
8389 process .isInitializeSent = true
8490 }
8591
@@ -100,10 +106,10 @@ func (t *Stdio) Execute(method string, params any) (map[string]any, error) {
100106 }
101107
102108 response , err := t .readResponse (process .stdout )
109+ t .printStderr (process )
103110 if err != nil {
104111 return nil , err
105112 }
106-
107113 err = t .closeProcess (process )
108114 if err != nil {
109115 return nil , err
@@ -112,6 +118,22 @@ func (t *Stdio) Execute(method string, params any) (map[string]any, error) {
112118 return response .Result , nil
113119}
114120
121+ // printStderr prints and clears any accumulated stderr output.
122+ func (t * Stdio ) printStderr (process * stdioProcess ) {
123+ if ! t .showServerLogs {
124+ return
125+ }
126+ if process .stderrBuf .Len () > 0 {
127+ for _ , line := range strings .SplitAfter (process .stderrBuf .String (), "\n " ) {
128+ line = strings .TrimSuffix (line , "\n " )
129+ if line != "" {
130+ fmt .Fprintf (os .Stderr , "[>] %s\n " , line )
131+ }
132+ }
133+ process .stderrBuf .Reset () // Clear the buffer after reading
134+ }
135+ }
136+
115137// closeProcess waits for the command to finish, returning any error.
116138func (t * Stdio ) closeProcess (process * stdioProcess ) error {
117139 if t .process != nil {
@@ -130,13 +152,10 @@ func (t *Stdio) closeProcess(process *stdioProcess) error {
130152 case waitErr := <- done :
131153 if t .debug {
132154 fmt .Fprintf (os .Stderr , "DEBUG: Command completed with err: %v\n " , waitErr )
133- if process .stderrBuf .Len () > 0 {
134- fmt .Fprintf (os .Stderr , "DEBUG: stderr output:\n %s\n " , process .stderrBuf .String ())
135- }
136155 }
137156
138157 if waitErr != nil && process .stderrBuf .Len () > 0 {
139- return fmt .Errorf ("command error: %w, stderr: %s " , waitErr , process . stderrBuf . String () )
158+ return fmt .Errorf ("command error: %w" , waitErr )
140159 }
141160 case <- time .After (1 * time .Second ):
142161 if t .debug {
0 commit comments