mongodb_insert_document
Insert JSON documents into MongoDB collections through the MCP Mac Apps Server. Specify database, collection, and document data to add records to your MongoDB database.
Instructions
Вставляет документ в коллекцию
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| databaseName | Yes | Имя базы данных | |
| collectionName | Yes | Имя коллекции | |
| document | Yes | JSON строка с документом для вставки |
Implementation Reference
- src/index.ts:749-776 (handler)The primary handler function that executes the mongodb_insert_document tool. Connects to MongoDB via helper, parses JSON document, performs insertOne operation, and returns success message with inserted ID.private async mongodbInsertDocument( databaseName: string, collectionName: string, documentJson: string ) { const client = await this.getMongoClient(); try { const db = client.db(databaseName); const collection = db.collection(collectionName); const document = JSON.parse(documentJson); const result = await collection.insertOne(document); return { content: [ { type: "text", text: `Документ успешно вставлен в коллекцию "${collectionName}". ID: ${result.insertedId}`, }, ], }; } catch (error) { throw new Error( `Ошибка вставки документа: ${error instanceof Error ? error.message : String(error)}` ); } finally { await client.close(); } }
- src/index.ts:230-250 (schema)Input schema for the mongodb_insert_document tool defining required parameters: databaseName (string), collectionName (string), document (string JSON).name: "mongodb_insert_document", description: "Вставляет документ в коллекцию", inputSchema: { type: "object", properties: { databaseName: { type: "string", description: "Имя базы данных", }, collectionName: { type: "string", description: "Имя коллекции", }, document: { type: "string", description: "JSON строка с документом для вставки", }, }, required: ["databaseName", "collectionName", "document"], }, },
- src/index.ts:359-364 (registration)Registration in the tool call dispatcher switch statement that routes calls to the mongodbInsertDocument handler.case "mongodb_insert_document": return await this.mongodbInsertDocument( args?.databaseName as string, args?.collectionName as string, args?.document as string );
- src/index.ts:615-619 (helper)Shared helper function to establish a MongoDB client connection, reused across all MongoDB-related tools.private async getMongoClient(): Promise<MongoClient> { const client = new MongoClient(MONGODB_URI); await client.connect(); return client; }
- src/server.py:499-513 (handler)Python implementation of the handler for mongodb_insert_document tool (note: dispatch is commented out in handle_request).def mongodb_insert_document( database_name: str, collection_name: str, document_json: str ) -> str: """Inserts document into collection""" client = MongoClient(MONGODB_URI) try: db = client[database_name] collection = db[collection_name] document = json.loads(document_json) result = collection.insert_one(document) return f'Document successfully inserted into collection "{collection_name}". ID: {result.inserted_id}' except Exception as e: raise Exception(f"Error inserting document: {str(e)}") finally: client.close()