get_file_metadata
Retrieve metadata for specific files within a project by providing the project name and file path. Supports code analysis and context management using tree-sitter.
Instructions
Get metadata for a file.
Args:
project: Project name
path: File path relative to project root
Returns:
File metadata
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| project | Yes |
Implementation Reference
- Core implementation of file metadata retrieval, including stat info, line count, security validation, and error handling.def get_file_info(project: Any, path: str) -> Dict[str, Any]: """ Get metadata about a file. Args: project: Project object path: Path to the file, relative to project root Returns: Dictionary with file information Raises: ProjectError: If project not found FileAccessError: If file access fails """ try: file_path = project.get_file_path(path) except ProjectError as e: raise FileAccessError(str(e)) from e try: validate_file_access(file_path, project.root_path) except Exception as e: raise FileAccessError(f"Access denied: {e}") from e try: stat = file_path.stat() return { "path": str(path), "size": stat.st_size, "last_modified": stat.st_mtime, "created": stat.st_ctime, "is_directory": file_path.is_dir(), "extension": file_path.suffix[1:] if file_path.suffix else None, "line_count": count_lines(file_path) if file_path.is_file() else None, } except FileNotFoundError as e: raise FileAccessError(f"File not found: {path}") from e except PermissionError as e: raise FileAccessError(f"Permission denied: {path}") from e except Exception as e: raise FileAccessError(f"Error getting file info: {e}") from e
- src/mcp_server_tree_sitter/tools/registration.py:215-229 (registration)MCP tool registration for 'get_file_metadata', which wraps and delegates to the get_file_info handler.@mcp_server.tool() def get_file_metadata(project: str, path: str) -> Dict[str, Any]: """Get metadata for a file. Args: project: Project name path: File path relative to project root Returns: File metadata """ from ..tools.file_operations import get_file_info return get_file_info(project_registry.get_project(project), path)
- Helper function used by get_file_info to compute line count efficiently.def count_lines(file_path: Path) -> int: """ Count lines in a file efficiently. Args: file_path: Path to the file Returns: Number of lines """ try: with open(file_path, "rb") as f: return sum(1 for _ in f) except (IOError, OSError): return 0