WriteFileTool.ts•1.09 kB
import { MCPTool } from "mcp-framework";
import { z } from "zod";
import fs from "fs/promises";
interface WriteFileInput {
path: string;
content: string;
}
class WriteFileTool extends MCPTool<WriteFileInput> {
name = "write_file";
description = "Create a new file or completely overwrite an existing file with new content. " +
"Use with caution as it will overwrite existing files without warning. " +
"Handles text content with proper encoding. Only works within allowed directories.";
schema = {
path: {
type: z.string(),
description: "Path where to write the file",
},
content: {
type: z.string(),
description: "Content to write to the file",
},
};
async execute(input: WriteFileInput) {
try {
await fs.writeFile(input.path, input.content, "utf-8");
return `Successfully wrote to ${input.path}`;
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to write file: ${errorMessage}`);
}
}
}
export default WriteFileTool;