getCollections
Retrieve all collection names from a MongoDB database to understand available data structures and plan database operations.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:26-46 (handler)Inline handler function for the 'getCollections' tool. It ensures a connection to MongoDB, lists all collections in the database, and returns their names as a comma-separated string in the MCP response format. Handles connection errors gracefully.this.mcpServer.tool("getCollections", async () => { 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 collections = await db.listCollections().toArray(); return { content: [ { type: "text", text: collections.map((c) => c.name).join(", ") }, ], }; } catch (error) { console.error(error); return { content: [{ type: "text", text: "Error: " + error }], }; } });