write_file
Write specified content to a file at a given path using the Edit-MCP server. Supports custom encoding for file creation or updates.
Instructions
Write content to a file
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | Content to write to the file | |
| encoding | No | Encoding to use when writing the file (default: utf8) | |
| path | Yes | Path to the file to write |
Input Schema (JSON Schema)
{
"properties": {
"content": {
"description": "Content to write to the file",
"type": "string"
},
"encoding": {
"description": "Encoding to use when writing the file (default: utf8)",
"type": "string"
},
"path": {
"description": "Path to the file to write",
"type": "string"
}
},
"required": [
"path",
"content"
],
"type": "object"
}
Implementation Reference
- src/index.ts:161-188 (registration)Registers the 'write_file' tool with MCP server, including schema for path, content, encoding.mcpServer.registerTool({ name: 'write_file', description: 'Write content to a file', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file to write' }, content: { type: 'string', description: 'Content to write to the file' }, encoding: { type: 'string', description: 'Encoding to use when writing the file (default: utf8)' } }, required: ['path', 'content'] }, annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false } });
- src/index.ts:164-180 (schema)Input schema definition for the write_file tool parameters.inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file to write' }, content: { type: 'string', description: 'Content to write to the file' }, encoding: { type: 'string', description: 'Encoding to use when writing the file (default: utf8)' } }, required: ['path', 'content']
- Core handler function that performs the actual file write operation using promisified fs.writeFile, ensures parent directory exists, and emits change event.public async writeFile(filePath: string, content: string, encoding: BufferEncoding = 'utf8'): Promise<void> { try { // Ensure the directory exists await this.ensureDirectoryExists(path.dirname(filePath)); await writeFile(filePath, content, { encoding }); this.emitChangeEvent('update', filePath); } catch (error: any) { throw new Error(`Failed to write file ${filePath}: ${error.message}`); } }
- Operation router dispatches 'write_file_content' operation to FileSystemManager.writeFile method.case 'write_file_content': return this.fileSystemManager.writeFile(operation.params.path, operation.params.content);
- Promisifies Node.js fs.writeFile for async usage in the manager.const writeFile = util.promisify(fs.writeFile);