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