-
Notifications
You must be signed in to change notification settings - Fork 0
feat: code vectorization pipeline and search-code MCP tool #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Commands; | ||
|
|
||
| use App\Services\CodeIndexerService; | ||
| use App\Services\SymbolIndexService; | ||
| use LaravelZero\Framework\Commands\Command; | ||
|
|
||
| use function Laravel\Prompts\error; | ||
| use function Laravel\Prompts\info; | ||
| use function Laravel\Prompts\note; | ||
|
|
||
| class VectorizeCodeCommand extends Command | ||
| { | ||
| protected $signature = 'vectorize-code | ||
| {repo : Repository identifier (e.g. local/pstrax-laravel)} | ||
| {--kind=* : Symbol kinds to include (e.g. class, method, function)} | ||
| {--language= : Filter by language (e.g. php, typescript)}'; | ||
|
|
||
| protected $description = 'Vectorize tree-sitter symbols into Qdrant for semantic code search'; | ||
|
|
||
| public function handle(SymbolIndexService $symbolIndex, CodeIndexerService $codeIndexer): int | ||
| { | ||
| $repo = $this->argument('repo'); | ||
| if (! is_string($repo)) { | ||
| error('Repository argument is required.'); | ||
|
|
||
| return self::FAILURE; | ||
| } | ||
|
|
||
| $home = getenv('HOME') !== false ? (string) getenv('HOME') : '/tmp'; | ||
| $indexPath = "{$home}/.code-index/".str_replace('/', '-', $repo).'.json'; | ||
|
|
||
| if (! file_exists($indexPath)) { | ||
| error("Index not found at {$indexPath}. Run index-code first."); | ||
|
|
||
| return self::FAILURE; | ||
| } | ||
|
|
||
| if (! $codeIndexer->ensureCollection()) { | ||
| error('Failed to create/verify Qdrant code collection.'); | ||
|
|
||
| return self::FAILURE; | ||
| } | ||
|
|
||
| /** @var array<string> $kinds */ | ||
| $kinds = $this->option('kind'); | ||
| $language = $this->option('language'); | ||
| $language = is_string($language) ? $language : null; | ||
|
|
||
| $label = $repo; | ||
| if ($kinds !== []) { | ||
| $label .= ' ('.implode(', ', $kinds).')'; | ||
| } | ||
| if ($language !== null) { | ||
| $label .= " [{$language}]"; | ||
| } | ||
|
|
||
| info("Vectorizing symbols from {$label}"); | ||
|
|
||
| $lastReport = 0; | ||
| $result = $codeIndexer->vectorizeFromIndex( | ||
| $indexPath, | ||
| $repo, | ||
| $symbolIndex, | ||
| $kinds, | ||
| $language, | ||
| function (int $success, int $failed, int $total) use (&$lastReport): void { | ||
| $done = $success + $failed; | ||
| if ($done - $lastReport >= 100 || $done === $total) { | ||
| $lastReport = $done; | ||
| note("{$done}/{$total} processed ({$success} ok, {$failed} failed)"); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| info("Done: {$result['success']}/{$result['total']} symbols vectorized, {$result['failed']} failed"); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Mcp\Tools; | ||
|
|
||
| use App\Services\CodeIndexerService; | ||
| use Illuminate\Contracts\JsonSchema\JsonSchema; | ||
| use Laravel\Mcp\Request; | ||
| use Laravel\Mcp\Response; | ||
| use Laravel\Mcp\Server\Attributes\Description; | ||
| use Laravel\Mcp\Server\Tool; | ||
| use Laravel\Mcp\Server\Tools\Annotations\IsIdempotent; | ||
| use Laravel\Mcp\Server\Tools\Annotations\IsReadOnly; | ||
|
|
||
| #[Description('Semantic code search across indexed repositories. Search by natural language to find classes, methods, functions, and their source code.')] | ||
| #[IsReadOnly] | ||
| #[IsIdempotent] | ||
| class SearchCodeTool extends Tool | ||
| { | ||
| public function __construct( | ||
| private readonly CodeIndexerService $codeIndexer, | ||
| ) {} | ||
|
|
||
| public function handle(Request $request): Response | ||
| { | ||
| /** @var string $query */ | ||
| $query = $request->get('query'); | ||
|
|
||
| if (! is_string($query) || strlen($query) < 2) { | ||
| return Response::error('A search query of at least 2 characters is required.'); | ||
| } | ||
|
|
||
| $limit = is_int($request->get('limit')) ? min($request->get('limit'), 20) : 10; | ||
|
|
||
| $filters = array_filter([ | ||
| 'repo' => is_string($request->get('repo')) ? $request->get('repo') : null, | ||
| 'language' => is_string($request->get('language')) ? $request->get('language') : null, | ||
| ]); | ||
|
|
||
| $results = $this->codeIndexer->search($query, $limit, $filters); | ||
|
|
||
| if ($results === []) { | ||
| return Response::text(json_encode([ | ||
| 'results' => [], | ||
| 'meta' => ['query' => $query, 'total' => 0], | ||
| ], JSON_THROW_ON_ERROR)); | ||
| } | ||
|
|
||
| $formatted = array_map(fn (array $r): array => [ | ||
| 'filepath' => $r['filepath'], | ||
| 'repo' => $r['repo'], | ||
| 'language' => $r['language'], | ||
| 'symbol_name' => $r['symbol_name'] ?? null, | ||
| 'symbol_kind' => $r['symbol_kind'] ?? null, | ||
| 'line' => $r['start_line'], | ||
| 'score' => round($r['score'], 3), | ||
| 'content' => $r['content'], | ||
| ], $results); | ||
|
|
||
| return Response::text(json_encode([ | ||
| 'results' => $formatted, | ||
| 'meta' => [ | ||
| 'query' => $query, | ||
| 'total' => count($formatted), | ||
| ], | ||
| ], JSON_THROW_ON_ERROR)); | ||
| } | ||
|
|
||
| public function schema(JsonSchema $schema): array | ||
| { | ||
| return [ | ||
| 'query' => $schema->string() | ||
| ->description('Natural language query (e.g., "rate limiting middleware", "database migration logic")') | ||
| ->required(), | ||
| 'repo' => $schema->string() | ||
| ->description('Filter to a specific repo (e.g., "local/pstrax-laravel").'), | ||
| 'language' => $schema->string() | ||
| ->description('Filter by language (php, typescript, javascript, python).'), | ||
| 'limit' => $schema->integer() | ||
| ->description('Max results (default 10, max 20).') | ||
| ->default(10), | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded Python path breaks portability.
The path
/opt/homebrew/opt/python@3.12/bin/python3.12is specific to macOS with Homebrew on Apple Silicon. This will fail on:/usr/local/opt/...)🔧 Suggested fix: Use configurable or discoverable path
Then add to
config/services.php:🤖 Prompt for AI Agents