From 084112e8e1877e447f04a7dda2aac3ecba5c550d Mon Sep 17 00:00:00 2001 From: lxowalle Date: Fri, 13 Mar 2026 15:58:53 +0800 Subject: [PATCH] add tips for mcp --- pkg/agent/loop_mcp.go | 16 ++++++++++++++++ pkg/agent/loop_test.go | 12 +++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pkg/agent/loop_mcp.go b/pkg/agent/loop_mcp.go index 2795db52a..962789a06 100644 --- a/pkg/agent/loop_mcp.go +++ b/pkg/agent/loop_mcp.go @@ -63,6 +63,22 @@ func (al *AgentLoop) ensureMCPInitialized(ctx context.Context) error { return nil } + if al.cfg.Tools.MCP.Servers == nil || len(al.cfg.Tools.MCP.Servers) == 0 { + logger.WarnCF("agent", "MCP is enabled but no servers are configured, skipping MCP initialization", nil) + return nil + } + + findValidServer := false + for _, serverCfg := range al.cfg.Tools.MCP.Servers { + if serverCfg.Enabled { + findValidServer = true + } + } + if !findValidServer { + logger.WarnCF("agent", "MCP is enabled but no valid servers are configured, skipping MCP initialization", nil) + return nil + } + al.mcp.initOnce.Do(func() { mcpManager := mcp.NewManager() diff --git a/pkg/agent/loop_test.go b/pkg/agent/loop_test.go index cab82e176..d3689a24f 100644 --- a/pkg/agent/loop_test.go +++ b/pkg/agent/loop_test.go @@ -770,13 +770,18 @@ func TestAgentLoop_ContextExhaustionRetry(t *testing.T) { } } -func TestProcessDirectWithChannel_InitializesMCPInAgentMode(t *testing.T) { +// TestProcessDirectWithChannel_TriggersMCPInitialization verifies that +// ProcessDirectWithChannel triggers MCP initialization when MCP is enabled. +// Note: Manager is only initialized when at least one MCP server is configured +// and successfully connected. +func TestProcessDirectWithChannel_TriggersMCPInitialization(t *testing.T) { tmpDir, err := os.MkdirTemp("", "agent-test-*") if err != nil { t.Fatalf("Failed to create temp dir: %v", err) } defer os.RemoveAll(tmpDir) + // Test with MCP enabled but no servers - should not initialize manager cfg := &config.Config{ Agents: config.AgentsConfig{ Defaults: config.AgentDefaults{ @@ -791,6 +796,11 @@ func TestProcessDirectWithChannel_InitializesMCPInAgentMode(t *testing.T) { ToolConfig: config.ToolConfig{ Enabled: true, }, + Servers: map[string]config.MCPServerConfig{ + "test-server": { + Enabled: true, + }, + }, }, }, }