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
| Name | Required | Description | Default |
|---|---|---|---|
| path | No | . |
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
- src/markdown_editor/server.py:400-433 (registration)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"} } } } } } ),
- src/markdown_editor/server.py:524-527 (handler)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))