append_to_file
Add specified content to the end of a file, optionally inserting a newline before appending. Supports file management within the Enhanced Directory Context MCP Server.
Instructions
Append content to the end of a file
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | Content to append | |
| newline_before | No | Add newline before appending | |
| path | Yes | File path relative to working directory |
Input Schema (JSON Schema)
{
"properties": {
"content": {
"description": "Content to append",
"type": "string"
},
"newline_before": {
"default": true,
"description": "Add newline before appending",
"type": "boolean"
},
"path": {
"description": "File path relative to working directory",
"type": "string"
}
},
"required": [
"path",
"content"
],
"type": "object"
}
Implementation Reference
- server.js:602-636 (handler)The core handler function for the 'append_to_file' tool. It resolves the file path, reads existing content if any, conditionally adds a newline, appends the new content, writes back to the file, and returns a success message.async handleAppendToFile(args) { const { path: filePath, content, newline_before = true } = args; const fullPath = path.resolve(this.workingDirectory, filePath); try { // Check if file exists let existingContent = ''; try { existingContent = await fs.readFile(fullPath, 'utf8'); } catch (error) { // File doesn't exist, will be created } // Prepare content to append let finalContent = existingContent; if (existingContent && newline_before && !existingContent.endsWith('\n')) { finalContent += '\n'; } finalContent += content; // Write file await fs.writeFile(fullPath, finalContent, 'utf8'); return { content: [ { type: 'text', text: `Content appended to file: ${filePath}`, }, ], }; } catch (error) { throw new McpError(ErrorCode.InternalError, `Failed to append to file: ${error.message}`); } }
- server.js:256-274 (schema)Input schema definition for the 'append_to_file' tool, specifying parameters: path (required), content (required), and newline_before (optional boolean with default true).inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'File path relative to working directory', }, content: { type: 'string', description: 'Content to append', }, newline_before: { type: 'boolean', description: 'Add newline before appending', default: true, }, }, required: ['path', 'content'], },
- server.js:479-480 (registration)Dispatcher case in the CallToolRequestSchema handler that routes 'append_to_file' calls to the handleAppendToFile method.case 'append_to_file': return await this.handleAppendToFile(args);
- server.js:253-275 (registration)Tool registration in the ListToolsRequestSchema response, including name, description, and full input schema.{ name: 'append_to_file', description: 'Append content to the end of a file', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'File path relative to working directory', }, content: { type: 'string', description: 'Content to append', }, newline_before: { type: 'boolean', description: 'Add newline before appending', default: true, }, }, required: ['path', 'content'], }, },
- server.js:750-750 (helper)Usage of handleAppendToFile within the batch_file_operations tool handler for 'append' operations.result = await this.handleAppendToFile(params);