Skip to main content
Glama

MongoDB MCP Server

Official
by mongodb-js
listCollections.ts1.41 kB
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js"; import type { ToolArgs, OperationType } from "../../tool.js"; import { formatUntrustedData } from "../../tool.js"; export class ListCollectionsTool extends MongoDBToolBase { public name = "list-collections"; protected description = "List all collections for a given database"; protected argsShape = { database: DbOperationArgs.database, }; public operationType: OperationType = "metadata"; protected async execute({ database }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); const collections = await provider.listCollections(database); if (collections.length === 0) { return { content: [ { type: "text", text: `Found 0 collections for database "${database}". To create a collection, use the "create-collection" tool.`, }, ], }; } return { content: formatUntrustedData( `Found ${collections.length} collections for database "${database}".`, collections.map((collection) => `"${collection.name}"`).join("\n") ), }; } }

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/mongodb-js/mongodb-mcp-server'

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