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
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| database | Yes | Database name | |
| dropTarget | No | If true, drops the target collection if it exists | |
| newName | Yes | The 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"), };
- src/tools/mongodb/tools.ts:15-15 (registration)Barrel export registering the RenameCollectionTool in the MongoDB tools module.export { RenameCollectionTool } from "./update/renameCollection.js";
- src/tools/mongodb/update/renameCollection.ts:7-7 (registration)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); }