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;
      }
    }

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