GetFileInfoTool.ts•1.68 kB
import { MCPTool } from "mcp-framework";
import { z } from "zod";
import fs from "fs/promises";
interface FileInfo {
size: number;
created: Date;
modified: Date;
accessed: Date;
isDirectory: boolean;
isFile: boolean;
permissions: string;
}
interface GetFileInfoInput {
path: string;
}
class GetFileInfoTool extends MCPTool<GetFileInfoInput> {
name = "get_file_info";
description = "Retrieve detailed metadata about a file or directory. Returns comprehensive " +
"information including size, creation time, last modified time, permissions, " +
"and type. This tool is perfect for understanding file characteristics " +
"without reading the actual content. Only works within allowed directories.";
schema = {
path: {
type: z.string(),
description: "Path to the file or directory to get information about",
},
};
private async getFileStats(filePath: string): Promise<FileInfo> {
const stats = await fs.stat(filePath);
return {
size: stats.size,
created: stats.birthtime,
modified: stats.mtime,
accessed: stats.atime,
isDirectory: stats.isDirectory(),
isFile: stats.isFile(),
permissions: stats.mode.toString(8).slice(-3),
};
}
async execute(input: GetFileInfoInput) {
try {
const info = await this.getFileStats(input.path);
return Object.entries(info)
.map(([key, value]) => `${key}: ${value}`)
.join("\n");
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to get file info: ${errorMessage}`);
}
}
}
export default GetFileInfoTool;