run method
Executes the tool with the given input map.
Should never throw — return ToolResult.failure on errors.
Implementation
@override
Future<ToolResult> run(Map<String, dynamic> input) async {
final path = (input['path'] as String? ?? '').trim();
if (path.isEmpty) {
return ToolResult.failure(name, 'Input "path" must not be empty.');
}
// Optional sandboxing
if (allowedDirectory != null &&
!path.startsWith(allowedDirectory!)) {
return ToolResult.failure(
name,
'Access denied: path must be within "$allowedDirectory".',
);
}
_logger.info('Reading file: $path');
try {
final file = File(path);
if (!await file.exists()) {
return ToolResult.failure(name, 'File not found: $path');
}
final content = await file.readAsString();
return ToolResult.success(name, content,
metadata: {
'path': path,
'size_bytes': content.length,
});
} catch (e) {
return ToolResult.failure(name, 'File read error: $e');
}
}