fix(agent): scope MCP manager cleanup to successful initialization
Move defer cleanup inside else block to only clean up when MCP servers are successfully initialized. This prevents unnecessary cleanup attempts when LoadFromMCPConfig fails. Addresses Copilot code review feedback.
This commit is contained in:
parent
535a8319af
commit
23cf757d46
1 changed files with 10 additions and 10 deletions
|
|
@ -150,6 +150,16 @@ func (al *AgentLoop) Run(ctx context.Context) error {
|
|||
"error": err.Error(),
|
||||
})
|
||||
} else {
|
||||
// Ensure MCP connections are cleaned up on exit, only if initialization succeeded
|
||||
defer func() {
|
||||
if err := mcpManager.Close(); err != nil {
|
||||
logger.ErrorCF("agent", "Failed to close MCP manager",
|
||||
map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
// Register MCP tools for all agents
|
||||
servers := mcpManager.GetServers()
|
||||
toolCount := 0
|
||||
|
|
@ -179,16 +189,6 @@ func (al *AgentLoop) Run(ctx context.Context) error {
|
|||
"tool_count": toolCount,
|
||||
})
|
||||
}
|
||||
|
||||
// Ensure MCP connections are cleaned up on exit
|
||||
defer func() {
|
||||
if err := mcpManager.Close(); err != nil {
|
||||
logger.ErrorCF("agent", "Failed to close MCP manager",
|
||||
map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
for al.running.Load() {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue