on main help sign in
fix(mcp): give a time limit for startup (#660)

* fix(mcp): give a time limit for startup If a MCP gets stuck starting/connecting, chatting will not work. This makes it so it'll timeout after 5s (we can discuss the time), and then everything works accordingly. Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: spinner Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * Update mcp-tools.go Co-authored-by: Andrey Nering <andreynering@users.noreply.github.com> --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> Co-authored-by: Andrey Nering <andreynering@users.noreply.github.com>

commit42a346ceb034cd1ebe0a10b696ac7c796984521d authorCarlos Alexandro Becker <caarlos0@users.noreply.github.com> dateFri 8 Aug 2025, 16:18 (1 year ago) parente00a70a
2 files changed+6−1
internal/llm/agent/agent.go+4−1 internal/llm/agent/mcp-tools.go+2
internal/llm/agent/agent.go+4−1
@@ -449,8 +449,8 @@ func (a *agent) createUserMessage(ctx context.Context, sessionID, content string
449449
450450func (a *agent) streamAndHandleEvents(ctx context.Context, sessionID string, msgHistory []message.Message) (message.Message, *message.Message, error) {
451451 ctx = context.WithValue(ctx, tools.SessionIDContextKey, sessionID)
452− eventChan := a.provider.StreamResponse(ctx, msgHistory, slices.Collect(a.tools.Seq()))
453452
453+ // Create the assistant message first so the spinner shows immediately
454454 assistantMsg, err := a.messages.Create(ctx, sessionID, message.CreateMessageParams{
455455 Role: message.Assistant,
456456 Parts: []message.ContentPart{},
@@ -461,6 +461,9 @@ func (a *agent) streamAndHandleEvents(ctx context.Context, sessionID string, msg
461461 return assistantMsg, nil, fmt.Errorf("failed to create assistant message: %w", err)
462462 }
463463
464+ // Now collect tools (which may block on MCP initialization)
465+ eventChan := a.provider.StreamResponse(ctx, msgHistory, slices.Collect(a.tools.Seq()))
466+
464467 // Add the session and message ID into the context if needed by tools.
465468 ctx = context.WithValue(ctx, tools.MessageIDContextKey, assistantMsg.ID)
466469
internal/llm/agent/mcp-tools.go+2
@@ -274,6 +274,8 @@ func doGetMCPTools(ctx context.Context, permissions permission.Service, cfg *con
274274 }
275275 }()
276276
277+ ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
278+ defer cancel()
277279 c, err := createMcpClient(m)
278280 if err != nil {
279281 updateMCPState(name, MCPStateError, err, nil, 0)