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; } }

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