Skip to main content
Glama
ricleedo

MCP Server Boilerplate

by ricleedo

mongo-list-collections

Retrieve all collections from a 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

  • src/index.ts:293-318 (registration)
    Registration of the 'mongo-list-collections' tool using server.tool(), including name, description, input schema, and inline handler function.
    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'}`);
        }
      }
    );
  • The core handler function that ensures a connection to the specified database, lists collections using db.listCollections(), extracts names, and returns formatted text output.
    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'}`);
      }
    }
  • Input schema using Zod, requiring a 'database' string parameter.
    {
      database: z.string().describe("Database name"),
    },
  • Helper function to establish and cache MongoDB connection and database instance, called by the handler.
    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)!;
    }

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed2 schema fields changedv1.0.0
    • addedInput schema / $schema
      Added value: +"http://json-schema.org/draft-07/schema#"
    • addedInput schema / additionalProperties
      Added value: +false
  2. First observed

TDQS

B3.1/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states a read operation ('List'), implying it's non-destructive, but doesn't cover aspects like permissions needed, rate limits, error conditions, or what the output looks like (e.g., format, pagination). This leaves significant gaps for a tool that interacts with a database.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's function without unnecessary words. It's front-loaded and wastes no space, making it easy for an agent to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's low complexity (one parameter, no output schema, no annotations), the description is minimally adequate. It covers the basic purpose but lacks details on behavior, usage context, and output, which could be important for database operations. This aligns with a score of 3, indicating clear gaps in completeness despite the simple structure.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, with the 'database' parameter clearly documented. The description adds no additional meaning beyond the schema, such as examples or constraints (e.g., database must exist). This meets the baseline score of 3, as the schema adequately covers parameter semantics without extra value from the description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('List all collections') and resource ('in a MongoDB database'), making the purpose unambiguous. However, it doesn't explicitly differentiate from sibling tools like 'mongo-find-documents' or 'mongo-aggregate', which operate on documents rather than collections, leaving some room for improvement in sibling distinction.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It lacks context about prerequisites (e.g., database existence), exclusions, or comparisons to siblings like 'mongo-find-documents' for document-level operations, leaving the agent to infer usage based on tool names alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.