Skip to main content
Glama

diff_preview

Preview file changes before applying them by showing a diff of modifications that would be made by sed, awk, or perl commands.

Instructions

Preview what changes would be made by showing a diff

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileYesFile to preview changes for
commandYesCommand that would make changes (e.g., "s/old/new/g")
toolNoWhich tool to use for the previewperl

Implementation Reference

  • The main handler function for the 'diff_preview' tool. It creates a temporary copy of the file, applies the specified command using the chosen tool (perl, sed, or awk), generates a unified diff between original and modified temp file, returns the diff preview, and cleans up the temp file.
    case 'diff_preview': {
      const { file, command, tool = 'perl' } = args;
      
      if (!existsSync(file)) {
        throw new Error(`File not found: ${file}`);
      }
      
      // Create temp file
      const tempFile = `${file}.preview.tmp`;
      await execAsync(`rm -f .bak; cp '${file}' '${tempFile}'`);
      
      // Apply command to temp file
      let editCmd;
      switch (tool) {
        case 'perl':
          editCmd = `perl -i -pe '${command}' '${tempFile}'`;
          break;
        case 'sed':
          editCmd = `sed -i.tmp '${command}' '${tempFile}' && rm -f '${tempFile}.tmp'`;
          break;
        case 'awk':
          editCmd = `awk '${command}' '${tempFile}' > '${tempFile}.new' && mv '${tempFile}.new' '${tempFile}'`;
          break;
      }
      
      await execAsync(editCmd);
      
      // Generate diff
      const { stdout } = await execAsync(`diff -u '${file}' '${tempFile}' || true`);
      
      // Cleanup
      await execAsync(`rm -f '${tempFile}'`);
      
      return {
        content: [{
          type: 'text',
          text: stdout ? `Preview of changes:\n${stdout}` : 'No changes would be made'
        }]
      };
    }
  • src/index.ts:203-226 (registration)
    Tool registration object including name, description, and input schema, added to the tools array for server.setTools().
    {
      name: 'diff_preview', 
      description: 'Preview what changes would be made by showing a diff',
      inputSchema: {
        type: 'object',
        properties: {
          file: {
            type: 'string',
            description: 'File to preview changes for'
          },
          command: {
            type: 'string',
            description: 'Command that would make changes (e.g., "s/old/new/g")'
          },
          tool: {
            type: 'string',
            enum: ['sed', 'perl', 'awk'],
            default: 'perl',
            description: 'Which tool to use for the preview'
          }
        },
        required: ['file', 'command']
      }
    },
  • Input schema defining the parameters for the diff_preview tool: file, command (required), and optional tool (perl/sed/awk).
    type: 'object',
    properties: {
      file: {
        type: 'string',
        description: 'File to preview changes for'
      },
      command: {
        type: 'string',
        description: 'Command that would make changes (e.g., "s/old/new/g")'
      },
      tool: {
        type: 'string',
        enum: ['sed', 'perl', 'awk'],
        default: 'perl',
        description: 'Which tool to use for the preview'
      }
    },
    required: ['file', 'command']
  • Documentation and usage examples for the diff_preview tool in the helpContent object.
      diff_preview: `diff_preview - Preview changes
    ============================
    See what changes would be made before applying.
    
    Examples:
      // Preview perl substitution
      diff_preview({ file: "config.json", command: "s/8080/3000/g" })
      
      // Preview with sed syntax
      diff_preview({ file: "data.txt", command: "10,20d", tool: "sed" })
      
      // Preview AWK processing  
      diff_preview({ file: "log.csv", command: "BEGIN{FS=\",\"} {print $2}", tool: "awk" })
    
    Output:
    - Shows unified diff format
    - No changes made to original file
    - Temp files are cleaned up
    `,

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/MikeyBeez/mcp-smalledit'

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