Skip to main content
Glama
CaptainCrouton89

MCP Server Boilerplate

mongo-create-document

Insert JSON documents into MongoDB collections using this MCP server tool. Specify database, collection, and document data to create new records in your MongoDB database.

Instructions

Create a new document in a MongoDB collection

Input Schema

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

Implementation Reference

  • Handler function that connects to the MongoDB database, retrieves the collection, inserts the document using insertOne, and returns success message with inserted ID.
    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'}`);
      }
    }
  • Zod schema defining input parameters: database, collection, and document.
    {
      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)
    Full registration of the mongo-create-document tool using McpServer.tool(), including name, description, schema, and inline handler.
    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 to ensure MongoDB client connection and retrieve or cache the database instance.
    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)!;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the tool creates a document but doesn't mention whether this requires specific permissions, how errors are handled (e.g., duplicate keys), what the return value looks like, or any side effects. For a mutation tool with zero annotation coverage, this is a significant gap in transparency.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without unnecessary words. It's appropriately sized and front-loaded, making it easy for an agent to parse quickly, earning full marks for conciseness.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (a write operation with 3 parameters and no output schema) and lack of annotations, the description is insufficient. It doesn't explain what happens after creation (e.g., returns an ID), error handling, or how it differs from sibling tools, leaving critical gaps for an agent to understand the full context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description adds no parameter-specific information beyond what's already in the schema, which has 100% coverage with clear descriptions for 'database', 'collection', and 'document'. Since the schema does the heavy lifting, the baseline score of 3 is appropriate, as the description doesn't compensate with additional semantic context.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Create a new document') and resource ('in a MongoDB collection'), making the purpose immediately understandable. However, it doesn't distinguish this tool from its siblings like 'mongo-update-document' or 'mongo-delete-document' beyond the basic verb difference, missing explicit differentiation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'mongo-update-document' for modifying existing documents or 'mongo-find-documents' for reading. There's no mention of prerequisites, error conditions, or typical use cases, leaving the agent with minimal contextual direction.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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