Skip to main content
Glama
officialpraise

MongoDB MCP Server

count

Read-only

Count documents in a MongoDB collection using db.collection.count() with optional query filters to tally specific records.

Instructions

Gets the number of documents in a MongoDB collection using db.collection.count() and query as an optional filter parameter

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
collectionYesCollection name
queryNoA filter/query parameter. Allows users to filter the documents to count. Matches the syntax of the filter argument of db.collection.count().

Implementation Reference

  • The execute method of the CountTool class implements the logic to count documents in a MongoDB collection, optionally checking index usage and returning the count.
    protected async execute({ database, collection, query }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
        const provider = await this.ensureConnected();
    
        // Check if count operation uses an index if enabled
        if (this.config.indexCheck) {
            await checkIndexUsage(provider, database, collection, "count", async () => {
                return provider.runCommandWithCheck(database, {
                    explain: {
                        count: collection,
                        query,
                    },
                    verbosity: "queryPlanner",
                });
            });
        }
    
        const count = await provider.count(database, collection, query);
    
        return {
            content: [
                {
                    text: `Found ${count} documents in the collection "${collection}"`,
                    type: "text",
                },
            ],
        };
    }
  • Zod schema defining the input arguments for the count tool, specifically the optional query filter.
    export const CountArgs = {
        query: z
            .record(z.string(), z.unknown())
            .optional()
            .describe(
                "A filter/query parameter. Allows users to filter the documents to count. Matches the syntax of the filter argument of db.collection.count()."
            ),
    };
  • The CountTool is registered (included) in the MongoDbTools array which lists all MongoDB tools.
    export const MongoDbTools = [
        ConnectTool,
        ListCollectionsTool,
        ListDatabasesTool,
        CollectionIndexesTool,
        CreateIndexTool,
        CollectionSchemaTool,
        FindTool,
        InsertManyTool,
        DeleteManyTool,
        CollectionStorageSizeTool,
        CountTool,
        DbStatsTool,
        AggregateTool,
        UpdateManyTool,
        RenameCollectionTool,
        DropDatabaseTool,
        DropCollectionTool,
        ExplainTool,
        CreateCollectionTool,
        LogsTool,
    ];
  • Import statement for the CountTool in the MongoDB tools registration file.
    import { CountTool } from "./read/count.js";
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already indicate readOnlyHint=true and destructiveHint=false, so the agent knows this is a safe read operation. The description adds value by specifying the MongoDB method (db.collection.count()) and that the query is optional, but does not disclose additional behavioral traits like performance implications, rate limits, or error handling.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core purpose ('Gets the number of documents') and includes essential details (MongoDB context, method, optional parameter) without any wasted words. Every part of the sentence contributes directly to understanding the tool.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's low complexity (a simple count operation), high schema coverage (100%), and annotations covering safety, the description is mostly complete. However, the lack of an output schema means it does not explain return values (e.g., the count as a number), which is a minor gap for clarity.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents the parameters (database, collection, query). The description adds minimal semantics by mentioning the query as an 'optional filter parameter' and referencing the MongoDB method, but does not provide significant additional meaning beyond what the schema already covers.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Gets the number of documents'), resource ('in a MongoDB collection'), and method ('using db.collection.count()'), distinguishing it from siblings like 'find' (which retrieves documents) or 'aggregate' (which performs complex operations). It precisely communicates the tool's purpose without redundancy.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for counting documents with optional filtering, but does not explicitly state when to use this tool versus alternatives like 'aggregate' for counting with grouping or 'find' for retrieving documents. It provides basic context but lacks guidance on exclusions or specific scenarios.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/officialpraise/mcp'

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