list_directory
Lists directory contents to view files and folders at a specified path, enabling navigation and management of file structures.
Instructions
Lists directory contents using system ls/dir command
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- The core handler function for the 'list_directory' tool. Validates the path, checks if it's a directory, executes the appropriate system command ('ls -la' on Unix or 'dir' on Windows), and returns the raw output as a string.async def list_directory(self, path: str) -> str: """List contents of a directory using system ls/dir command. Args: path: Directory path to list Returns: Raw command output as string """ path = await self.validate_path(path) if not path.is_dir(): raise ValueError(f"Path {path} is not a directory") if sys.platform == "win32": cmd = ["dir", path] else: cmd = ["ls", "-la", path] proc = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) stdout, _ = await proc.communicate() return stdout.decode()
- Pydantic BaseModel defining the input schema for the 'list_directory' tool, specifying a 'path' parameter of type str or Path.class ListDirectory(BaseModel): path: str | Path
- src/mcp_server_code_assist/server.py:80-83 (registration)Tool registration in the @server.list_tools() handler, defining the tool name, description, and input schema.name=CodeAssistTools.LIST_DIRECTORY, description="Lists directory contents using system ls/dir command", inputSchema=ListDirectory.model_json_schema(), ),
- src/mcp_server_code_assist/server.py:161-164 (registration)Tool invocation handler in the @server.call_tool() function, which parses arguments using the schema, calls the dir_tools.list_directory method, and returns the result as TextContent.case CodeAssistTools.LIST_DIRECTORY: model = ListDirectory(path=arguments["path"]) result = await dir_tools.list_directory(model.path) return [TextContent(type="text", text=result)]
- Helper method used by list_directory to validate and resolve the input path, ensuring it's within allowed directories.async def validate_path(self, path: str) -> Path: """Validate and resolve path. Args: path: Path to validate Returns: Resolved Path object Raises: ValueError: If path is outside allowed directories """ abs_path = os.path.abspath(path) if not any(abs_path.startswith(p) for p in self.allowed_paths): raise ValueError(f"Path {path} is outside allowed directories") return Path(abs_path)