fix(mcp): surface MCP init failures to command handlers
This commit is contained in:
parent
e5a6960078
commit
5a13616b64
2 changed files with 47 additions and 0 deletions
|
|
@ -106,6 +106,7 @@ func (al *AgentLoop) ensureMCPInitialized(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := mcpManager.LoadFromMCPConfig(ctx, al.cfg.Tools.MCP, workspacePath); err != nil {
|
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",
|
logger.WarnCF("agent", "Failed to load MCP servers, MCP tools will not be available",
|
||||||
map[string]any{
|
map[string]any{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ package agent
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/sipeed/picoclaw/pkg/config"
|
"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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue