Skip to main content
Glama
PWalaGov

Enhanced Directory Context MCP Server

by PWalaGov

append_to_file

Add content to the end of a file, with optional newline insertion, for file management operations within directory contexts.

Instructions

Append content to the end of a file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesFile path relative to working directory
contentYesContent to append
newline_beforeNoAdd newline before appending

Implementation Reference

  • The main handler function for 'append_to_file' tool. Reads existing file content if it exists, optionally adds a newline before appending new content, writes the updated content 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}`);
      }
    }
  • 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)
    Switch case in the tool call handler that registers and dispatches '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 ListTools response, defining name, description, and input schema for 'append_to_file'.
    {
      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'],
      },
    },
  • Usage of handleAppendToFile in the batch_file_operations tool for 'append' operation.
    result = await this.handleAppendToFile(params);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/PWalaGov/File-Control-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server