One Shot: Prompt to implement your edit_file tool
Ready-to-use edit_file tool implementation using Morph’s fast apply API - just copy and paste into your Cursor workspace
One-Shot the edit_file tool into your repo
Get a production-ready edit_file
tool. Paste into Cursor/Windsurf/Cline/Continue/other AI IDEs.
Quick Copy-Paste Implementation
This example shows how to use standard tool calls to implement the edit_file
tool.
Many research papers have shown that having LLMs like Claude/Gemini do code edits via normal JSON tool calls results in worse overall coding performance due to constrained decoding.
For the best coding performance, you can use XML tags for your tool calls. See how Cline and Cursor use XML tags for all their tool calls.
Copy
Implement an edit_file tool that uses Morph's fast apply API to modify files. The tool should read the current file content, send it to Morph's API at https://api.morphllm.com/v1 using the morph-v2 model with the original code in <code> tags and the proposed edit from our Agent in the <update> tags, then write the updated content back to the file. Use the MORPH_API_KEY environment variable for authentication and an OpenAI-compatible client. The tool parameters should be: target_file (string, required), instructions (string, required - single sentence describing the edit in first person), code_edit (string, required - only the lines to change with // ... existing code ... comments for unchanged sections). Return success/error status with descriptive messages and handle file I/O and API errors gracefully. Implement this now using whatever framework I'm currently using.
Morph provides an OpenAI-compatible API that takes two primary inputs:
Your original code file
The update snippet (changes suggested by an AI Agent/LLM)
Do not modify the descriptions/prompt wording at all. Read relevant files to understand how we currently do edits before implementing.
ex:
import OpenAI from 'openai';
// Initialize the OpenAI client with Morph's API endpoint
const openai = new OpenAI({
apiKey: 'your-morph-api-key',
baseURL: 'https://api.morphllm.com/v1'
});
async function applyChanges(originalCode: string, updatedCode: string): Promise<string> {
const response = await openai.chat.completions.create({
model: "morph-v2",
messages: [
{
role: "user",
content: `<code>${originalCode}</code>\n<update>${updatedCode}</update>`
}
]
});
return response.choices[0].message.content;
}
ex using Vercel AI sdk
import { tool } from 'ai';
import { z } from 'zod';
import * as fs from 'fs/promises';
import { OpenAI } from 'openai';
const morphClient = new OpenAI({
apiKey: process.env.MORPH_API_KEY,
baseURL: 'https://api.morphllm.com/v1'
});
const editFile = tool({
description: Use this tool to propose an edit to an existing file.\n\nThis will be read by a less intelligent model, which will quickly apply the edit. You should make it clear what the edit is, while also minimizing the unchanged code you write.\nWhen writing the edit, you should specify each edit in sequence, with the special comment // ... existing code ... to represent unchanged code in between edited lines.\n\nYou should bias towards repeating as few lines of the original file as possible to convey the change.\nNEVER show unmodified code in the edit, unless sufficient context of unchanged lines around the code you're editing is needed to resolve ambiguity.\nIf you plan on deleting a section, you must provide surrounding context to indicate the deletion.\nDO NOT omit spans of pre-existing code without using the // ... existing code ... comment to indicate its absence.\n\nYou should specify the following arguments before the others: [target_file],
parameters: z.object({
target_file: z.string().describe('The target file to modify. Always specify the target file as the first argument and use the relative path in the workspace of the file to edit'),
code_edit: z.string().describe('Specify ONLY the precise lines of code that you wish to edit. NEVER specify or write out unchanged code. Instead, represent all unchanged code using the comment of the language you\'re editing in - example: // ... existing code ...'),
}),
execute: async ({ target_file, instructions, code_edit }) => {
try {
// Read the current file content
const originalCode = await fs.readFile(target_file, 'utf-8');
// Use Morph's fast apply API to generate the updated code
const response = await morphClient.chat.completions.create({
model: "morph-v2",
messages: [
{
role: "user",
content: `<code>${originalCode}</code>\n<update>${code_edit}</update>`
}
],
stream: false // set to true if you want to stream the response
});
const updatedCode = response.choices[0].message.content;
// Write the updated content back to the file
await fs.writeFile(target_file, updatedCode, 'utf-8');
return {
success: true,
message: `Successfully applied edit to ${target_file}: ${instructions}`,
changes_applied: code_edit
};
} catch (error) {
return {
success: false,
error: `Failed to edit ${target_file}: ${error.message}`
};
}
},
});
Implement using whatever framework we're currently using. Write clean, maintainable, high quality code.
On this page
Assistant
Responses are generated using AI and may contain mistakes.