list-collections
Retrieve all collections within a specified database for MongoDB, enabling efficient database management and exploration via the MongoDB MCP Server.
Instructions
List all collections for a given database
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"database": {
"description": "Database name",
"type": "string"
}
},
"required": [
"database"
],
"type": "object"
}
Implementation Reference
- The main handler function that executes the tool logic: connects to the MongoDB provider, lists collections for the given database, and returns formatted text results or a message if none found.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: `No collections found for database "${database}". To create a collection, use the "create-collection" tool.`, }, ], }; } return { content: collections.map((collection) => { return { text: `Name: "${collection.name}"`, type: "text", }; }), }; }
- Tool metadata including name, description, input schema (requires 'database' parameter), and operation type.protected name = "list-collections"; protected description = "List all collections for a given database"; protected argsShape = { database: DbOperationArgs.database, }; protected operationType: OperationType = "metadata";
- src/tools/mongodb/tools.ts:22-43 (registration)Registers the ListCollectionsTool (imported earlier) within the array of all MongoDB tools, which is later used in server.ts to add tools to the MCP server.export const MongoDbTools = [ ConnectTool, ListCollectionsTool, ListDatabasesTool, CollectionIndexesTool, CreateIndexTool, CollectionSchemaTool, FindTool, InsertManyTool, DeleteManyTool, CollectionStorageSizeTool, CountTool, DbStatsTool, AggregateTool, UpdateManyTool, RenameCollectionTool, DropDatabaseTool, DropCollectionTool, ExplainTool, CreateCollectionTool, LogsTool, ];