Skip to main content
Glama
yaoxiaolinglong

MCP-MongoDB-MySQL-Server

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
NameRequiredDescriptionDefault
collectionYesCollection name
optionsNoCollection options

Implementation Reference

  • 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);

Latest Blog Posts

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/yaoxiaolinglong/mcp-mongodb-mysql-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server