feat: Enhance MCP tools registration and management with context support
- Updated RegisterMCPTools and GetMCPToolsInfo functions to accept a context parameter, improving control over timeouts and cancellations. - Implemented timeout handling for ListTools operations to prevent hanging during long-running requests. - Enhanced GetFunctionDefinitions to include all MCP tools regardless of the allowed tools list, ensuring comprehensive tool access. - Improved MCP service management by adding checks for service enabled state and critical configuration changes, ensuring robust client connection handling. - Refactored session handling to utilize AgentModeEnabled for better session configuration management.
This commit is contained in:
@@ -119,7 +119,7 @@ func (s *agentService) CreateAgentEngine(
|
||||
|
||||
// Register MCP tools
|
||||
if len(enabledServices) > 0 {
|
||||
if err := tools.RegisterMCPTools(toolRegistry, enabledServices, s.mcpManager); err != nil {
|
||||
if err := tools.RegisterMCPTools(ctx, toolRegistry, enabledServices, s.mcpManager); err != nil {
|
||||
logger.Warnf(ctx, "Failed to register MCP tools: %v", err)
|
||||
} else {
|
||||
logger.Infof(ctx, "Registered MCP tools from %d enabled services", len(enabledServices))
|
||||
|
||||
@@ -105,6 +105,9 @@ func (s *mcpServiceService) UpdateMCPService(ctx context.Context, service *types
|
||||
return fmt.Errorf("MCP service not found")
|
||||
}
|
||||
|
||||
// Store old enabled state BEFORE any updates
|
||||
oldEnabled := existing.Enabled
|
||||
|
||||
// Merge updates: only update fields that are provided (non-zero or explicitly set)
|
||||
// This ensures that false values for enabled field are properly updated
|
||||
// Handler ensures that service.Enabled is only set if "enabled" key exists in the request
|
||||
@@ -146,10 +149,35 @@ func (s *mcpServiceService) UpdateMCPService(ctx context.Context, service *types
|
||||
return fmt.Errorf("failed to update MCP service: %w", err)
|
||||
}
|
||||
|
||||
// Close existing client connection to force reconnect with new config
|
||||
s.mcpManager.CloseClient(service.ID)
|
||||
// Check if critical configuration changed (URL, transport type, or auth config)
|
||||
configChanged := false
|
||||
if service.URL != "" && service.URL != existing.URL {
|
||||
configChanged = true
|
||||
}
|
||||
if service.TransportType != "" && service.TransportType != existing.TransportType {
|
||||
configChanged = true
|
||||
}
|
||||
if service.AuthConfig != nil {
|
||||
configChanged = true
|
||||
}
|
||||
|
||||
logger.GetLogger(ctx).Infof("MCP service updated: %s (ID: %s)", existing.Name, service.ID)
|
||||
// Close existing client connection if:
|
||||
// 1. Service is now disabled (need to close connection)
|
||||
// 2. Critical configuration changed (need to reconnect with new config)
|
||||
if !existing.Enabled {
|
||||
s.mcpManager.CloseClient(service.ID)
|
||||
logger.GetLogger(ctx).Infof("MCP service disabled, connection closed: %s (ID: %s)", existing.Name, service.ID)
|
||||
} else if configChanged {
|
||||
s.mcpManager.CloseClient(service.ID)
|
||||
logger.GetLogger(ctx).Infof("MCP service config changed, connection closed for reconnect: %s (ID: %s)", existing.Name, service.ID)
|
||||
} else if oldEnabled != existing.Enabled && existing.Enabled {
|
||||
// Service was just enabled (was disabled, now enabled)
|
||||
// Close any existing connection to ensure clean state
|
||||
s.mcpManager.CloseClient(service.ID)
|
||||
logger.GetLogger(ctx).Infof("MCP service enabled, existing connection closed for clean state: %s (ID: %s)", existing.Name, service.ID)
|
||||
}
|
||||
|
||||
logger.GetLogger(ctx).Infof("MCP service updated: %s (ID: %s), enabled: %v", existing.Name, service.ID, existing.Enabled)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -795,7 +795,7 @@ func (s *sessionService) AgentQA(ctx context.Context, session *types.Session, qu
|
||||
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
|
||||
|
||||
// Check if agent is enabled at session level
|
||||
if session.AgentConfig == nil || !session.AgentConfig.Enabled {
|
||||
if session.AgentConfig == nil || !session.AgentConfig.AgentModeEnabled {
|
||||
logger.Warnf(ctx, "Agent not enabled for session: %s", sessionID)
|
||||
return errors.New("agent not enabled for this session")
|
||||
}
|
||||
@@ -810,7 +810,7 @@ func (s *sessionService) AgentQA(ctx context.Context, session *types.Session, qu
|
||||
// Tenant config provides the runtime parameters (MaxIterations, Temperature, Tools, Models)
|
||||
// Session config provides Enabled and KnowledgeBases
|
||||
agentConfig := &types.AgentConfig{
|
||||
Enabled: session.AgentConfig.Enabled,
|
||||
Enabled: session.AgentConfig.AgentModeEnabled,
|
||||
MaxIterations: tenantInfo.AgentConfig.MaxIterations,
|
||||
ReflectionEnabled: tenantInfo.AgentConfig.ReflectionEnabled,
|
||||
AllowedTools: tenantInfo.AgentConfig.AllowedTools,
|
||||
|
||||
Reference in New Issue
Block a user