Skip to main content
Glama
manpreet2000

MCP Database Server

getCollection

Retrieve documents from a MongoDB collection using query filters, with options to limit results and specify field projections.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collectionNameYes
limitNo
queryNo
projectionNo

Implementation Reference

  • The handler function for the 'getCollection' tool. It connects to the MongoDB database if necessary, retrieves the specified collection, queries documents using optional parameters (limit, query, projection), and returns the results as a formatted text response.
    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 Zod schema defining input parameters for the 'getCollection' tool: collectionName (required string), optional limit (number 1-1000, default 10), optional query and projection objects.
    { 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 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 used by the getCollection handler for database connections.
    import { MongoClient, Db } from "mongodb"; 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; } } export const mongodbConnection = new MongoDBConnection();

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