Skip to main content
Glama

MCP Server Boilerplate

by ricleedo

mongo-find-documents

Query documents from a MongoDB collection using database name, collection name, and optional filters to retrieve specific data records.

Instructions

Query documents from a MongoDB collection

Input Schema

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

Input Schema (JSON Schema)

{ "properties": { "collection": { "description": "Collection name", "type": "string" }, "database": { "description": "Database name", "type": "string" }, "filter": { "additionalProperties": {}, "description": "Query filter as JSON object (optional)", "type": "object" }, "limit": { "description": "Maximum number of documents to return (optional)", "type": "number" } }, "required": [ "database", "collection" ], "type": "object" }

Implementation Reference

  • The core handler function for the 'mongo-find-documents' tool. It connects to the specified MongoDB database, queries the collection using the provided filter (default empty), applies an optional limit, retrieves the documents, formats the output using a helper, and returns a 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'}`); } }
  • Zod input schema defining the parameters for the tool: required database and collection names, optional filter object and limit 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)
    The server.tool() call that registers the 'mongo-find-documents' tool with its name, description, input schema, and inline 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 used by the tool handler to format and truncate large JSON outputs for safe display in 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 function to ensure a MongoDB client connection exists and return the database instance, cached per database name. Used in the tool handler.
    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/ricleedo/mongo-boilerplate-mcp'

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