list_files
List files in a project by specifying a project name, optional glob pattern, file extensions, and maximum directory depth. Returns a list of file paths for streamlined codebase access.
Instructions
List files in a project.
Args:
project: Project name
pattern: Optional glob pattern (e.g., "**/*.py")
max_depth: Maximum directory depth
extensions: List of file extensions to include (without dot)
Returns:
List of file paths
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| extensions | No | ||
| max_depth | No | ||
| pattern | No | ||
| project | Yes |
Implementation Reference
- src/mcp_server_tree_sitter/server.py:152-155 (registration)The point where register_tools is called to register all MCP tools, including 'list_files', on the server instance.from .tools.registration import register_tools register_capabilities(mcp) register_tools(mcp, container)
- The handler function for the 'list_files' tool, decorated with @mcp_server.tool(). It validates input via type hints and delegates execution to the list_project_files helper.@mcp_server.tool() def list_files( project: str, pattern: Optional[str] = None, max_depth: Optional[int] = None, extensions: Optional[List[str]] = None, ) -> List[str]: """List files in a project. Args: project: Project name pattern: Optional glob pattern (e.g., "**/*.py") max_depth: Maximum directory depth extensions: List of file extensions to include (without dot) Returns: List of file paths """ from ..tools.file_operations import list_project_files return list_project_files(project_registry.get_project(project), pattern, max_depth, extensions) @mcp_server.tool()
- The core helper function implementing the file listing logic using pathlib.Path.glob() with support for glob patterns, max_depth limiting, and extension filtering. Returns sorted list of relative file paths.def list_project_files( project: Any, pattern: Optional[str] = None, max_depth: Optional[int] = None, filter_extensions: Optional[List[str]] = None, ) -> List[str]: """ List files in a project, optionally filtered by pattern. Args: project: Project object pattern: Glob pattern for files (e.g., "**/*.py") max_depth: Maximum directory depth to traverse filter_extensions: List of file extensions to include (without dot) Returns: List of relative file paths """ root = project.root_path pattern = pattern or "**/*" files = [] # Handle max_depth=0 specially to avoid glob patterns with /* if max_depth == 0: # For max_depth=0, only list files directly in root directory for path in root.iterdir(): if path.is_file(): # Skip files that don't match extension filter if filter_extensions and path.suffix.lower()[1:] not in filter_extensions: continue # Get path relative to project root rel_path = path.relative_to(root) files.append(str(rel_path)) return sorted(files) # Handle max depth for glob pattern for max_depth > 0 if max_depth is not None and max_depth > 0 and "**" in pattern: parts = pattern.split("**") if len(parts) == 2: pattern = f"{parts[0]}{'*/' * max_depth}{parts[1]}" # Ensure pattern doesn't start with / to avoid NotImplementedError if pattern.startswith("/"): pattern = pattern[1:] # Convert extensions to lowercase for case-insensitive matching if filter_extensions: filter_extensions = [ext.lower() for ext in filter_extensions] for path in root.glob(pattern): if path.is_file(): # Skip files that don't match extension filter if filter_extensions and path.suffix.lower()[1:] not in filter_extensions: continue # Get path relative to project root rel_path = path.relative_to(root) files.append(str(rel_path)) return sorted(files)