mongodb_insert
Insert documents into MongoDB collections through the MCP-MongoDB-MySQL-Server, enabling structured data storage with specified collection names and document arrays.
Instructions
Insert documents into a MongoDB collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| documents | Yes | Documents to insert |
Implementation Reference
- src/index.ts:1010-1033 (handler)The main handler function that executes the mongodb_insert tool by ensuring MongoDB connection, validating inputs, and performing insertMany operation on the specified collection.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:445-460 (schema)Defines the input schema for the mongodb_insert tool, specifying required collection name and array of documents.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:442-462 (registration)Registers the mongodb_insert tool in the MCP server's tool list, including name, description, and input schema.{ 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:558-559 (registration)Registers the dispatch handler in the switch statement for CallToolRequestSchema, routing mongodb_insert calls to handleMongoDBInsert.case 'mongodb_insert': return await this.handleMongoDBInsert(request.params.arguments);