Skip to main content
Glama
lofcz

MCP Smart Filesystem Server

by lofcz

read_file

Read file contents from the filesystem, handling large files through chunked reading with line-based pagination for efficient processing.

Instructions

Read file contents. For large files (>500 lines), use start_line to read in chunks (e.g., 0, 500, 1000). Each call returns up to 500 lines. Binary files return metadata only.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesFile path to read
start_lineNoLine number to start reading from (0-indexed). For large files, read in chunks: start_line=0 (first 500), start_line=500 (next 500), etc.

Implementation Reference

  • Main handler for the 'read_file' tool. Handles input validation with Zod, path validation, binary file detection, file reading via helper, pagination logic, and formats the response as MCP content.
    case 'read_file': { const schema = z.object({ path: z.string(), start_line: z.number().optional().default(0) }); const { path, start_line } = schema.parse(args); const validatedPath = await validatePath(path); // Check if binary const isBinary = await isBinaryFile(validatedPath); if (isBinary) { const stats = await getFileStats(validatedPath); return { content: [{ type: 'text', text: JSON.stringify({ path: validatedPath, error: 'Binary file', message: 'This appears to be a binary file. Use get_file_info for metadata.', size: stats.size, type: 'binary' }, null, 2) }] }; } // Read file content const content = await readFileContent(validatedPath); const totalLines = countLines(content); // Check if pagination is needed if (shouldPaginate(totalLines) || start_line > 0) { const result = paginateFileContent(validatedPath, content, start_line, LINES_PER_CHUNK); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; } // Return full file return { content: [{ type: 'text', text: JSON.stringify({ path: validatedPath, content, startLine: 0, endLine: totalLines - 1, totalLines, hasMore: false }, null, 2) }] }; }
  • MCP tool definition for 'read_file' including name, description, and JSON schema for input parameters (path and optional start_line). This schema is returned by ListTools.
    { name: 'read_file', description: 'Read file contents. For large files (>500 lines), use start_line to read in chunks (e.g., 0, 500, 1000). Each call returns up to 500 lines. Binary files return metadata only.', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'File path to read' }, start_line: { type: 'number', description: 'Line number to start reading from (0-indexed). For large files, read in chunks: start_line=0 (first 500), start_line=500 (next 500), etc.', default: 0 } }, required: ['path'] } },
  • index.ts:260-262 (registration)
    Registration of the ListTools handler which returns the static tools array containing the 'read_file' tool definition.
    server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools }));
  • Core helper function that performs the actual file reading using Node.js fs.readFile. Called by the read_file handler.
    export async function readFileContent(filePath: string, encoding: string = 'utf-8'): Promise<string> { return await fs.readFile(filePath, encoding as BufferEncoding); }

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/lofcz/mcp-filesystem-smart'

If you have feedback or need assistance with the MCP directory API, please join our Discord server