list_collections
Retrieve all collection names in a MongoDB database to identify available data structures before executing queries.
Instructions
List all collections in a database.
Start here to understand what collections are available before querying.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | No | Database name (optional if default database is configured) |
Implementation Reference
- src/index.ts:1024-1043 (handler)The handler function for the 'list_collections' tool. It extracts the optional database name from arguments, connects to the specified or default database, lists all collections using db.listCollections().toArray(), and returns the result as JSON-formatted text content.case 'list_collections': { const { database } = request.params.arguments as { database?: string }; const dbName = database || this.defaultDatabase; if (!dbName) { throw new McpError( ErrorCode.InvalidRequest, 'Database name is required when no default database is configured' ); } const db = client.db(dbName); const collections = await db.listCollections().toArray(); return { content: [ { type: 'text', text: JSON.stringify(collections, null, 2), }, ], }; }
- src/index.ts:325-333 (schema)Input schema definition for the 'list_collections' tool, specifying an optional 'database' string parameter.inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional if default database is configured)', }, }, },
- src/index.ts:320-334 (registration)Registration of the 'list_collections' tool in the list of tools provided by the ListToolsRequestSchema handler, including name, description, and input schema.{ name: 'list_collections', description: `List all collections in a database. Start here to understand what collections are available before querying.`, inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional if default database is configured)', }, }, }, },