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:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/mcp"
|
||||
@@ -164,27 +165,41 @@ func sanitizeName(name string) string {
|
||||
}
|
||||
|
||||
// RegisterMCPTools registers MCP tools from given services
|
||||
func RegisterMCPTools(registry *ToolRegistry, services []*types.MCPService, mcpManager *mcp.MCPManager) error {
|
||||
func RegisterMCPTools(ctx context.Context, registry *ToolRegistry, services []*types.MCPService, mcpManager *mcp.MCPManager) error {
|
||||
if len(services) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
// Use provided context, but don't add timeout here
|
||||
// The GetOrCreateClient has its own timeout for connection/init
|
||||
// For ListTools, we use a reasonable timeout to prevent hanging
|
||||
// but longer than before since ListTools may need time for SSE communication
|
||||
listToolsTimeout := 30 * time.Second
|
||||
if ctx == nil || ctx == context.Background() {
|
||||
// If no context provided, create one with timeout
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(context.Background(), listToolsTimeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
for _, service := range services {
|
||||
if !service.Enabled {
|
||||
continue
|
||||
}
|
||||
|
||||
// Get or create client
|
||||
// Get or create client (this may take time, but has its own timeout)
|
||||
client, err := mcpManager.GetOrCreateClient(service)
|
||||
if err != nil {
|
||||
logger.GetLogger(ctx).Errorf("Failed to create MCP client for service %s: %v", service.Name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// List tools from the service
|
||||
tools, err := client.ListTools(ctx)
|
||||
// List tools from the service with timeout
|
||||
// Create a new context with timeout for this specific operation
|
||||
listCtx, cancel := context.WithTimeout(ctx, listToolsTimeout)
|
||||
tools, err := client.ListTools(listCtx)
|
||||
cancel() // Cancel after ListTools completes
|
||||
|
||||
if err != nil {
|
||||
logger.GetLogger(ctx).Errorf("Failed to list tools from MCP service %s: %v", service.Name, err)
|
||||
continue
|
||||
@@ -202,9 +217,12 @@ func RegisterMCPTools(registry *ToolRegistry, services []*types.MCPService, mcpM
|
||||
}
|
||||
|
||||
// GetMCPToolsInfo returns information about available MCP tools
|
||||
func GetMCPToolsInfo(services []*types.MCPService, mcpManager *mcp.MCPManager) (map[string][]string, error) {
|
||||
func GetMCPToolsInfo(ctx context.Context, services []*types.MCPService, mcpManager *mcp.MCPManager) (map[string][]string, error) {
|
||||
result := make(map[string][]string)
|
||||
ctx := context.Background()
|
||||
|
||||
// Use provided context with timeout
|
||||
infoCtx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
|
||||
for _, service := range services {
|
||||
if !service.Enabled {
|
||||
@@ -216,7 +234,7 @@ func GetMCPToolsInfo(services []*types.MCPService, mcpManager *mcp.MCPManager) (
|
||||
continue
|
||||
}
|
||||
|
||||
tools, err := client.ListTools(ctx)
|
||||
tools, err := client.ListTools(infoCtx)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package tools
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
@@ -59,6 +60,7 @@ func (r *ToolRegistry) ListTools() []string {
|
||||
|
||||
// GetFunctionDefinitions returns function definitions for allowed tools
|
||||
// This is used for LLM function calling
|
||||
// MCP tools (names starting with "mcp_") are always included, regardless of allowedTools list
|
||||
func (r *ToolRegistry) GetFunctionDefinitions(allowedTools []string) []types.FunctionDefinition {
|
||||
definitions := make([]types.FunctionDefinition, 0)
|
||||
|
||||
@@ -80,9 +82,10 @@ func (r *ToolRegistry) GetFunctionDefinitions(allowedTools []string) []types.Fun
|
||||
allowedMap[name] = true
|
||||
}
|
||||
|
||||
// Only include allowed tools
|
||||
// Include allowed tools AND all MCP tools (names starting with "mcp_")
|
||||
for name, tool := range r.tools {
|
||||
if allowedMap[name] {
|
||||
// Include if it's in the allowed list OR if it's an MCP tool
|
||||
if allowedMap[name] || strings.HasPrefix(name, "mcp_") {
|
||||
definitions = append(definitions, types.FunctionDefinition{
|
||||
Name: tool.Name(),
|
||||
Description: tool.Description(),
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -62,7 +62,7 @@ func (h *Handler) CreateSession(c *gin.Context) {
|
||||
// - Knowledge base can be specified in each query request (AgentQA/KnowledgeQA)
|
||||
// - Agent mode can access multiple knowledge bases via AgentConfig.KnowledgeBases
|
||||
// - Knowledge base can be switched during conversation
|
||||
isAgentMode := request.AgentConfig != nil && request.AgentConfig.Enabled
|
||||
isAgentMode := request.AgentConfig != nil && request.AgentConfig.AgentModeEnabled
|
||||
hasAgentKnowledgeBases := request.AgentConfig != nil && len(request.AgentConfig.KnowledgeBases) > 0
|
||||
|
||||
logger.Infof(
|
||||
|
||||
@@ -151,6 +151,8 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
|
||||
logger.Infof(ctx, "Agent QA request, session ID: %s, query: %s", sessionID, request.Query)
|
||||
|
||||
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
|
||||
|
||||
// Get session information first
|
||||
session, err := h.sessionService.GetSession(ctx, sessionID)
|
||||
if err != nil {
|
||||
@@ -213,6 +215,14 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
logger.Infof(ctx, "Web search mode changed from %v to %v", currentWebSearchEnabled, request.WebSearchEnabled)
|
||||
configChanged = true
|
||||
}
|
||||
summaryModelID := request.SummaryModelID
|
||||
if summaryModelID == "" {
|
||||
summaryModelID = tenantInfo.AgentConfig.ThinkingModelID
|
||||
}
|
||||
if summaryModelID != session.SummaryModelID {
|
||||
configChanged = true
|
||||
session.SummaryModelID = summaryModelID
|
||||
}
|
||||
|
||||
// If configuration changed, clear context and update session
|
||||
if configChanged {
|
||||
@@ -230,6 +240,7 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
||||
session.AgentConfig.KnowledgeBases = request.KnowledgeBaseIDs
|
||||
session.AgentConfig.AgentModeEnabled = request.AgentEnabled
|
||||
session.AgentConfig.WebSearchEnabled = request.WebSearchEnabled
|
||||
session.SummaryModelID = summaryModelID
|
||||
// Persist the session changes
|
||||
if err := h.sessionService.UpdateSession(ctx, session); err != nil {
|
||||
logger.Errorf(ctx, "Failed to update session %s: %v", sessionID, err)
|
||||
|
||||
+24
-8
@@ -36,6 +36,11 @@ func NewMCPManager() *MCPManager {
|
||||
|
||||
// GetOrCreateClient gets an existing client or creates a new one
|
||||
func (m *MCPManager) GetOrCreateClient(service *types.MCPService) (MCPClient, error) {
|
||||
// Check if service is enabled
|
||||
if !service.Enabled {
|
||||
return nil, fmt.Errorf("MCP service %s is not enabled", service.Name)
|
||||
}
|
||||
|
||||
// Check if client already exists
|
||||
m.clientsMu.RLock()
|
||||
client, exists := m.clients[service.ID]
|
||||
@@ -65,16 +70,27 @@ func (m *MCPManager) GetOrCreateClient(service *types.MCPService) (MCPClient, er
|
||||
return nil, fmt.Errorf("failed to create MCP client: %w", err)
|
||||
}
|
||||
|
||||
// Connect to service
|
||||
ctx, cancel := context.WithTimeout(m.ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := client.Connect(ctx); err != nil {
|
||||
// For SSE connections, Connect() starts a persistent connection that needs a long-lived context
|
||||
// Use manager's context (m.ctx) which persists for the lifetime of the manager
|
||||
// The HTTP client's timeout will handle connection timeouts, not context cancellation
|
||||
if err := client.Connect(m.ctx); err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to MCP service: %w", err)
|
||||
}
|
||||
|
||||
// Initialize
|
||||
if _, err := client.Initialize(ctx); err != nil {
|
||||
// Initialize needs a timeout to prevent hanging, but the connection context should remain alive
|
||||
// Use a timeout context only for the initialization request
|
||||
initTimeout := 30 * time.Second
|
||||
if service.AdvancedConfig != nil && service.AdvancedConfig.Timeout > 0 {
|
||||
initTimeout = time.Duration(service.AdvancedConfig.Timeout) * time.Second
|
||||
if initTimeout > 60*time.Second {
|
||||
initTimeout = 60 * time.Second
|
||||
}
|
||||
}
|
||||
|
||||
initCtx, initCancel := context.WithTimeout(m.ctx, initTimeout)
|
||||
defer initCancel() // Safe to cancel after init - it's a one-time operation
|
||||
|
||||
if _, err := client.Initialize(initCtx); err != nil {
|
||||
client.Disconnect()
|
||||
return nil, fmt.Errorf("failed to initialize MCP client: %w", err)
|
||||
}
|
||||
@@ -82,7 +98,7 @@ func (m *MCPManager) GetOrCreateClient(service *types.MCPService) (MCPClient, er
|
||||
// Store client
|
||||
m.clients[service.ID] = client
|
||||
|
||||
logger.GetLogger(ctx).Infof("MCP client created and initialized for service: %s", service.Name)
|
||||
logger.GetLogger(m.ctx).Infof("MCP client created and initialized for service: %s", service.Name)
|
||||
return client, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user