Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

list-databases

Retrieve a list of all databases available in a MongoDB connection to identify and access database resources.

Instructions

List all databases for a MongoDB connection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The `execute` method implements the core logic of the 'list-databases' tool, connecting to MongoDB, fetching databases with sizes, and returning formatted text and structured content.
    protected async execute(): Promise<CallToolResult> {
        const provider = await this.ensureConnected();
        const dbs = (await provider.listDatabases("")).databases as { name: string; sizeOnDisk: bson.Long }[];
        const databases = dbs.map((db) => ({
            name: db.name,
            size: Number(db.sizeOnDisk),
        }));
    
        return {
            content: formatUntrustedData(
                `Found ${dbs.length} databases`,
                ...dbs.map((db) => `Name: ${db.name}, Size: ${db.sizeOnDisk.toString()} bytes`)
            ),
            structuredContent: {
                databases,
                totalCount: databases.length,
            },
        };
    }
  • Zod schema defining the structured output for the list-databases tool, including an array of database names and sizes, and total count.
    export const ListDatabasesOutputSchema = {
        databases: z.array(
            z.object({
                name: z.string(),
                size: z.number(),
            })
        ),
        totalCount: z.number(),
    };
  • Exports the ListDatabasesTool class, making it available for inclusion in the AllTools array used by the server for tool registration.
    export { ListDatabasesTool } from "./metadata/listDatabases.js";
  • The full ListDatabasesTool class definition, including name, description, schemas, and execute handler for the 'list-databases' tool.
    export class ListDatabasesTool extends MongoDBToolBase {
        public name = "list-databases";
        protected description = "List all databases for a MongoDB connection";
        protected argsShape = {};
        protected override outputSchema = ListDatabasesOutputSchema;
        static operationType: OperationType = "metadata";
    
        protected async execute(): Promise<CallToolResult> {
            const provider = await this.ensureConnected();
            const dbs = (await provider.listDatabases("")).databases as { name: string; sizeOnDisk: bson.Long }[];
            const databases = dbs.map((db) => ({
                name: db.name,
                size: Number(db.sizeOnDisk),
            }));
    
            return {
                content: formatUntrustedData(
                    `Found ${dbs.length} databases`,
                    ...dbs.map((db) => `Name: ${db.name}, Size: ${db.sizeOnDisk.toString()} bytes`)
                ),
                structuredContent: {
                    databases,
                    totalCount: databases.length,
                },
            };
        }
    }
  • AllTools array construction that includes MongoDbTools (which exports ListDatabasesTool), used by server.ts for registering all tools including 'list-databases'.
    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