on main help sign in
fix(mcp): SSE MCPs not working (#1157)

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

commit6cd1733aed022fefe5ec54416c2b01d42c797288 authorCarlos Alexandro Becker <caarlos0@users.noreply.github.com> dateTue 30 Sep 2025, 10:55 (1 year ago) parentcfa3244
1 file changed+10−7
internal/llm/agent/mcp-tools.go+10−7
internal/llm/agent/mcp-tools.go+10−7
@@ -341,29 +341,32 @@ func createAndInitializeClient(ctx context.Context, name string, m config.MCPCon
341341 return nil, err
342342 }
343343
344+ // XXX: ideally we should be able to use context.WithTimeout here, but,
345+ // the SSE MCP client will start failing once that context is canceled.
344346 timeout := mcpTimeout(m)
345− initCtx, cancel := context.WithTimeout(ctx, timeout)
346− defer cancel()
347−
348− if err := c.Start(initCtx); err != nil {
347+ mcpCtx, cancel := context.WithCancel(ctx)
348+ cancelTimer := time.AfterFunc(timeout, cancel)
349+ if err := c.Start(mcpCtx); err != nil {
349350 updateMCPState(name, MCPStateError, maybeTimeoutErr(err, timeout), nil, 0)
350351 slog.Error("error starting mcp client", "error", err, "name", name)
351352 _ = c.Close()
353+ cancel()
352354 return nil, err
353355 }
354− if _, err := c.Initialize(initCtx, mcpInitRequest); err != nil {
356+ if _, err := c.Initialize(mcpCtx, mcpInitRequest); err != nil {
355357 updateMCPState(name, MCPStateError, maybeTimeoutErr(err, timeout), nil, 0)
356358 slog.Error("error initializing mcp client", "error", err, "name", name)
357359 _ = c.Close()
360+ cancel()
358361 return nil, err
359362 }
360−
363+ cancelTimer.Stop()
361364 slog.Info("Initialized mcp client", "name", name)
362365 return c, nil
363366}
364367
365368func maybeTimeoutErr(err error, timeout time.Duration) error {
366− if errors.Is(err, context.DeadlineExceeded) {
369+ if errors.Is(err, context.Canceled) {
367370 return fmt.Errorf("timed out after %s", timeout)
368371 }
369372 return err