Skip to main content
Glama

tree

Display directory tree structure to visualize file organization and hierarchy within virtual filesystem workspaces.

Instructions

Display directory tree structure.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathNo.
max_depthNo

Implementation Reference

  • Core handler implementing the 'tree' tool logic: resolves path, recursively builds TreeNode structure up to max_depth using nested build_tree function, handles files/directories, truncation, and returns TreeResponse.
    async def tree(self, path: str = ".", max_depth: int = 3) -> TreeResponse: """ Display directory tree structure. Args: path: Root path for tree max_depth: Maximum depth to traverse Returns: TreeResponse with nested tree structure """ vfs = self.workspace_manager.get_current_vfs() resolved_path = self.workspace_manager.resolve_path(path) async def build_tree(current_path: str, depth: int) -> TreeNode: if depth > max_depth: return TreeNode(name="...", type=NodeType.DIRECTORY, truncated=True) node_info = await vfs.get_node_info(current_path) if not node_info: return TreeNode(name="???", type=NodeType.FILE, size=0) node_type = NodeType.DIRECTORY if node_info.is_dir else NodeType.FILE if not node_info.is_dir: return TreeNode( name=Path(current_path).name, type=node_type, size=node_info.size ) # Recursively build tree for directory children: list[TreeNode] = [] filenames = await vfs.ls(current_path) for name in filenames: if current_path == "/": child_path = f"/{name}" else: child_path = f"{current_path}/{name}" child_tree = await build_tree(child_path, depth + 1) children.append(child_tree) return TreeNode( name=Path(current_path).name if current_path != "/" else "/", type=node_type, children=children if children else None, ) root = await build_tree(resolved_path, 0) return TreeResponse(root=root)
  • MCP tool registration for 'tree': thin wrapper delegating to VFSTools.tree method.
    @server.tool async def tree(path: str = ".", max_depth: int = 3): """Display directory tree structure.""" return await vfs_tools.tree(path, max_depth)
  • Pydantic models defining the input/output schema: TreeNode (recursive structure for tree nodes) and TreeResponse (wraps root node). NodeType enum used for type field.
    class TreeNode(BaseModel): """Node in a directory tree""" name: str type: NodeType size: int | None = None children: list["TreeNode"] | None = None truncated: bool = False class TreeResponse(BaseModel): """Response from tree operation""" root: TreeNode

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/chrishayuk/chuk-mcp-vfs'

If you have feedback or need assistance with the MCP directory API, please join our Discord server