OpenCode plugin for Morph Fast Apply - 10x faster code editing with lazy edit markers.
- 10,500+ tokens/sec code editing via Morph's Fast Apply API
- Lazy edit markers (
// ... existing code ...) - no exact string matching needed - 98% accuracy with intelligent code merging
- Pre-flight validation - prevents accidental file deletions when markers are missing
- Unified diff output with context for easy review
- Custom TUI display - branded titles like
Morph: src/file.ts +15/-3 (450ms) - Graceful fallback - suggests native
edittool on API failure
Note: This is an OpenCode plugin that wraps Morph's Fast Apply API. For the official Morph MCP server (which includes WarpGrep search), see @morphllm/morphmcp. This plugin uses
morph_editas the tool name to avoid conflicts if you have both installed.
Preferred when your setup syncs packaged instructions into ~/.config/opencode/instructions/:
Add to your global config (~/.config/opencode/opencode.json):
{
"instructions": [
"~/.config/opencode/instructions/morph-tools.md"
],
"plugin": [
"github:JRedeker/opencode-morph-fast-apply"
]
}Or pin to a specific version:
{
"instructions": [
"~/.config/opencode/instructions/morph-tools.md"
],
"plugin": [
"github:JRedeker/opencode-morph-fast-apply#v1.8.2"
]
}Recommended setup summary:
- Prefer the stable synced instruction path:
~/.config/opencode/instructions/morph-tools.md - Use the packaged
node_modules/.../morph-tools.mdpath only when your setup does not sync instructions - Keep the routing policy in the OpenCode
instructionsarray, not a skill
If you are installing the plugin directly and do not sync packaged instructions into
~/.config/opencode/instructions/, you can point OpenCode at the packaged file
instead:
{
"instructions": [
"~/.config/opencode/node_modules/opencode-morph-fast-apply/instructions/morph-tools.md"
],
"plugin": [
"github:JRedeker/opencode-morph-fast-apply"
]
}The morph_edit tool description remains self-contained, but for more reliable
tool selection you should also load the packaged always-on instruction file
shown above. This avoids agents defaulting to native edit when morph_edit
is the better fit for large, scattered, or whitespace-sensitive edits.
Important: the active agent must also expose morph_edit in its tool manifest.
If an agent profile marks morph_edit: false, the model cannot choose Morph
even when the plugin is installed and the always-on instruction is present.
Get an API key at morphllm.com/dashboard, then add to your shell profile:
export MORPH_API_KEY="sk-your-key-here"The morph_edit tool will now be available.
The LLM uses morph_edit for efficient partial file edits:
morph_edit({
target_filepath: "src/auth.ts",
instructions: "I am adding error handling for invalid tokens",
code_edit: `// ... existing code ...
function validateToken(token) {
if (!token) {
throw new Error("Token is required");
}
// ... existing code ...
}
// ... existing code ...`
})
| Situation | Tool | Reason |
|---|---|---|
| Small, exact replacement | edit |
Fast, no API call |
| Large file (500+ lines) | morph_edit |
Handles partial snippets |
| Multiple scattered changes | morph_edit |
Batch efficiently |
| Whitespace-sensitive | morph_edit |
Forgiving with formatting |
| Variable | Default | Description |
|---|---|---|
MORPH_API_KEY |
(required) | Your Morph API key |
MORPH_API_URL |
https://api.morphllm.com |
API endpoint |
MORPH_MODEL |
morph-v3-fast |
Model to use (see below) |
MORPH_TIMEOUT |
30000 |
Request timeout in ms |
| Model | Speed | Accuracy | Best For |
|---|---|---|---|
morph-v3-fast |
10,500+ tok/sec | 96% | Real-time applications, quick edits |
morph-v3-large |
5,000+ tok/sec | 98% | Complex changes, highest accuracy |
auto |
5,000-10,500 tok/sec | ~98% | Recommended - automatically selects optimal model |
- Reads the original file content
- Sends to Morph API:
<instruction>- Your intent description<code>- The complete original file<update>- Your partial edit with markers
- Morph intelligently merges the lazy edit markers with original code
- Writes the merged result back to the file
- Returns a unified diff showing what changed
Ensure the environment variable is set and exported:
echo $MORPH_API_KEY # Should show your keyIncrease the timeout for large files:
export MORPH_TIMEOUT=60000 # 60 secondsIf code is being deleted unexpectedly, ensure your code_edit includes // ... existing code ... markers at the start and end. Omitting these markers tells Morph to replace the entire file.
The plugin now blocks unsafe Morph responses before writing files:
- Marker leakage guard: If merged output contains
// ... existing code ...but the original file did not, the write is aborted. - Catastrophic truncation guard: If merged output loses more than 60% of characters and more than 50% of lines (for marker-based edits), the write is aborted.
In both cases, morph_edit returns a detailed error with recovery options (retry with tighter anchors, use native edit, or split into smaller edits).
If an AI agent wraps code_edit in markdown fences (for example, ```typescript ... ```), morph_edit strips the outer fence before sending content to Morph.
If edits are applied to the wrong location, add more unique context around your changes and make your instructions more specific about which function/section you're modifying.
The morph_edit tool is disabled in readonly agent modes (plan, explore). Switch to a build/code mode to make edits.
- Tool exposure requirement docs —
instructions/morph-tools.mdnow documents that agent profiles must exposemorph_edit: truein their tool manifest for the routing policy to take effect - README alignment — Installation section notes the tool manifest requirement alongside the always-on instruction
- Test coverage — Tests verify the tool exposure section exists in the instruction file and README
- Stable instruction path guidance - README now prefers
~/.config/opencode/instructions/morph-tools.mdand keeps the packagednode_modules/...path as a fallback for direct installs - Canonical routing policy wording -
instructions/morph-tools.mdnow explicitly says morph routing belongs in always-on instructions, not a skill - Docs/test alignment - Tests now verify the stable instruction path and canonical policy wording
- Packaged always-on instruction -
instructions/morph-tools.mdnow ships with the plugin so OpenCode can load reliablemorph_editselection guidance through theinstructionsarray - Packaging coverage - package metadata now includes the
instructions/directory, and tests verify that the shipped instruction path is documented - Installation docs updated - README now recommends adding the packaged always-on instruction to global OpenCode config for more reliable
morph_editselection
- Self-contained tool description — Core
morph_editguidance still lives in the tool description so the tool is usable even without extra setup. - Packaged always-on instruction —
instructions/morph-tools.mdcan be added to your OpenCodeinstructionsarray so agents choosemorph_editmore reliably. - Removed skill pattern — Deleted
skills/morph/SKILL.mdandMORPH_INSTRUCTIONS.md. The skill added a round-trip and split-brain problem where guidance lived in multiple places. - Removed
MORPH_SKILL_LOAD_HINT— Agents no longer need to load a skill before usingmorph_edit.
- Post-merge safety guards - Blocks unsafe Morph output before writing to disk:
- Marker leakage guard - Detects when merge model treats
// ... existing code ...as literal text - Truncation guard - Dual-metric validation (>60% char + >50% line loss) prevents silent data loss
- Marker leakage guard - Detects when merge model treats
- Input normalization - Strips markdown fences from
code_editto prevent merge confusion - TUI guard titles - Shows
Morph: blocked (marker leakage)orMorph: blocked (truncation)on guard failures - 32 test cases - Comprehensive coverage of normalization, leakage detection, and truncation edge cases
- Custom TUI display - Tool output shows branded titles like
Morph: src/file.ts +15/-3 (450ms) - API timing - Response time tracked and displayed in tool title
- Structured metadata - Provider, version, model info included for future TUI enhancements
- Fixed stdout pollution - Replaced
console.log/console.warnwithclient.app.log()SDK method - Readonly agent protection - Blocks
morph_editinplanandexploremodes to prevent accidental edits - Stderr fallback - Graceful logging if SDK method unavailable
- Pre-flight validation prevents accidental deletions - Tool now returns an error if
code_editis missing markers for files >10 lines, preventing catastrophic code loss - Improved code_edit parameter description - Changed from "Only the changed lines" to "The code changes wrapped with markers" to emphasize wrapping
- Added explicit wrapping rule - Tool description now includes "ALWAYS wrap your changes with markers at the start AND end"
- Restored comprehensive examples -
MORPH_INSTRUCTIONS.mdnow includes all key examples (adding functions, modifying code, deletions) - Added Quick Reference - Aligned with Morph's official agent instructions format
- Added fetch timeout example - From official Morph documentation
- Tool schema aligned with Morph official spec - Tool description now matches Morph's recommended format
- Concise AI agent instructions -
MORPH_INSTRUCTIONS.mdreduced by 72% (237→67 lines) while keeping critical guidance - Added marker validation warning - Server logs warn when
code_editis missing markers (helps debug unexpected deletions) - Documentation improvements - Added troubleshooting section, model comparison table, updated config paths
- Initial public release
- Morph Fast Apply integration with lazy edit markers
- Unified diff output
- Graceful fallback suggestions
Contributions welcome! This plugin could potentially be integrated into OpenCode core.