Skip to content

[Bug]: Issue with the count #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 11 tasks
shashank-yelluri opened this issue May 5, 2025 · 6 comments
Open
1 of 11 tasks

[Bug]: Issue with the count #202

shashank-yelluri opened this issue May 5, 2025 · 6 comments

Comments

@shashank-yelluri
Copy link

App

  • Cursor
  • Windsurf
  • VSCode
  • VSCode Insiders
  • Claude Desktop
  • Other

Affected Models (if applicable)

  • Claude 3.5 Sonnet
  • Claude 3.7 Sonnet
  • GPT-4a
  • o4-mini
  • Other

Bug Description

Hello Team,

We've tried to use all the tools built. The count tool is not working as the find. With the tool of find, we are able to extract the information, but the count is always returning the complete number of records from the collection. Please verify this & share me if anyone else is facing the same issue.

Note: Tried with all different kinds of prompting & the proof is that find is working well.

Thank You.

@johnnorlin
Copy link

johnnorlin commented May 14, 2025

I was also seeing this behavior but was able to fix it sometimes by just manually telling my model to call it differently.

Essentially .find is expecting a filter object with a property name of 'filter', and .count is expecting it with a property name of 'query'. My model was defaulting to using the 'filter' property name when using .count, and so the server logic didn't grab the filter object properly and just returned the result of .count with no filter object.

I tried renaming the expected property name but my fix didn't work.

Additional Info:

I'm using a custom mcp client (backed by Azure openAI-provided gpt-4.1 model)

@Phenome
Copy link

Phenome commented May 14, 2025

I'm having the same issue. I'll see if I can override the tool spec

@johnnorlin
Copy link

johnnorlin commented May 14, 2025

I did try that but had no luck @Phenome - I'll be looking out for an update though!

It also looks like collections.count is deprecated in favor of .countDocuments/.estimatedDocumentCount (although that API also uses 'query' so this issue would remain) - wondering if anyone has insight?

@Phenome
Copy link

Phenome commented May 15, 2025

Hmmm weird. The output of the tools generated by the mcp, for count, looks good:

{
  description: "Gets the number of documents in a MongoDB collection",
  parameters: {
    _type: undefined,
    jsonSchema: {
      type: "object",
      properties: {
        database: {
          type: "string",
          description: "Database name",
        },
        collection: {
          type: "string",
          description: "Collection name",
        },
        query: {
          type: "object",
          additionalProperties: {},
          description: "The query filter to count documents. Matches the syntax of the filter argument of db.collection.count()",
        },
      },
      required: [ "database", "collection" ],
      additionalProperties: false,
      $schema: "http://json-schema.org/draft-07/schema#",
    },
    validate: undefined,
  },
}

However, my modal is calling "count" with "filter" instead of "query".

@johnnorlin
Copy link

johnnorlin commented May 16, 2025

This might be the wrong track, but I tried this and no luck - trying to just coerce the filter to be pulled in to the execute args regardless of name:

import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
import { ToolArgs, OperationType } from "../../tool.js";
import { z } from "zod";

const FilterSchema = z
    .record(z.string(), z.unknown())
    .describe("A MongoDB filter query object");

export const CountArgs = {
    query: FilterSchema.optional(),
    filter: FilterSchema.optional(),
};


export class CountTool extends MongoDBToolBase {
    protected name = "count";
    protected description = "Gets the number of documents in a MongoDB collection";
    protected argsShape = {
        ...DbOperationArgs,
        ...CountArgs,
    };

    protected operationType: OperationType = "read";

    protected async execute(args: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
        const { database, collection } = args;
        const query = args.query ?? args.filter ?? {};

        const provider = await this.ensureConnected();
        const count = await provider.count(database, collection, query);

        return {
            content: [
                {
                    text: `Found ${count} documents in the collection "${collection}"`,
                    type: "text",
                },
            ],
        };
    }

}

So far what I have below is the only method that worked - manually setting the property name in the model system instructions:

"For ALL COUNT CALLS, ALWAYS use the property name "query" (not "filter") to specify search criteria, regardless of standard MongoDB conventions."

@gagik
Copy link
Collaborator

gagik commented May 17, 2025

Thanks for raising this! We're looking into a potential solution with #254.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants