Skip to main content
Glama
CaptainCrouton89

MCP Server Boilerplate

mongo-update-document

Modify existing documents in MongoDB collections by specifying database, collection, filter criteria, and update operations. Supports single or multiple document updates.

Instructions

Update documents in a MongoDB collection

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
collectionYesCollection name
filterYesQuery filter to match documents to update
updateYesUpdate operations as JSON object
updateManyNoWhether to update multiple documents (default: false)

Implementation Reference

  • Handler function that executes the MongoDB update operation, either updateOne or updateMany based on the updateMany flag.
    async ({ database: dbName, collection: collectionName, filter, update, updateMany = false }) => {
      try {
        const db = await ensureConnection(dbName);
        const collection: Collection = db.collection(collectionName);
        
        const result = updateMany 
          ? await collection.updateMany(filter, update)
          : await collection.updateOne(filter, update);
        
        return {
          content: [
            {
              type: "text",
              text: `Update operation completed. Matched: ${result.matchedCount}, Modified: ${result.modifiedCount}`,
            },
          ],
        };
      } catch (error) {
        throw new Error(`Failed to update document(s): ${error instanceof Error ? error.message : 'Unknown error'}`);
      }
    }
  • Zod input schema defining parameters: database, collection, filter, update, and optional updateMany.
    {
      database: z.string().describe("Database name"),
      collection: z.string().describe("Collection name"),
      filter: z.record(z.any()).describe("Query filter to match documents to update"),
      update: z.record(z.any()).describe("Update operations as JSON object"),
      updateMany: z.boolean().optional().describe("Whether to update multiple documents (default: false)"),
    },
  • src/index.ts:168-199 (registration)
    Registration of the mongo-update-document tool with McpServer.tool(), including schema and inline handler implementation.
    server.tool(
      "mongo-update-document",
      "Update documents in a MongoDB collection",
      {
        database: z.string().describe("Database name"),
        collection: z.string().describe("Collection name"),
        filter: z.record(z.any()).describe("Query filter to match documents to update"),
        update: z.record(z.any()).describe("Update operations as JSON object"),
        updateMany: z.boolean().optional().describe("Whether to update multiple documents (default: false)"),
      },
      async ({ database: dbName, collection: collectionName, filter, update, updateMany = false }) => {
        try {
          const db = await ensureConnection(dbName);
          const collection: Collection = db.collection(collectionName);
          
          const result = updateMany 
            ? await collection.updateMany(filter, update)
            : await collection.updateOne(filter, update);
          
          return {
            content: [
              {
                type: "text",
                text: `Update operation completed. Matched: ${result.matchedCount}, Modified: ${result.modifiedCount}`,
              },
            ],
          };
        } catch (error) {
          throw new Error(`Failed to update document(s): ${error instanceof Error ? error.message : 'Unknown error'}`);
        }
      }
    );
  • Helper function to establish and cache MongoDB connection and database instance, used by the 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)!;
    }
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 action is an update but doesn't cover critical aspects like whether it's destructive, what permissions are needed, how errors are handled, or the response format. This is a significant gap for a mutation tool with complex parameters.

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 any fluff. It's front-loaded and wastes no words, making it easy for an agent to parse quickly.

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 complexity of a MongoDB update operation with 5 parameters, no annotations, and no output schema, the description is insufficient. It lacks details on behavior, error handling, return values, and how to use parameters effectively, making it incomplete for safe and accurate tool invocation.

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 doesn't add any parameter-specific information beyond what's already in the schema, which has 100% coverage. It mentions 'documents' and 'collection' but doesn't explain the semantics of filter, update, or updateMany. The baseline score of 3 reflects adequate but minimal value added over the schema.

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 ('Update') and resource ('documents in a MongoDB collection'), making the purpose immediately understandable. However, it doesn't differentiate from sibling tools like mongo-create-document or mongo-delete-document beyond the verb 'Update', which is why it doesn't reach a perfect score.

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. It doesn't mention prerequisites, when to choose update over create/delete, or how it relates to siblings like mongo-aggregate or mongo-find-documents, leaving the agent to infer usage context.

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