@@ -51,6 +51,9 @@ type AgentWorkerCMD struct {
5151 // NATS subjects
5252 Subject string `env:"LOCALAI_AGENT_SUBJECT" default:"agent.execute" help:"NATS subject for agent execution" group:"distributed"`
5353 Queue string `env:"LOCALAI_AGENT_QUEUE" default:"agent-workers" help:"NATS queue group name" group:"distributed"`
54+
55+ // Timeouts
56+ MCPCIJobTimeout string `env:"LOCALAI_MCP_CI_JOB_TIMEOUT" default:"10m" help:"Timeout for MCP CI job execution" group:"distributed"`
5457}
5558
5659func (cmd * AgentWorkerCMD ) Run (ctx * cliContext.Context ) error {
@@ -90,7 +93,10 @@ func (cmd *AgentWorkerCMD) Run(ctx *cliContext.Context) error {
9093 }
9194
9295 // Start heartbeat
93- heartbeatInterval , _ := time .ParseDuration (cmd .HeartbeatInterval )
96+ heartbeatInterval , err := time .ParseDuration (cmd .HeartbeatInterval )
97+ if err != nil && cmd .HeartbeatInterval != "" {
98+ xlog .Warn ("invalid heartbeat interval, using default 10s" , "input" , cmd .HeartbeatInterval , "error" , err )
99+ }
94100 heartbeatInterval = cmp .Or (heartbeatInterval , 10 * time .Second )
95101 // Context cancelled on shutdown — used by heartbeat and other background goroutines
96102 shutdownCtx , shutdownCancel := context .WithCancel (context .Background ())
@@ -145,8 +151,14 @@ func (cmd *AgentWorkerCMD) Run(ctx *cliContext.Context) error {
145151 // Subscribe to MCP CI job execution (load-balanced across agent workers).
146152 // In distributed mode, MCP CI jobs are routed here because the frontend
147153 // cannot create MCP sessions (e.g., stdio servers using docker).
154+ mcpCIJobTimeout , err := time .ParseDuration (cmd .MCPCIJobTimeout )
155+ if err != nil && cmd .MCPCIJobTimeout != "" {
156+ xlog .Warn ("invalid MCP CI job timeout, using default 10m" , "input" , cmd .MCPCIJobTimeout , "error" , err )
157+ }
158+ mcpCIJobTimeout = cmp .Or (mcpCIJobTimeout , config .DefaultMCPCIJobTimeout )
159+
148160 natsClient .QueueSubscribe (messaging .SubjectMCPCIJobsNew , messaging .QueueWorkers , func (data []byte ) {
149- handleMCPCIJob (shutdownCtx , data , apiURL , cmd .APIToken , natsClient )
161+ handleMCPCIJob (shutdownCtx , data , apiURL , cmd .APIToken , natsClient , mcpCIJobTimeout )
150162 })
151163
152164 // Subscribe to backend stop events to clean up cached MCP sessions.
@@ -277,7 +289,7 @@ func sendMCPDiscoveryReply(reply func([]byte), servers []mcpRemote.MCPServerInfo
277289
278290// handleMCPCIJob processes an MCP CI job on the agent worker.
279291// The agent worker can create MCP sessions (has docker) and call the LocalAI API for inference.
280- func handleMCPCIJob (shutdownCtx context.Context , data []byte , apiURL , apiToken string , natsClient * messaging.Client ) {
292+ func handleMCPCIJob (shutdownCtx context.Context , data []byte , apiURL , apiToken string , natsClient messaging.MessagingClient , jobTimeout time. Duration ) {
281293 var evt jobs.JobEvent
282294 if err := json .Unmarshal (data , & evt ); err != nil {
283295 xlog .Error ("Failed to unmarshal job event" , "error" , err )
@@ -353,7 +365,7 @@ func handleMCPCIJob(shutdownCtx context.Context, data []byte, apiURL, apiToken s
353365 llm := clients .NewLocalAILLM (task .Model , apiToken , apiURL )
354366
355367 // Build cogito options
356- ctx , cancel := context .WithTimeout (shutdownCtx , 10 * time . Minute )
368+ ctx , cancel := context .WithTimeout (shutdownCtx , jobTimeout )
357369 defer cancel ()
358370
359371 // Update job status to running in DB
@@ -433,11 +445,11 @@ func handleMCPCIJob(shutdownCtx context.Context, data []byte, apiURL, apiToken s
433445 xlog .Info ("MCP CI job completed" , "jobID" , evt .JobID , "resultLen" , len (result ))
434446}
435447
436- func publishJobStatus (nc * messaging.Client , jobID , status , message string ) {
448+ func publishJobStatus (nc messaging.MessagingClient , jobID , status , message string ) {
437449 jobs .PublishJobProgress (nc , jobID , status , message )
438450}
439451
440- func publishJobResult (nc * messaging.Client , jobID , status , result , errMsg string ) {
452+ func publishJobResult (nc messaging.MessagingClient , jobID , status , result , errMsg string ) {
441453 jobs .PublishJobResult (nc , jobID , status , result , errMsg )
442454}
443455
0 commit comments