|
| 1 | +import { z } from "zod"; |
| 2 | +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 3 | +import { AtlasToolBase } from "./atlasTool.js"; |
| 4 | +import { ToolArgs } from "../tool.js"; |
| 5 | +import { CloudDatabaseUser, DatabaseUserRole, UserScope } from "../../common/atlas/openapi.js"; |
| 6 | + |
| 7 | +export class CreateDBUserTool extends AtlasToolBase { |
| 8 | + protected name = "atlas-create-db-user"; |
| 9 | + protected description = "Create an MongoDB Atlas user"; |
| 10 | + protected argsShape = { |
| 11 | + projectId: z.string().describe("Atlas project ID"), |
| 12 | + username: z.string().describe("Username for the new user"), |
| 13 | + password: z.string().describe("Password for the new user"), |
| 14 | + roles: z.array(z.object({ |
| 15 | + roleName: z.string().describe("Role name"), |
| 16 | + databaseName: z.string().describe("Database name").default("admin"), |
| 17 | + collectionName: z.string().describe("Collection name").optional(), |
| 18 | + })).describe("Roles for the new user"), |
| 19 | + clusters: z.array(z.string()).describe("Clusters to assign the user to, leave empty for access to all clusters").optional(), |
| 20 | + }; |
| 21 | + |
| 22 | + protected async execute({ projectId, username, password, roles, clusters }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { |
| 23 | + await this.ensureAuthenticated(); |
| 24 | + |
| 25 | + const input = { |
| 26 | + groupId: projectId, |
| 27 | + awsIAMType: "NONE", |
| 28 | + databaseName: "admin", |
| 29 | + ldapAuthType: "NONE", |
| 30 | + oidcAuthType: "NONE", |
| 31 | + x509Type: "NONE", |
| 32 | + username, |
| 33 | + password, |
| 34 | + roles: roles as unknown as DatabaseUserRole[], |
| 35 | + scopes: clusters?.length ? clusters.map(cluster => ({ |
| 36 | + type: "CLUSTER", |
| 37 | + name: cluster, |
| 38 | + })) : undefined, |
| 39 | + } as CloudDatabaseUser; |
| 40 | + |
| 41 | + await this.apiClient!.createDatabaseUser(projectId, input); |
| 42 | + |
| 43 | + return { |
| 44 | + content: [ |
| 45 | + { type: "text", text: `User "${username}" created sucessfully.` }, |
| 46 | + ], |
| 47 | + }; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function formatRoles(roles?: DatabaseUserRole[]) { |
| 52 | + if (!roles?.length) { |
| 53 | + return "N/A"; |
| 54 | + } |
| 55 | + return roles.map(role => `${role.roleName}@${role.databaseName}${role.collectionName ? `:${role.collectionName}` : ""}`).join(", "); |
| 56 | +} |
| 57 | + |
| 58 | +function formatScopes(scopes?: UserScope[]) { |
| 59 | + if (!scopes?.length) { |
| 60 | + return "All"; |
| 61 | + } |
| 62 | + return scopes.map(scope => `${scope.type}:${scope.name}`).join(", "); |
| 63 | +} |
0 commit comments