Essential Supporting Tools

Agent Workflow

Effective agents follow this pattern:
  1. 🔍 Search: Find relevant code with codebase_search or grep_search
  2. 📖 Read: Get context with read_file before editing
  3. ✏️ Edit: Make precise changes with edit_file
  4. ✅ Verify: Read again to confirm changes worked

Common Patterns

Delete a section in between:
// ... existing code ...
function keepThis() {
  return "stay";
}

function alsoKeepThis() {
  return "also stay";
}
// ... existing code ...
Add imports:
import { useState, useEffect } from "react";
import { calculateTax } from "./utils"; // New import
// ... existing code ...
Update configuration:
{
  "name": "my-app",
  "version": "2.0.0",
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "test": "jest"
  }
}
Add error handling:
// ... existing code ...
function divide(a, b) {
  if (b === 0) {
    throw new Error("Cannot divide by zero");
  }
  return a / b;
}
// ... existing code ...
Update function parameters:
// ... existing code ...
function authenticateUser(email, password) {
  const result = await verifyUser(email, password);
  if (result) {
    return "Authenticated";
  } else {
    return "Unauthenticated";
  }
}
// ... existing code ...
Add new methods to a class:
// ... existing code ...
class UserService {
  async getUser(id) {
    return await this.db.findUser(id);
  }

  async updateUser(id, data) {
    return await this.db.updateUser(id, data);
  }
}
// ... existing code ...

Error Handling

Morph is trained to be robust to poor quality update snippets, but you should still follow these steps to ensure the best quality. When tools fail, follow these steps:
  1. Check file permissions: Ensure the target file is writable
  2. Verify file path: Confirm the file exists and path is correct
  3. Review syntax: Check that your edit snippet follows the // ... existing code ... pattern
  4. Retry with context: Read the file again and provide more context around your changes
  5. Simplify changes: Break complex edits into smaller, focused changes
Common Error Patterns:
// ❌ Wrong - missing context
function newFunction() {
  return "hello";
}

// ✅ Correct - with context
// ... existing code ...
function newFunction() {
  return "hello";
}
// ... existing code ...

Next Steps

Ready to start building with Morph? Here’s what to do next:

Explore the Apply API

Learn about the Apply API endpoints, models, and message formats for production use

Quickstart Guide

Step-by-step guide to configure your agent with the edit_file tool and integrate with Morph’s Fast Apply API

One-Shot Implementation

Copy-paste ready edit_file tool implementation for Cursor, Windsurf, and other AI IDEs
For complex refactoring across multiple files, consider using multiple edit_file calls in sequence. For failed edits, read the file again and provide more context around your changes.