Skip to main content
Glama
CaptainCrouton89

MCP Server Boilerplate

mongo-list-collections

Retrieve all collection names from a specified MongoDB database to view available data structures and manage database organization.

Instructions

List all collections in a MongoDB database

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name

Implementation Reference

  • The handler function that ensures connection to the MongoDB database, lists all collections using db.listCollections(), maps to names, and returns a formatted text response with the list of collection names.
    async ({ database: dbName }) => {
      try {
        const db = await ensureConnection(dbName);
        
        const collections = await db.listCollections().toArray();
        const collectionNames = collections.map(col => col.name);
        
        return {
          content: [
            {
              type: "text",
              text: `Collections in database '${dbName}':\n${collectionNames.join('\n')}`,
            },
          ],
        };
      } catch (error) {
        throw new Error(`Failed to list collections: ${error instanceof Error ? error.message : 'Unknown error'}`);
      }
    }
  • Zod schema defining the input parameter 'database' as a required string.
    {
      database: z.string().describe("Database name"),
    },
  • src/index.ts:293-318 (registration)
    Registration of the 'mongo-list-collections' tool with server.tool(), including name, description, schema, and handler.
    server.tool(
      "mongo-list-collections",
      "List all collections in a MongoDB database",
      {
        database: z.string().describe("Database name"),
      },
      async ({ database: dbName }) => {
        try {
          const db = await ensureConnection(dbName);
          
          const collections = await db.listCollections().toArray();
          const collectionNames = collections.map(col => col.name);
          
          return {
            content: [
              {
                type: "text",
                text: `Collections in database '${dbName}':\n${collectionNames.join('\n')}`,
              },
            ],
          };
        } catch (error) {
          throw new Error(`Failed to list collections: ${error instanceof Error ? error.message : 'Unknown error'}`);
        }
      }
    );
  • Helper function used by the tool to establish and cache MongoDB connection and database instance.
    async function ensureConnection(dbName: string): Promise<Db> {
      if (!mongoClient) {
        const uri = getMongoUri();
        mongoClient = new MongoClient(uri);
        await mongoClient.connect();
      }
      
      if (!databases.has(dbName)) {
        databases.set(dbName, mongoClient.db(dbName));
      }
      
      return databases.get(dbName)!;
    }

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/CaptainCrouton89/mongo-mcp'

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