Skip to main content
Glama
manpreet2000

MCP Database Server

getCollection

Retrieve documents from a MongoDB collection by specifying collection name, query filters, and optional result limits or field projections.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionNameYes
limitNo
queryNo
projectionNo

Implementation Reference

  • The asynchronous handler function that implements the core logic of the 'getCollection' tool. It connects to MongoDB if necessary, retrieves the specified collection, queries documents with optional parameters, and returns them as JSON strings.
    async ({
      collectionName,
      limit,
      query,
      projection,
    }: {
      collectionName: string;
      limit?: number;
      query?: any;
      projection?: any;
    }) => {
      try {
        let db = mongodbConnection.getDb();
        if (!db) {
          await mongodbConnection.connect(this.MONGODB_URI);
          db = mongodbConnection.getDb();
          if (!db) throw new Error("Failed to connect to database");
        }
        const collection = db.collection(collectionName);
        const documents = await collection
          .find(query ?? {})
          .limit(limit ?? 100)
          .project(projection ?? {})
          .toArray();
        return {
          content: [
            {
              type: "text",
              text: documents.map((d) => JSON.stringify(d)).join("\n"),
            },
          ],
        };
      } catch (error) {
        console.error(error);
        return {
          content: [{ type: "text", text: "Error: " + error }],
        };
      }
    }
  • Zod schema defining the input parameters for the 'getCollection' tool: collectionName (required string), limit (optional number 1-1000, default 10), query (optional object), projection (optional object).
    {
      collectionName: z.string(),
      limit: z.number().min(1).max(1000).optional().default(10),
      query: z.object({}).optional(),
      projection: z.object({}).optional(),
    },
  • src/index.ts:48-95 (registration)
    The registration of the 'getCollection' tool on the MCP server using this.mcpServer.tool(), including name, input schema, and handler function.
    this.mcpServer.tool(
      "getCollection",
      {
        collectionName: z.string(),
        limit: z.number().min(1).max(1000).optional().default(10),
        query: z.object({}).optional(),
        projection: z.object({}).optional(),
      },
      async ({
        collectionName,
        limit,
        query,
        projection,
      }: {
        collectionName: string;
        limit?: number;
        query?: any;
        projection?: any;
      }) => {
        try {
          let db = mongodbConnection.getDb();
          if (!db) {
            await mongodbConnection.connect(this.MONGODB_URI);
            db = mongodbConnection.getDb();
            if (!db) throw new Error("Failed to connect to database");
          }
          const collection = db.collection(collectionName);
          const documents = await collection
            .find(query ?? {})
            .limit(limit ?? 100)
            .project(projection ?? {})
            .toArray();
          return {
            content: [
              {
                type: "text",
                text: documents.map((d) => JSON.stringify(d)).join("\n"),
              },
            ],
          };
        } catch (error) {
          console.error(error);
          return {
            content: [{ type: "text", text: "Error: " + error }],
          };
        }
      }
    );
  • The MongoDBConnection class and singleton instance 'mongodbConnection' used by the 'getCollection' handler for database connections, getDb(), and close operations.
    export class MongoDBConnection {
      private client: MongoClient | null = null;
      private db: Db | null = null;
    
      async connect(databaseUrl: string) {
        try {
          this.client = new MongoClient(databaseUrl);
          await this.client.connect();
          this.db = this.client.db();
          return this.db;
        } catch (error) {
          console.error("MongoDB connection error:", error);
          throw error;
        }
      }
    
      async close() {
        await this.client?.close();
      }
    
      getClient(): MongoClient | null {
        return this.client;
      }
    
      getDb(): Db | null {
        return this.db;
      }
    }
Behavior1/5

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

Tool has no description.

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

Conciseness1/5

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

Tool has no description.

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

Completeness1/5

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

Tool has no description.

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

Parameters1/5

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

Tool has no description.

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

Purpose1/5

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

Tool has no description.

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

Usage Guidelines1/5

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

Tool has no description.

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

Install Server

Other Tools

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/manpreet2000/mcp-database-server'

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