Skip to main content
Glama
modelcontextprotocol

Filesystem MCP Server

Official

Read Text File

read_text_file
Read-only

Read complete or partial text file contents from allowed directories, handling various encodings with detailed error reporting.

Instructions

Read the complete contents of a file from the file system as text. Handles various text encodings and provides detailed error messages if the file cannot be read. Use this tool when you need to examine the contents of a single file. Use the 'head' parameter to read only the first N lines of a file, or the 'tail' parameter to read only the last N lines of a file. Operates on the file as text regardless of extension. Only works within allowed directories.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes
tailNoIf provided, returns only the last N lines of the file
headNoIf provided, returns only the first N lines of the file

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYes

Implementation Reference

  • The async handler function that implements the core logic for the 'read_text_file' tool. It validates the input path, checks for mutually exclusive head/tail parameters, reads the full file content or first/last N lines using helper functions, and returns the content in the expected MCP format.
    const readTextFileHandler = async (args: z.infer<typeof ReadTextFileArgsSchema>) => {
      const validPath = await validatePath(args.path);
    
      if (args.head && args.tail) {
        throw new Error("Cannot specify both head and tail parameters simultaneously");
      }
    
      let content: string;
      if (args.tail) {
        content = await tailFile(validPath, args.tail);
      } else if (args.head) {
        content = await headFile(validPath, args.head);
      } else {
        content = await readFileContent(validPath);
      }
    
      return {
        content: [{ type: "text" as const, text: content }],
        structuredContent: { content }
      };
    };
  • Zod schema defining the input parameters for the read_text_file tool: 'path' (required string), 'tail' and 'head' (optional numbers for limiting output to last or first N lines).
    const ReadTextFileArgsSchema = z.object({
      path: z.string(),
      tail: z.number().optional().describe('If provided, returns only the last N lines of the file'),
      head: z.number().optional().describe('If provided, returns only the first N lines of the file')
    });
  • Registration of the 'read_text_file' tool with the MCP server, specifying title, detailed description, inline input schema, output schema, read-only annotation, and linking to the shared readTextFileHandler.
    server.registerTool(
      "read_text_file",
      {
        title: "Read Text File",
        description:
          "Read the complete contents of a file from the file system as text. " +
          "Handles various text encodings and provides detailed error messages " +
          "if the file cannot be read. Use this tool when you need to examine " +
          "the contents of a single file. Use the 'head' parameter to read only " +
          "the first N lines of a file, or the 'tail' parameter to read only " +
          "the last N lines of a file. Operates on the file as text regardless of extension. " +
          "Only works within allowed directories.",
        inputSchema: {
          path: z.string(),
          tail: z.number().optional().describe("If provided, returns only the last N lines of the file"),
          head: z.number().optional().describe("If provided, returns only the first N lines of the file")
        },
        outputSchema: { content: z.string() },
        annotations: { readOnlyHint: true }
      },
      readTextFileHandler
    );
  • Helper function that performs the actual reading of the full file content using Node.js fs.readFile with UTF-8 encoding. Called by the handler when neither head nor tail is specified.
    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?

The annotations provide readOnlyHint=true, which the description aligns with by describing a read operation. The description adds valuable behavioral context beyond annotations: it mentions handling various text encodings, providing detailed error messages, operating on files as text regardless of extension, and working only within allowed directories. These are useful disclosures about capabilities and constraints that annotations don't cover.

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 efficiently structured with four sentences that each add value: the core functionality, error handling, usage guidance, and operational constraints. It's front-loaded with the main purpose and avoids redundancy. Every sentence earns its place by providing distinct information.

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

Completeness5/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 (3 parameters, read-only operation), the description is complete. It covers purpose, usage, behavioral traits, and parameter hints. With annotations covering safety (readOnlyHint) and an output schema presumably detailing return values, the description doesn't need to explain those aspects. It addresses all necessary contextual elements for a file-reading tool.

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

Parameters4/5

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

With 67% schema description coverage (2 out of 3 parameters have descriptions in the schema), the description adds meaningful context. It explains that 'head' and 'tail' parameters read first or last N lines, which clarifies their purpose beyond the schema's basic descriptions. However, it doesn't address the 'path' parameter's semantics (e.g., format or allowed directories), leaving some gap. Since schema coverage is moderate, the description compensates well but not fully.

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 the complete contents of a file'), resource ('file from the file system'), and scope ('as text'). It distinguishes from siblings like 'read_file' (which might handle binary) and 'read_multiple_files' (which handles multiple files). The phrase 'examine the contents of a single file' reinforces the specific use case.

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

Usage Guidelines4/5

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

The description explicitly states 'Use this tool when you need to examine the contents of a single file,' providing clear context for when to use it. However, it doesn't explicitly mention when NOT to use it or name specific alternatives (e.g., 'read_file' or 'read_multiple_files' from the sibling list), which would be needed for a score of 5.

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/modelcontextprotocol/filesystem'

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