Skip to main content
Glama

read_file

Access and retrieve the complete contents of a specified file from the file system, supporting various text encodings. Requires a file path and maxBytes parameter to limit data read. Ideal for examining file content within permitted directories.

Instructions

Read the complete contents of a file from the file system. 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. Requires maxBytes parameter. Only works within allowed directories.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
maxBytesYesMaximum bytes to read from the file. Must be a positive integer. Handler default: 10KB.
pathYes

Implementation Reference

  • The handleReadFile function that executes the core logic of the read_file tool: parses arguments using the schema, validates the file path against allowed directories, checks file size limit, reads the file content using fs.readFile, and returns it in a structured format.
    export async function handleReadFile( args: unknown, allowedDirectories: string[], symlinksMap: Map<string, string>, noFollowSymlinks: boolean ) { const { path: filePath, maxBytes } = parseArgs(ReadFileArgsSchema, args, 'read_file'); const validPath = await validatePath(filePath, allowedDirectories, symlinksMap, noFollowSymlinks); // Check file size before reading const stats = await fs.stat(validPath); const effectiveMaxBytes = maxBytes ?? (10 * 1024); // Default 10KB if (stats.size > effectiveMaxBytes) { throw new Error(`File size (${stats.size} bytes) exceeds the maximum allowed size (${effectiveMaxBytes} bytes).`); } const content = await fs.readFile(validPath, "utf-8"); return { content: [{ type: "text", text: content }], }; }
  • TypeBox schema definition for read_file tool inputs: requires 'path' (string) and optional 'maxBytes' (positive integer with description). Also defines the TypeScript type ReadFileArgs.
    export const ReadFileArgsSchema = Type.Object({ path: Type.String(), maxBytes: Type.Integer({ minimum: 1, description: 'Maximum bytes to read from the file. Must be a positive integer. Handler default: 10KB.' }) }); export type ReadFileArgs = Static<typeof ReadFileArgsSchema>;
  • index.ts:167-168 (registration)
    Registration of the read_file handler in the toolHandlers object, mapping the tool name to the execution of handleReadFile with context parameters.
    read_file: (a: unknown) => handleReadFile(a, allowedDirectories, symlinksMap, noFollowSymlinks),
  • index.ts:305-305 (registration)
    Declaration of the read_file tool metadata (name and description) in the allTools array used for server registration.
    { name: "read_file", description: "Read file contents" },
  • Re-export and mapping of ReadFileArgsSchema to the 'read_file' key in the central toolSchemas object used by the server.
    read_file: ReadFileArgsSchema,

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/rawr-ai/mcp-filesystem'

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