Morph

Morph#

Gives the LLM agent the ability to edit files using Morph Fast Apply v2.

Morph is a specialized code-patching LLM that applies edits at 4000+ tokens per second. It uses a different format than the patch tool: <code>original</code><update>changes</update>

Environment Variables:

OPENROUTER_API_KEY: Required for accessing Morph via OpenRouter

Instructions

Use this tool to propose an edit to an existing file.

### When to use morph vs patch

Use morph for large or complex edits where the changed lines are scattered
across a file and patch context markers would be verbose. Prefer patch for
small, targeted edits with clear context. Morph requires OPENROUTER_API_KEY.

Write a clear edit while minimizing unchanged code.
List each edit in sequence, using `// ... existing code ...` for untouched spans.
Repeat only enough original context to disambiguate the change.
If you delete a section, include surrounding context to show what is removed.

Examples

```morph example.py
// ... existing code ...
FIRST_EDIT
// ... existing code ...
SECOND_EDIT
// ... existing code ...
THIRD_EDIT
// ... existing code ...
```
gptme.tools.morph.execute_morph(code: str | None, args: list[str] | None, kwargs: dict[str, str] | None) Generator[Message, None, None]

Applies the morph edit.

gptme.tools.morph.execute_morph_impl(content: str, path: Path | None, expected_original_content: str) Generator[Message, None, None]

Actual morph implementation - writes the edited content to file.

gptme.tools.morph.is_openrouter_available() bool

Check if OpenRouter is available for Morph tool.

gptme.tools.morph.preview_morph(content: str, path: Path | None) str | None

Prepare preview content for morph operation.