Revert "chore: do not wait for agents to start, add timeout on MCP connections"

This reverts commit 7cc0c3e85b.
This commit is contained in:
Ettore Di Giacinto
2026-01-31 09:19:26 +01:00
parent 7cc0c3e85b
commit b527cbc332
2 changed files with 12 additions and 33 deletions

View File

@@ -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())
}

11
main.go
View File

@@ -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"))