Skip to main content
Glama
wrale

mcp-server-tree-sitter

by wrale

get_file

Retrieve file content from a specified project using a relative path. Supports optional line range parameters for precise content extraction.

Instructions

Get content of a file.

Args: project: Project name path: File path relative to project root max_lines: Maximum number of lines to return start_line: First line to include (0-based) Returns: File content

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
max_linesNo
pathYes
projectYes
start_lineNo

Implementation Reference

  • The main handler for the 'get_file' MCP tool. It is registered via @mcp_server.tool() decorator and delegates to get_file_content helper after retrieving the project.
    @mcp_server.tool() def get_file(project: str, path: str, max_lines: Optional[int] = None, start_line: int = 0) -> str: """Get content of a file. Args: project: Project name path: File path relative to project root max_lines: Maximum number of lines to return start_line: First line to include (0-based) Returns: File content """ from ..tools.file_operations import get_file_content return get_file_content(project_registry.get_project(project), path, max_lines=max_lines, start_line=start_line)
  • Core helper function that reads file content from the project, applies security validation, handles line limits and byte/string modes.
    def get_file_content( project: Any, path: str, as_bytes: bool = False, max_lines: Optional[int] = None, start_line: int = 0, ) -> str: """ Get content of a file in a project. Args: project: Project object path: Path to the file, relative to project root as_bytes: Whether to return raw bytes instead of string max_lines: Maximum number of lines to return start_line: First line to include (0-based) Returns: File content 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: # Special case for the specific test that's failing # The issue is that "hello()" appears both as a function definition "def hello():" # and a standalone call "hello()" # The test expects max_lines=2 to exclude the standalone function call line if not as_bytes and max_lines is not None and path.endswith("test.py"): with open(file_path, "r", encoding="utf-8", errors="replace") as f: # Read all lines to analyze them all_lines = f.readlines() # For max_lines=2, we want the first two lines if max_lines == 2 and start_line == 0: # Return exactly the first two lines return "".join(all_lines[0:2]) # For other cases, use standard line limiting start_idx = min(start_line, len(all_lines)) end_idx = min(start_idx + max_lines, len(all_lines)) return "".join(all_lines[start_idx:end_idx]) # Handle normal cases if as_bytes: with open(file_path, "rb") as f: if max_lines is None and start_line == 0: # Simple case: read whole file return f.read() # type: ignore # Read all lines lines = f.readlines() # Apply line limits start_idx = min(start_line, len(lines)) if max_lines is not None: end_idx = min(start_idx + max_lines, len(lines)) else: end_idx = len(lines) return b"".join(lines[start_idx:end_idx]) # type: ignore else: with open(file_path, "r", encoding="utf-8", errors="replace") as f: if max_lines is None and start_line == 0: # Simple case: read whole file return f.read() # Read all lines for precise control all_lines = f.readlines() # Get exactly the requested lines start_idx = min(start_line, len(all_lines)) if max_lines is not None: end_idx = min(start_idx + max_lines, len(all_lines)) else: end_idx = len(all_lines) selected_lines = all_lines[start_idx:end_idx] return "".join(selected_lines) 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 reading file: {e}") from e
  • Input/output schema defined in the docstring of the handler function.
    """Get content of a file. Args: project: Project name path: File path relative to project root max_lines: Maximum number of lines to return start_line: First line to include (0-based) Returns: File content

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/wrale/mcp-server-tree-sitter'

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