write_file
Create or overwrite files with specified content in allowed directories using the Filesystem MCP Server. Ensures proper text encoding for accurate file handling.
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 |
|---|---|---|---|
| content | Yes | ||
| path | Yes |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"content": {
"type": "string"
},
"path": {
"type": "string"
}
},
"required": [
"path",
"content"
],
"type": "object"
}
Implementation Reference
- src/filesystem/index.ts:335-343 (handler)The handler function for the 'write_file' tool that validates the path, writes the content using writeFileContent, and returns a success message.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:94-97 (schema)Zod schema defining the input arguments for the write_file tool: path and content.const WriteFileArgsSchema = z.object({ path: z.string(), content: z.string(), });
- src/filesystem/index.ts:320-344 (registration)Registration of the 'write_file' tool with the MCP server, specifying title, description, input/output schemas, annotations, and handler function.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/lib.ts:138-162 (helper)Helper function implementing secure atomic file writing, handling new files and overwrites with symlink protection and race condition prevention.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; } } }