CreateDirectoryTool.ts•1.12 kB
import { MCPTool } from "mcp-framework";
import { z } from "zod";
import fs from "fs/promises";
interface CreateDirectoryInput {
path: string;
}
class CreateDirectoryTool extends MCPTool<CreateDirectoryInput> {
name = "create_directory";
description = "Create a new directory or ensure a directory exists. Can create multiple " +
"nested directories in one operation. If the directory already exists, " +
"this operation will succeed silently. Perfect for setting up directory " +
"structures for projects or ensuring required paths exist. Only works within allowed directories.";
schema = {
path: {
type: z.string(),
description: "Path where to create the directory",
},
};
async execute(input: CreateDirectoryInput) {
try {
await fs.mkdir(input.path, { recursive: true });
return `Successfully created directory ${input.path}`;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to create directory: ${errorMessage}`);
}
}
}
export default CreateDirectoryTool;