mongodb_insert
Add documents to a specified MongoDB collection using a standardized interface for database operations. Input includes collection name and documents to insert.
Instructions
Insert documents into a MongoDB collection
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| documents | Yes | Documents to insert |
Input Schema (JSON Schema)
{
"properties": {
"collection": {
"description": "Collection name",
"type": "string"
},
"documents": {
"description": "Documents to insert",
"items": {
"type": "object"
},
"type": "array"
}
},
"required": [
"collection",
"documents"
],
"type": "object"
}
Implementation Reference
- src/index.ts:1010-1033 (handler)The main handler function for the mongodb_insert tool. Ensures MongoDB connection, validates input (collection and documents), performs insertMany operation, and returns success message with inserted count.private async handleMongoDBInsert(args: any) { await this.ensureMongoConnection(); if (!args.collection || !args.documents) { throw new McpError(ErrorCode.InvalidParams, 'Collection name and documents are required'); } try { const collection = this.mongoDB!.collection(args.collection); const result = await collection.insertMany(args.documents); return { content: [ { type: 'text', text: `Successfully inserted ${result.insertedCount} documents`, }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to insert documents: ${getErrorMessage(error)}` ); }
- src/index.ts:442-461 (registration)Tool registration in the list of tools provided by ListToolsRequestSchema. Includes the tool name, description, and input schema definition.{ name: 'mongodb_insert', description: 'Insert documents into a MongoDB collection', inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name', }, documents: { type: 'array', items: { type: 'object', }, description: 'Documents to insert', }, }, required: ['collection', 'documents'], },
- src/index.ts:445-460 (schema)Input schema definition for the mongodb_insert tool, specifying required properties: collection (string) and documents (array of objects).inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name', }, documents: { type: 'array', items: { type: 'object', }, description: 'Documents to insert', }, }, required: ['collection', 'documents'],