Skip to main content
Search code using natural language queries. Two-stage retrieval: vector search (fast, broad) + GPU reranking (precise).
Push your code with morph.git.push() first (see Repo Storage). Embedding takes 3-8 seconds in background.

Installation

npm install @morphllm/morphsdk

Quick Start

  • Anthropic
  • OpenAI
  • Vercel AI SDK
  • Direct
import Anthropic from '@anthropic-ai/sdk';
import { createCodebaseSearchTool } from '@morphllm/morphsdk/tools/codebase-search/anthropic';

const anthropic = new Anthropic();
const tool = createCodebaseSearchTool({ 
  repoId: 'my-project', // will use latest main
  // Optional: search specific branch or commit
  // branch: 'develop',
  // or:
  // commitHash: 'abc123...'
});

const response = await anthropic.messages.create({
  model: "claude-sonnet-4-5-20250929",
  tools: [tool],
  messages: [{ 
    role: "user", 
    content: "Find the authentication code" 
  }],
  max_tokens: 12000
});

How It Works

Two-stage retrieval (~1000ms total):
  1. Vector search (~240ms) - Embed query, HNSW index retrieves top 50 candidates
  2. GPU rerank (~630ms) - morph-rerank-v3 scores for precision
  3. Returns top 10 most relevant
Why two stages? Vector search is fast but imprecise. Reranking is slow but accurate. Together = fast + accurate.

Direct Usage

const results = await morph.codebaseSearch.search({
  query: "Where is JWT validation implemented?",
  repoId: 'my-project',
  target_directories: [], // Empty = all, or ["src/auth"]
  limit: 10,
  // Optional: search specific branch or commit
  // branch: 'develop',        // Uses latest commit on 'develop'
  // commitHash: 'abc123...'   // Uses exact commit (takes precedence)
});

console.log(`Found ${results.results.length} matches in ${results.stats.searchTimeMs}ms`);
results.results.forEach(r => {
  console.log(`${r.filepath} - ${(r.rerankScore * 100).toFixed(1)}% match`);
  console.log(r.content);
});

Search Tips

Good queries:
  • “Where is JWT validation implemented?”
  • “Show database error handling”
  • “Find the login flow”
Avoid:
  • Single words (“auth”)
  • Too vague (“code”)
  • Too broad (“everything”)

Searching Specific Branches or Commits

By default, semantic search uses the latest commit on main. You can search specific branches or exact commits:
  • Latest Main (default)
  • Specific Branch
  • Exact Commit
// Searches latest commit on 'main' branch
const results = await morph.codebaseSearch.search({
  query: "How does auth work?",
  repoId: 'my-project'
});
Priority: commitHash (if provided) > branch (if provided) > main (default)

API

Input:
{
  query: string,              // Natural language question
  repoId: string,             // Repository ID
  branch?: string,            // Optional: branch name (uses latest commit)
  commitHash?: string,        // Optional: specific commit (takes precedence)
  target_directories: string[], // Filter paths, or [] for all
  limit?: number              // Max results (default: 10)
}
Returns:
{
  success: boolean,
  results: [{
    filepath: string,         // "auth.ts::login@L5-L20"
    content: string,          // Code chunk
    rerankScore: number,      // 0-1 relevance (use this!)
    language: string,
    startLine: number,
    endLine: number
  }],
  stats: { searchTimeMs: number }
}