Skip to main content
Glama

Filesystem MCP Server

Official

read_file

Read the complete contents of a file or specific lines (first N or last N) as text using the Filesystem MCP Server.

Instructions

Read the complete contents of a file as text. DEPRECATED: Use read_text_file instead.

Input Schema

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

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "head": { "description": "If provided, returns only the first N lines of the file", "type": "number" }, "path": { "type": "string" }, "tail": { "description": "If provided, returns only the last N lines of the file", "type": "number" } }, "required": [ "path" ], "type": "object" }

Implementation Reference

  • The handler function that implements the core logic for the 'read_file' tool. It validates the path, handles optional 'head' and 'tail' parameters for partial reads, and returns the file content as text.
    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 arguments for the 'read_file' tool, including path (required), and optional tail/head for line limits.
    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_file' tool with the MCP server, specifying title, description, schemas, annotations, and linking to the shared readTextFileHandler.
    server.registerTool( "read_file", { title: "Read File (Deprecated)", description: "Read the complete contents of a file as text. DEPRECATED: Use read_text_file instead.", inputSchema: ReadTextFileArgsSchema.shape, outputSchema: { content: z.string() }, annotations: { readOnlyHint: true } }, readTextFileHandler );
  • Core helper function that performs the actual file read operation using Node.js fs.readFile, called by the tool handler.
    export async function readFileContent(filePath: string, encoding: string = 'utf-8'): Promise<string> { return await fs.readFile(filePath, encoding as BufferEncoding); }

Other Tools

Related 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