read_file
Retrieve text content from files on your system by specifying the file path. This tool enables reading file contents for analysis, editing, or processing within the File System Explorer MCP Server environment.
Instructions
Read the contents of a text file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | The file path to read |
Implementation Reference
- src/index.ts:270-295 (handler)The main handler logic for the 'read_file' tool. Validates the input path using ReadFileArgsSchema, resolves and sanitizes the path, checks if it's a file and not too large, reads the file content using fs.readFile, formats the output with file info and content, and returns it in the expected MCP format.case "read_file": { const { path: filePath } = ReadFileArgsSchema.parse(args); const safePath = validatePath(filePath); const stats = await fs.stat(safePath); if (stats.isDirectory()) { throw new Error("Cannot read a directory as a file"); } // Check file size to prevent reading huge files const maxSize = 1024 * 1024; // 1MB limit if (stats.size > maxSize) { throw new Error(`File too large (${formatFileSize(stats.size)}). Maximum size is ${formatFileSize(maxSize)}`); } const content = await fs.readFile(safePath, 'utf-8'); return { content: [ { type: "text", text: `File: ${safePath}\nSize: ${formatFileSize(stats.size)}\nModified: ${stats.mtime.toLocaleString()}\n\n--- Content ---\n${content}` } ] }; }
- src/index.ts:36-38 (schema)Zod schema for validating the input arguments of the 'read_file' tool, specifically the 'path' parameter.const ReadFileArgsSchema = z.object({ path: z.string().describe("The file path to read") });
- src/index.ts:158-171 (registration)Registration of the 'read_file' tool in the ListToolsRequestSchema handler, defining its name, description, and input schema for MCP clients to discover and use it.{ name: "read_file", description: "Read the contents of a text file", inputSchema: { type: "object", properties: { path: { type: "string", description: "The file path to read" } }, required: ["path"] } },