Write File
write_fileWrite text content to a file at a specified path, creating or overwriting the file. Use with caution as overwrites occur without warning.
Instructions
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.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| content | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes |
Implementation Reference
- src/filesystem/lib.ts:161-185 (handler)Core function that writes file content with security measures: uses 'wx' flag for exclusive creation (fails if file/symlink exists), and falls back to atomic rename with a temp file if the file already exists (to prevent symlink race conditions).
export async function writeFileContent(filePath: string, content: string): Promise<void> { try { // Security: 'wx' flag ensures exclusive creation - fails if file/symlink exists, // preventing writes through pre-existing symlinks await fs.writeFile(filePath, content, { encoding: "utf-8", flag: 'wx' }); } catch (error) { if ((error as NodeJS.ErrnoException).code === 'EEXIST') { // Security: Use atomic rename to prevent race conditions where symlinks // could be created between validation and write. Rename operations // replace the target file atomically and don't follow symlinks. const tempPath = `${filePath}.${randomBytes(16).toString('hex')}.tmp`; try { await fs.writeFile(tempPath, content, 'utf-8'); await fs.rename(tempPath, filePath); } catch (renameError) { try { await fs.unlink(tempPath); } catch {} throw renameError; } } else { throw error; } } } - src/filesystem/index.ts:339-363 (registration)Registration of the 'write_file' tool on the MCP server, with its metadata (title, description, annotations) and the handler that validates the path then calls writeFileContent.
server.registerTool( "write_file", { title: "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.", inputSchema: { path: z.string(), content: z.string() }, outputSchema: { content: z.string() }, annotations: { readOnlyHint: false, idempotentHint: true, destructiveHint: true } }, async (args: z.infer<typeof WriteFileArgsSchema>) => { const validPath = await validatePath(args.path); await writeFileContent(validPath, args.content); const text = `Successfully wrote to ${args.path}`; return { content: [{ type: "text" as const, text }], structuredContent: { content: text } }; } ); - src/filesystem/index.ts:113-116 (schema)Zod schema defining the input arguments for write_file: path (string) and content (string).
const WriteFileArgsSchema = z.object({ path: z.string(), content: z.string(), }); - src/filesystem/index.ts:16-17 (helper)Import of the writeFileContent helper function from lib.js into the main index.ts file.
import { getValidRootDirectories } from './roots-utils.js'; import {