Please contact us at info@morphllm.com for access to the Morph Embeddings and Morph Reranking models.

Agent Tools for Code and File Manipulation

This guide explores the key tools available to AI agents for code and file manipulation, providing best practices for implementing and using these tools in your agent prompts.

Understanding Tool Architecture

Tools transform agents from conversational assistants to action-oriented systems. Effective agents require:

  • Clear tool definitions: Functions with precise descriptions and parameters
  • Usage protocols: Guidelines for when and how to use each tool
  • Error handling: Procedures for when tools don’t behave as expected

The Edit File Tool

The edit_file tool is the most critical tool for coding agents. It enables precise, fast modifications to any file in your codebase without requiring the reasoning agent to rewrite entire files.

How Edit File Works

The tool operates on a simple principle: show only what changes, mark everything else as unchanged. This approach:

  • Minimizes token usage by not repeating large unchanged sections
  • Reduces errors by providing clear context about what should change
  • Enables precise edits in large files without full rewrites
  • Works with any file type - code, configs, docs, anything

Key Concepts

  1. Precision: Only specify the lines that need to change
  2. Context Markers: Use // ... existing code ... to indicate unchanged sections
  3. Clear Boundaries: Provide enough surrounding context for proper placement

Edit Format Examples

JavaScript/TypeScript files:

// ... existing code ...
function calculateTotal(items) {
  return items.reduce((total, item) => {
    // Add tax calculation
    const tax = item.price * 0.08;
    return total + item.price + tax;
  }, 0);
}
// ... existing code ...

Python files:

# ... existing code ...
def calculate_total(items):
    total = 0
    for item in items:
        // Add tax calculation
        tax = item.price * 0.08
        total += item.price + tax
    return total
# ... existing code ...

Best Practices

  • Read before you edit: Always examine the file first to understand its structure
  • Minimal changes: Only include the lines that actually need modification
  • Sufficient context: Include enough surrounding lines for unambiguous placement
  • Language-appropriate comments: Use the correct comment syntax for each file type
  • Clear instructions: Provide concise descriptions of what you’re changing

Common Patterns

Adding new code:

// ... existing code ...
export function newFunction() {
  return "This is new functionality";
}
// ... existing code ...

Modifying existing code:

// ... existing code ...
function existingFunction() {
  // Updated implementation
  return "Modified behavior";
}
// ... existing code ...

Multiple changes in sequence:

// ... existing code ...
const FIRST_CHANGE = "new value";
// ... existing code ...
function secondChange() {
  return "updated function";
}
// ... existing code ...
const THIRD_CHANGE = "another new value";
// ... existing code ...

Core File Manipulation Tools

Discovery and Navigation Tools

Advanced Tools

Tool Usage Workflow

Effective coding agents typically follow this tool usage pattern:

  1. Discovery: Use list_dir to understand project structure
  2. Search: Use codebase_search or grep_search to find relevant code - Use Morph Embeddings and Morph Reranking to implement the tools - contact info@morphllm.com for access
  3. Context Building: Use read_file to examine important sections
  4. Modification: Use edit_file to make precise changes
  5. Verification: Use read_file again to verify changes were applied correctly

Tool Prompting Guidelines

When implementing tools in your agent prompts:

  1. Clear function definitions: Provide comprehensive JSONSchema definitions
  2. Usage protocols: Define when and how each tool should be used
  3. Error handling: Include procedures for tool failures
  4. Context management: Guide the agent to build sufficient context before actions
  5. Progressive approach: Start with discovery, then focused understanding, then action

Common Tool Anti-patterns

Avoid these common mistakes in agent tool implementations:

  • Insufficient context: Editing without fully understanding the code
  • Over-verbosity: Including too much unchanged code in edits
  • Premature editing: Making changes before properly exploring the codebase
  • Context fragmentation: Reading small disconnected sections without building overall understanding
  • Ignoring error recovery: Not implementing fallbacks when tools fail

Conclusion

Effective tool usage transforms AI agents from passive assistants to active agentic partners. By combining comprehensive tool definitions with clear usage protocols and error handling procedures, you can create agents that navigate, understand, and modify code with high accuracy and efficiency.