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
| Name | Required | Description | Default |
|---|---|---|---|
| head | No | If provided, returns only the first N lines of the file | |
| path | Yes | ||
| tail | No | If 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
- src/filesystem/index.ts:172-192 (handler)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 } }; };
- src/filesystem/index.ts:77-81 (schema)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') });
- src/filesystem/index.ts:194-204 (registration)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 );
- src/filesystem/lib.ts:134-136 (helper)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); }