-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathlistProjects.ts
More file actions
63 lines (56 loc) · 2.34 KB
/
listProjects.ts
File metadata and controls
63 lines (56 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { AtlasToolBase } from "../atlasTool.js";
import type { OperationType } from "../../tool.js";
import { formatUntrustedData } from "../../tool.js";
import type { ToolArgs } from "../../tool.js";
import { AtlasArgs } from "../../args.js";
export class ListProjectsTool extends AtlasToolBase {
public name = "atlas-list-projects";
protected description = "List MongoDB Atlas projects";
static operationType: OperationType = "read";
protected argsShape = {
orgId: AtlasArgs.organizationId()
.describe("Atlas organization ID to filter projects. If not provided, projects for all orgs are returned.")
.optional(),
};
protected async execute({ orgId }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
const orgData = await this.session.apiClient.listOrgs();
if (!orgData?.results?.length) {
return {
content: [{ type: "text", text: "No organizations found in your MongoDB Atlas account." }],
};
}
const orgs: Record<string, string> = orgData.results
.filter((org) => org.id)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.reduce((acc, org) => ({ ...acc, [org.id!]: org.name }), {});
const data = orgId
? await this.session.apiClient.getOrgGroups({
params: {
path: {
orgId,
},
},
})
: await this.session.apiClient.listGroups();
if (!data?.results?.length) {
return {
content: [{ type: "text", text: `No projects found in organization ${orgId}.` }],
};
}
const serializedProjects = JSON.stringify(
data.results.map((project) => ({
name: project.name,
id: project.id,
orgId: project.orgId,
orgName: orgs[project.orgId] ?? "N/A",
created: project.created ? new Date(project.created).toLocaleString() : "N/A",
})),
null,
2
);
return {
content: formatUntrustedData(`Found ${data.results.length} projects`, serializedProjects),
};
}
}