fix: handle delayed mcp init

Carlos Alexandro Becker created

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

Change summary

internal/agent/coordinator.go | 22 ++++++++++------------
internal/tui/tui.go           | 17 +++++++++++++----
2 files changed, 23 insertions(+), 16 deletions(-)

Detailed changes

internal/agent/coordinator.go 🔗

@@ -348,22 +348,20 @@ func (c *coordinator) buildTools(ctx context.Context, agent config.Agent) ([]fan
 		if agent.AllowedMCP == nil {
 			// No MCP restrictions
 			filteredTools = append(filteredTools, tool)
-		} else if len(agent.AllowedMCP) == 0 {
-			// no mcps allowed
+			continue
+		}
+		if len(agent.AllowedMCP) == 0 {
+			// No MCPs allowed
+			slog.Warn("MCPs not allowed")
 			break
 		}
 
 		for mcp, tools := range agent.AllowedMCP {
-			if mcp == tool.MCP() {
-				if len(tools) == 0 {
-					filteredTools = append(filteredTools, tool)
-				}
-				for _, t := range tools {
-					if t == tool.MCPToolName() {
-						filteredTools = append(filteredTools, tool)
-					}
-				}
-				break
+			if mcp != tool.MCP() {
+				continue
+			}
+			if len(tools) == 0 || slices.Contains(tools, tool.MCPToolName()) {
+				filteredTools = append(filteredTools, tool)
 			}
 		}
 	}

internal/tui/tui.go 🔗

@@ -143,10 +143,12 @@ func (a *appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 
 	case pubsub.Event[mcp.Event]:
 		switch msg.Payload.Type {
+		case mcp.EventStateChanged:
+			return a, a.handleStateChanged(context.Background())
 		case mcp.EventPromptsListChanged:
-			return a, a.handleMCPPromptsEvent(context.Background(), msg.Payload.Name)
+			return a, handleMCPPromptsEvent(context.Background(), msg.Payload.Name)
 		case mcp.EventToolsListChanged:
-			return a, a.handleMCPToolsEvent(context.Background(), msg.Payload.Name)
+			return a, handleMCPToolsEvent(context.Background(), msg.Payload.Name)
 		}
 
 	// Completions messages
@@ -627,14 +629,21 @@ func (a *appModel) View() tea.View {
 	return view
 }
 
-func (a *appModel) handleMCPPromptsEvent(ctx context.Context, name string) tea.Cmd {
+func (a *appModel) handleStateChanged(ctx context.Context) tea.Cmd {
+	return func() tea.Msg {
+		a.app.UpdateAgentModel(ctx)
+		return nil
+	}
+}
+
+func handleMCPPromptsEvent(ctx context.Context, name string) tea.Cmd {
 	return func() tea.Msg {
 		mcp.RefreshPrompts(ctx, name)
 		return nil
 	}
 }
 
-func (a *appModel) handleMCPToolsEvent(ctx context.Context, name string) tea.Cmd {
+func handleMCPToolsEvent(ctx context.Context, name string) tea.Cmd {
 	return func() tea.Msg {
 		mcp.RefreshTools(ctx, name)
 		return nil