Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

aggregate

Read-only

Process and analyze MongoDB data by running aggregation pipelines on collections, supporting stages for filtering, grouping, vector search, and text search.

Instructions

Run an aggregation against a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
pipelineYesAn array of aggregation stages to execute. If the user has asked for a vector search, `$vectorSearch` **MUST** be the first stage of the pipeline, or the first stage of a `$unionWith` subpipeline. If the user has asked for lexical/Atlas search, use `$search` instead of `$text`. ### Usage Rules for `$vectorSearch` - **Index Type Detection:** Use the collection-indexes tool to determine if the target field has a classic vector index (type: 'vector') or an auto-embed index (type: 'autoEmbed'). - **Classic Vector Search (type: 'vector'):** Use 'queryVector' with embeddings as an array of numbers. - **Auto-Embed Vector Search (type: 'autoEmbed'):** Use 'query' - MongoDB automatically generates embeddings at query time. Do NOT use 'queryVector' or 'embeddingParameters' for auto-embed indexes. - **Unset embeddings:** Unless the user explicitly requests the embeddings, add an `$unset` stage **at the end of the pipeline** to remove the embedding field and avoid context limits. **The $unset stage in this situation is mandatory**. - **Pre-filtering:** If the user requests additional filtering, include filters in `$vectorSearch.filter` only for pre-filter fields in the vector index. NEVER include fields in $vectorSearch.filter that are not part of the vector index. - **Post-filtering:** For all remaining filters, add a $match stage after $vectorSearch. - If unsure which fields are filterable, use the collection-indexes tool to determine valid prefilter fields. - If no requested filters are valid prefilters, omit the filter key from $vectorSearch. ### Usage Rules for `$search` - Include the index name, unless you know for a fact there's a default index. If unsure, use the collection-indexes tool to determine the index name. - The `$search` stage supports multiple operators, such as 'autocomplete', 'text', 'geoWithin', and others. Choose the approprate operator based on the user's query. If unsure of the exact syntax, consult the MongoDB Atlas Search documentation, which can be found here: https://www.mongodb.com/docs/atlas/atlas-search/operators-and-collectors/
collectionYesCollection name
responseBytesLimitNoThe maximum number of bytes to return in the response. This value is capped by the server's configured maxBytesPerQuery and cannot be exceeded. Note to LLM: If the entire aggregation result is required, use the "export" tool instead of increasing this limit.

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
countYesThe total number of documents returned by the aggregation pipeline
documentsYesThe documents returned by the aggregation pipeline
appliedLimitsYesThe limits applied to the aggregation pipeline
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already indicate read-only behavior and non-destructiveness. The description adds critical behavioral details: response size limits, truncation, mandatory $unset for embeddings, and cursor consumption, far exceeding annotations.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is long but well-structured with sections and bullet points. It front-loads the purpose and organizes rules logically. Some verbosity is justified by complexity but could be tightened.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity and presence of an output schema, the description covers all necessary aspects: behavior, parameter details, constraints, and usage rules. No gaps remain.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, baseline 3. The description significantly enriches parameter meaning, especially for pipeline with detailed rules for $vectorSearch and $search stages, including index type detection and usage guidelines.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool runs an aggregation against a MongoDB collection, using a specific verb and resource. It distinguishes from siblings like find and count, and is not a tautology.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides extensive usage context, including when to use vector search vs. Atlas search, pre/post filtering rules, and alternatives like the export tool. However, it does not explicitly state when not to use this tool or contrast it with aggregate-db.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mongodb-js/mongodb-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server