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
    `,
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool shows a diff preview without making changes, which implies it's non-destructive, but doesn't clarify output format, error handling, or any constraints like file size limits or supported diff formats. This leaves significant gaps for a tool that interacts with files.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, clear sentence that efficiently conveys the core functionality without unnecessary words. It's front-loaded and every part earns its place, making it easy for an agent to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of file manipulation and diff generation, the description is incomplete. With no annotations and no output schema, it fails to explain what the preview output looks like, potential side effects, or error conditions. This makes it inadequate for safe and effective use by an AI agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents all parameters. The description adds no additional meaning beyond what's in the schema, such as examples of diff output or how 'command' interacts with 'tool'. This meets the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Preview what changes would be made by showing a diff.' It specifies the verb ('preview') and resource ('changes'), but doesn't explicitly differentiate from sibling tools like 'quick_replace' or 'sed_edit' that might also involve changes or diffs.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools or contexts where this preview is preferred over direct editing tools like 'sed_edit' or 'perl_edit', leaving the agent to guess based on the name alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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