list-collections
Retrieve all collections from a specified MongoDB database to view available data structures and manage database organization.
Instructions
List all collections for a given database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name |
Implementation Reference
- The handler function that implements the core logic of the 'list-collections' tool: connects to the MongoDB provider, lists collections for the given database, and formats the response appropriately, handling empty cases.protected async execute({ database }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); const collections = await provider.listCollections(database); if (collections.length === 0) { return { content: [ { type: "text", text: `Found 0 collections for database "${database}". To create a collection, use the "create-collection" tool.`, }, ], }; } return { content: formatUntrustedData( `Found ${collections.length} collections for database "${database}".`, collections.map((collection) => `"${collection.name}"`).join("\n") ), }; }
- Defines the input schema for the tool, requiring a 'database' parameter from DbOperationArgs.protected argsShape = { database: DbOperationArgs.database, };
- src/tools/mongodb/metadata/listCollections.ts:7-7 (registration)Sets the tool's name to 'list-collections', identifying it in the MCP tool registry.public name = "list-collections";
- src/tools/mongodb/tools.ts:2-2 (registration)Re-exports the ListCollectionsTool class from its implementation file for inclusion in the MongoDB tools module.export { ListCollectionsTool } from "./metadata/listCollections.js";
- src/tools/index.ts:3-11 (registration)Imports the MongoDB tools module (including ListCollectionsTool) and includes it in the AllTools array, which is used by the server to register all available tools.import * as MongoDbTools from "./mongodb/tools.js"; import type { ToolClass } from "./tool.js"; // Export the collection of tools for easier reference export const AllTools: ToolClass[] = Object.values({ ...MongoDbTools, ...AtlasTools, ...AtlasLocalTools, });