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:
wizardchen
2025-11-11 02:47:38 +08:00
parent 244aaab328
commit 2d9d930824
8 changed files with 101 additions and 25 deletions
+5 -2
View File
@@ -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(),