Skip to content

Commit 45c5044

Browse files
authored
[log] Add debug logging to writeGatewayConfig in cmd/root.go (#1533)
## Summary Adds 5 meaningful `debugLog.Printf` calls to the `writeGatewayConfig` function in `internal/cmd/root.go` to improve observability when troubleshooting gateway configuration output issues. ## Changes The `writeGatewayConfig` function previously had **zero** debug logging calls despite being the key function that builds and writes the gateway's output configuration (per MCP Gateway Spec §5.4). When clients receive unexpected config output, there was no way to trace what the function was computing. **Added logging at:** 1. **Function entry** — logs `listenAddr`, `mode`, and total server count 2. **After address parsing** — logs resolved `host` and `port` (helps debug IPv6/IPv4 detection) 3. **Auth config** — logs whether an API key is configured (without exposing the key) 4. **Per-server URL** — logs server name, computed URL, and tool count 5. **Successful completion** — logs final server count after encoding ## Quality Checklist - [x] Exactly 1 file modified - [x] No test files modified - [x] Reuses existing `debugLog` logger (already declared as `var debugLog = logger.New("cmd:root")`) - [x] Logger naming follows `pkg:filename` convention (`cmd:root`) - [x] Logger arguments don't compute anything or cause side effects (`len()` is a builtin, no function calls) - [x] Logging messages are meaningful and helpful for debugging - [x] No duplication with existing logs ## Example Debug Output ``` DEBUG=cmd:root ./awmg --config config.toml cmd:root Writing gateway config: listenAddr=127.0.0.1:3000, mode=routed, serverCount=2 cmd:root Parsed listen address: host=127.0.0.1, port=3000 cmd:root Gateway config: auth_enabled=true cmd:root Writing server config: name=github, url=(127.0.0.1/redacted) toolCount=0 cmd:root Writing server config: name=slack, url=(127.0.0.1/redacted) toolCount=0 cmd:root Gateway config written successfully: serverCount=2 ``` > Generated by [Go Logger Enhancement](https://github.com/github/gh-aw-mcpg/actions/runs/22546627400) <!-- gh-aw-agentic-workflow: Go Logger Enhancement, engine: copilot, id: 22546627400, workflow_id: go-logger, run: https://github.com/github/gh-aw-mcpg/actions/runs/22546627400 --> <!-- gh-aw-workflow-id: go-logger -->
2 parents 96b2d96 + b925db2 commit 45c5044

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

internal/cmd/root.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ func writeGatewayConfigToStdout(cfg *config.Config, listenAddr, mode string) err
341341
}
342342

343343
func writeGatewayConfig(cfg *config.Config, listenAddr, mode string, w io.Writer) error {
344+
debugLog.Printf("Writing gateway config: listenAddr=%s, mode=%s, serverCount=%d", listenAddr, mode, len(cfg.Servers))
345+
344346
// Parse listen address to extract host and port
345347
// Use net.SplitHostPort which properly handles both IPv4 and IPv6 addresses
346348
host, port := DefaultListenIPv4, DefaultListenPort
@@ -352,6 +354,7 @@ func writeGatewayConfig(cfg *config.Config, listenAddr, mode string, w io.Writer
352354
port = p
353355
}
354356
}
357+
debugLog.Printf("Parsed listen address: host=%s, port=%s", host, port)
355358

356359
// Determine domain (use host from listen address)
357360
domain := host
@@ -361,6 +364,7 @@ func writeGatewayConfig(cfg *config.Config, listenAddr, mode string, w io.Writer
361364
if cfg.Gateway != nil {
362365
apiKey = cfg.Gateway.APIKey
363366
}
367+
debugLog.Printf("Gateway config: auth_enabled=%v", apiKey != "")
364368

365369
// Build output configuration
366370
outputConfig := map[string]interface{}{
@@ -374,12 +378,15 @@ func writeGatewayConfig(cfg *config.Config, listenAddr, mode string, w io.Writer
374378
"type": "http",
375379
}
376380

381+
var serverURL string
377382
if mode == "routed" {
378-
serverConfig["url"] = fmt.Sprintf("http://%s:%s/mcp/%s", domain, port, name)
383+
serverURL = fmt.Sprintf("http://%s:%s/mcp/%s", domain, port, name)
379384
} else {
380385
// Unified mode - all servers use /mcp endpoint
381-
serverConfig["url"] = fmt.Sprintf("http://%s:%s/mcp", domain, port)
386+
serverURL = fmt.Sprintf("http://%s:%s/mcp", domain, port)
382387
}
388+
serverConfig["url"] = serverURL
389+
debugLog.Printf("Writing server config: name=%s, url=%s, toolCount=%d", name, serverURL, len(server.Tools))
383390

384391
// Add auth headers per MCP Gateway Specification Section 5.4
385392
// Authorization header contains API key directly (not Bearer scheme per spec 7.1)
@@ -404,6 +411,7 @@ func writeGatewayConfig(cfg *config.Config, listenAddr, mode string, w io.Writer
404411
if err := encoder.Encode(outputConfig); err != nil {
405412
return fmt.Errorf("failed to encode configuration: %w", err)
406413
}
414+
debugLog.Printf("Gateway config written successfully: serverCount=%d", len(servers))
407415

408416
// Flush stdout buffer if it's a regular file
409417
// Note: Sync() fails on pipes and character devices like /dev/stdout,

0 commit comments

Comments
 (0)