Skip to main content
Glama
jonfreeland

MongoDB MCP Server

by jonfreeland

aggregate

Analyze MongoDB data by applying read-only aggregation pipelines to filter, group, sort, join, and transform collection documents for reporting and insights.

Instructions

Execute a read-only aggregation pipeline on a collection.

Supported Stages:

  • $match: Filter documents

  • $group: Group documents by a key

  • $sort: Sort documents

  • $project: Shape the output

  • $addFields - Include additional or calculated fields

  • $lookup: Perform left outer joins

  • $unwind: Deconstruct array fields

Unsafe/Blocked Stages:

  • $out: Write results to collection

  • $merge: Merge results into collection

  • $set: Set field values

  • $unset: Remove fields

  • $replaceRoot: Replace document structure

  • $replaceWith: Replace document

Example - User Statistics by Role: use_mcp_tool with server_name: "mongodb", tool_name: "aggregate", arguments: { "collection": "users", "pipeline": [ { "$match": { "active": true } }, { "$group": { "_id": "$role", "count": { "$sum": 1 }, "avgAge": { "$avg": "$age" } }}, { "$sort": { "count": -1 } } ], "limit": 100 }

Example - Posts with Author Details: use_mcp_tool with server_name: "mongodb", tool_name: "aggregate", arguments: { "collection": "posts", "pipeline": [ { "$match": { "published": true } }, { "$lookup": { "from": "users", "localField": "authorId", "foreignField": "_id", "as": "author" }}, { "$unwind": "$author" }, { "$project": { "title": 1, "authorName": "$author.name", "publishDate": 1 }} ] }

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseNoDatabase name (optional if default database is configured)
collectionYesCollection name
pipelineYesMongoDB aggregation pipeline stages (read-only operations only)
limitNoMaximum number of documents to return (optional)

Implementation Reference

  • src/index.ts:460-549 (registration)
    Registration of the 'aggregate' tool in the ListTools response, including name, description, and input schema definition.
            {
              name: 'aggregate',
              description: `Execute a read-only aggregation pipeline on a collection.
    
    Supported Stages:
    - $match: Filter documents
    - $group: Group documents by a key
    - $sort: Sort documents
    - $project: Shape the output
    - $addFields - Include additional or calculated fields
    - $lookup: Perform left outer joins
    - $unwind: Deconstruct array fields
    
    
    Unsafe/Blocked Stages:
    - $out: Write results to collection
    - $merge: Merge results into collection
    - $set: Set field values
    - $unset: Remove fields
    - $replaceRoot: Replace document structure
    - $replaceWith: Replace document
    
    Example - User Statistics by Role:
    use_mcp_tool with
      server_name: "mongodb",
      tool_name: "aggregate",
      arguments: {
        "collection": "users",
        "pipeline": [
          { "$match": { "active": true } },
          { "$group": {
              "_id": "$role",
              "count": { "$sum": 1 },
              "avgAge": { "$avg": "$age" }
          }},
          { "$sort": { "count": -1 } }
        ],
        "limit": 100
      }
    
    Example - Posts with Author Details:
    use_mcp_tool with
      server_name: "mongodb",
      tool_name: "aggregate",
      arguments: {
        "collection": "posts",
        "pipeline": [
          { "$match": { "published": true } },
          { "$lookup": {
              "from": "users",
              "localField": "authorId",
              "foreignField": "_id",
              "as": "author"
          }},
          { "$unwind": "$author" },
          { "$project": {
              "title": 1,
              "authorName": "$author.name",
              "publishDate": 1
          }}
        ]
      }`,
              inputSchema: {
                type: 'object',
                properties: {
                  database: {
                    type: 'string',
                    description: 'Database name (optional if default database is configured)',
                  },
                  collection: {
                    type: 'string',
                    description: 'Collection name',
                  },
                  pipeline: {
                    type: 'array',
                    description: 'MongoDB aggregation pipeline stages (read-only operations only)',
                    items: {
                      type: 'object',
                    },
                  },
                  limit: {
                    type: 'number',
                    description: 'Maximum number of documents to return (optional)',
                    minimum: 1,
                    maximum: 1000,
                  },
                },
                required: ['collection', 'pipeline'],
              },
            },
  • Input schema definition for the 'aggregate' tool specifying parameters like database, collection, pipeline, and limit.
    inputSchema: {
      type: 'object',
      properties: {
        database: {
          type: 'string',
          description: 'Database name (optional if default database is configured)',
        },
        collection: {
          type: 'string',
          description: 'Collection name',
        },
        pipeline: {
          type: 'array',
          description: 'MongoDB aggregation pipeline stages (read-only operations only)',
          items: {
            type: 'object',
          },
        },
        limit: {
          type: 'number',
          description: 'Maximum number of documents to return (optional)',
          minimum: 1,
          maximum: 1000,
        },
      },
      required: ['collection', 'pipeline'],
    },
  • Handler for the 'aggregate' tool: validates pipeline safety, executes MongoDB aggregation on the specified collection, applies optional limit, fetches results, generates visualization hints, and returns formatted JSON output.
    case 'aggregate': {
      const { database, collection, pipeline, limit } = request.params.arguments as {
        database?: string;
        collection: string;
        pipeline: any[];
        limit?: number;
      };
      const dbName = database || this.defaultDatabase;
      if (!dbName) {
        throw new McpError(
          ErrorCode.InvalidRequest,
          'Database name is required when no default database is configured'
        );
      }
    
      this.validateAggregationPipeline(pipeline);
    
      const db = client.db(dbName);
      let aggregation = db.collection(collection).aggregate(pipeline);
    
      if (limit) {
        aggregation = aggregation.limit(limit);
      }
    
      const results = await aggregation.toArray();
      const vizHint = this.generateVisualizationHint(results);
    
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(results, null, 2) + (vizHint ? `\n\nVisualization Hint:\n${vizHint}` : ''),
          },
        ],
      };
    }
  • Helper function to validate the aggregation pipeline, ensuring it only contains read-only stages and blocks potentially data-modifying operations.
    private validateAggregationPipeline(pipeline: any[]): void {
      if (!Array.isArray(pipeline)) {
        throw new McpError(
          ErrorCode.InvalidRequest,
          'Aggregation pipeline must be an array'
        );
      }
    
      if (pipeline.length === 0) {
        throw new McpError(
          ErrorCode.InvalidRequest,
          'Aggregation pipeline cannot be empty'
        );
      }
    
      const unsafeStages = ['$out', '$merge', '$set', '$unset', '$replaceRoot', '$replaceWith'];
      const unsafeStageFound = pipeline.find(stage => 
        Object.keys(stage).some(key => unsafeStages.includes(key))
      );
    
      if (unsafeStageFound) {
        throw new McpError(
          ErrorCode.InvalidRequest,
          'Pipeline contains unsafe stages that could modify data. Only read-only operations are allowed.'
        );
      }
    }
Behavior4/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively communicates that the tool is read-only (critical safety context), lists allowed and blocked operations, and provides detailed examples showing input structure and expected behavior. It doesn't mention rate limits, authentication needs, or pagination, but covers the core operational constraints well.

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

Conciseness3/5

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

The description is front-loaded with the core purpose, but includes extensive stage listings and two lengthy examples. While the examples are helpful, they make the description quite long. Some information (like listing all supported/blocked stages) could be more concise. Every sentence adds value, but the overall structure could be tighter.

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

Completeness4/5

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

For a complex tool with 4 parameters, no annotations, and no output schema, the description does well. It explains the tool's purpose, behavioral constraints, parameter usage through examples, and distinguishes it from write operations. The main gap is lack of output format explanation (what the aggregation returns), but given the examples show expected transformations, it's mostly complete.

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

Parameters4/5

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

Schema description coverage is 100%, so the baseline is 3. The description adds significant value by explaining the 'pipeline' parameter's semantics through supported/blocked stages and two comprehensive examples that illustrate how to construct pipelines. This goes beyond the schema's generic description of 'MongoDB aggregation pipeline stages (read-only operations only).'

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's purpose: 'Execute a read-only aggregation pipeline on a collection.' It specifies the verb ('execute'), resource ('aggregation pipeline'), and scope ('read-only'), distinguishing it from write operations. This differentiates it from siblings like 'query' or 'count_documents' by focusing on multi-stage data processing.

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 clear context for when to use this tool by listing supported stages (e.g., $match, $group) and explicitly blocking unsafe stages (e.g., $out, $merge). It implies usage for complex data transformations and joins. However, it doesn't explicitly compare when to use 'aggregate' versus alternatives like 'query' or 'get_distinct_values' for simpler tasks.

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/jonfreeland/mongodb-mcp'

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