Skip to main content
Glama

read_large_file_chunk

Read specific sections of large files using intelligent chunking that automatically determines optimal size based on file type, enabling efficient processing without loading entire files into memory.

Instructions

Read a specific chunk of a large file with intelligent chunking based on file type. Automatically determines optimal chunk size.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYesAbsolute path to the file
chunkIndexNoZero-based chunk index to read (default: 0)
linesPerChunkNoNumber of lines per chunk (optional, auto-detected if not provided)
includeLineNumbersNoInclude line numbers in output (default: false)

Implementation Reference

  • The primary handler function `readChunk` that implements the core tool logic: verifies the file exists, computes metadata and optimal chunk size based on file type, calculates start/end lines with overlap, reads the line range, formats content with optional line numbers, and returns a structured `FileChunk`.
    static async readChunk(
      filePath: string,
      chunkIndex: number,
      options: ChunkOptions = {}
    ): Promise<FileChunk> {
      await this.verifyFile(filePath);
    
      const metadata = await this.getMetadata(filePath);
      const linesPerChunk = options.linesPerChunk ||
        this.getOptimalChunkSize(metadata.fileType, metadata.totalLines);
      const overlapLines = options.overlapLines || 10;
    
      const startLine = Math.max(1, chunkIndex * linesPerChunk - overlapLines + 1);
      const endLine = Math.min(metadata.totalLines, (chunkIndex + 1) * linesPerChunk);
    
      const lines = await this.readLines(filePath, startLine, endLine);
      const content = options.includeLineNumbers
        ? lines.map((line, idx) => `${startLine + idx}: ${line}`).join('\n')
        : lines.join('\n');
    
      const totalChunks = Math.ceil(metadata.totalLines / linesPerChunk);
    
      return {
        content,
        startLine,
        endLine,
        totalLines: metadata.totalLines,
        chunkIndex,
        totalChunks,
        filePath,
        byteOffset: 0, // Calculated if needed
        byteSize: Buffer.byteLength(content, 'utf-8'),
      };
    }
  • The MCP server-specific handler `handleReadChunk` that parses tool arguments, applies caching via CacheManager, delegates to FileHandler.readChunk, and formats the JSON response for MCP protocol.
    private async handleReadChunk(
      args: Record<string, unknown>
    ): Promise<{ content: Array<{ type: string; text: string }> }> {
      const filePath = args.filePath as string;
      const chunkIndex = (args.chunkIndex as number) || 0;
      const linesPerChunk = args.linesPerChunk as number | undefined;
      const includeLineNumbers = (args.includeLineNumbers as boolean) || false;
    
      const cacheKey = `chunk:${filePath}:${chunkIndex}:${linesPerChunk}:${includeLineNumbers}`;
      let chunk = this.chunkCache.get(cacheKey);
    
      if (!chunk) {
        chunk = await FileHandler.readChunk(filePath, chunkIndex, {
          linesPerChunk,
          includeLineNumbers,
          overlapLines: this.config.defaultOverlap,
        });
        this.chunkCache.set(cacheKey, chunk);
      }
    
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(chunk, null, 2),
          },
        ],
      };
    }
  • src/server.ts:90-115 (registration)
    Registers the 'read_large_file_chunk' tool with the MCP SDK Server by defining its name, description, and inputSchema in the tools list returned by getTools().
    {
      name: 'read_large_file_chunk',
      description: 'Read a specific chunk of a large file with intelligent chunking based on file type. Automatically determines optimal chunk size.',
      inputSchema: {
        type: 'object',
        properties: {
          filePath: {
            type: 'string',
            description: 'Absolute path to the file',
          },
          chunkIndex: {
            type: 'number',
            description: 'Zero-based chunk index to read (default: 0)',
          },
          linesPerChunk: {
            type: 'number',
            description: 'Number of lines per chunk (optional, auto-detected if not provided)',
          },
          includeLineNumbers: {
            type: 'boolean',
            description: 'Include line numbers in output (default: false)',
          },
        },
        required: ['filePath'],
      },
    },
  • TypeScript interface `FileChunk` defining the structured output returned by the read_large_file_chunk tool handler, including content, line ranges, metadata, and byte info.
    export interface FileChunk {
      /** Chunk content */
      content: string;
      /** Starting line number (1-indexed) */
      startLine: number;
      /** Ending line number (1-indexed) */
      endLine: number;
      /** Total lines in file */
      totalLines: number;
      /** Chunk index */
      chunkIndex: number;
      /** Total number of chunks */
      totalChunks: number;
      /** File path */
      filePath: string;
      /** Byte offset start */
      byteOffset: number;
      /** Chunk size in bytes */
      byteSize: number;
    }
  • Helper function `getOptimalChunkSize` that determines the ideal number of lines per chunk based on file type and size, enabling intelligent chunking central to the tool's 'automatic' sizing feature.
    static getOptimalChunkSize(fileType: FileType, totalLines: number): number {
      const baseSizes: Record<FileType, number> = {
        [FileType.LOG]: 500,
        [FileType.CSV]: 1000,
        [FileType.JSON]: 100,
        [FileType.CODE]: 300,
        [FileType.TEXT]: 500,
        [FileType.MARKDOWN]: 200,
        [FileType.XML]: 200,
        [FileType.BINARY]: 1000,
        [FileType.UNKNOWN]: 500,
      };
    
      const baseSize = baseSizes[fileType] || 500;
    
      // Adjust for very large files
      if (totalLines > 100000) {
        return Math.min(baseSize * 2, 2000);
      }
    
      return baseSize;
    }
Behavior2/5

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

No annotations are provided, so the description carries the full burden. It mentions 'intelligent chunking based on file type' and 'automatically determines optimal chunk size', which adds some behavioral context beyond basic reading. However, it does not disclose critical details like performance implications, error handling, memory usage, or output format, which are important for a tool handling large files.

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 a single, efficient sentence that is front-loaded with the core purpose ('Read a specific chunk of a large file') and adds key details ('with intelligent chunking based on file type. Automatically determines optimal chunk size.') without any wasted words. Every part earns its place.

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

Completeness2/5

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

Given the complexity of handling large files with chunking, no annotations, and no output schema, the description is incomplete. It lacks details on return values (e.g., chunk content format), error conditions, performance trade-offs, or how 'intelligent chunking' works in practice, which are crucial for effective tool use.

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 all parameters (filePath, chunkIndex, linesPerChunk, includeLineNumbers) with descriptions. The description adds minimal value by implying chunking logic but does not provide additional syntax, format, or usage details beyond what the schema specifies. Baseline 3 is appropriate when schema does the heavy lifting.

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 tool's purpose with a specific verb ('Read') and resource ('a specific chunk of a large file'), and distinguishes it from siblings by mentioning 'intelligent chunking based on file type' and 'automatically determines optimal chunk size', which is not implied in sibling tool names like 'get_file_structure' or 'search_in_large_file'.

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

Usage Guidelines3/5

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

The description implies usage for reading chunks of large files with auto-detected chunk sizes, but does not explicitly state when to use this tool versus alternatives like 'stream_large_file' or 'navigate_to_line'. It provides some context (e.g., for large files with intelligent chunking) but lacks clear exclusions or named alternatives.

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/willianpinho/large-file-mcp'

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