Skip to main content
Glama

list_directory

Browse files and folders in your workspace to locate Markdown documents for editing.

Instructions

Lists files and folders in the workspace.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathNo.

Implementation Reference

  • Core handler function in FileOperationsTool class that lists directory contents using os.scandir, builds item dictionaries with name, is_dir, size, and relative path.
    async def list_directory(self, path: str = ".") -> Optional[List[Dict[str, Any]]]: """List files and folders""" try: abs_path = self._get_abs_path(path) if not os.path.exists(abs_path): return None items = [] for entry in os.scandir(abs_path): items.append({ "name": entry.name, "is_dir": entry.is_dir(), "size": entry.stat().st_size if entry.is_file() else 0, "path": os.path.relpath(entry.path, self.base_path) }) return items except Exception as e: logger.error(f"Error listing directory {path}: {e}") return None
  • Registration of the list_directory tool within the MCP server's list_tools() handler, defining name, title, description, inputSchema, and outputSchema.
    Tool( name="list_directory", title="List Directory", description="Lists files and folders in the workspace.", inputSchema={ "type": "object", "properties": { "path": { "type": "string", "default": ".", "examples": [".", "./docs", "/path/to/directory"] } }, "additionalProperties": False }, outputSchema={ "type": "object", "properties": { "items": { "type": "array", "description": "List of directory entries", "items": { "type": "object", "properties": { "name": {"type": "string"}, "is_dir": {"type": "boolean"}, "size": {"type": "integer"}, "path": {"type": "string"} } } } } } ),
  • Dispatch handler in the server's call_tool() function that extracts arguments, calls the list_directory function, and formats the MCP response.
    if name == "list_directory": path = arguments.get("path", ".") items = await list_directory(path) return {"content": [TextContent(type="text", text=str(items))], "structuredContent": {"items": items}, "isError": items is None}
  • Async wrapper function imported by server.py that delegates to the singleton FileOperationsTool instance.
    async def list_directory(path: str = "."): return await _instance.list_directory(path)
  • Private helper method to resolve input path to absolute path within the configured base directory for path traversal security.
    def _get_abs_path(self, path: str) -> str: # Ensure work within base directory if os.path.isabs(path): return path return os.path.abspath(os.path.join(self.base_path, path))

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/KazKozDev/markdown-editor-mcp-server'

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