|
| 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 } 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 |
| 15 | + .array( |
| 16 | + z.object({ |
| 17 | + roleName: z.string().describe("Role name"), |
| 18 | + databaseName: z.string().describe("Database name").default("admin"), |
| 19 | + collectionName: z.string().describe("Collection name").optional(), |
| 20 | + }) |
| 21 | + ) |
| 22 | + .describe("Roles for the new user"), |
| 23 | + clusters: z |
| 24 | + .array(z.string()) |
| 25 | + .describe("Clusters to assign the user to, leave empty for access to all clusters") |
| 26 | + .optional(), |
| 27 | + }; |
| 28 | + |
| 29 | + protected async execute({ |
| 30 | + projectId, |
| 31 | + username, |
| 32 | + password, |
| 33 | + roles, |
| 34 | + clusters, |
| 35 | + }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { |
| 36 | + await this.ensureAuthenticated(); |
| 37 | + |
| 38 | + const input = { |
| 39 | + groupId: projectId, |
| 40 | + awsIAMType: "NONE", |
| 41 | + databaseName: "admin", |
| 42 | + ldapAuthType: "NONE", |
| 43 | + oidcAuthType: "NONE", |
| 44 | + x509Type: "NONE", |
| 45 | + username, |
| 46 | + password, |
| 47 | + roles: roles as unknown as DatabaseUserRole[], |
| 48 | + scopes: clusters?.length |
| 49 | + ? clusters.map((cluster) => ({ |
| 50 | + type: "CLUSTER", |
| 51 | + name: cluster, |
| 52 | + })) |
| 53 | + : undefined, |
| 54 | + } as CloudDatabaseUser; |
| 55 | + |
| 56 | + await this.apiClient!.createDatabaseUser(projectId, input); |
| 57 | + |
| 58 | + return { |
| 59 | + content: [{ type: "text", text: `User "${username}" created sucessfully.` }], |
| 60 | + }; |
| 61 | + } |
| 62 | +} |
0 commit comments