|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MongoDB\Laravel\Tools; |
| 6 | + |
| 7 | +use Illuminate\Contracts\JsonSchema\JsonSchema; |
| 8 | +use Illuminate\JsonSchema\Types\Type; |
| 9 | +use Illuminate\Support\Facades\Cache; |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | +use Illuminate\Support\Facades\Log; |
| 12 | +use Laravel\Mcp\Request; |
| 13 | +use Laravel\Mcp\Response; |
| 14 | +use Laravel\Mcp\Server\Tool; |
| 15 | +use Laravel\Mcp\Server\Tools\Annotations\IsReadOnly; |
| 16 | +use MongoDB\Database; |
| 17 | +use MongoDB\Laravel\Connection; |
| 18 | +use MongoDB\Model\CollectionInfo; |
| 19 | +use Throwable; |
| 20 | + |
| 21 | +use function assert; |
| 22 | +use function rescue; |
| 23 | +use function sprintf; |
| 24 | +use function str_contains; |
| 25 | +use function strtolower; |
| 26 | + |
| 27 | +/** |
| 28 | + * MongoDB equivalent of Boost's DatabaseSchema tool. |
| 29 | + */ |
| 30 | +#[IsReadOnly] |
| 31 | +class DatabaseInfo extends Tool |
| 32 | +{ |
| 33 | + /** |
| 34 | + * The tool's description. |
| 35 | + */ |
| 36 | + protected string $description = 'Read information about a MongoDB database. MongoDB does not use tables and columns, so this returns a summary of the collections. Use "summary" mode first (the default) to get an overview (collection names, types, and estimated document counts), then call again with "summary" set to false and a "filter" to get full details for specific collections (indexes and options). Only use this tool with a connection you already know uses the "mongodb" driver. Params: "connection" (required) - the name of a MongoDB database connection to inspect; "summary" (default true) - collection names, types, and estimated document counts only; "filter" - substring match on collection names.'; |
| 37 | + |
| 38 | + /** |
| 39 | + * Get the tool's input schema. |
| 40 | + * |
| 41 | + * @return array<string, Type> |
| 42 | + */ |
| 43 | + public function schema(JsonSchema $schema): array |
| 44 | + { |
| 45 | + return [ |
| 46 | + 'summary' => $schema->boolean() |
| 47 | + ->description('Return only collection names, types, and estimated document counts. Use this first to understand the database, then request full details for specific collections using "filter". Defaults to false.'), |
| 48 | + 'connection' => $schema->string() |
| 49 | + ->description('Name of the MongoDB database connection to inspect. Must be a connection that uses the "mongodb" driver.') |
| 50 | + ->required(), |
| 51 | + 'filter' => $schema->string() |
| 52 | + ->description('Filter collections by name (substring match).'), |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Handle the tool request. |
| 58 | + */ |
| 59 | + public function handle(Request $request): Response |
| 60 | + { |
| 61 | + $summary = (bool) $request->get('summary', false); |
| 62 | + $connectionName = (string) $request->get('connection'); |
| 63 | + $filter = (string) ($request->get('filter') ?? ''); |
| 64 | + |
| 65 | + $connection = DB::connection($connectionName); |
| 66 | + |
| 67 | + if (! $connection instanceof Connection) { |
| 68 | + return Response::error(sprintf('The [%s] connection is not a MongoDB connection.', $connectionName)); |
| 69 | + } |
| 70 | + |
| 71 | + $cacheKey = sprintf( |
| 72 | + 'mongodb:mcp:database-info:%s:%s:%d', |
| 73 | + $connectionName, |
| 74 | + $filter, |
| 75 | + (int) $summary, |
| 76 | + ); |
| 77 | + |
| 78 | + try { |
| 79 | + $info = rescue( |
| 80 | + fn (): array => Cache::remember($cacheKey, 20, fn (): array => $this->getDatabaseInfo($connection, $connectionName, $filter, $summary)), |
| 81 | + fn (): array => $this->getDatabaseInfo($connection, $connectionName, $filter, $summary), |
| 82 | + report: false, |
| 83 | + ); |
| 84 | + } catch (Throwable $exception) { |
| 85 | + Log::error('Failed to read information for MongoDB connection: ' . $connectionName, [ |
| 86 | + 'error' => $exception->getMessage(), |
| 87 | + ]); |
| 88 | + |
| 89 | + return Response::error(sprintf('Failed to read information for the [%s] connection.', $connectionName)); |
| 90 | + } |
| 91 | + |
| 92 | + return Response::json($info); |
| 93 | + } |
| 94 | + |
| 95 | + /** @return array{database: string, connection: string, collections: array<string, mixed>} */ |
| 96 | + protected function getDatabaseInfo(Connection $connection, string $connectionName, string $filter, bool $summary): array |
| 97 | + { |
| 98 | + $database = $connection->getDatabase(); |
| 99 | + |
| 100 | + $collections = []; |
| 101 | + |
| 102 | + foreach ($database->listCollections() as $collectionInfo) { |
| 103 | + assert($collectionInfo instanceof CollectionInfo); |
| 104 | + $name = $collectionInfo->getName(); |
| 105 | + |
| 106 | + if ($filter !== '' && ! str_contains(strtolower($name), strtolower($filter))) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + $collections[$name] = $summary |
| 111 | + ? $this->getCollectionSummary($database, $collectionInfo) |
| 112 | + : $this->getCollectionDetails($database, $collectionInfo); |
| 113 | + } |
| 114 | + |
| 115 | + return [ |
| 116 | + 'database' => $database->getDatabaseName(), |
| 117 | + 'connection' => $connectionName, |
| 118 | + 'collections' => $collections, |
| 119 | + ]; |
| 120 | + } |
| 121 | + |
| 122 | + /** @return array{type: string, estimated_document_count: int} */ |
| 123 | + protected function getCollectionSummary(Database $database, CollectionInfo $collectionInfo): array |
| 124 | + { |
| 125 | + return [ |
| 126 | + 'type' => $collectionInfo->getType(), |
| 127 | + 'estimated_document_count' => $this->estimatedDocumentCount($database, $collectionInfo->getName()), |
| 128 | + ]; |
| 129 | + } |
| 130 | + |
| 131 | + /** @return array<string, mixed> */ |
| 132 | + protected function getCollectionDetails(Database $database, CollectionInfo $collectionInfo): array |
| 133 | + { |
| 134 | + $name = $collectionInfo->getName(); |
| 135 | + |
| 136 | + try { |
| 137 | + return [ |
| 138 | + 'type' => $collectionInfo->getType(), |
| 139 | + 'estimated_document_count' => $this->estimatedDocumentCount($database, $name), |
| 140 | + 'options' => $collectionInfo->getOptions(), |
| 141 | + 'indexes' => $this->getIndexes($database, $name), |
| 142 | + ]; |
| 143 | + } catch (Throwable $exception) { |
| 144 | + Log::error('Failed to get details for MongoDB collection: ' . $name, [ |
| 145 | + 'error' => $exception->getMessage(), |
| 146 | + ]); |
| 147 | + |
| 148 | + return ['error' => sprintf('Failed to get details for collection [%s].', $name)]; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + protected function estimatedDocumentCount(Database $database, string $collection): int |
| 153 | + { |
| 154 | + try { |
| 155 | + return $database->getCollection($collection)->estimatedDocumentCount(); |
| 156 | + } catch (Throwable) { |
| 157 | + return 0; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** @return array<string, array{keys: array<string, mixed>, unique: bool}> */ |
| 162 | + protected function getIndexes(Database $database, string $collection): array |
| 163 | + { |
| 164 | + $indexes = []; |
| 165 | + |
| 166 | + foreach ($database->getCollection($collection)->listIndexes() as $index) { |
| 167 | + $indexes[$index->getName()] = [ |
| 168 | + 'keys' => $index->getKey(), |
| 169 | + 'unique' => $index->isUnique(), |
| 170 | + ]; |
| 171 | + } |
| 172 | + |
| 173 | + return $indexes; |
| 174 | + } |
| 175 | +} |
0 commit comments