fix(mcp): surface MCP init failures to command handlers

This commit is contained in:
afjcjsbx
2026-04-21 09:03:17 +02:00
parent e5a6960078
commit 5a13616b64
2 changed files with 47 additions and 0 deletions
+1
View File
@@ -106,6 +106,7 @@ func (al *AgentLoop) ensureMCPInitialized(ctx context.Context) error {
}
if err := mcpManager.LoadFromMCPConfig(ctx, al.cfg.Tools.MCP, workspacePath); err != nil {
al.mcp.setInitErr(fmt.Errorf("failed to load MCP servers: %w", err))
logger.WarnCF("agent", "Failed to load MCP servers, MCP tools will not be available",
map[string]any{
"error": err.Error(),
+46
View File
@@ -9,6 +9,7 @@ package agent
import (
"context"
"errors"
"strings"
"testing"
"github.com/sipeed/picoclaw/pkg/config"
@@ -133,3 +134,48 @@ func TestServerIsDeferred(t *testing.T) {
})
}
}
func TestEnsureMCPInitialized_LoadFailureSetsInitErr(t *testing.T) {
al, cfg, _, _, cleanup := newTestAgentLoop(t)
defer cleanup()
defer al.Close()
cfg.Tools = config.ToolsConfig{
MCP: config.MCPConfig{
ToolConfig: config.ToolConfig{Enabled: true},
Servers: map[string]config.MCPServerConfig{
"broken": {
Enabled: true,
Command: "picoclaw-command-that-does-not-exist-for-mcp-tests",
},
},
},
}
err := al.ensureMCPInitialized(context.Background())
if err == nil {
t.Fatal("ensureMCPInitialized() error = nil, want load failure")
}
if !strings.Contains(err.Error(), "failed to load MCP servers") {
t.Fatalf("ensureMCPInitialized() error = %q, want wrapped load failure", err.Error())
}
initErr := al.mcp.getInitErr()
if initErr == nil {
t.Fatal("getInitErr() = nil, want cached load failure")
}
if !strings.Contains(initErr.Error(), "failed to load MCP servers") {
t.Fatalf("getInitErr() = %q, want wrapped load failure", initErr.Error())
}
if al.mcp.getManager() != nil {
t.Fatal("expected MCP manager to remain nil after load failure")
}
err = al.ensureMCPInitialized(context.Background())
if err == nil {
t.Fatal("second ensureMCPInitialized() error = nil, want cached load failure")
}
if !strings.Contains(err.Error(), "failed to load MCP servers") {
t.Fatalf("second ensureMCPInitialized() error = %q, want wrapped load failure", err.Error())
}
}