Skip to main content
Glama
MarkusPfundstein

MCP server for Obsidian

obsidian_list_files_in_dir

List files and directories within a specific Obsidian vault folder to manage and navigate your notes.

Instructions

Lists all files and directories that exist in a specific Obsidian directory.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dirpathYesPath to list files from (relative to your vault root). Note that empty directories will not be returned.

Implementation Reference

  • The ListFilesInDirToolHandler class is the core handler for the 'obsidian_list_files_in_dir' tool. It defines the tool's schema (inputSchema for 'dirpath') and implements the run_tool method, which calls the Obsidian API to list files in the specified directory and returns the JSON-formatted result.
    class ListFilesInDirToolHandler(ToolHandler):
        def __init__(self):
            super().__init__(TOOL_LIST_FILES_IN_DIR)
    
        def get_tool_description(self):
            return Tool(
                name=self.name,
                description="Lists all files and directories that exist in a specific Obsidian directory.",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "dirpath": {
                            "type": "string",
                            "description": "Path to list files from (relative to your vault root). Note that empty directories will not be returned."
                        },
                    },
                    "required": ["dirpath"]
                }
            )
    
        def run_tool(self, args: dict) -> Sequence[TextContent | ImageContent | EmbeddedResource]:
    
            if "dirpath" not in args:
                raise RuntimeError("dirpath argument missing in arguments")
    
            api = obsidian.Obsidian(api_key=api_key, host=obsidian_host)
    
            files = api.list_files_in_dir(args["dirpath"])
    
            return [
                TextContent(
                    type="text",
                    text=json.dumps(files, indent=2)
                )
            ]
  • Registers the ListFilesInDirToolHandler instance in the tool_handlers dictionary, making the 'obsidian_list_files_in_dir' tool available to the MCP server.
    add_tool_handler(tools.ListFilesInDirToolHandler())
  • The Obsidian client's list_files_in_dir method performs the HTTP GET request to the Obsidian API endpoint '/vault/{dirpath}/' to retrieve the list of files and directories in the specified path.
    def list_files_in_dir(self, dirpath: str) -> Any:
        url = f"{self.get_base_url()}/vault/{dirpath}/"
        
        def call_fn():
            response = requests.get(url, headers=self._get_headers(), verify=self.verify_ssl, timeout=self.timeout)
            response.raise_for_status()
            
            return response.json()['files']
    
        return self._safe_call(call_fn)
  • Defines the constant string for the tool name used in the handler initialization.
    TOOL_LIST_FILES_IN_DIR = "obsidian_list_files_in_dir"

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/MarkusPfundstein/mcp-obsidian'

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