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
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| filter | No | MongoDB query filter | |
| limit | No | Maximum number of documents to return | |
| skip | No | Number of documents to skip | |
| sort | No | Sort criteria |
Implementation Reference
- src/index.ts:977-1009 (handler)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)}` ); } }
- src/index.ts:411-440 (schema)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);