list-databases
Retrieve a list of all databases available in a MongoDB connection to identify and access database resources.
Instructions
List all databases for a MongoDB connection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The `execute` method implements the core logic of the 'list-databases' tool, connecting to MongoDB, fetching databases with sizes, and returning formatted text and structured content.protected async execute(): Promise<CallToolResult> { const provider = await this.ensureConnected(); const dbs = (await provider.listDatabases("")).databases as { name: string; sizeOnDisk: bson.Long }[]; const databases = dbs.map((db) => ({ name: db.name, size: Number(db.sizeOnDisk), })); return { content: formatUntrustedData( `Found ${dbs.length} databases`, ...dbs.map((db) => `Name: ${db.name}, Size: ${db.sizeOnDisk.toString()} bytes`) ), structuredContent: { databases, totalCount: databases.length, }, }; }
- Zod schema defining the structured output for the list-databases tool, including an array of database names and sizes, and total count.export const ListDatabasesOutputSchema = { databases: z.array( z.object({ name: z.string(), size: z.number(), }) ), totalCount: z.number(), };
- src/tools/mongodb/tools.ts:4-4 (registration)Exports the ListDatabasesTool class, making it available for inclusion in the AllTools array used by the server for tool registration.export { ListDatabasesTool } from "./metadata/listDatabases.js";
- The full ListDatabasesTool class definition, including name, description, schemas, and execute handler for the 'list-databases' tool.export class ListDatabasesTool extends MongoDBToolBase { public name = "list-databases"; protected description = "List all databases for a MongoDB connection"; protected argsShape = {}; protected override outputSchema = ListDatabasesOutputSchema; static operationType: OperationType = "metadata"; protected async execute(): Promise<CallToolResult> { const provider = await this.ensureConnected(); const dbs = (await provider.listDatabases("")).databases as { name: string; sizeOnDisk: bson.Long }[]; const databases = dbs.map((db) => ({ name: db.name, size: Number(db.sizeOnDisk), })); return { content: formatUntrustedData( `Found ${dbs.length} databases`, ...dbs.map((db) => `Name: ${db.name}, Size: ${db.sizeOnDisk.toString()} bytes`) ), structuredContent: { databases, totalCount: databases.length, }, }; } }
- src/tools/index.ts:7-11 (registration)AllTools array construction that includes MongoDbTools (which exports ListDatabasesTool), used by server.ts for registering all tools including 'list-databases'.export const AllTools: ToolClass[] = Object.values({ ...MongoDbTools, ...AtlasTools, ...AtlasLocalTools, });