write_file
Replace file contents entirely with new data for large modifications or when partial edits fail. Operates within permitted directories to ensure controlled file overwrites.
Instructions
Completely replace file contents. Best for large changes (>20% of file) or when edit_block fails. Use with caution as it will overwrite existing files. Only works within allowed directories.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | ||
| path | Yes |
Implementation Reference
- src/server.ts:280-285 (handler)Dispatches the write_file tool call: parses arguments using schema, calls the writeFile function, and returns success response.case "write_file": { const parsed = WriteFileArgsSchema.parse(args); await writeFile(parsed.path, parsed.content); return { content: [{ type: "text", text: `Successfully wrote to ${parsed.path}` }], };
- src/tools/filesystem.ts:70-73 (handler)Core handler function that validates the path and writes the content to the file using fs.writeFile.export async function writeFile(filePath: string, content: string): Promise<void> { const validPath = await validatePath(filePath); await fs.writeFile(validPath, content, "utf-8"); }
- src/tools/schemas.ts:40-43 (schema)Zod schema defining input arguments for write_file: path (string) and content (string). Used for validation in handler and registration.export const WriteFileArgsSchema = z.object({ path: z.string(), content: z.string(), });
- src/server.ts:142-148 (registration)Registers the write_file tool in the MCP server's ListTools response with name, description, and input schema.{ name: "write_file", description: "Completely replace file contents. Best for large changes (>20% of file) or when edit_block fails. " + "Use with caution as it will overwrite existing files. Only works within allowed directories.", inputSchema: zodToJsonSchema(WriteFileArgsSchema), },