Skip to main content
Glama
Preston-Harrison

Filesystem MCP Server

search_files

Search for files by name pattern within directories, including subdirectories, while respecting .gitignore and excluding hidden items.

Instructions

Search for files by name pattern in a directory recursively.

Args: dir (str): Directory to search in (absolute or relative to allowed directories) pattern (str): Glob-style pattern to match file names (e.g., '.py', 'test_') exclude (str, optional): Glob-style pattern to exclude file names

Returns: List[str] | str: List of matching absolute file paths, or error message if failed

Note: - Directory must be within allowed directory roots - Searches recursively through subdirectories - Respects .gitignore files, and ignores hidden files and folders - Returns list for successful searches, string for errors

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dirYes
patternYes
excludeNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • main.py:461-498 (handler)
    The core handler function for the 'search_files' MCP tool. It is decorated with @mcp.tool, which registers it automatically with the name 'search_files'. The function performs recursive file search using glob patterns, respects .gitignore via _skip_ignored, applies exclusions, and operates only within allowed directories using _resolve. Returns list of matching paths or error string.
    @mcp.tool
    def search_files(dir: str, pattern: str, exclude: str | None = None) -> List[str] | str:
        """Search for files by name pattern in a directory recursively.
    
        Args:
            dir (str): Directory to search in (absolute or relative to allowed directories)
            pattern (str): Glob-style pattern to match file names (e.g., '*.py', 'test_*')
            exclude (str, optional): Glob-style pattern to exclude file names
    
        Returns:
            List[str] | str: List of matching absolute file paths, or error message if failed
    
        Note:
            - Directory must be within allowed directory roots
            - Searches recursively through subdirectories
            - Respects .gitignore files, and ignores hidden files and folders
            - Returns list for successful searches, string for errors
        """
        try:
            root = _resolve(dir)
            if not root.is_dir():
                return f"Error searching files: '{root}' is not a directory"
            spec_cache: Dict[Path, Optional[pathspec.PathSpec]] = {}
            matches: List[str] = []
            for file in root.rglob("*"):
                if file.is_dir():
                    continue
                if exclude and fnmatch.fnmatch(file.name, exclude):
                    continue
                if not fnmatch.fnmatch(file.name, pattern):
                    continue
                if _skip_ignored(file, root, spec_cache):
                    continue
                matches.append(str(file))
            return matches
        except Exception as e:
            return _human_error(e, "searching files")
  • main.py:461-461 (registration)
    The @mcp.tool decorator registers the search_files function as an MCP tool named 'search_files'.
    @mcp.tool
  • The docstring provides the input schema (parameters with descriptions) and output schema for the tool, which FastMCP uses for validation.
    """Search for files by name pattern in a directory recursively.
    
    Args:
        dir (str): Directory to search in (absolute or relative to allowed directories)
        pattern (str): Glob-style pattern to match file names (e.g., '*.py', 'test_*')
        exclude (str, optional): Glob-style pattern to exclude file names
    
    Returns:
        List[str] | str: List of matching absolute file paths, or error message if failed
    
    Note:
        - Directory must be within allowed directory roots
        - Searches recursively through subdirectories
        - Respects .gitignore files, and ignores hidden files and folders
        - Returns list for successful searches, string for errors
    """
Behavior4/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure and does so effectively. It describes key behavioral traits: directory restrictions ('within allowed directory roots'), recursion behavior, respect for .gitignore, ignoring hidden files/folders, and return format (list for success, string for errors). This provides comprehensive operational context beyond basic functionality.

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 perfectly structured with clear sections (purpose, Args, Returns, Note) and every sentence earns its place. The initial statement is front-loaded with core functionality, followed by organized details. No wasted words while maintaining complete clarity.

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

Completeness5/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 (3 parameters, recursive search behavior) and the presence of an output schema (which handles return value documentation), the description is complete. It covers purpose, parameters, behavioral constraints, and error handling, leaving no significant gaps for agent understanding.

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

Parameters5/5

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

With 0% schema description coverage, the description fully compensates by explaining all three parameters: 'dir' (directory to search, absolute/relative to allowed directories), 'pattern' (glob-style pattern for matching), and 'exclude' (optional glob-style pattern for exclusion). It provides concrete examples ('*.py', 'test_*') and clarifies the optional nature of 'exclude', adding significant value beyond the bare schema.

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 specific action ('Search for files by name pattern') and resource ('in a directory recursively'), distinguishing it from siblings like 'list_directory' (non-recursive listing) or 'grep' (content search). The verb+resource combination is precise and unambiguous.

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

Usage Guidelines4/5

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

The description provides clear context about when to use this tool (recursive file search by name pattern) but doesn't explicitly mention when not to use it or name specific alternatives. It implies usage scenarios but lacks explicit exclusions or comparisons with sibling tools like 'grep' for content-based searches.

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/Preston-Harrison/fs-mcp-py'

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