Skip to main content
Glama
yaoxiaolinglong

MCP-MongoDB-MySQL-Server

mongodb_find

Query documents in a MongoDB collection using filters, sorting, and pagination to retrieve specific data.

Instructions

Find documents in a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name
filterNoMongoDB query filter
limitNoMaximum number of documents to return
skipNoNumber of documents to skip
sortNoSort criteria

Implementation Reference

  • Executes the mongodb_find tool: ensures MongoDB connection, validates collection name, constructs filter and options (limit, skip, sort), performs find query on the collection, and returns results as JSON string.
    private async handleMongoDBFind(args: any) {
      await this.ensureMongoConnection();
    
      if (!args.collection) {
        throw new McpError(ErrorCode.InvalidParams, 'Collection name is required');
      }
    
      try {
        const collection = this.mongoDB!.collection(args.collection);
        const filter = args.filter || {};
        const options: any = {};
        
        if (args.limit) options.limit = args.limit;
        if (args.skip) options.skip = args.skip;
        if (args.sort) options.sort = args.sort;
    
        const documents = await collection.find(filter, options).toArray();
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(documents, null, 2),
            },
          ],
        };
      } catch (error) {
        throw new McpError(
          ErrorCode.InternalError,
          `Failed to find documents: ${getErrorMessage(error)}`
        );
      }
    }
  • Defines the input schema for the mongodb_find tool, specifying parameters like collection (required), filter, limit, skip, and sort (all optional).
    inputSchema: {
      type: 'object',
      properties: {
        collection: {
          type: 'string',
          description: 'Collection name',
        },
        filter: {
          type: 'object',
          description: 'MongoDB query filter',
          optional: true
        },
        limit: {
          type: 'number',
          description: 'Maximum number of documents to return',
          optional: true
        },
        skip: {
          type: 'number',
          description: 'Number of documents to skip',
          optional: true
        },
        sort: {
          type: 'object',
          description: 'Sort criteria',
          optional: true
        }
      },
      required: ['collection'],
    },
  • src/index.ts:408-441 (registration)
    Registers the mongodb_find tool in the MCP server's tools list, providing name, description, and input schema for the ListTools request.
    {
      name: 'mongodb_find',
      description: 'Find documents in a MongoDB collection',
      inputSchema: {
        type: 'object',
        properties: {
          collection: {
            type: 'string',
            description: 'Collection name',
          },
          filter: {
            type: 'object',
            description: 'MongoDB query filter',
            optional: true
          },
          limit: {
            type: 'number',
            description: 'Maximum number of documents to return',
            optional: true
          },
          skip: {
            type: 'number',
            description: 'Number of documents to skip',
            optional: true
          },
          sort: {
            type: 'object',
            description: 'Sort criteria',
            optional: true
          }
        },
        required: ['collection'],
      },
    },
  • src/index.ts:556-557 (registration)
    Switch case in the CallToolRequestSchema handler that routes mongodb_find tool calls to the handleMongoDBFind method.
    case 'mongodb_find':
      return await this.handleMongoDBFind(request.params.arguments);

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

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