|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/github/gh-aw-mcpg/internal/logger" |
| 9 | + "github.com/github/gh-aw-mcpg/internal/logger/sanitize" |
| 10 | +) |
| 11 | + |
| 12 | +// ConnectionErrorContext holds all context needed to produce a detailed connection |
| 13 | +// failure diagnostic. Fields left at their zero values are omitted from the output. |
| 14 | +type ConnectionErrorContext struct { |
| 15 | + ServerID string |
| 16 | + SessionID string |
| 17 | + Command string |
| 18 | + Args []string |
| 19 | + Env map[string]string |
| 20 | + RunningInContainer bool |
| 21 | + IsDirectCommand bool |
| 22 | + StartupTimeout time.Duration |
| 23 | + StderrOutput string |
| 24 | +} |
| 25 | + |
| 26 | +// LogConnectionError logs detailed diagnostics for a connection failure, including |
| 27 | +// command context, captured stderr, and actionable hints based on the error type |
| 28 | +// and execution environment. All callers (launcher and mcp connection) use this |
| 29 | +// single function so that hint analysis and output format remain consistent. |
| 30 | +func LogConnectionError(errCtx ConnectionErrorContext, err error) { |
| 31 | + suffix := "" |
| 32 | + if errCtx.SessionID != "" { |
| 33 | + suffix = " for session '" + errCtx.SessionID + "'" |
| 34 | + } |
| 35 | + |
| 36 | + // Structured log via file logger. |
| 37 | + if errCtx.ServerID != "" { |
| 38 | + logger.LogErrorWithServer(errCtx.ServerID, "backend", |
| 39 | + "MCP backend connection failed%s: server=%s, command=%s, args=%v, error=%v", |
| 40 | + suffix, errCtx.ServerID, errCtx.Command, sanitize.SanitizeArgs(errCtx.Args), err) |
| 41 | + } else { |
| 42 | + logger.LogErrorMd("backend", |
| 43 | + "MCP backend connection failed, command=%s, args=%v, error=%v", |
| 44 | + errCtx.Command, sanitize.SanitizeArgs(errCtx.Args), err) |
| 45 | + } |
| 46 | + |
| 47 | + // Human-readable console output. |
| 48 | + if errCtx.ServerID != "" { |
| 49 | + log.Printf("[LAUNCHER] ❌ FAILED to connect to server '%s'%s", errCtx.ServerID, suffix) |
| 50 | + } else { |
| 51 | + log.Printf("[LAUNCHER] ❌ MCP Connection Failed") |
| 52 | + } |
| 53 | + log.Printf("[LAUNCHER] Error: %v", err) |
| 54 | + log.Printf("[LAUNCHER] Debug Information:") |
| 55 | + log.Printf("[LAUNCHER] - Command: %s", errCtx.Command) |
| 56 | + log.Printf("[LAUNCHER] - Args: %v", sanitize.SanitizeArgs(errCtx.Args)) |
| 57 | + if len(errCtx.Env) > 0 { |
| 58 | + log.Printf("[LAUNCHER] - Env vars: %v", sanitize.TruncateSecretMap(errCtx.Env)) |
| 59 | + } |
| 60 | + if errCtx.RunningInContainer || errCtx.IsDirectCommand { |
| 61 | + log.Printf("[LAUNCHER] - Running in container: %v", errCtx.RunningInContainer) |
| 62 | + log.Printf("[LAUNCHER] - Is direct command: %v", errCtx.IsDirectCommand) |
| 63 | + } |
| 64 | + if errCtx.StartupTimeout > 0 { |
| 65 | + log.Printf("[LAUNCHER] - Startup timeout: %v", errCtx.StartupTimeout) |
| 66 | + } |
| 67 | + |
| 68 | + // Log captured stderr output from the container/process. |
| 69 | + if errCtx.StderrOutput != "" { |
| 70 | + sanitizedStderr := sanitize.SanitizeString(errCtx.StderrOutput) |
| 71 | + logger.LogErrorMd("backend", "MCP backend stderr output:\n%s", sanitizedStderr) |
| 72 | + log.Printf("[LAUNCHER] 📋 Process stderr output:") |
| 73 | + for _, line := range strings.Split(sanitizedStderr, "\n") { |
| 74 | + log.Printf("[LAUNCHER] %s", line) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Hints based on execution context (launcher-specific). |
| 79 | + if errCtx.IsDirectCommand && errCtx.RunningInContainer { |
| 80 | + log.Printf("[LAUNCHER] ⚠️ Possible causes:") |
| 81 | + log.Printf("[LAUNCHER] - Command '%s' may not be installed in the gateway container", errCtx.Command) |
| 82 | + log.Printf("[LAUNCHER] - Consider using 'container' config instead of 'command'") |
| 83 | + log.Printf("[LAUNCHER] - Or add '%s' to the gateway's Dockerfile", errCtx.Command) |
| 84 | + } else if errCtx.IsDirectCommand { |
| 85 | + log.Printf("[LAUNCHER] ⚠️ Possible causes:") |
| 86 | + log.Printf("[LAUNCHER] - Command '%s' may not be in PATH", errCtx.Command) |
| 87 | + log.Printf("[LAUNCHER] - Check if '%s' is installed: which %s", errCtx.Command, errCtx.Command) |
| 88 | + log.Printf("[LAUNCHER] - Verify file permissions and execute bit") |
| 89 | + } |
| 90 | + |
| 91 | + // Hints based on error message content. |
| 92 | + errStr := err.Error() |
| 93 | + if strings.Contains(errStr, "executable file not found") || strings.Contains(errStr, "no such file or directory") { |
| 94 | + logger.LogErrorMd("backend", "MCP backend command not found, command=%s", errCtx.Command) |
| 95 | + log.Printf("[LAUNCHER] ⚠️ Command '%s' not found in PATH", errCtx.Command) |
| 96 | + log.Printf("[LAUNCHER] ⚠️ Verify the command is installed and executable") |
| 97 | + } |
| 98 | + |
| 99 | + if strings.Contains(errStr, "EOF") || strings.Contains(errStr, "broken pipe") { |
| 100 | + logger.LogErrorMd("backend", "MCP backend connection/protocol error, command=%s", errCtx.Command) |
| 101 | + log.Printf("[LAUNCHER] ⚠️ Process started but terminated unexpectedly") |
| 102 | + log.Printf("[LAUNCHER] ⚠️ Check if the command supports MCP protocol over stdio") |
| 103 | + } |
| 104 | +} |
0 commit comments