{"record":{"id":"2ba4bf5c41f0a9eb","repo":"mongodb/laravel-mongodb","slug":"please-pass-a-valid-mongodb-command","errorCode":null,"errorMessage":"Please pass a valid MongoDB command","messagePattern":"Please pass a valid MongoDB command","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Tools/DatabaseQuery.php","lineNumber":106,"sourceCode":"            return Response::json($this->handleMql($request->array('command'), $connection));\n        } catch (InvalidArgumentException $exception) {\n            return Response::error($exception->getMessage());\n        } catch (Throwable $exception) {\n            return Response::error('Query failed: ' . $exception->getMessage());\n        }\n    }\n\n    /**\n     * @param array<string, mixed> $command\n     *\n     * @return array<int, mixed>\n     *\n     * @throws InvalidArgumentException\n     */\n    private function handleMql(array $command, Connection $connection): array\n    {\n        if ($command === []) {\n            throw new InvalidArgumentException('Please pass a valid MongoDB command');\n        }\n\n        // Allowed CRUD commands (https://www.mongodb.com/docs/manual/reference/mql/crud-commands/)\n        $allowList = ['aggregate', 'count', 'distinct', 'find'];\n        $operation = array_key_first($command);\n\n        if (! in_array($operation, $allowList, true)) {\n            throw new InvalidArgumentException(sprintf('Only read commands are allowed (%s).', implode(', ', $allowList)));\n        }\n\n        if ($operation === 'aggregate') {\n            // Check nested write ops recursively with conservative allow list\n            $this->ensureNoNestedWriteInAggregation($command['pipeline'] ?? []);\n        }\n\n        return $connection->getDatabase()->command($command)->toArray();\n    }\n","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/mongodb/laravel-mongodb/blob/0634653039468ceb0268a69192bdac64469ed043/src/Tools/DatabaseQuery.php#L88-L124","documentation":"The database-query-mongodb MCP tool received an empty `command` argument object. The tool runs MQL commands against a MongoDB connection, so the first key of the command document is the operation name; with no keys at all there is nothing to run. The library throws InvalidArgumentException before dispatching anything to the server.","triggerScenarios":"Calling the tool with `command` missing, or explicitly passing an empty array/object (e.g. `command: []` or `{}`), so that handleMql receives `$command === []` at src/Tools/DatabaseQuery.php:105.","commonSituations":"MCP client drops or sends an empty `command` field; a code generator builds the arguments dynamically and the command object ends up empty; a template/variable interpolating the command is unset; confusion with SQL-style tools where you pass a query string instead of a command document.","solutions":["Pass a non-empty MQL command document as `command`, with the operation as the first key, e.g. {\"find\": \"users\", \"filter\": {\"active\": true}}.","Verify the MCP client/request actually serializes the `command` argument (check for typos like `commands` or `query`).","Remember this tool takes MQL, never SQL; do not pass a string like `db.users.find()` — build the document form."],"exampleFix":"// before\n{\"connection\": \"mongodb\", \"command\": {}}\n// after\n{\"connection\": \"mongodb\", \"command\": {\"find\": \"users\", \"filter\": {\"active\": true}}}","handlingStrategy":"validation","validationCode":"if (empty($command) || !is_array($command)) {\n    throw new \\InvalidArgumentException('command must be a non-empty MQL command document');\n}","typeGuard":"$isValid = is_array($command) && $command !== [] && is_string(array_key_first($command));","tryCatchPattern":null,"preventionTips":["Always include the operation as the first key: {\"find\": \"<collection>\", ...}.","Never send SQL strings to this tool — it accepts MQL documents only.","Check the tool description/schema in your MCP client before invoking."],"tags":["mcp-tool","mongodb","empty-argument","mql"],"backgroundTag":"empty-required-field","analyzedSha":"0634653039468ceb0268a69192bdac64469ed043","analyzedAt":"2026-09-15T02:56:37.067Z","contentChangedAt":"2026-09-15T02:56:37.067Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}