Skip to main content
Glama

MongoDB MCP Server

Official
by mongodb-js

rename-collection

Renames a MongoDB collection within a specified database, optionally replacing an existing collection with the same name. Simplifies collection management tasks in MongoDB Atlas.

Instructions

Renames a collection in a MongoDB database

Input Schema

NameRequiredDescriptionDefault
collectionYesCollection name
databaseYesDatabase name
dropTargetNoIf true, drops the target collection if it exists
newNameYesThe new name for the collection

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "collection": { "description": "Collection name", "type": "string" }, "database": { "description": "Database name", "type": "string" }, "dropTarget": { "default": false, "description": "If true, drops the target collection if it exists", "type": "boolean" }, "newName": { "description": "The new name for the collection", "type": "string" } }, "required": [ "database", "collection", "newName" ], "type": "object" }

Implementation Reference

  • The execute method implements the core logic of the 'rename-collection' tool, connecting to MongoDB provider and calling renameCollection with the provided arguments.
    protected async execute({ database, collection, newName, dropTarget, }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); const result = await provider.renameCollection(database, collection, newName, { dropTarget, }); return { content: [ { text: `Collection "${collection}" renamed to "${result.collectionName}" in database "${database}".`, type: "text", }, ], }; }
  • Zod schema defining the input arguments for the tool: database, collection from DbOperationArgs, plus newName and optional dropTarget.
    protected argsShape = { ...DbOperationArgs, newName: z.string().describe("The new name for the collection"), dropTarget: z.boolean().optional().default(false).describe("If true, drops the target collection if it exists"), };
  • Barrel export registering the RenameCollectionTool in the MongoDB tools module.
    export { RenameCollectionTool } from "./update/renameCollection.js";
  • The tool's public name property set to 'rename-collection', used for MCP tool identification.
    public name = "rename-collection";
  • Custom error handling for specific MongoDB errors like NamespaceNotFound and NamespaceExists.
    protected handleError( error: unknown, args: ToolArgs<typeof this.argsShape> ): Promise<CallToolResult> | CallToolResult { if (error instanceof Error && "codeName" in error) { switch (error.codeName) { case "NamespaceNotFound": return { content: [ { text: `Cannot rename "${args.database}.${args.collection}" because it doesn't exist.`, type: "text", }, ], isError: true, }; case "NamespaceExists": return { content: [ { text: `Cannot rename "${args.database}.${args.collection}" to "${args.newName}" because the target collection already exists. If you want to overwrite it, set the "dropTarget" argument to true.`, type: "text", }, ], isError: true, }; } } return super.handleError(error, args); }

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/mongodb-js/mongodb-mcp-server'

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