write_file
Create or overwrite files with specified content to manage project files directly from Claude Desktop, enabling file operations within your local development environment.
Instructions
Create or overwrite a file with new content. Use this to create new files or completely replace existing ones.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to the file (relative to projects directory or absolute) | |
| content | Yes | Content to write to the file |
Implementation Reference
- index.js:228-241 (handler)The main handler function for the 'write_file' tool. It resolves the file path, ensures the directory exists, writes the content using fs.writeFile, and returns a success message.async writeFile(filePath, content) { const resolvedPath = this.resolvePath(filePath); const dir = path.dirname(resolvedPath); await fs.mkdir(dir, { recursive: true }); await fs.writeFile(resolvedPath, content, 'utf-8'); return { content: [ { type: 'text', text: `File written successfully: ${resolvedPath}`, }, ], }; }
- index.js:62-78 (schema)The input schema definition for the 'write_file' tool, registered in the ListTools response.name: 'write_file', description: 'Create or overwrite a file with new content. Use this to create new files or completely replace existing ones.', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file (relative to projects directory or absolute)', }, content: { type: 'string', description: 'Content to write to the file', }, }, required: ['path', 'content'], }, },
- index.js:166-167 (registration)The dispatch case in the CallToolRequestSchema handler that routes 'write_file' calls to the writeFile method.case 'write_file': return await this.writeFile(args.path, args.content);
- index.js:205-213 (helper)Helper method to resolve relative file paths to absolute paths based on the PROJECTS_DIR.resolvePath(inputPath) { if (!inputPath || inputPath === '.') { return PROJECTS_DIR; } if (path.isAbsolute(inputPath)) { return inputPath; } return path.join(PROJECTS_DIR, inputPath); }