create-collection
Create a new MongoDB collection in a specified database, automatically generating the database if it doesn’t exist. Simplify database management with this MCP Server tool.
Instructions
Creates a new collection in a database. If the database doesn't exist, it will be created automatically.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| database | Yes | Database name |
Implementation Reference
- The CreateCollectionTool class provides the core implementation of the "create-collection" tool, including its name, description, input schema reference, operation type, and the execute method that connects to MongoDB and creates the collection.export class CreateCollectionTool extends MongoDBToolBase { public name = "create-collection"; protected description = "Creates a new collection in a database. If the database doesn't exist, it will be created automatically."; protected argsShape = DbOperationArgs; static operationType: OperationType = "create"; protected async execute({ collection, database }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); await provider.createCollection(database, collection); return { content: [ { type: "text", text: `Collection "${collection}" created in database "${database}".`, }, ], }; } }
- Zod schema DbOperationArgs defining the input parameters (database and collection names) used by the create-collection tool and other similar MongoDB tools.export const DbOperationArgs = { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), };
- src/tools/mongodb/tools.ts:19-19 (registration)Re-export of the CreateCollectionTool class from its source file, making it available as part of the MongoDB tools collection.export { CreateCollectionTool } from "./create/createCollection.js";
- src/tools/index.ts:7-11 (registration)The AllTools array aggregates all tool classes, including those from MongoDbTools (which encompasses CreateCollectionTool), used by the server for tool registration.export const AllTools: ToolClass[] = Object.values({ ...MongoDbTools, ...AtlasTools, ...AtlasLocalTools, });
- src/server.ts:101-102 (registration)In the Server constructor, toolConstructors is set to AllTools by default, which includes the CreateCollectionTool; later instantiated and registered in registerTools().this.toolConstructors = tools ?? AllTools; }