Skip to main content
Glama
CaptainCrouton89

MCP Server Boilerplate

mongo-create-document

Insert new documents into MongoDB collections using JSON data. Specify database, collection, and document content to add records to your database.

Instructions

Create a new document in a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionYesCollection name
databaseYesDatabase name
documentYesDocument to insert as JSON object

Implementation Reference

  • The asynchronous handler function for the 'mongo-create-document' tool. It ensures a connection to the specified database, retrieves the collection, inserts the provided document using insertOne, and returns a success message with the inserted document's ID. Errors are caught and rethrown with a descriptive message.
    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'}`); } }
  • The input schema for the 'mongo-create-document' tool, defined using Zod. It validates the database name, collection name (both strings), and the document to insert (any JSON object).
    { 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)
    The registration of the 'mongo-create-document' tool on the MCP server using server.tool(). Includes the tool name, description, input schema, and inline 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'}`); } } );
  • Helper function used by the tool handler to establish and reuse MongoDB client connection and database instance caching.
    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)!; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/CaptainCrouton89/mongo-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server