Skip to main content
Glama

MCP Server Boilerplate

by ricleedo

mongo-aggregate

Execute aggregation pipelines on MongoDB collections to process and analyze data through multiple transformation stages for complex querying and reporting.

Instructions

Execute aggregation pipeline on a MongoDB collection

Input Schema

NameRequiredDescriptionDefault
collectionYesCollection name
databaseYesDatabase name
pipelineYesAggregation pipeline as array of stage objects

Input Schema (JSON Schema)

{ "properties": { "collection": { "description": "Collection name", "type": "string" }, "database": { "description": "Database name", "type": "string" }, "pipeline": { "description": "Aggregation pipeline as array of stage objects", "items": { "additionalProperties": {}, "type": "object" }, "type": "array" } }, "required": [ "database", "collection", "pipeline" ], "type": "object" }

Implementation Reference

  • Executes the MongoDB aggregation pipeline: connects to DB, runs aggregate on collection, formats output using helper functions, returns formatted result or throws error.
    async ({ database: dbName, collection: collectionName, pipeline }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const documents = await collection.aggregate(pipeline).toArray(); const formattedOutput = formatJsonOutput(documents); return { content: [ { type: "text", text: `Aggregation returned ${documents.length} document(s):\n\n${formattedOutput}`, }, ], }; } catch (error) { throw new Error(`Failed to execute aggregation: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
  • Zod schema defining input parameters for the tool: database name, collection name, and aggregation pipeline array.
    { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), pipeline: z.array(z.record(z.any())).describe("Aggregation pipeline as array of stage objects"), },
  • src/index.ts:233-262 (registration)
    Registers the 'mongo-aggregate' tool with MCP server, providing name, description, input schema, and handler function.
    server.tool( "mongo-aggregate", "Execute aggregation pipeline on a MongoDB collection", { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), pipeline: z.array(z.record(z.any())).describe("Aggregation pipeline as array of stage objects"), }, async ({ database: dbName, collection: collectionName, pipeline }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const documents = await collection.aggregate(pipeline).toArray(); const formattedOutput = formatJsonOutput(documents); return { content: [ { type: "text", text: `Aggregation returned ${documents.length} document(s):\n\n${formattedOutput}`, }, ], }; } catch (error) { throw new Error(`Failed to execute aggregation: ${error instanceof Error ? error.message : 'Unknown error'}`); } } );
  • Helper function to ensure MongoDB client connection and retrieve or create database instance, used by all tools including mongo-aggregate.
    async function ensureConnection(dbName: string): Promise<Db> { if (!mongoClient) { const uri = getMongoUri(); mongoClient = new MongoClient(uri); await mongoClient.connect(); } if (!databases.has(dbName)) { databases.set(dbName, mongoClient.db(dbName)); } return databases.get(dbName)!; }
  • Helper function to format and truncate large JSON outputs for tool responses, used in mongo-aggregate to handle aggregation results.
    function formatJsonOutput(data: unknown): string { const truncatedData = truncateForOutput(data); let outputText = JSON.stringify(truncatedData, null, 2); outputText = outputText.replace( /"\.\.\.(\d+) more items"/g, "...$1 more items" ); outputText = outputText.replace( /"\.\.\.(\d+) more properties": "\.\.\.?"/g, "...$1 more properties" ); return outputText; }

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/ricleedo/mongo-boilerplate-mcp'

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