list_directory
List all Python files in a specified directory or subdirectory with the MCP Python Interpreter. Input a directory path to retrieve file names efficiently.
Instructions
List all Python files in a directory or subdirectory.
Args:
directory_path: Path to directory (relative to working directory or absolute, empty for working directory)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| directory_path | No |
Implementation Reference
- mcp_python_interpreter/server.py:806-868 (handler)The handler function for the 'list_directory' tool. It lists Python files (.py) recursively in the specified directory (or working directory if empty), groups them by subdirectory, and formats the output nicely. Uses helper find_python_files. Includes security check with is_path_allowed. The @mcp.tool() decorator handles registration and schema from signature/docstring.@mcp.tool() def list_directory(directory_path: str = "") -> str: """ List all Python files in a directory. Args: directory_path: Path to directory (empty for working directory) """ try: if not directory_path: path = WORKING_DIR else: path = Path(directory_path) if path.is_absolute(): if not is_path_allowed(path): return f"Access denied: Can only list files in working directory: {WORKING_DIR}" else: path = WORKING_DIR / directory_path if not path.exists(): return f"Error: Directory '{directory_path}' not found" if not path.is_dir(): return f"Error: '{directory_path}' is not a directory" files = find_python_files(path) if not files: return f"No Python files found in {directory_path or 'working directory'}" result = f"Python files in: {directory_path or str(WORKING_DIR)}\n\n" files_by_dir = {} base_dir = path if ALLOW_SYSTEM_ACCESS else WORKING_DIR for file in files: file_path = Path(file["path"]) try: relative_path = file_path.relative_to(base_dir) parent = str(relative_path.parent) if parent == ".": parent = "(root)" except ValueError: parent = str(file_path.parent) if parent not in files_by_dir: files_by_dir[parent] = [] files_by_dir[parent].append({ "name": file["name"], "size": file["size"] }) for dir_name, dir_files in sorted(files_by_dir.items()): result += f"📁 {dir_name}:\n" for file in sorted(dir_files, key=lambda x: x["name"]): size_kb = round(file["size"] / 1024, 1) result += f" 📄 {file['name']} ({size_kb} KB)\n" result += "\n" return result except Exception as e: return f"Error listing directory: {str(e)}"
- Helper function called by list_directory to recursively find all .py files in the directory and return their metadata (path, name, size, modified time).def find_python_files(directory: Path) -> List[Dict[str, str]]: """Find all Python files in a directory.""" files = [] if not directory.exists(): return files for path in directory.rglob("*.py"): if path.is_file(): files.append({ "path": str(path), "name": path.name, "size": path.stat().st_size, "modified": path.stat().st_mtime }) return files
- Helper function used by list_directory to check if the directory path is within the allowed working directory (unless system access is enabled).def is_path_allowed(path: Path) -> bool: """Check if a path is allowed based on security settings.""" if ALLOW_SYSTEM_ACCESS: return True try: path.resolve().relative_to(WORKING_DIR.resolve()) return True except ValueError: return False