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
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name | |
| collection | Yes | Collection name | |
| filter | No | Query filter as JSON object (optional) | |
| limit | No | Maximum number of documents to return (optional) |
Implementation Reference
- src/index.ts:140-165 (handler)The handler function that executes the mongo-find-documents tool: connects to MongoDB database, finds documents using the provided filter and optional limit, formats the output as truncated JSON, and returns it in the MCP response format.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'}`); } }
- src/index.ts:134-139 (schema)Zod schema for the tool's input parameters: 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)Registration of the "mongo-find-documents" tool with the MCP server, specifying 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'}`); } } );
- src/index.ts:88-100 (helper)Helper function to ensure a MongoDB connection and database instance is available, used by 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)!; }
- src/index.ts:64-78 (helper)Helper function to format and clean up truncated JSON output for the tool response, called by the handler.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; }