feat: capture and log container stderr when MCP connection fails#615
feat: capture and log container stderr when MCP connection fails#615
Conversation
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.
There was a problem hiding this comment.
Pull request overview
This PR adds stderr capture and logging functionality to help diagnose MCP container/process startup failures. When a connection to an MCP backend server fails, the stderr output from the failed process is now captured and logged to provide better debugging information.
Changes:
- Added stderr capture using a bytes.Buffer before attempting MCP connection
- Log captured stderr output when connection fails, with formatted output for better readability
- Added explanatory comments about why stderr can be safely captured (CommandTransport uses stdin/stdout)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| stderrOutput := strings.TrimSpace(stderrBuf.String()) | ||
| if stderrOutput != "" { | ||
| logger.LogErrorMd("backend", "MCP backend stderr output:\n%s", stderrOutput) | ||
| log.Printf(" 📋 Container/Process stderr output:") | ||
| for _, line := range strings.Split(stderrOutput, "\n") { | ||
| log.Printf(" %s", line) | ||
| } | ||
| } |
There was a problem hiding this comment.
Potential race condition: When client.Connect fails and the context is cancelled (line 208), the process may still be writing to stderr. Reading stderrBuf.String() immediately could miss output or cause a data race with concurrent writes. Consider calling cmd.Wait() after the context cancellation to ensure the process has fully terminated and all stderr output has been written to the buffer before reading it. This is especially important for container/process diagnostics which is the purpose of this feature.
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.