Skip to main content
Glama

MongoDB MCP Server

Official
by mongodb-js
renameCollection.ts2.61 kB
import { z } from "zod"; import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js"; import type { ToolArgs, OperationType } from "../../tool.js"; export class RenameCollectionTool extends MongoDBToolBase { public name = "rename-collection"; protected description = "Renames a collection in a MongoDB database"; 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"), }; public operationType: OperationType = "update"; 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", }, ], }; } 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); } }

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