Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

collection-storage-size

Retrieve storage size for a MongoDB collection to monitor database capacity and optimize resource allocation.

Instructions

Gets the size of the collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
collectionYesCollection name

Implementation Reference

  • Executes an aggregation pipeline using $collStats to compute the total storage size of the specified collection, scales the value to human-readable units, and returns a formatted text response.
    protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
        const provider = await this.ensureConnected();
        const [{ value }] = (await provider
            .aggregate(database, collection, [
                { $collStats: { storageStats: {} } },
                { $group: { _id: null, value: { $sum: "$storageStats.size" } } },
            ])
            .toArray()) as [{ value: number }];
    
        const { units, value: scaledValue } = CollectionStorageSizeTool.getStats(value);
    
        return {
            content: [
                {
                    text: `The size of "${database}.${collection}" is \`${scaledValue.toFixed(2)} ${units}\``,
                    type: "text",
                },
            ],
        };
    }
  • Zod schema defining the required input arguments for the tool: database and collection names.
    export const DbOperationArgs = {
        database: z.string().describe("Database name"),
        collection: z.string().describe("Collection name"),
    };
  • The tool class definition with the 'name' property set to 'collection-storage-size', which identifies and registers the tool.
    export class CollectionStorageSizeTool extends MongoDBToolBase {
        public name = "collection-storage-size";
  • Custom error handling for cases where the collection does not exist (NamespaceNotFound), providing a user-friendly message.
    protected handleError(
        error: unknown,
        args: ToolArgs<typeof this.argsShape>
    ): Promise<CallToolResult> | CallToolResult {
        if (error instanceof Error && "codeName" in error && error.codeName === "NamespaceNotFound") {
            return {
                content: [
                    {
                        text: `The size of "${args.database}.${args.collection}" cannot be determined because the collection does not exist.`,
                        type: "text",
                    },
                ],
                isError: true,
            };
        }
    
        return super.handleError(error, args);
    }
  • Utility function to convert raw storage size in bytes to a scaled human-readable value with appropriate units (bytes, KB, MB, GB).
    private static getStats(value: number): { value: number; units: string } {
        const kb = 1024;
        const mb = kb * 1024;
        const gb = mb * 1024;
    
        if (value > gb) {
            return { value: value / gb, units: "GB" };
        }
    
        if (value > mb) {
            return { value: value / mb, units: "MB" };
        }
        if (value > kb) {
            return { value: value / kb, units: "KB" };
        }
        return { value, units: "bytes" };
    }

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