morph-warpgrep について
Morph WarpGrep & Fast Applyは、コーディングエージェント向けのコードベース検索とコード編集を支援するスキルです。WarpGrepはローカルのファイルツールを反復的に使って関連箇所を探し、Fast Applyは生成された変更を既存コードに反映します。
主な機能
- 01WarpGrepによるコードベース検索
- 02ローカルファイルツールで検索を絞り込み
- 03関連するコードコンテキストの提示
- 04Fast Applyによる生成編集の統合
- 05MCP検索ツールとしての利用
活用シーン
- 01大規模コードベースのロジック追跡
- 02バグに関連するコードの特定
- 03生成された変更を既存ファイルに適用
MCP 対応
READMEでは、Claude CodeやCursorなどのクライアントに導入できるMCPサーバーを案内しています。`warpgrep_codebase_search`ツールを提供します。
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ letta-ai/ skills --skill morph-warpgrepインストール後、Claude Code で/morph-warpgrepを入力して読み込まれたか確認します。
同梱ファイルLICENSEscripts/test-warpgrep.ts
詳細
Morph WarpGrep と Fast Apply
Morph は、コーディングエージェントの性能を大幅に向上させる 2 つのツールを提供します。
- WarpGrep:通常の検索より 5 倍高速なエージェント型コード検索です。ツール呼び出しを並列実行し、約 4 ステップで F1 スコア 0.73 を達成します。
- Fast Apply:AI による編集を 10,500 tok/s、精度 98%(検索と置換の 2 倍の速度)でコードにマージします。
前提条件
- https://www.morphllm.com/dashboard から Morph API キーを取得します。
- 環境変数を設定します。
export MORPH_API_KEY="your-api-key"
- Morph SDK をインストールします。
bun add @morphllm/morphsdk
# or
npm install @morphllm/morphsdk
ripgrepがインストールされていることを確認します(ローカル検索に必要です)。
# macOS
brew install ripgrep
# Ubuntu/Debian
sudo apt install ripgrep
# Verify installation
rg --version
クイックテスト
セットアップ後、任意のローカルリポジトリで付属のテストスクリプトを実行します。
# Clone a test repo (or use any existing codebase)
git clone https://github.com/letta-ai/letta-code.git test-repo
# Install SDK
cd test-repo
bun add @morphllm/morphsdk
# Run test script
export MORPH_API_KEY="your-key"
bun ../scripts/test-warpgrep.ts .
期待される出力:
======================================================================
MORPH WARPGREP TEST
======================================================================
Repo: .
SDK: @morphllm/morphsdk
======================================================================
| Query | Result | Time | Files |
|------------------------------------|--------|--------|-------|
| Find the main entry point | ✅ | 5.2s | 2 |
| Find authentication logic | ✅ | 4.1s | 4 |
| Find where configuration is handled | ✅ | 3.8s | 3 |
| Find error handling patterns | ✅ | 4.5s | 5 |
======================================================================
Results: 4 passed, 0 failed
======================================================================
使用する場面
WarpGrep を使用する場面:
- 大規模なコードベース(1000 ファイル以上)を検索する場合
- バグの追跡、コードパス、制御フローの分析など、深いロジックに関する問い合わせ
- コンテキストウィンドウを不要な情報で埋めずに、関連するコンテキストを見つけたい場合
- 通常の grep では無関係な結果が多すぎる場合
Fast Apply を使用する場面:
- AI が生成したコード編集を既存ファイルに適用する場合
- 信頼性の高い編集のマージが必要な場合(検索と置換の約 70% に対し、精度 98%)
- 差分形式がうまく機能しない大きなファイルを扱う場合
使用しない場面:
- 単純な完全一致検索(通常の
grep/rgは無料で十分高速です) - セマンティック検索で十分な表層的な問い合わせ
- コストが重大な懸念事項である場合(Morph API の利用には費用がかかります)
クイックスタート:WarpGrep
基本的な使い方
import { MorphClient } from '@morphllm/morphsdk';
const morph = new MorphClient({ apiKey: process.env.MORPH_API_KEY });
const result = await morph.warpGrep.execute({
query: 'Find authentication middleware',
repoRoot: '.'
});
if (result.success) {
for (const ctx of result.contexts) {
console.log(`File: ${ctx.file}`);
console.log(ctx.content);
}
} else {
console.error('Search failed');
}
応答形式
interface WarpGrepResult {
success: boolean;
contexts: Array<{
file: string; // File path relative to repo root
content: string; // File content with relevant code
}>;
summary?: string; // Human-readable summary
}
エージェントのツールとして使用する
import { MorphClient } from '@morphllm/morphsdk';
import Anthropic from '@anthropic-ai/sdk';
const morph = new MorphClient({ apiKey: process.env.MORPH_API_KEY });
const anthropic = new Anthropic();
// Define WarpGrep as a tool
const tools = [{
name: 'warpgrep_search',
description: 'Search codebase for relevant code. Use for finding implementations, tracing bugs, or understanding code flow.',
input_schema: {
type: 'object',
properties: {
query: { type: 'string', description: 'What to search for' }
},
required: ['query']
}
}];
// Handle tool calls
async function handleToolCall(name: string, input: { query: string }) {
if (name === 'warpgrep_search') {
const result = await morph.warpGrep.execute({
query: input.query,
repoRoot: process.cwd()
});
if (result.success) {
return result.contexts.map(c => `## ${c.file}\n${c.content}`).join('\n\n');
}
return 'No results found';
}
}
クイックスタート:Fast Apply
Fast Apply は AI が生成した編集を既存のコードにマージします。
import { MorphClient } from '@morphllm/morphsdk';
const morph = new MorphClient({ apiKey: process.env.MORPH_API_KEY });
const result = await morph.fastApply.apply({
originalCode: `function divide(a, b) {
return a / b;
}`,
editSnippet: `function divide(a, b) {
if (b === 0) throw new Error("Division by zero");
return a / b;
}`
});
console.log(result.mergedCode);
直接 API を使用する(代替方法)
const response = await fetch('https://api.morphllm.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.MORPH_API_KEY}`
},
body: JSON.stringify({
model: 'morph-v3-fast', // or 'morph-v3-large' for complex edits
messages: [{
role: 'user',
content: `<instruction>Add error handling</instruction>
<code>function divide(a, b) { return a / b; }</code>
<update>function divide(a, b) {
if (b === 0) throw new Error("Division by zero");
return a / b;
}</update>`
}],
temperature: 0
})
});
const data = await response.json();
const mergedCode = data.choices[0].message.content;
テスト結果
SDK v0.2.103 を使用し、letta-code リポジトリ(TypeScript ファイル約 300 個)でテストしました。
| クエリ | 結果 | 時間 | 見つかったファイル |
|---|---|---|---|
| 「認証ロジックを探す」 | ✅ | 4.2s | src/auth/oauth.ts、src/auth/setup.ts、+2 |
| 「メインの CLI エントリーポイントを探す」 | ✅ | 5.8s | src/index.ts、src/cli/App.tsx |
| 「モデルの設定場所を探す」 | ✅ | 3.1s | src/agent/model.ts、src/models.json、+1 |
| 「メモリブロックの仕組みを探す」 | ✅ | 3.9s | src/agent/memory.ts、src/agent/memoryFilesystem.ts、+1 |
| 「設定マネージャーを探す」 | ✅ | 2.9s | src/settings-manager.ts、src/settings.ts |
5 件中 5 件のテストに合格
パフォーマンスの概要
- 平均時間:4.0 秒
- トークン効率:手動検索と比べて入力トークンが 39% 少なくなります
- 精度:2~4 ターンで関連コードを見つけます
WarpGrep の仕組み
WarpGrep は、最大 4 ターン実行するエージェント型検索です。
┌─────────────────────────────────────────────────────────────┐
│ Turn 1: Analyze query, map repo structure, initial search │
├─────────────────────────────────────────────────────────────┤
│ Turn 2-3: Refine search, read specific files │
├─────────────────────────────────────────────────────────────┤
│ Turn 4: Return all relevant code locations │
└─────────────────────────────────────────────────────────────┘
SDK は複数ターンの会話を自動的に処理し、ローカルツールを実行します。
| ツール | 説明 | 実装 |
|---|---|---|
grep | ファイル全体を正規表現で検索 | ripgrep(rg)を使用 |
read | ファイルの内容を読み取る | ローカルファイルシステム |
list_dir | ディレクトリ構造を表示 | ローカルファイルシステム |
アーキテクチャ
┌──────────────────────────────────────────────────────────────┐
│ Your Code / Agent │
└──────────────────────────┬───────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────┐
│ @morphllm/morphsdk │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ 1. Build repo structure │ │
│ │ 2. Send query to Morph API │ │
│ │ 3. Execute local tools (grep, read, list_dir) │ │
│ │ 4. Multi-turn refinement │ │
│ │ 5. Return relevant code contexts │ │
│ └─────────────────────────────────────────────────────────┘ │
└──────────────────────────┬───────────────────────────────────┘
│
┌──────────────┴──────────────┐
▼ ▼
┌───────────────────────┐ ┌───────────────────────┐
│ Morph API │ │ Local Filesystem │
│ (morph-warp-grep-v1) │ │ (ripgrep, fs) │
└───────────────────────┘ └───────────────────────┘
Morph のベンチマーク
Claude 4.5 Opus を使用した Morph の SWE-bench 評価結果:
| 指標 | WarpGrep なし | WarpGrep あり | 改善 |
|---|---|---|---|
| 入力トークン | 14K | 9K | 39% 削減 |
| エージェントのターン数 | 35.0 | 26.0 | 26% 削減 |
| 解決したタスク | 74.4% | 81.9% | 10% 増加 |
よく使われるパターン
調査してから実行
import { MorphClient } from '@morphllm/morphsdk';
const morph = new MorphClient({ apiKey: process.env.MORPH_API_KEY });
// 1. Search for relevant code
const result = await morph.warpGrep.execute({
query: 'Where is the payment processing logic?',
repoRoot: '.'
});
// 2. Use found contexts to inform next steps
if (result.success) {
const relevantFiles = result.contexts.map(c => c.file);
console.log('Found relevant files:', relevantFiles);
// Now read/edit these specific files
}
WarpGrep と Fast Apply を組み合わせる
import { MorphClient } from '@morphllm/morphsdk';
const morph = new MorphClient({ apiKey: process.env.MORPH_API_KEY });
// 1. Find the code to modify
const search = await morph.warpGrep.execute({
query: 'Find the user validation function',
repoRoot: '.'
});
if (search.success && search.contexts.length > 0) {
const targetFile = search.contexts[0];
// 2. Apply an edit
const result = await morph.fastApply.apply({
originalCode: targetFile.content,
editSnippet: '// Add your modified version here'
});
console.log(result.mergedCode);
}
MCP 統合
Claude Code、Cursor、その他の MCP クライアントで個人利用する場合:
# Install MCP server
claude mcp add morph --scope user -e MORPH_API_KEY=YOUR_API_KEY --npx -y @morphllm/morphmcp
これにより、MCP クライアントに warpgrep_codebase_search ツールが追加されます。
トラブルシューティング
ripgrep が見つからない
# Install ripgrep
brew install ripgrep # macOS
sudo apt install ripgrep # Ubuntu/Debian
# Verify
rg --version
API キーの問題
# Verify API key works
curl -X POST https://api.morphllm.com/v1/chat/completions \
-H "Authorization: Bearer $MORPH_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"morph-v3-fast","messages":[{"role":"user","content":"test"}]}'
SDK バージョンの問題
最新の SDK を使用していることを確認してください。
bun add @morphllm/morphsdk@latest
# or
npm install @morphllm/morphsdk@latest
コストに関する考慮事項
- WarpGrep は検索ごとに 1~4 回の API 呼び出しを使用します(通常は 2~3 回)
- Fast Apply は編集ごとに 1 回の API 呼び出しを使用します
- 料金:100 万トークンあたり 0.80 ドル(入力と出力)
- Morph Dashboard で使用状況を確認できます
- 単純な完全一致検索には、通常の grep/ripgrep を使用してください(無料)