read_file
Retrieve file contents from a specified path with options for encoding, line ranges, and size limits to access and process text data.
Instructions
Read the contents of a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to the file | |
| encoding | No | File encoding (default: utf-8) | |
| start_line | No | Start line (1-indexed) | |
| end_line | No | End line (inclusive) | |
| max_size_kb | No | Max file size in KB (default: 10240) |
Implementation Reference
- src/tools/read.ts:22-133 (handler)The core logic for reading files, including validation, file size checks, and optional line range parsing.
async function readFileImpl(input: ReadFileInput): Promise<ToolResult> { try { const absolutePath = path.resolve(input.path); // Check if file exists const stats = await fs.stat(absolutePath); if (!stats.isFile()) { return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'INVALID_PATH', message: `Path is not a file: ${input.path}`, }), }, ], }; } // Check file size const maxBytes = input.max_size_kb * 1024; if (stats.size > maxBytes) { return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'FILE_TOO_LARGE', message: `File size ${stats.size} bytes exceeds limit of ${maxBytes} bytes`, details: { file_size: stats.size, limit: maxBytes, }, }), }, ], }; } // Read file content const content = await fs.readFile(absolutePath, { encoding: input.encoding as BufferEncoding, }); // Handle line ranges if specified let result = content; if (input.start_line !== undefined || input.end_line !== undefined) { const lines = content.split('\n'); const startIdx = (input.start_line ?? 1) - 1; const endIdx = input.end_line ?? lines.length; result = lines.slice(startIdx, endIdx).join('\n'); } return { content: [ { type: 'text', text: result, }, ], }; } catch (error) { const err = error as NodeJS.ErrnoException; if (err.code === 'ENOENT') { return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'FILE_NOT_FOUND', message: `File not found: ${input.path}`, }), }, ], }; } if (err.code === 'EACCES') { return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'PERMISSION_DENIED', message: `Permission denied: ${input.path}`, }), }, ], }; } return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'UNKNOWN_ERROR', message: `Error reading file: ${err.message}`, }), }, ], }; } } - src/tools/read.ts:356-371 (registration)Registration of the 'read_file' tool with the MCP server using zod schema validation.
// read_file tool server.tool( 'read_file', 'Read the contents of a file', { path: z.string().describe('Path to the file'), encoding: z.string().optional().describe('File encoding (default: utf-8)'), start_line: z.number().optional().describe('Start line (1-indexed)'), end_line: z.number().optional().describe('End line (inclusive)'), max_size_kb: z.number().optional().describe('Max file size in KB (default: 10240)'), }, async (args) => { const input = ReadFileInputSchema.parse(args); return await readFileImpl(input); } );