fix(mcp): return aggregated error when all servers fail to connect

- Add errors.Join to return aggregated error when all enabled MCP servers fail
- Track enabled server count separately from total configured servers
- Return error only when all servers fail, not for partial failures
- Improve logging with accurate server counts (enabled vs connected)
- Maintains fault tolerance: partial failures don't stop initialization
This commit is contained in:
yuchou87 2026-02-16 19:33:31 +08:00
parent 27f5494038
commit abd4830e80

View file

@ -3,6 +3,7 @@ package mcp
import (
"bufio"
"context"
"errors"
"fmt"
"net/http"
"os"
@ -129,6 +130,7 @@ func (m *Manager) LoadFromConfig(ctx context.Context, cfg *config.Config) error
var wg sync.WaitGroup
errs := make(chan error, len(cfg.Tools.MCP.Servers))
enabledCount := 0
for name, serverCfg := range cfg.Tools.MCP.Servers {
if !serverCfg.Enabled {
@ -139,6 +141,7 @@ func (m *Manager) LoadFromConfig(ctx context.Context, cfg *config.Config) error
continue
}
enabledCount++
wg.Add(1)
go func(name string, serverCfg config.MCPServerConfig) {
defer wg.Done()
@ -163,20 +166,32 @@ func (m *Manager) LoadFromConfig(ctx context.Context, cfg *config.Config) error
allErrors = append(allErrors, err)
}
connectedCount := len(m.GetServers())
// If all enabled servers failed to connect, return aggregated error
if enabledCount > 0 && connectedCount == 0 {
logger.ErrorCF("mcp", "All MCP servers failed to connect",
map[string]interface{}{
"failed": len(allErrors),
"total": enabledCount,
})
return errors.Join(allErrors...)
}
if len(allErrors) > 0 {
logger.WarnCF("mcp", "Some MCP servers failed to connect",
map[string]interface{}{
"failed": len(allErrors),
"total": len(cfg.Tools.MCP.Servers),
"failed": len(allErrors),
"connected": connectedCount,
"total": enabledCount,
})
// Don't fail completely if some servers fail to connect
// Don't fail completely if some servers successfully connected
}
connectedCount := len(m.GetServers())
logger.InfoCF("mcp", "MCP server initialization complete",
map[string]interface{}{
"connected": connectedCount,
"total": len(cfg.Tools.MCP.Servers),
"total": enabledCount,
})
return nil