Skip to main content
Glama

readFile

Read file contents from local filesystem with optional line range selection for targeted data extraction.

Instructions

Read a file from the local filesystem

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYesThe absolute path to the file to read
offsetNoThe line number to start reading from
limitNoThe number of lines to read

Implementation Reference

  • MCP tool handler for readFile: calls the readFile utility, handles errors, and returns formatted text content.
    async ({ file_path, offset, limit }) => {
      try {
        const content = await readFile(file_path, offset, limit);
        return {
          content: [{ type: "text", text: content }]
        };
      } catch (error) {
        return {
          content: [{ 
            type: "text", 
            text: error instanceof Error ? error.message : String(error)
          }],
          isError: true
        };
      }
    }
  • Input schema using Zod for readFile tool parameters: file_path (string), offset (number optional), limit (number optional).
    {
      file_path: z.string().describe("The absolute path to the file to read"),
      offset: z.number().optional().describe("The line number to start reading from"),
      limit: z.number().optional().describe("The number of lines to read")
    },
  • Registers the readFile tool on the MCP server with name, description, input schema, and handler function.
    server.tool(
      "readFile",
      "Read a file from the local filesystem",
      {
        file_path: z.string().describe("The absolute path to the file to read"),
        offset: z.number().optional().describe("The line number to start reading from"),
        limit: z.number().optional().describe("The number of lines to read")
      },
      async ({ file_path, offset, limit }) => {
        try {
          const content = await readFile(file_path, offset, limit);
          return {
            content: [{ type: "text", text: content }]
          };
        } catch (error) {
          return {
            content: [{ 
              type: "text", 
              text: error instanceof Error ? error.message : String(error)
            }],
            isError: true
          };
        }
      }
    );
  • Utility function implementing file reading logic with fs.promises.readFile, supporting line-based offset and limit.
    export async function readFile(
      filePath: string,
      offset?: number,
      limit?: number
    ): Promise<string> {
      try {
        let content = await fs.readFile(filePath, 'utf-8');
        
        if (offset || limit) {
          const lines = content.split('\n');
          const startLine = offset ? offset - 1 : 0;
          const endLine = limit ? startLine + limit : lines.length;
          content = lines.slice(startLine, endLine).join('\n');
        }
        
        return content;
      } catch (error) {
        throw error;
      }
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the basic action but lacks details on error handling (e.g., for missing files), performance considerations, or output format (e.g., text vs. binary). This leaves significant gaps in understanding how the tool behaves in practice.

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?

The description is a single, direct sentence with no wasted words, making it highly efficient and easy to parse. It front-loads the core purpose without unnecessary elaboration, which is ideal for quick understanding.

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

Completeness2/5

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

Given the tool's complexity (file I/O with parameters) and lack of annotations and output schema, the description is insufficient. It doesn't cover critical aspects like return values, error cases, or security implications, leaving the agent poorly equipped to use the tool effectively in real scenarios.

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?

The schema description coverage is 100%, with clear descriptions for all parameters (file_path, offset, limit). The description doesn't add any semantic details beyond what the schema provides, such as explaining how offset and limit interact or file encoding issues. This meets the baseline for high schema coverage.

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?

The description clearly states the action ('Read') and resource ('a file from the local filesystem'), making the purpose immediately understandable. However, it doesn't differentiate from sibling tools like 'grep' or 'searchGlob' that also involve file reading operations, which prevents a perfect score.

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?

The description provides no guidance on when to use this tool versus alternatives like 'grep' for searching or 'listFiles' for directory contents. There's no mention of prerequisites, such as file existence or permissions, which leaves the agent without context for appropriate tool selection.

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/auchenberg/claude-code-mcp'

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