getCollection
Retrieve documents from a MongoDB collection by specifying collection name, query filters, and optional result limits or field projections.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collectionName | Yes | ||
| limit | No | ||
| query | No | ||
| projection | No |
Implementation Reference
- src/index.ts:56-94 (handler)The asynchronous handler function that implements the core logic of the 'getCollection' tool. It connects to MongoDB if necessary, retrieves the specified collection, queries documents with optional parameters, and returns them as JSON strings.async ({ collectionName, limit, query, projection, }: { collectionName: string; limit?: number; query?: any; projection?: any; }) => { try { let db = mongodbConnection.getDb(); if (!db) { await mongodbConnection.connect(this.MONGODB_URI); db = mongodbConnection.getDb(); if (!db) throw new Error("Failed to connect to database"); } const collection = db.collection(collectionName); const documents = await collection .find(query ?? {}) .limit(limit ?? 100) .project(projection ?? {}) .toArray(); return { content: [ { type: "text", text: documents.map((d) => JSON.stringify(d)).join("\n"), }, ], }; } catch (error) { console.error(error); return { content: [{ type: "text", text: "Error: " + error }], }; } }
- src/index.ts:50-55 (schema)Zod schema defining the input parameters for the 'getCollection' tool: collectionName (required string), limit (optional number 1-1000, default 10), query (optional object), projection (optional object).{ collectionName: z.string(), limit: z.number().min(1).max(1000).optional().default(10), query: z.object({}).optional(), projection: z.object({}).optional(), },
- src/index.ts:48-95 (registration)The registration of the 'getCollection' tool on the MCP server using this.mcpServer.tool(), including name, input schema, and handler function.this.mcpServer.tool( "getCollection", { collectionName: z.string(), limit: z.number().min(1).max(1000).optional().default(10), query: z.object({}).optional(), projection: z.object({}).optional(), }, async ({ collectionName, limit, query, projection, }: { collectionName: string; limit?: number; query?: any; projection?: any; }) => { try { let db = mongodbConnection.getDb(); if (!db) { await mongodbConnection.connect(this.MONGODB_URI); db = mongodbConnection.getDb(); if (!db) throw new Error("Failed to connect to database"); } const collection = db.collection(collectionName); const documents = await collection .find(query ?? {}) .limit(limit ?? 100) .project(projection ?? {}) .toArray(); return { content: [ { type: "text", text: documents.map((d) => JSON.stringify(d)).join("\n"), }, ], }; } catch (error) { console.error(error); return { content: [{ type: "text", text: "Error: " + error }], }; } } );
- src/mongodb/index.ts:3-30 (helper)The MongoDBConnection class and singleton instance 'mongodbConnection' used by the 'getCollection' handler for database connections, getDb(), and close operations.export class MongoDBConnection { private client: MongoClient | null = null; private db: Db | null = null; async connect(databaseUrl: string) { try { this.client = new MongoClient(databaseUrl); await this.client.connect(); this.db = this.client.db(); return this.db; } catch (error) { console.error("MongoDB connection error:", error); throw error; } } async close() { await this.client?.close(); } getClient(): MongoClient | null { return this.client; } getDb(): Db | null { return this.db; } }