From 509643bbabff0f755addcd1529c9ead57ef4b958 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Sat, 4 Apr 2026 19:27:37 +0200 Subject: [PATCH] fix: avoid triggering toolset startup from emitToolsChanged callback emitToolsChanged uses a 5-second timeout context that races with slow toolset initialization (e.g. RAG file indexing). When an MCP server connects and fires OnToolsChanged before RAG finishes, the timeout causes a context deadline exceeded error and a spurious warning. Add Agent.StartedTools() that lists tools only from already-started toolsets without calling ensureToolSetsAreStarted(), and use it in emitToolsChanged so the callback never triggers initialization. Assisted-By: docker-agent --- pkg/agent/agent.go | 14 +++++++++++++- pkg/runtime/runtime.go | 3 ++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index 0578e5653..8dfd60619 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -209,11 +209,23 @@ func (a *Agent) Hooks() *latest.HooksConfig { // Tools returns the tools available to this agent func (a *Agent) Tools(ctx context.Context) ([]tools.Tool, error) { a.ensureToolSetsAreStarted(ctx) + return a.collectTools(ctx) +} + +// StartedTools returns tools only from toolsets that have already been started, +// without triggering initialization of unstarted toolsets. This is useful for +// notifications (e.g. MCP tool list changes) that should not block on slow +// toolset startup such as RAG file indexing. +func (a *Agent) StartedTools(ctx context.Context) ([]tools.Tool, error) { + return a.collectTools(ctx) +} +// collectTools gathers tools from all started toolsets plus static tools. +func (a *Agent) collectTools(ctx context.Context) ([]tools.Tool, error) { var agentTools []tools.Tool for _, toolSet := range a.toolsets { if !toolSet.IsStarted() { - // Toolset failed to start; skip it + // Toolset not started; skip it continue } ta, err := toolSet.Tools(ctx) diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go index 8482f3e51..f6a4de8bc 100644 --- a/pkg/runtime/runtime.go +++ b/pkg/runtime/runtime.go @@ -808,7 +808,8 @@ func (r *LocalRuntime) emitToolsChanged() { } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - agentTools, err := r.CurrentAgentTools(ctx) + a := r.CurrentAgent() + agentTools, err := a.StartedTools(ctx) if err != nil { return }