get_folder_structure
Retrieve the entire folder hierarchy of an Obsidian vault to understand its organization.
Instructions
Get the complete folder structure of the vault.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- server.py:293-300 (registration)MCP tool registration for get_folder_structure in server.py using the @mcp.tool() decorator. It delegates to the client's get_folder_structure method.
@mcp.tool() def get_folder_structure() -> dict: """Get the complete folder structure of the vault.""" try: client = get_vault_client() return client.get_folder_structure() except Exception as e: return {"error": str(e)} - obsidian_client.py:265-291 (handler)Core handler implementation. Recursively walks the vault directory, skipping hidden files, collecting .md files as 'note' type and subdirectories as 'folder' type with nested children.
def get_folder_structure(self) -> Dict[str, Any]: """Get the complete folder structure of the vault.""" structure = {'name': self.vault_path.name, 'type': 'folder', 'children': []} def build_structure(current_path: Path, current_node: Dict): for item in sorted(current_path.iterdir()): if item.name.startswith('.'): continue if item.is_file() and item.suffix.lower() == '.md': current_node['children'].append({ 'name': item.name, 'type': 'note', 'path': str(item.relative_to(self.vault_path)) }) elif item.is_dir(): folder_node = { 'name': item.name, 'type': 'folder', 'path': str(item.relative_to(self.vault_path)), 'children': [] } current_node['children'].append(folder_node) build_structure(item, folder_node) build_structure(self.vault_path, structure) return structure - server.py:44-58 (helper)Helper function that lazily initializes and returns the ObsidianVaultClient singleton, used by the tool handler.
def get_vault_client() -> ObsidianVaultClient: """Get or create the vault client.""" global _vault_client, _vault_path if _vault_client is None: if _vault_path is None: # Try to get from environment variable _vault_path = os.getenv("OBSIDIAN_VAULT_PATH") if not _vault_path: raise ValueError("Obsidian vault path not configured. Set OBSIDIAN_VAULT_PATH environment variable.") _vault_client = ObsidianVaultClient(_vault_path, backup_on_write=backup_on_write_enabled()) logger.info(f"Connected to vault: {_vault_path}") return _vault_client