Skip to main content
Glama

find_files

Search for files in code repositories using glob patterns to locate specific files, check file existence, or gather file lists for analysis.

Instructions

Find files matching a glob pattern using pre-built file index.

Use when:
- Looking for files by pattern (e.g., "*.py", "test_*.js")
- Searching by filename only (e.g., "README.md" finds all README files)
- Checking if specific files exist in the project
- Getting file lists for further analysis

Pattern matching:
- Supports both full path and filename-only matching
- Uses standard glob patterns (*, ?, [])
- Fast lookup using in-memory file index
- Uses forward slashes consistently across all platforms

Args:
    pattern: Glob pattern to match files (e.g., "*.py", "test_*.js", "README.md")

Returns:
    List of file paths matching the pattern

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
patternYes

Implementation Reference

  • The primary MCP tool handler and registration for the 'find_files' tool. It defines the tool interface, documentation, and delegates execution to FileDiscoveryService.
    @handle_mcp_tool_errors(return_type='list')
    def find_files(pattern: str, ctx: Context) -> List[str]:
        """
        Find files matching a glob pattern using pre-built file index.
    
        Use when:
        - Looking for files by pattern (e.g., "*.py", "test_*.js")
        - Searching by filename only (e.g., "README.md" finds all README files)
        - Checking if specific files exist in the project
        - Getting file lists for further analysis
    
        Pattern matching:
        - Supports both full path and filename-only matching
        - Uses standard glob patterns (*, ?, [])
        - Fast lookup using in-memory file index
        - Uses forward slashes consistently across all platforms
    
        Args:
            pattern: Glob pattern to match files (e.g., "*.py", "test_*.js", "README.md")
    
        Returns:
            List of file paths matching the pattern
        """
        return FileDiscoveryService(ctx).find_files(pattern)
  • Helper service implementing business logic for file discovery: input validation, limiting results, and delegation to the shallow index manager.
    def find_files(self, pattern: str, max_results: Optional[int] = None) -> List[str]:
        """
        Find files matching the given pattern using JSON indexing.
    
        Args:
            pattern: Glob pattern to search for (e.g., "*.py", "test_*.js")
            max_results: Maximum number of results to return (None for no limit)
    
        Returns:
            List of file paths matching the pattern
    
        Raises:
            ValueError: If pattern is invalid or project not set up
        """
        # Business validation
        self._validate_discovery_request(pattern)
    
        # Get files from JSON index
        files = self._index_manager.find_files(pattern)
        
        # Apply max_results limit if specified
        if max_results and len(files) > max_results:
            files = files[:max_results]
        
        return files
  • Core implementation of glob pattern matching in the ShallowIndexManager. Converts glob patterns to regex, applies multiple matching strategies (exact, recursive, case-insensitive), and returns matching file paths from the in-memory index.
    def find_files(self, pattern: str = "*") -> List[str]:
        with self._lock:
            if not isinstance(pattern, str):
                return []
            norm = (pattern.strip() or "*").replace('\\\\','/').replace('\\','/')
            files = self._file_list or []
    
            # Fast path: wildcard all
            if norm == "*":
                return list(files)
    
            # 1) Exact, case-sensitive
            exact_regex = self._compile_glob_regex(norm)
            exact_hits = [f for f in files if exact_regex.match(f) is not None]
            if exact_hits or '/' in norm:
                return exact_hits
    
            # 2) Recursive **/ fallback (case-sensitive)
            recursive_pattern = f"**/{norm}"
            rec_regex = self._compile_glob_regex(recursive_pattern)
            rec_hits = [f for f in files if rec_regex.match(f) is not None]
            if rec_hits:
                return self._dedupe_preserve_order(exact_hits + rec_hits)
    
            # 3) Case-insensitive (root only)
            ci_regex = self._compile_glob_regex(norm, ignore_case=True)
            ci_hits = [f for f in files if ci_regex.match(f) is not None]
            if ci_hits:
                return self._dedupe_preserve_order(exact_hits + rec_hits + ci_hits)
    
            # 4) Case-insensitive recursive
            rec_ci_regex = self._compile_glob_regex(recursive_pattern, ignore_case=True)
            rec_ci_hits = [f for f in files if rec_ci_regex.match(f) is not None]
            if rec_ci_hits:
                return self._dedupe_preserve_order(
                    exact_hits + rec_hits + ci_hits + rec_ci_hits
                )
    
            return []

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/johnhuang316/code-index-mcp'

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