import { MCPTool } from "mcp-framework";
import { z } from "zod";
import fs from "fs/promises";
interface ListDirectoryInput {
path: string;
}
class ListDirectoryTool extends MCPTool<ListDirectoryInput> {
name = "list_directory";
description = "Get a detailed listing of all files and directories in a specified path. " +
"Results clearly distinguish between files and directories with [FILE] and [DIR] " +
"prefixes. This tool is essential for understanding directory structure and " +
"finding specific files within a directory. Only works within allowed directories.";
schema = {
path: {
type: z.string(),
description: "Path to the directory to list",
},
};
async execute(input: ListDirectoryInput) {
try {
const entries = await fs.readdir(input.path, { withFileTypes: true });
const formatted = entries
.map((entry) => `${entry.isDirectory() ? "[DIR]" : "[FILE]"} ${entry.name}`)
.join("\n");
return formatted;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to list directory: ${errorMessage}`);
}
}
}
export default ListDirectoryTool;