Skip to main content
AI agents edit files using // ... existing code ... markers instead of sending full files. Morph merges server-side at 10,500 tokens/s. Why this matters: Traditional search-replace uses 40% more tokens and takes more turns. Fast Apply is instant.

Installation

npm install @morphllm/morphsdk

Quick Start

import { MorphClient } from '@morphllm/morphsdk';
import Anthropic from '@anthropic-ai/sdk';
import { createEditFileTool } from '@morphllm/morphsdk/tools/fastapply/anthropic';

// Initialize
const morph = new MorphClient({ apiKey: process.env.MORPH_API_KEY });
const anthropic = new Anthropic();

// Give tool to agent
const response = await anthropic.messages.create({
  model: "claude-sonnet-4-5-20250929",
  tools: [createEditFileTool()],
  messages: [{ 
    role: "user", 
    content: "Add error handling to src/auth.ts" 
  }]
});

<Note>
  The `instructions` parameter provides crucial context for ambiguous edits, helping the apply model make correct decisions and achieve near perfect accuracy. Have the parent model generate the instructions.
</Note>

// Or use directly
const result = await morph.fastApply.execute({
  target_filepath: 'src/auth.ts',
  instructions: 'I will add null check',
  code_edit: '// ... existing code ...\nif (!user) throw new Error("Not found");\n// ... existing code ...'
});

How It Works

Agent outputs lazy edit:
async function login(email: string, password: string) {
  // ... existing code ...
  
  if (!user) {
    throw new Error('Invalid credentials');
  }
  
  // ... existing code ...
}
Morph merges into your actual file:
@@ -12,6 +12,10 @@
   const user = await db.findUser(email);
+  
+  if (!user) {
+    throw new Error('Invalid credentials');
+  }
   
   return createSession(user);
Key: The // ... existing code ... markers tell Morph where to insert changes without sending the full file.

Direct Usage

Use without an agent:
const result = await morph.fastApply.execute({
  target_filepath: 'src/auth.ts',
  instructions: 'I will add null check',
  code_edit: '// ... existing code ...\nif (!user) throw new Error("Not found");\n// ... existing code ...'
});

console.log(result.success); // true
console.log(`+${result.changes.linesAdded} -${result.changes.linesRemoved}`);

Configuration

import { createEditFileTool } from '@morphllm/morphsdk/tools/fastapply/anthropic';

const tool = createEditFileTool({
  baseDir: './src',       // Default: process.cwd()
  morphApiKey: 'sk-...',  // Or MORPH_API_KEY env var
  autoWrite: true,        // Auto-write files (default: true)
  generateUdiff: true     // Return diff (default: true)
});

API

Input:
{
  target_filepath: string,  // Relative to baseDir
  instructions: string,     // What the model is changing
  code_edit: string        // Code with // ... existing code ...
}
Returns:
{
  success: boolean,
  changes: { linesAdded, linesRemoved, linesModified },
  udiff?: string,
  error?: string
}

Error Handling

if (!result.success) {
  console.error(result.error);
  // "File not found" | "Invalid filepath" | "API error"
}