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