Skip to main content
Glama
manpreet2000

MCP Database Server

insertOne

Add a single document to a specified MongoDB collection through the MCP Database Server, enabling data insertion via natural language commands.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionNameYes
documentYes

Implementation Reference

  • The main execution logic for the 'insertOne' MCP tool: connects to MongoDB, gets the collection, calls insertOne(document), and returns success/error response.
    async ({ collectionName, document }) => { try { let db = mongodbConnection.getDb(); if (!db) { await mongodbConnection.connect(this.MONGODB_URI); db = mongodbConnection.getDb(); if (!db) throw new Error("Failed to connect to database"); } const collection = db.collection(collectionName); await collection.insertOne(document); return { content: [{ type: "text", text: "Document inserted successfully" }], }; } catch (error) { console.error(error); return { content: [{ type: "text", text: "Error: " + error }], }; } }
  • Zod input schema validation for the 'insertOne' tool parameters: collectionName (string) and document (any object).
    { collectionName: z.string(), document: z.object({}), },
  • src/index.ts:97-123 (registration)
    Registration of the 'insertOne' tool on the MCP server instance using the tool name, schema, and handler function.
    this.mcpServer.tool( "insertOne", { collectionName: z.string(), document: z.object({}), }, async ({ collectionName, document }) => { try { let db = mongodbConnection.getDb(); if (!db) { await mongodbConnection.connect(this.MONGODB_URI); db = mongodbConnection.getDb(); if (!db) throw new Error("Failed to connect to database"); } const collection = db.collection(collectionName); await collection.insertOne(document); return { content: [{ type: "text", text: "Document inserted successfully" }], }; } catch (error) { console.error(error); return { content: [{ type: "text", text: "Error: " + error }], }; } } );

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/manpreet2000/mcp-database-server'

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