Skip to main content
Glama

readFile

Extract specific file content by specifying file path, starting line offset, and number of lines to read using the MCP server for streamlined file access.

Instructions

Read a file from the local filesystem

Input Schema

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

Implementation Reference

  • The MCP tool handler for 'readFile' that receives parameters, calls the readFile utility, and returns the file content or error response.
    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 }; } }
  • Zod input schema defining parameters for the readFile tool: file_path (required string), offset and limit (optional numbers).
    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") },
  • Registration of the 'readFile' tool via server.tool, including name, description, schema, and handler.
    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 }; } } );
  • Helper utility function that reads file content using fs.promises.readFile and supports optional 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; } }

Other Tools

Related 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