From b527cbc332ffb89ea17f81539943d460ef12bb7c Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sat, 31 Jan 2026 09:19:26 +0100 Subject: [PATCH] Revert "chore: do not wait for agents to start, add timeout on MCP connections" This reverts commit 7cc0c3e85b4d6de9f71fe992593b986709103378. --- core/agent/mcp.go | 34 ++++++++-------------------------- main.go | 11 ++++------- 2 files changed, 12 insertions(+), 33 deletions(-) diff --git a/core/agent/mcp.go b/core/agent/mcp.go index 8bea7da..dad5f88 100644 --- a/core/agent/mcp.go +++ b/core/agent/mcp.go @@ -66,10 +66,10 @@ type ToolInputSchema struct { Required []string `json:"required,omitempty"` } -func (a *Agent) addToolsWithContext(ctx context.Context, client *mcp.ClientSession) (types.Actions, error) { +func (a *Agent) addTools(client *mcp.ClientSession) (types.Actions, error) { var generatedActions types.Actions - tools, err := client.ListTools(ctx, nil) + tools, err := client.ListTools(a.context, nil) if err != nil { xlog.Error("Failed to list tools", "error", err.Error()) return nil, err @@ -150,25 +150,17 @@ func (a *Agent) initMCPActions() error { for _, mcpServer := range a.options.mcpServers { // Create HTTP client with custom roundtripper for bearer token injection httpclient := &http.Client{ - Timeout: 30 * time.Second, // Reduced from 360s to fail faster + Timeout: 360 * time.Second, Transport: newBearerTokenRoundTripper(mcpServer.Token, http.DefaultTransport), } - // Add timeout context for connection attempts to prevent blocking - connectCtx, cancel := context.WithTimeout(a.context, 30*time.Second) - defer cancel() - streamableTransport := &mcp.StreamableClientTransport{HTTPClient: httpclient, Endpoint: mcpServer.URL} - session, err := client.Connect(connectCtx, streamableTransport, nil) + session, err := client.Connect(a.context, streamableTransport, nil) if err != nil { xlog.Error("Failed to connect to MCP server via StreamableClientTransport", "server", mcpServer, "error", err.Error()) - // Retry with SSE transport, but create a new timeout context - connectCtx2, cancel2 := context.WithTimeout(a.context, 30*time.Second) - defer cancel2() - sseTransport := &mcp.SSEClientTransport{HTTPClient: httpclient, Endpoint: mcpServer.URL} - session, err = client.Connect(connectCtx2, sseTransport, nil) + session, err = client.Connect(a.context, sseTransport, nil) if err != nil { xlog.Error("Failed to connect to MCP server via SSEClientTransport", "server", mcpServer, "error", err.Error()) continue @@ -177,10 +169,7 @@ func (a *Agent) initMCPActions() error { a.mcpSessions = append(a.mcpSessions, session) xlog.Debug("Adding tools for MCP server", "server", mcpServer) - // Add timeout for ListTools call - toolsCtx, cancel := context.WithTimeout(a.context, 10*time.Second) - defer cancel() - actions, err := a.addToolsWithContext(toolsCtx, session) + actions, err := a.addTools(session) if err != nil { xlog.Error("Failed to add tools for MCP server", "server", mcpServer, "error", err.Error()) } @@ -204,12 +193,8 @@ func (a *Agent) initMCPActions() error { command.Env = os.Environ() command.Env = append(command.Env, mcpStdioServer.Env...) - // Add timeout context for connection attempts to prevent blocking - connectCtx, cancel := context.WithTimeout(a.context, 30*time.Second) - defer cancel() - // Create a new client - session, err := client.Connect(connectCtx, &mcp.CommandTransport{ + session, err := client.Connect(a.context, &mcp.CommandTransport{ Command: command}, nil) if err != nil { xlog.Error("Failed to connect to MCP server", "server", mcpStdioServer, "error", err.Error()) @@ -218,10 +203,7 @@ func (a *Agent) initMCPActions() error { a.mcpSessions = append(a.mcpSessions, session) xlog.Debug("Adding tools for MCP server (stdio)", "server", mcpStdioServer) - // Add timeout for ListTools call - toolsCtx, cancel := context.WithTimeout(a.context, 10*time.Second) - defer cancel() - actions, err := a.addToolsWithContext(toolsCtx, session) + actions, err := a.addTools(session) if err != nil { xlog.Error("Failed to add tools for MCP server", "server", mcpStdioServer, "error", err.Error()) } diff --git a/main.go b/main.go index 4b82b65..162427c 100644 --- a/main.go +++ b/main.go @@ -102,13 +102,10 @@ func main() { webui.WithStateDir(stateDir), ) - // Start the agents in a goroutine so HTTP server can start in parallel - // This prevents MCP server connection issues from blocking the web server - go func() { - if err := pool.StartAll(); err != nil { - log.Printf("Error starting agents: %v", err) - } - }() + // Start the agents + if err := pool.StartAll(); err != nil { + panic(err) + } // Start the web server log.Fatal(app.Listen(":3000"))