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);
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure and does so effectively. It describes key behaviors: chunked reading for large files (up to 500 lines per call), handling of binary files (metadata only), and the 0-indexed line numbering. It doesn't cover all possible behaviors (e.g., error cases or permissions), but provides substantial operational context.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is front-loaded with the core purpose and efficiently structured into three sentences that each add critical information: basic function, chunking guidance, and binary file handling. There is no wasted text, and it's appropriately sized for the tool's complexity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (2 parameters, no output schema, no annotations), the description is largely complete. It covers the main use cases, limitations (binary files, chunking), and operational details. However, it doesn't address potential error conditions or return format specifics, leaving minor gaps in contextual understanding.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents both parameters thoroughly. The description adds minimal value beyond the schema by reinforcing the chunking logic for 'start_line' with an example, but doesn't provide additional semantic context or clarify parameter interactions beyond what's in the schema descriptions.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Read file contents') and resource ('file'), distinguishing it from siblings like 'get_file_info' (metadata only) or 'search_in_file' (content search). It precisely defines the tool's function beyond just reading by specifying handling of large files and binary files.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use this tool vs. alternatives: it specifies to use 'start_line' for large files (>500 lines) to read in chunks, and notes that binary files return metadata only (implying other tools might be needed for binary content). It directly addresses usage scenarios without being misleading.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

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

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