read-file
Extract file contents by specifying the file path within React applications. Simplifies file reading in the react-mcp server to enhance app development workflows.
Instructions
Read the contents of a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Path to the file to read |
Implementation Reference
- index.js:344-370 (handler)The main execution handler for the 'read-file' tool. Validates input, checks file existence, reads content with fs.readFileSync, and returns file details.async function handleReadFile(params) { try { const { filePath } = params; if (!filePath) { throw new Error("File path is required"); } // Check if file exists if (!fs.existsSync(filePath)) { throw new Error(`File ${filePath} does not exist`); } // Read file content const content = fs.readFileSync(filePath, "utf8"); return { filePath: filePath, content: content, size: Buffer.byteLength(content, "utf8"), }; } catch (error) { return { error: `Error reading file: ${error.message}`, }; } }
- index.js:457-459 (schema)Zod schema defining the input parameters for the 'read-file' tool: requires 'filePath' as string.const ReadFileSchema = z.object({ filePath: z.string(), });
- index.js:582-595 (registration)Tool registration in the listTools handler, providing name, description, and input schema for 'read-file'.{ name: "read-file", description: "Read the contents of a file", inputSchema: { type: "object", properties: { filePath: { type: "string", description: "Path to the file to read", }, }, required: ["filePath"], }, },
- index.js:669-672 (registration)Dispatch case in the CallToolRequestSchema handler that parses arguments with ReadFileSchema and invokes the handleReadFile function.case "read-file": const readArgs = ReadFileSchema.parse(args); result = await handleReadFile(readArgs); break;