read_file
Extract and read the full contents of a file from your file system, handling multiple text encodings and providing clear error messages. Works within specified allowed directories for secure access.
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. Only works within allowed directories.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- src/server.ts:266-272 (handler)Handler for the 'read_file' tool that parses the input arguments using ReadFileArgsSchema, calls the readFile helper function, and returns the file content wrapped in the expected response format.case "read_file": { const parsed = ReadFileArgsSchema.parse(args); const content = await readFile(parsed.path); return { content: [{ type: "text", text: content }], }; }
- src/tools/schemas.ts:32-34 (schema)Zod schema defining the input for the read_file tool: an object with a required 'path' string parameter.export const ReadFileArgsSchema = z.object({ path: z.string(), });
- src/server.ts:126-132 (registration)Registration of the 'read_file' tool in the ListToolsRequest handler, specifying name, description, and input schema.name: "read_file", description: "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. Only works within allowed directories.", inputSchema: zodToJsonSchema(ReadFileArgsSchema), },
- src/tools/filesystem.ts:65-68 (helper)Helper function that validates the file path using validatePath and reads the file contents using Node.js fs.readFile.export async function readFile(filePath: string): Promise<string> { const validPath = await validatePath(filePath); return fs.readFile(validPath, "utf-8"); }