mongodb_create_collection
Create a new collection in MongoDB with specified name and optional settings using the MCP server for efficient database management.
Instructions
Create a new collection in MongoDB
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| options | No | Collection options |
Input Schema (JSON Schema)
{
"properties": {
"collection": {
"description": "Collection name",
"type": "string"
},
"options": {
"description": "Collection options",
"optional": true,
"type": "object"
}
},
"required": [
"collection"
],
"type": "object"
}
Implementation Reference
- src/index.ts:1102-1125 (handler)The handler function that executes the mongodb_create_collection tool. It ensures the MongoDB connection, validates the collection name, creates the collection using db.createCollection(), handles errors, and returns a success message.private async handleMongoDBCreateCollection(args: any) { await this.ensureMongoConnection(); if (!args.collection) { throw new McpError(ErrorCode.InvalidParams, 'Collection name is required'); } try { await this.mongoDB!.createCollection(args.collection, args.options || {}); return { content: [ { type: 'text', text: `Collection ${args.collection} created successfully`, }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to create collection: ${getErrorMessage(error)}` ); } }
- src/index.ts:513-531 (registration)Registration of the mongodb_create_collection tool in the ListTools handler, specifying name, description, and input schema.{ name: 'mongodb_create_collection', description: 'Create a new collection in MongoDB', inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name', }, options: { type: 'object', description: 'Collection options', optional: true } }, required: ['collection'], }, }
- src/index.ts:516-530 (schema)Input schema for the mongodb_create_collection tool, defining required 'collection' parameter and optional 'options'.inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name', }, options: { type: 'object', description: 'Collection options', optional: true } }, required: ['collection'], },
- src/index.ts:564-565 (registration)Dispatch case in the CallToolRequest handler that routes to the mongodb_create_collection handler function.case 'mongodb_create_collection': return await this.handleMongoDBCreateCollection(request.params.arguments);