ReadFileTool.ts•981 B
import { MCPTool } from "mcp-framework";
import { z } from "zod";
import fs from "fs/promises";
interface ReadFileInput {
path: string;
}
class ReadFileTool extends MCPTool<ReadFileInput> {
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. Use this tool when you need to examine " +
"the contents of a single file. Only works within allowed directories.";
schema = {
path: {
type: z.string(),
description: "Path to the file to read",
},
};
async execute(input: ReadFileInput) {
try {
const content = await fs.readFile(input.path, "utf-8");
return content;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to read file: ${errorMessage}`);
}
}
}
export default ReadFileTool;