execute method

  1. @override
Future<MCPToolResult> execute(
  1. Map<String, dynamic> arguments
)
override

Executes the tool actions using the supplied LLM arguments.

Implementation

@override
Future<MCPToolResult> execute(Map<String, dynamic> arguments) async {
  final toolName = arguments['tool_name'] as String? ?? '';
  final toolArgs = arguments['arguments'] is Map
      ? Map<String, dynamic>.from(arguments['arguments'] as Map)
      : <String, dynamic>{};

  if (toolName.trim().isEmpty) {
    return MCPToolResult.error('tool_name parameter is required.');
  }

  // Check local tools first if provided
  if (localTools != null) {
    final localMatch = localTools!.where((t) => t.name == toolName).toList();
    if (localMatch.isNotEmpty) {
      try {
        return await localMatch.first.execute(toolArgs);
      } catch (e) {
        return MCPToolResult.error('Failed to execute local tool "$toolName": $e');
      }
    }
  }

  try {
    return await mcpManager.callTool(toolName, toolArgs);
  } catch (e) {
    return MCPToolResult.error('Failed to execute MCP tool "$toolName": $e');
  }
}