Skip to main content
Glama

MCP-MongoDB-MySQL-Server

mongodb_find

Query and retrieve documents from a MongoDB collection using a customizable filter, sorting, and pagination. Ideal for extracting specific data from MongoDB databases via the MCP server.

Instructions

Find documents in a MongoDB collection

Input Schema

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

Input Schema (JSON Schema)

{ "properties": { "collection": { "description": "Collection name", "type": "string" }, "filter": { "description": "MongoDB query filter", "optional": true, "type": "object" }, "limit": { "description": "Maximum number of documents to return", "optional": true, "type": "number" }, "skip": { "description": "Number of documents to skip", "optional": true, "type": "number" }, "sort": { "description": "Sort criteria", "optional": true, "type": "object" } }, "required": [ "collection" ], "type": "object" }

Implementation Reference

  • The handler function that implements the core logic for the 'mongodb_find' tool. It ensures MongoDB connection, validates collection name, constructs query filter and options (limit, skip, sort), executes find query, and returns JSON-formatted documents.
    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)}` ); } }
  • Input schema definition for the 'mongodb_find' tool, specifying parameters like collection (required), filter, limit, skip, and sort.
    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)
    Tool registration in the ListTools response, including name, description, and input schema for 'mongodb_find'.
    { 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)
    Dispatcher case in the CallToolRequestHandler switch statement that routes 'mongodb_find' calls to the handleMongoDBFind method.
    case 'mongodb_find': return await this.handleMongoDBFind(request.params.arguments);

Other Tools

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

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