Skip to main content
Glama

List Dir

list_dir
Read-only

Scan directories to list files and subfolders, optionally with recursive searching and ignored file filtering.

Instructions

Lists files and directories in the given directory (optionally with recursion). Returns a JSON object with the names of directories and files within the given directory.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
relative_pathYesThe relative path to the directory to list; pass "." to scan the project root.
recursiveYesWhether to scan subdirectories recursively.
skip_ignored_filesNoWhether to skip files and directories that are ignored.
max_answer_charsNoIf the output is longer than this number of characters, no content will be returned. -1 means the default value from the config will be used. Don't adjust unless there is really no other way to get the content required for the task.

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The handler function for the 'list_dir' tool. It lists files and directories in a given relative path, optionally recursively, skipping ignored files, and returns a JSON object with directories and files.
    class ListDirTool(Tool):
        """
        Lists files and directories in the given directory (optionally with recursion).
        """
    
        def apply(self, relative_path: str, recursive: bool, skip_ignored_files: bool = False, max_answer_chars: int = -1) -> str:
            """
            Lists files and directories in the given directory (optionally with recursion).
    
            :param relative_path: the relative path to the directory to list; pass "." to scan the project root
            :param recursive: whether to scan subdirectories recursively
            :param skip_ignored_files: whether to skip files and directories that are ignored
            :param max_answer_chars: if the output is longer than this number of characters,
                no content will be returned. -1 means the default value from the config will be used.
                Don't adjust unless there is really no other way to get the content required for the task.
            :return: a JSON object with the names of directories and files within the given directory
            """
            # Check if the directory exists before validation
            if not self.project.relative_path_exists(relative_path):
                error_info = {
                    "error": f"Directory not found: {relative_path}",
                    "project_root": self.get_project_root(),
                    "hint": "Check if the path is correct relative to the project root",
                }
                return self._to_json(error_info)
    
            self.project.validate_relative_path(relative_path, require_not_ignored=skip_ignored_files)
    
            dirs, files = scan_directory(
                os.path.join(self.get_project_root(), relative_path),
                relative_to=self.get_project_root(),
                recursive=recursive,
                is_ignored_dir=self.project.is_ignored_path if skip_ignored_files else None,
                is_ignored_file=self.project.is_ignored_path if skip_ignored_files else None,
            )
    
            result = self._to_json({"dirs": dirs, "files": files})
            return self._limit_length(result, max_answer_chars)
  • The get_name_from_cls method derives the tool name 'list_dir' from the class name 'ListDirTool' by removing 'Tool' suffix and converting to snake_case.
    @classmethod
    def get_name_from_cls(cls) -> str:
        name = cls.__name__
        if name.endswith("Tool"):
            name = name[:-4]
        # convert to snake_case
        name = "".join(["_" + c.lower() if c.isupper() else c for c in name]).lstrip("_")
        return name
    
    def get_name(self) -> str:
        return self.get_name_from_cls()
  • ToolRegistry.__init__ automatically discovers all subclasses of Tool in serena.tools packages and registers ListDirTool as 'list_dir'.
    for cls in iter_subclasses(Tool):
        if not any(cls.__module__.startswith(pkg) for pkg in tool_packages):
            continue
        is_optional = issubclass(cls, ToolMarkerOptional)
        name = cls.get_name_from_cls()
        if name in self._tool_dict:
            raise ValueError(f"Duplicate tool name found: {name}. Tool classes must have unique names.")
        self._tool_dict[name] = RegisteredTool(tool_class=cls, is_optional=is_optional, tool_name=name)
  • Derives the input schema and description for the MCP tool from the apply method's signature and docstring using func_metadata.
    def get_apply_fn_metadata_from_cls(cls) -> FuncMetadata:
        """Get the metadata for the apply method from the class (static metadata).
        Needed for creating MCP tools in a separate process without running into serialization issues.
        """
        # First try to get from __dict__ to handle dynamic docstring changes
        if "apply" in cls.__dict__:
            apply_fn = cls.__dict__["apply"]
        else:
            # Fall back to getattr for inherited methods
            apply_fn = getattr(cls, "apply", None)
            if apply_fn is None:
                raise AttributeError(f"apply method not defined in {cls}. Did you forget to implement it?")
    
        return func_metadata(apply_fn, skip_names=["self", "cls"])
  • __init__.py imports file_tools making ListDirTool available for discovery by ToolRegistry.
    from .tools_base import *
    from .file_tools import *
    from .symbol_tools import *
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare readOnlyHint=true and destructiveHint=false, indicating a safe read operation. The description adds value by specifying the return format ('JSON object with names of directories and files') and hinting at recursion behavior, but does not disclose additional traits like rate limits, auth needs, or error conditions beyond what annotations provide.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is front-loaded with the core purpose in the first sentence and adds useful details in the second. Both sentences earn their place by clarifying functionality and output format without redundancy or unnecessary elaboration, making it efficient and well-structured.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity, rich annotations (readOnlyHint, destructiveHint), and the presence of an output schema, the description is largely complete. It covers purpose, optional recursion, and return format, though it could benefit from more explicit usage guidelines or edge-case handling to be fully comprehensive.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so parameters are fully documented in the schema. The description adds minimal semantics by mentioning recursion and the return format, but does not provide extra details on parameter usage or interactions beyond what the schema already covers, aligning with the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb ('Lists') and resource ('files and directories'), specifies the scope ('in the given directory'), and mentions an optional feature ('with recursion'). It distinguishes itself from sibling tools like 'find_file' or 'search_for_pattern' by focusing on directory listing rather than searching or filtering.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for directory listing but does not explicitly state when to use this tool versus alternatives like 'find_file' or 'search_for_pattern'. It mentions recursion as an option but lacks guidance on scenarios where recursion is preferred or when to avoid it, leaving usage context somewhat implied.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/oraios/serena'

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