fs_write_file
Write content to files with automatic directory creation and file overwriting capabilities for development workflows.
Instructions
Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Can create parent directories.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute or relative path to the file to write | |
| content | Yes | Content to write to the file | |
| encoding | No | File encoding | utf8 |
| createDirs | No | Create parent directories if they don't exist |
Implementation Reference
- src/tools/filesystem.ts:104-140 (handler)The handler function that executes the fs_write_file tool: validates args with writeFileSchema, creates parent dirs if specified, writes content to file using fs.promises.writeFile, returns success/error JSON.export async function writeFile(args: z.infer<typeof writeFileSchema>): Promise<ToolResponse> { try { // Create parent directories if needed if (args.createDirs) { const dir = path.dirname(args.path); await fs.mkdir(dir, { recursive: true }); } await fs.writeFile(args.path, args.content, args.encoding as BufferEncoding); return { content: [ { type: "text" as const, text: JSON.stringify({ success: true, path: args.path, bytesWritten: args.content.length }, null, 2) } ] }; } catch (error) { return { content: [ { type: "text" as const, text: JSON.stringify({ success: false, error: error instanceof Error ? error.message : String(error) }, null, 2) } ], isError: true }; } }
- src/tools/filesystem.ts:18-22 (schema)Zod input schema for fs_write_file tool used for validation in handler and dispatch.export const writeFileSchema = z.object({ path: z.string().describe('Absolute or relative path to the file to write'), content: z.string().describe('Content to write to the file'), encoding: z.enum(['utf8', 'binary', 'base64']).default('utf8').describe('File encoding'), createDirs: z.boolean().default(true).describe('Create parent directories if they don\'t exist')
- src/index.ts:309-311 (registration)Registration and dispatch logic in the main MCP server request handler: parses args with writeFileSchema and calls the writeFile handler.if (name === 'fs_write_file') { const validated = writeFileSchema.parse(args); return await writeFile(validated);
- src/tools/filesystem.ts:475-502 (schema)MCP tool definition including name, description, and inline inputSchema for fs_write_file.{ name: 'fs_write_file', description: 'Write content to a file. Creates the file if it doesn\'t exist, overwrites if it does. Can create parent directories.', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Absolute or relative path to the file to write' }, content: { type: 'string', description: 'Content to write to the file' }, encoding: { type: 'string', enum: ['utf8', 'binary', 'base64'], default: 'utf8', description: 'File encoding' }, createDirs: { type: 'boolean', default: true, description: 'Create parent directories if they don\'t exist' } }, required: ['path', 'content'] }