mongo-create-document
Create new documents in MongoDB collections by specifying database, collection, and JSON document data for structured data insertion.
Instructions
Create a new document in a MongoDB collection
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| database | Yes | Database name | |
| document | Yes | Document to insert as JSON object |
Input Schema (JSON Schema)
{
"properties": {
"collection": {
"description": "Collection name",
"type": "string"
},
"database": {
"description": "Database name",
"type": "string"
},
"document": {
"additionalProperties": {},
"description": "Document to insert as JSON object",
"type": "object"
}
},
"required": [
"database",
"collection",
"document"
],
"type": "object"
}
Implementation Reference
- src/index.ts:110-128 (handler)The handler function for the 'mongo-create-document' tool. It ensures a connection to the specified database, retrieves the collection, inserts the provided document using MongoDB's insertOne method, and returns a success message with the inserted document's ID. Errors are caught and rethrown with context.async ({ database: dbName, collection: collectionName, document }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const result = await collection.insertOne(document); return { content: [ { type: "text", text: `Document created successfully with ID: ${result.insertedId}`, }, ], }; } catch (error) { throw new Error(`Failed to create document: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/index.ts:105-109 (schema)Zod schema defining the input parameters for the tool: database name (string), collection name (string), and document (record/object to insert).{ database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), document: z.record(z.any()).describe("Document to insert as JSON object"), },
- src/index.ts:102-129 (registration)Registration of the 'mongo-create-document' tool on the MCP server, specifying name, description, input schema, and handler function.server.tool( "mongo-create-document", "Create a new document in a MongoDB collection", { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), document: z.record(z.any()).describe("Document to insert as JSON object"), }, async ({ database: dbName, collection: collectionName, document }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const result = await collection.insertOne(document); return { content: [ { type: "text", text: `Document created successfully with ID: ${result.insertedId}`, }, ], }; } catch (error) { throw new Error(`Failed to create document: ${error instanceof Error ? error.message : 'Unknown error'}`); } } );
- src/index.ts:88-100 (helper)Helper function to establish and cache MongoDB client connection and database instances, used by the tool handler.async function ensureConnection(dbName: string): Promise<Db> { if (!mongoClient) { const uri = getMongoUri(); mongoClient = new MongoClient(uri); await mongoClient.connect(); } if (!databases.has(dbName)) { databases.set(dbName, mongoClient.db(dbName)); } return databases.get(dbName)!; }