Skip to main content
Glama

read_code

Read file content from specified paths, optionally extracting specific line ranges to access code sections directly for analysis or review.

Instructions

Read file content, optionally by line range (1-based).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
start_lineNo
end_lineNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The `read_code` tool handler function implementation, which reads a file and optionally returns a range of lines.
    async def read_code(
        file_path: str,
        start_line: int | None = None,
        end_line: int | None = None
    ) -> str:
        """Read file content, optionally by line range (1-based)."""
        path = Path(file_path).resolve()
        if not path.exists():
            return _error(f"File not found: {file_path}")
    
        try:
            content = path.read_text(encoding="utf-8")
            lines = content.splitlines()
            total_lines = len(lines)
    
            start = (start_line or 1) - 1
            end = end_line or total_lines
    
            if start < 0:
                start = 0
            if end > total_lines:
                end = total_lines
            if start >= total_lines:
                return _error(f"start_line {start_line} exceeds file length {total_lines}")
    
            selected = {i + 1: lines[i] for i in range(start, end)}
    
            return _ok({
                "file": path.name,
                "path": str(path),
                "range": {"start": start + 1, "end": end},
                "total_lines": total_lines,
                "lines": selected
            })
Behavior3/5

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

No annotations provided, so description carries full burden. Adds critical behavioral detail '1-based' indexing for line ranges (essential for correctness), but omits error handling (missing files, invalid ranges), output format, and auth requirements.

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?

Single sentence, 9 words. Main action front-loaded, modifiers follow. Both 'optionally' and '1-based' are essential information; zero waste.

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

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Has output schema (covering return values) and simple functionality. Given 0% schema coverage and no annotations, the 1-based indexing detail is the minimal necessary addition. Adequate but could include file existence or range validation behavior.

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

Parameters3/5

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

Schema has 0% description coverage (titles only). Description compensates partially by explaining 'line range' semantics and the '1-based' indexing for start_line/end_line, but does not describe file_path constraints or interaction between range parameters (e.g., can provide only start_line?).

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

Clear verb 'Read' and resource 'file content', with specific scope detail about line ranges. Distinguishes from write-oriented siblings (apply_code_action, safe_rename) implicitly via 'Read', though does not explicitly differentiate from read_context.

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

Usage Guidelines2/5

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

No explicit guidance on when to use versus siblings like read_context or analyze_file. Only mentions that line ranges are optional, which implies usage but does not constitute explicit when-to-use guidance.

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/qinsehm1128/mcp-ty'

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