Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

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
NameRequiredDescriptionDefault
databaseYesDatabase 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,
    };
  • Sets the tool's name to 'list-collections', identifying it in the MCP tool registry.
    public name = "list-collections";
  • Re-exports the ListCollectionsTool class from its implementation file for inclusion in the MongoDB tools module.
    export { ListCollectionsTool } from "./metadata/listCollections.js";
  • 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,
    });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mongodb-js/mongodb-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server