Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

find

Read-only

Query MongoDB collections to retrieve documents matching specific criteria, with options to filter, sort, limit results, and control response size.

Instructions

Run a find query against a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
collectionYesCollection name
filterNoThe query filter, matching the syntax of the query argument of db.collection.find()
projectionNoThe projection, matching the syntax of the projection argument of db.collection.find()
limitNoThe maximum number of documents to return
sortNoA document, describing the sort order, matching the syntax of the sort argument of cursor.sort(). The keys of the object are the fields to sort on, while the values are the sort directions (1 for ascending, -1 for descending).
responseBytesLimitNoThe maximum number of bytes to return in the response. This value is capped by the server's configured maxBytesPerQuery and cannot be exceeded. Note to LLM: If the entire query result is required, use the "export" tool instead of increasing this limit.

Implementation Reference

  • The FindTool class is the core implementation of the 'find' tool. It extends MongoDBToolBase, sets name to 'find', defines argsShape using FindArgs, and implements the execute method to run find queries on MongoDB collections with limits, index checks, and result formatting.
    export class FindTool extends MongoDBToolBase {
        public name = "find";
        protected description = "Run a find query against a MongoDB collection";
        protected argsShape = {
            ...DbOperationArgs,
            ...FindArgs,
        };
        static operationType: OperationType = "read";
    
        protected async execute(
            { database, collection, filter, projection, limit, sort, responseBytesLimit }: ToolArgs<typeof this.argsShape>,
            { signal }: ToolExecutionContext
        ): Promise<CallToolResult> {
            let findCursor: FindCursor<unknown> | undefined = undefined;
            try {
                const provider = await this.ensureConnected();
    
                // Check if find operation uses an index if enabled
                if (this.config.indexCheck) {
                    await checkIndexUsage(provider, database, collection, "find", async () => {
                        return provider
                            .find(database, collection, filter, { projection, limit, sort })
                            .explain("queryPlanner");
                    });
                }
    
                const limitOnFindCursor = this.getLimitForFindCursor(limit);
    
                findCursor = provider.find(database, collection, filter, {
                    projection,
                    limit: limitOnFindCursor.limit,
                    sort,
                });
    
                const [queryResultsCount, cursorResults] = await Promise.all([
                    operationWithFallback(
                        () =>
                            provider.countDocuments(database, collection, filter, {
                                // We should be counting documents that the original
                                // query would have yielded which is why we don't
                                // use `limitOnFindCursor` calculated above, only
                                // the limit provided to the tool.
                                limit,
                                maxTimeMS: QUERY_COUNT_MAX_TIME_MS_CAP,
                            }),
                        undefined
                    ),
                    collectCursorUntilMaxBytesLimit({
                        cursor: findCursor,
                        configuredMaxBytesPerQuery: this.config.maxBytesPerQuery,
                        toolResponseBytesLimit: responseBytesLimit,
                        abortSignal: signal,
                    }),
                ]);
    
                return {
                    content: formatUntrustedData(
                        this.generateMessage({
                            collection,
                            queryResultsCount,
                            documents: cursorResults.documents,
                            appliedLimits: [limitOnFindCursor.cappedBy, cursorResults.cappedBy].filter((limit) => !!limit),
                        }),
                        ...(cursorResults.documents.length > 0 ? [EJSON.stringify(cursorResults.documents)] : [])
                    ),
                };
            } finally {
                if (findCursor) {
                    void this.safeCloseCursor(findCursor);
                }
            }
        }
    
        private async safeCloseCursor(cursor: FindCursor<unknown>): Promise<void> {
            try {
                await cursor.close();
            } catch (error) {
                this.session.logger.warning({
                    id: LogId.mongodbCursorCloseError,
                    context: "find tool",
                    message: `Error when closing the cursor - ${error instanceof Error ? error.message : String(error)}`,
                });
            }
        }
    
        private generateMessage({
            collection,
            queryResultsCount,
            documents,
            appliedLimits,
        }: {
            collection: string;
            queryResultsCount: number | undefined;
            documents: unknown[];
            appliedLimits: (keyof typeof CURSOR_LIMITS_TO_LLM_TEXT)[];
        }): string {
            const appliedLimitsText = appliedLimits.length
                ? `\
    while respecting the applied limits of ${appliedLimits.map((limit) => CURSOR_LIMITS_TO_LLM_TEXT[limit]).join(", ")}. \
    Note to LLM: If the entire query result is required then use "export" tool to export the query results.\
    `
                : "";
    
            return `\
    Query on collection "${collection}" resulted in ${queryResultsCount === undefined ? "indeterminable number of" : queryResultsCount} documents. \
    Returning ${documents.length} documents${appliedLimitsText ? ` ${appliedLimitsText}` : "."}\
    `;
        }
    
        private getLimitForFindCursor(providedLimit: number | undefined | null): {
            cappedBy: "config.maxDocumentsPerQuery" | undefined;
            limit: number | undefined;
        } {
            const configuredLimit: number = parseInt(String(this.config.maxDocumentsPerQuery), 10);
    
            // Setting configured maxDocumentsPerQuery to negative, zero or nullish
            // is equivalent to disabling the max limit applied on documents
            const configuredLimitIsNotApplicable = Number.isNaN(configuredLimit) || configuredLimit <= 0;
            if (configuredLimitIsNotApplicable) {
                return { cappedBy: undefined, limit: providedLimit ?? undefined };
            }
    
            const providedLimitIsNotApplicable = providedLimit === null || providedLimit === undefined;
            if (providedLimitIsNotApplicable) {
                return { cappedBy: "config.maxDocumentsPerQuery", limit: configuredLimit };
            }
    
            return {
                cappedBy: configuredLimit < providedLimit ? "config.maxDocumentsPerQuery" : undefined,
                limit: Math.min(providedLimit, configuredLimit),
            };
        }
    }
  • Zod schema definition for the input arguments of the 'find' tool, including filter, projection, limit, sort, and responseBytesLimit.
    export const FindArgs = {
        filter: zEJSON()
            .optional()
            .describe("The query filter, matching the syntax of the query argument of db.collection.find()"),
        projection: z
            .object({})
            .passthrough()
            .optional()
            .describe("The projection, matching the syntax of the projection argument of db.collection.find()"),
        limit: z.number().optional().default(10).describe("The maximum number of documents to return"),
        sort: z
            .object({})
            .catchall(z.custom<SortDirection>())
            .optional()
            .describe(
                "A document, describing the sort order, matching the syntax of the sort argument of cursor.sort(). The keys of the object are the fields to sort on, while the values are the sort directions (1 for ascending, -1 for descending)."
            ),
        responseBytesLimit: z.number().optional().default(ONE_MB).describe(`\
    The maximum number of bytes to return in the response. This value is capped by the server's configured maxBytesPerQuery and cannot be exceeded. \
    Note to LLM: If the entire query result is required, use the "export" tool instead of increasing this limit.\
    `),
    };
  • The FindTool is exported from src/tools/mongodb/tools.ts, which serves as the central registry exporting all MongoDB tools for use in the MCP server transports.
    export { FindTool } from "./read/find.js";
  • The 'find' tool schema (FindArgs) is reused in the export tool for defining find-based exports.
    import { FindArgs } from "./find.js";
    import { jsonExportFormat } from "../../../common/exportsManager.js";
    import { getAggregateArgs } from "./aggregate.js";
    
    export class ExportTool extends MongoDBToolBase {
        public name = "export";
        protected description = "Export a query or aggregation results in the specified EJSON format.";
        protected argsShape = {
            ...DbOperationArgs,
            exportTitle: z.string().describe("A short description to uniquely identify the export."),
            exportTarget: z
                .array(
                    z.discriminatedUnion("name", [
                        z.object({
                            name: z
                                .literal("find")
                                .describe("The literal name 'find' to represent a find cursor as target."),
                            arguments: z
                                .object({
                                    ...FindArgs,
                                    limit: FindArgs.limit.removeDefault(),
                                })
                                .describe("The arguments for 'find' operation."),
                        }),
                        z.object({
                            name: z
                                .literal("aggregate")
                                .describe("The literal name 'aggregate' to represent an aggregation cursor as target."),
                            arguments: z
                                .object(getAggregateArgs(this.isFeatureEnabled("search")))
  • Helper methods within FindTool for message generation, limit calculation, and cursor management.
        private async safeCloseCursor(cursor: FindCursor<unknown>): Promise<void> {
            try {
                await cursor.close();
            } catch (error) {
                this.session.logger.warning({
                    id: LogId.mongodbCursorCloseError,
                    context: "find tool",
                    message: `Error when closing the cursor - ${error instanceof Error ? error.message : String(error)}`,
                });
            }
        }
    
        private generateMessage({
            collection,
            queryResultsCount,
            documents,
            appliedLimits,
        }: {
            collection: string;
            queryResultsCount: number | undefined;
            documents: unknown[];
            appliedLimits: (keyof typeof CURSOR_LIMITS_TO_LLM_TEXT)[];
        }): string {
            const appliedLimitsText = appliedLimits.length
                ? `\
    while respecting the applied limits of ${appliedLimits.map((limit) => CURSOR_LIMITS_TO_LLM_TEXT[limit]).join(", ")}. \
    Note to LLM: If the entire query result is required then use "export" tool to export the query results.\
    `
                : "";
    
            return `\
    Query on collection "${collection}" resulted in ${queryResultsCount === undefined ? "indeterminable number of" : queryResultsCount} documents. \
    Returning ${documents.length} documents${appliedLimitsText ? ` ${appliedLimitsText}` : "."}\
    `;
        }
    
        private getLimitForFindCursor(providedLimit: number | undefined | null): {
            cappedBy: "config.maxDocumentsPerQuery" | undefined;
            limit: number | undefined;
        } {
            const configuredLimit: number = parseInt(String(this.config.maxDocumentsPerQuery), 10);
    
            // Setting configured maxDocumentsPerQuery to negative, zero or nullish
            // is equivalent to disabling the max limit applied on documents
            const configuredLimitIsNotApplicable = Number.isNaN(configuredLimit) || configuredLimit <= 0;
            if (configuredLimitIsNotApplicable) {
                return { cappedBy: undefined, limit: providedLimit ?? undefined };
            }
    
            const providedLimitIsNotApplicable = providedLimit === null || providedLimit === undefined;
            if (providedLimitIsNotApplicable) {
                return { cappedBy: "config.maxDocumentsPerQuery", limit: configuredLimit };
            }
    
            return {
                cappedBy: configuredLimit < providedLimit ? "config.maxDocumentsPerQuery" : undefined,
                limit: Math.min(providedLimit, configuredLimit),
            };
        }
    }
Behavior3/5

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

Annotations already declare readOnlyHint=true and destructiveHint=false, so the agent knows this is a safe read operation. The description adds minimal behavioral context beyond this - it mentions it's a 'find query' which implies document retrieval, but doesn't disclose rate limits, authentication requirements, or what happens when responseBytesLimit is exceeded. With annotations covering safety, a 3 is appropriate.

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 with zero wasted words. It's appropriately sized for a tool with comprehensive schema documentation and gets straight to the point without unnecessary elaboration.

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

Completeness3/5

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

Given the tool's complexity (7 parameters, nested objects) and lack of output schema, the description is minimally adequate. The annotations cover safety, and the schema documents parameters thoroughly, but the description doesn't help the agent understand result formats, error conditions, or practical usage patterns. For a query tool with no output schema, more context would be helpful.

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%, with each parameter well-documented in the schema itself. The description doesn't add any meaningful parameter semantics beyond what's already in the schema - it mentions 'find query' which aligns with the schema's documentation but provides no additional syntax examples, format details, or usage patterns. Baseline 3 is correct when schema does all the work.

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

Purpose4/5

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

The description clearly states the action ('Run a find query') and target ('against a MongoDB collection'), providing a specific verb+resource combination. However, it doesn't explicitly distinguish this from sibling tools like 'count' or 'export' that also query MongoDB collections, which prevents a perfect score.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools like 'count' for counting documents, 'export' for larger result sets, or 'aggregate' for complex queries. There's no context about appropriate use cases or limitations.

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/mongodb-js/mongodb-mcp-server'

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