mongodb_create_collection
Create a new collection in MongoDB to organize and store data, enabling structured database management through the MCP server interface.
Instructions
Create a new collection in MongoDB
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| options | No | Collection options |
Implementation Reference
- src/index.ts:1102-1125 (handler)The handler function that executes the tool logic: ensures MongoDB connection, validates collection name, creates the collection with optional options, returns success message or throws appropriate MCP error.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)Tool registration in the listTools response, defining the tool name, description, and input schema for validation.{ 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:564-565 (registration)Switch case in callTool handler that routes requests for this tool to the specific handler method.case 'mongodb_create_collection': return await this.handleMongoDBCreateCollection(request.params.arguments);