后端与接口
无需先读大文件时,用 Morph Apply API…
适用于文件很大或不在上下文中、已知变更位置语义时,通过带标记的代码片段快速应用编辑。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
无需先读大文件时,用 Morph App特定
解决思路
- 在 `--code_edit` 中使用代码标记标注编辑位置:`// ... existing code ...`,或使用对应语言的注释形式(如 Python 用 `# ... existing code ...`)。
- 把要新增/修改的代码放在这些标记之间,API 会智能地将编辑放到正确位置。
- 通过 Bash 调用:`uv run python -m runtime.harness scripts/mcp/morph_apply.py --file "<文件路径>" --instruction "<变更描述>" --code_edit "<带标记的代码片段>"`。
- 传入必填参数:`--file`(要编辑的文件路径)、`--instruction`(人类可读的变更描述)、`--code_edit`(带标记位置的代码片段)。
- 添加错误处理示例:`uv run python -m runtime.harness scripts/mcp/morph_apply.py --file "src/auth.py" --instruction "Add error handling to login function" --code_edit "# ... existing code ...\ntry:\n user = authenticate(credentials)\nexcept AuthError as e:\n log.error(f'Auth failed: {e}')\n raise\n# ... existing code ..."`。
- 添加日志示例:`uv run python -m runtime.harness scripts/mcp/morph_apply.py --file "src/api.py" --instruction "Add debug logging" --code_edit "# ... existing code ...\nlogger.debug(f'Processing request: {request.id}')\n# ... existing code ..."`。
- TypeScript 示例:`uv run python -m runtime.harness scripts/mcp/morph_apply.py --file "src/types.ts" --instruction "Add user validation" --code_edit "// ... existing code ...\nif (!user) throw new Error('User not found');\nif (!user.isActive) throw new Error('User inactive');\n// ... existing code ..."`。
- 批量编辑时,在一个 `--code_edit` 中放入多个相关变更的标记片段,让 API 在一次操作中完成。
- 利用其能力:无需先读取整个文件;处理速度为 10,500 tokens/sec,放置准确率为 98%。
适用边界 · 注意事项
- 未在 `mcp_config.json` 中配置 `morph` server 或缺少 `MORPH_API_KEY` 时不可用。
- 文件已通过 Read 进入上下文、文件很小(<200 行)、或需要非常精确的 old/new 字符串匹配时,应改用 Claude Edit。
- 需要 100% 逐字精确匹配的编辑不适用;Morph Apply 是按 instruction 智能放置,准确率为 98%。