list_directory
Retrieve and display directory contents with type annotations ([DIR] or [FILE]). Specify the path to list files and folders within allowed directories, sorted alphabetically.
Instructions
List the contents of a directory with type annotations.
Args: path (str): Directory path to list (absolute or relative to allowed directories)
Returns: str: Newline-separated list of entries with '[DIR]' or '[FILE]' prefixes, or error message if failed
Note: - Path must be within allowed directory roots - Fails if path is not a directory - Entries are sorted alphabetically - Format: '[DIR] dirname' or '[FILE] filename'
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- main.py:404-431 (handler)The handler function for the 'list_directory' tool. It resolves the path, checks if it's a directory, lists contents sorted alphabetically with [DIR] or [FILE] prefixes, and handles errors gracefully.@mcp.tool def list_directory(path: str) -> str: """List the contents of a directory with type annotations. Args: path (str): Directory path to list (absolute or relative to allowed directories) Returns: str: Newline-separated list of entries with '[DIR]' or '[FILE]' prefixes, or error message if failed Note: - Path must be within allowed directory roots - Fails if path is not a directory - Entries are sorted alphabetically - Format: '[DIR] dirname' or '[FILE] filename' """ try: rp = _resolve(path) if not rp.is_dir(): return f"Error listing directory: '{rp}' is not a directory" out = [] for child in sorted(rp.iterdir()): tag = "[DIR]" if child.is_dir() else "[FILE]" out.append(f"{tag} {child.name}") return "\n".join(out) except Exception as e: return _human_error(e, "listing directory")
- main.py:404-404 (registration)The @mcp.tool decorator registers the list_directory function as an MCP tool.@mcp.tool