|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravilt\Plugins\Mcp\Tools; |
| 4 | + |
| 5 | +use Illuminate\JsonSchema\JsonSchema; |
| 6 | +use Illuminate\Support\Facades\File; |
| 7 | +use Laravel\Mcp\Request; |
| 8 | +use Laravel\Mcp\Response; |
| 9 | +use Laravel\Mcp\Server\Tool; |
| 10 | + |
| 11 | +class SearchDocsTool extends Tool |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The tool's description. |
| 15 | + */ |
| 16 | + protected string $description = 'Search the Laravilt Plugins documentation to understand features, architecture, and usage'; |
| 17 | + |
| 18 | + /** |
| 19 | + * Handle the tool request. |
| 20 | + */ |
| 21 | + public function handle(Request $request): Response |
| 22 | + { |
| 23 | + $query = $request->string('query'); |
| 24 | + $pluginsPath = base_path('packages/laravilt/plugins'); |
| 25 | + |
| 26 | + // Collect all documentation files |
| 27 | + $docFiles = $this->getDocumentationFiles($pluginsPath); |
| 28 | + |
| 29 | + // Search through documentation |
| 30 | + $results = $this->searchDocumentation($docFiles, $query); |
| 31 | + |
| 32 | + if (empty($results)) { |
| 33 | + return Response::text("No documentation found matching '{$query}'."); |
| 34 | + } |
| 35 | + |
| 36 | + // Format results |
| 37 | + $output = "Documentation Search Results for: {$query}\n\n"; |
| 38 | + $output .= 'Found '.count($results)." relevant section(s):\n\n"; |
| 39 | + |
| 40 | + foreach ($results as $result) { |
| 41 | + $output .= "📄 {$result['file']}\n"; |
| 42 | + $output .= str_repeat('=', 60)."\n\n"; |
| 43 | + $output .= $result['content']."\n\n"; |
| 44 | + $output .= str_repeat('-', 60)."\n\n"; |
| 45 | + } |
| 46 | + |
| 47 | + return Response::text($output); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Get the tool's input schema. |
| 52 | + * |
| 53 | + * @return array<string, \Illuminate\JsonSchema\JsonSchema> |
| 54 | + */ |
| 55 | + public function schema(JsonSchema $schema): array |
| 56 | + { |
| 57 | + return [ |
| 58 | + 'query' => $schema->string() |
| 59 | + ->description('Search query (e.g., "plugin generation", "MCP tools", "factory pattern", "component types")') |
| 60 | + ->required(), |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get all documentation files. |
| 66 | + */ |
| 67 | + protected function getDocumentationFiles(string $path): array |
| 68 | + { |
| 69 | + $files = []; |
| 70 | + |
| 71 | + // README.md - Main documentation |
| 72 | + if (File::exists($path.'/README.md')) { |
| 73 | + $files[] = [ |
| 74 | + 'path' => $path.'/README.md', |
| 75 | + 'name' => 'README.md', |
| 76 | + 'content' => File::get($path.'/README.md'), |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + // All docs/ files |
| 81 | + $docsPath = $path.'/docs'; |
| 82 | + if (File::isDirectory($docsPath)) { |
| 83 | + foreach (File::allFiles($docsPath) as $file) { |
| 84 | + if ($file->getExtension() === 'md') { |
| 85 | + $relativePath = str_replace($path.'/', '', $file->getPathname()); |
| 86 | + $files[] = [ |
| 87 | + 'path' => $file->getPathname(), |
| 88 | + 'name' => $relativePath, |
| 89 | + 'content' => File::get($file->getPathname()), |
| 90 | + ]; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return $files; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Search documentation files for relevant content. |
| 100 | + */ |
| 101 | + protected function searchDocumentation(array $files, string $query): array |
| 102 | + { |
| 103 | + $results = []; |
| 104 | + $queryLower = strtolower($query); |
| 105 | + $keywords = explode(' ', $queryLower); |
| 106 | + |
| 107 | + foreach ($files as $file) { |
| 108 | + $content = $file['content']; |
| 109 | + $contentLower = strtolower($content); |
| 110 | + |
| 111 | + // Check if any keyword matches |
| 112 | + $matchCount = 0; |
| 113 | + foreach ($keywords as $keyword) { |
| 114 | + if (stripos($contentLower, $keyword) !== false) { |
| 115 | + $matchCount++; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // If matches found, extract relevant sections |
| 120 | + if ($matchCount > 0) { |
| 121 | + $sections = $this->extractRelevantSections($content, $query, $keywords); |
| 122 | + |
| 123 | + foreach ($sections as $section) { |
| 124 | + $results[] = [ |
| 125 | + 'file' => $file['name'], |
| 126 | + 'content' => $section, |
| 127 | + 'relevance' => $matchCount, |
| 128 | + ]; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Sort by relevance (highest first) |
| 134 | + usort($results, fn ($a, $b) => $b['relevance'] <=> $a['relevance']); |
| 135 | + |
| 136 | + // Limit to top 5 most relevant results |
| 137 | + return array_slice($results, 0, 5); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Extract relevant sections from content based on query. |
| 142 | + */ |
| 143 | + protected function extractRelevantSections(string $content, string $query, array $keywords): array |
| 144 | + { |
| 145 | + $sections = []; |
| 146 | + $lines = explode("\n", $content); |
| 147 | + |
| 148 | + // Split by markdown headers to get sections |
| 149 | + $currentSection = ''; |
| 150 | + $currentHeader = ''; |
| 151 | + $inRelevantSection = false; |
| 152 | + |
| 153 | + foreach ($lines as $line) { |
| 154 | + // Check if line is a header |
| 155 | + if (preg_match('/^#+\s+(.+)$/', $line, $matches)) { |
| 156 | + // Save previous section if it was relevant |
| 157 | + if ($inRelevantSection && ! empty(trim($currentSection))) { |
| 158 | + $sections[] = trim($currentHeader."\n\n".$currentSection); |
| 159 | + } |
| 160 | + |
| 161 | + // Start new section |
| 162 | + $currentHeader = $line; |
| 163 | + $currentSection = ''; |
| 164 | + |
| 165 | + // Check if header is relevant |
| 166 | + $headerLower = strtolower($matches[1]); |
| 167 | + $inRelevantSection = false; |
| 168 | + foreach ($keywords as $keyword) { |
| 169 | + if (stripos($headerLower, $keyword) !== false) { |
| 170 | + $inRelevantSection = true; |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + } else { |
| 175 | + // Add to current section |
| 176 | + $currentSection .= $line."\n"; |
| 177 | + |
| 178 | + // Check if content line is relevant |
| 179 | + if (! $inRelevantSection) { |
| 180 | + $lineLower = strtolower($line); |
| 181 | + foreach ($keywords as $keyword) { |
| 182 | + if (stripos($lineLower, $keyword) !== false) { |
| 183 | + $inRelevantSection = true; |
| 184 | + break; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + // Save last section if relevant |
| 192 | + if ($inRelevantSection && ! empty(trim($currentSection))) { |
| 193 | + $sections[] = trim($currentHeader."\n\n".$currentSection); |
| 194 | + } |
| 195 | + |
| 196 | + // If no sections found, return intro (first 50 lines that match) |
| 197 | + if (empty($sections)) { |
| 198 | + $matchingLines = []; |
| 199 | + foreach ($lines as $line) { |
| 200 | + $lineLower = strtolower($line); |
| 201 | + foreach ($keywords as $keyword) { |
| 202 | + if (stripos($lineLower, $keyword) !== false) { |
| 203 | + $matchingLines[] = $line; |
| 204 | + if (count($matchingLines) >= 50) { |
| 205 | + break 2; |
| 206 | + } |
| 207 | + break; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + if (! empty($matchingLines)) { |
| 213 | + $sections[] = implode("\n", $matchingLines); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return $sections; |
| 218 | + } |
| 219 | +} |
0 commit comments