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
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | The absolute path to the file to read | |
| limit | No | The number of lines to read | |
| offset | No | The line number to start reading from |
Implementation Reference
- src/server/tools.ts:63-78 (handler)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 }; } }
- src/server/tools.ts:59-62 (schema)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") },
- src/server/tools.ts:55-79 (registration)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 }; } } );
- src/utils/file.ts:15-34 (helper)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; } }