@@ -88,15 +88,36 @@ func WithErrorLogger(stdLogger *log.Logger) StdioOption {
8888// NewStdioServer creates a new stdio server wrapper around an MCPServer.
8989// It initializes the server with a default logger that logs to stderr.
9090func NewStdioServer (server * rest.MCPServer , opts ... StdioOption ) * StdioServer {
91- // Create default logger - always use stderr for STDIO servers
92- defaultLogger , err := logging .New (logging.Config {
93- Level : logging .InfoLevel ,
94- Development : true ,
95- OutputPaths : []string {"stderr" }, // Force stderr for all logging output
96- InitialFields : logging.Fields {
97- "component" : "stdio-server" ,
98- },
99- })
91+ // Check for environment variables that indicate logging should be disabled
92+ var defaultLogger * logging.Logger
93+ var err error
94+
95+ disableLogging := os .Getenv ("MCP_DISABLE_LOGGING" ) == "true" ||
96+ os .Getenv ("DISABLE_LOGGING" ) == "true"
97+
98+ if disableLogging {
99+ // Create a logger that sends nothing to stdout
100+ // We'll still log to stderr for critical errors
101+ defaultLogger , err = logging .New (logging.Config {
102+ Level : logging .ErrorLevel , // Only log errors
103+ Development : false ,
104+ OutputPaths : []string {"stderr" }, // Only stderr, never stdout
105+ InitialFields : logging.Fields {
106+ "component" : "stdio-server" ,
107+ },
108+ })
109+ } else {
110+ // Create default logger - always use stderr for STDIO servers
111+ defaultLogger , err = logging .New (logging.Config {
112+ Level : logging .InfoLevel ,
113+ Development : true ,
114+ OutputPaths : []string {"stderr" }, // Force stderr for all logging output
115+ InitialFields : logging.Fields {
116+ "component" : "stdio-server" ,
117+ },
118+ })
119+ }
120+
100121 if err != nil {
101122 // Fallback to a simple default logger if we can't create the structured one
102123 defaultLogger = logging .Default ()
@@ -222,19 +243,30 @@ func ServeStdio(server *rest.MCPServer, opts ...StdioOption) error {
222243
223244 go func () {
224245 sig := <- sigChan
225- s .logger .Info ("Received shutdown signal, stopping server..." , logging.Fields {"signal" : sig .String ()})
246+ // Only log if logging is enabled
247+ if os .Getenv ("MCP_DISABLE_LOGGING" ) != "true" && os .Getenv ("DISABLE_LOGGING" ) != "true" {
248+ s .logger .Info ("Received shutdown signal, stopping server..." , logging.Fields {"signal" : sig .String ()})
249+ }
226250 cancel ()
227251 }()
228252
229- s .logger .Info ("Starting MCP server in stdio mode" )
253+ // Only log if logging is enabled
254+ if os .Getenv ("MCP_DISABLE_LOGGING" ) != "true" && os .Getenv ("DISABLE_LOGGING" ) != "true" {
255+ s .logger .Info ("Starting MCP server in stdio mode" )
256+ }
230257
231258 err := s .Listen (ctx , os .Stdin , os .Stdout )
232259 if err != nil && err != context .Canceled {
260+ // Always log errors, but to stderr
233261 s .logger .Error ("Server exited with error" , logging.Fields {"error" : err })
234262 return err
235263 }
236264
237- s .logger .Info ("Server shutdown complete" )
265+ // Only log if logging is enabled
266+ if os .Getenv ("MCP_DISABLE_LOGGING" ) != "true" && os .Getenv ("DISABLE_LOGGING" ) != "true" {
267+ s .logger .Info ("Server shutdown complete" )
268+ }
269+
238270 return nil
239271}
240272
@@ -307,7 +339,11 @@ func (p *MessageProcessor) Process(ctx context.Context, message string) (interfa
307339 // Check if this is a notification (no ID field)
308340 // Notifications don't require responses
309341 if baseMessage .ID == nil && strings .HasPrefix (baseMessage .Method , "notifications/" ) {
310- p .logger .Info ("Received notification" , logging.Fields {"method" : baseMessage .Method })
342+ // In stdio mode we must be very careful about logging
343+ // Use a custom field to avoid JSON output format
344+ if os .Getenv ("MCP_DISABLE_LOGGING" ) != "true" && os .Getenv ("DISABLE_LOGGING" ) != "true" {
345+ p .logger .Info ("Received notification" , logging.Fields {"method" : baseMessage .Method })
346+ }
311347 // Process notification but don't return a response
312348 return nil , nil
313349 }
@@ -317,7 +353,11 @@ func (p *MessageProcessor) Process(ctx context.Context, message string) (interfa
317353
318354 // Handle notifications with a prefix
319355 if ! exists && strings .HasPrefix (baseMessage .Method , "notifications/" ) {
320- p .logger .Info ("Processed notification" , logging.Fields {"method" : baseMessage .Method })
356+ // In stdio mode we must be very careful about logging
357+ // Use a custom field to avoid JSON output format
358+ if os .Getenv ("MCP_DISABLE_LOGGING" ) != "true" && os .Getenv ("DISABLE_LOGGING" ) != "true" {
359+ p .logger .Info ("Processed notification" , logging.Fields {"method" : baseMessage .Method })
360+ }
321361 return nil , nil
322362 }
323363
0 commit comments