Skip to main content
Glama
CaptainCrouton89

MCP Server Boilerplate

mongo-find-documents

Query documents from MongoDB collections using filters and limits to retrieve specific data from your database.

Instructions

Query documents from a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name
databaseYesDatabase name
filterNoQuery filter as JSON object (optional)
limitNoMaximum number of documents to return (optional)

Implementation Reference

  • Implements the core logic: ensures DB connection, executes find query with optional filter/limit, formats output with truncation, returns formatted text response.
    async ({ database: dbName, collection: collectionName, filter = {}, limit }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); let cursor = collection.find(filter); if (limit) { cursor = cursor.limit(limit); } const documents = await cursor.toArray(); const formattedOutput = formatJsonOutput(documents); return { content: [ { type: "text", text: `Found ${documents.length} document(s):\n\n${formattedOutput}`, }, ], }; } catch (error) { throw new Error(`Failed to find documents: ${error instanceof Error ? error.message : 'Unknown error'}`); } } );
  • Defines input parameters using Zod: database (string), collection (string), filter (optional record), limit (optional number).
    { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), filter: z.record(z.any()).optional().describe("Query filter as JSON object (optional)"), limit: z.number().optional().describe("Maximum number of documents to return (optional)"), },
  • src/index.ts:131-166 (registration)
    Full registration of the mongo-find-documents tool via server.tool(), including name, description, schema, and handler function.
    server.tool( "mongo-find-documents", "Query documents from a MongoDB collection", { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), filter: z.record(z.any()).optional().describe("Query filter as JSON object (optional)"), limit: z.number().optional().describe("Maximum number of documents to return (optional)"), }, async ({ database: dbName, collection: collectionName, filter = {}, limit }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); let cursor = collection.find(filter); if (limit) { cursor = cursor.limit(limit); } const documents = await cursor.toArray(); const formattedOutput = formatJsonOutput(documents); return { content: [ { type: "text", text: `Found ${documents.length} document(s):\n\n${formattedOutput}`, }, ], }; } catch (error) { throw new Error(`Failed to find documents: ${error instanceof Error ? error.message : 'Unknown error'}`); } } );
  • Helper function to format and truncate large JSON outputs for the tool responses.
    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; }
  • Helper to lazily connect to MongoDB and cache database instances per name.
    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)!; }

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

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