Skip to content

Commit 16b80f1

Browse files
authored
feat: capture and log container stderr when MCP connection fails (#615)
When a container/process fails to launch, the stderr output is now captured and logged to help diagnose issues. This is particularly useful for debugging container startup failures where the error message is written to stderr. The SDK's CommandTransport only uses stdin/stdout for the MCP protocol, so we can safely capture stderr separately for debugging purposes.
2 parents 4fd4817 + fe8c3ad commit 16b80f1

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

internal/mcp/connection.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ func NewConnection(ctx context.Context, command string, args []string, env map[s
190190
}
191191
}
192192

193+
// Capture stderr to help diagnose container failures
194+
// The SDK's CommandTransport only uses stdin/stdout for MCP protocol,
195+
// so we can capture stderr separately for debugging
196+
var stderrBuf bytes.Buffer
197+
cmd.Stderr = &stderrBuf
198+
193199
logger.LogInfo("backend", "Starting MCP backend server, command=%s, args=%v", command, sanitize.SanitizeArgs(expandedArgs))
194200
log.Printf("Starting MCP server command: %s %v", command, sanitize.SanitizeArgs(expandedArgs))
195201
transport := &sdk.CommandTransport{Command: cmd}
@@ -208,6 +214,16 @@ func NewConnection(ctx context.Context, command string, args []string, env map[s
208214
log.Printf(" Args: %v", sanitize.SanitizeArgs(expandedArgs))
209215
log.Printf(" Error: %v", err)
210216

217+
// Log captured stderr output from the container/process
218+
stderrOutput := strings.TrimSpace(stderrBuf.String())
219+
if stderrOutput != "" {
220+
logger.LogErrorMd("backend", "MCP backend stderr output:\n%s", stderrOutput)
221+
log.Printf(" 📋 Container/Process stderr output:")
222+
for _, line := range strings.Split(stderrOutput, "\n") {
223+
log.Printf(" %s", line)
224+
}
225+
}
226+
211227
// Check if it's a command not found error
212228
if strings.Contains(err.Error(), "executable file not found") ||
213229
strings.Contains(err.Error(), "no such file or directory") {

0 commit comments

Comments
 (0)