Better error message on FileNotFoundError (#480)

- Check if directory exists before calling validate_relative_path
- Return structured JSON error with project_root context when directory not found
- Helps LLMs understand the current working directory context in MCP

Similar to fix in commit 8464f25 for CreateTextFileTool
This commit is contained in:
Abdelkader Boudih authored and GitHub committed 2025-08-14 14:42:15 +02:00
1 parent de173ce078
commit bcc4a9e5d9
1 file changed
+9
+9
View File
@@ -100,6 +100,15 @@ class ListDirTool(Tool):
required for the task.
:return: a JSON object with the names of directories and files within the given directory
"""
# Check if the directory exists before validation
if not self.project.relative_path_exists(relative_path):
error_info = {
"error": f"Directory not found: {relative_path}",
"project_root": self.get_project_root(),
"hint": "Check if the path is correct relative to the project root",
}
return json.dumps(error_info)
self.project.validate_relative_path(relative_path)
dirs, files = scan_directory(