Skip to content

Commit f75297f

Browse files
committed
fix checks
1 parent 6dcf1fb commit f75297f

29 files changed

+121
-86
lines changed

scripts/apply.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ async function main() {
5858
const httpCode = parseInt(code, 10);
5959
if (httpCode >= 200 && httpCode < 300) {
6060
const response = operation.responses[code];
61-
const responseObject = findObjectFromRef(response, openapi);
62-
if (responseObject.content) {
61+
const responseObject = findObjectFromRef(response, openapi) as OpenAPIV3_1.ResponseObject;
62+
if (responseObject && responseObject.content) {
6363
for (const contentType in responseObject.content) {
6464
const content = responseObject.content[contentType];
65-
hasResponseBody = !!content.schema;
65+
hasResponseBody = !!content?.schema;
6666
}
6767
}
6868
}
@@ -84,7 +84,7 @@ async function main() {
8484
operationId: operation.operationId || "",
8585
requiredParams,
8686
hasResponseBody,
87-
tag: operation.tags[0],
87+
tag: operation.tags && operation.tags.length > 0 && operation.tags[0] ? operation.tags[0] : "",
8888
});
8989
}
9090
}

src/common/atlas/cluster.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export function formatCluster(cluster: ClusterDescription20240805): Cluster {
5050
};
5151
});
5252

53-
const instanceSize = (regionConfigs.length <= 0 ? undefined : regionConfigs[0].instanceSize) || "UNKNOWN";
53+
const instanceSize =
54+
regionConfigs.length > 0 && regionConfigs[0]?.instanceSize ? regionConfigs[0].instanceSize : "UNKNOWN";
5455

5556
const clusterInstanceType = instanceSize == "M0" ? "FREE" : "DEDICATED";
5657

src/tools/atlas/create/createProject.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ export class CreateProjectTool extends AtlasToolBase {
2828
"No organizations were found in your MongoDB Atlas account. Please create an organization first."
2929
);
3030
}
31-
organizationId = organizations.results[0].id;
31+
const firstOrg = organizations.results[0];
32+
if (!firstOrg?.id) {
33+
throw new Error(
34+
"The first organization found does not have an ID. Please check your Atlas account."
35+
);
36+
}
37+
organizationId = firstOrg.id;
3238
assumedOrg = true;
3339
} catch {
3440
throw new Error(

src/tools/atlas/read/listProjects.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export class ListProjectsTool extends AtlasToolBase {
2121

2222
const orgs: Record<string, string> = orgData.results
2323
.map((org) => [org.id || "", org.name])
24-
.reduce((acc, [id, name]) => ({ ...acc, [id]: name }), {});
24+
.filter(([id]) => id)
25+
.reduce((acc, [id, name]) => ({ ...acc, [id as string]: name }), {});
2526

2627
const data = orgId
2728
? await this.session.apiClient.listOrganizationProjects({
@@ -41,7 +42,8 @@ export class ListProjectsTool extends AtlasToolBase {
4142
const rows = data.results
4243
.map((project) => {
4344
const createdAt = project.created ? new Date(project.created).toLocaleString() : "N/A";
44-
return `${project.name} | ${project.id} | ${orgs[project.orgId]} | ${project.orgId} | ${createdAt}`;
45+
const orgName = project.orgId && orgs[project.orgId] ? orgs[project.orgId] : "N/A";
46+
return `${project.name} | ${project.id} | ${orgName} | ${project.orgId} | ${createdAt}`;
4547
})
4648
.join("\n");
4749
const formattedProjects = `Project Name | Project ID | Organization Name | Organization ID | Created At

src/tools/tool.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export abstract class ToolBase {
9393
this.update = (updates: { name?: string; description?: string; inputSchema?: AnyZodObject }) => {
9494
const tools = server["_registeredTools"] as { [toolName: string]: RegisteredTool };
9595
const existingTool = tools[this.name];
96+
if (!existingTool) {
97+
// Optionally, throw or log an error here
98+
return;
99+
}
96100
existingTool.annotations = this.annotations;
97101

98102
if (updates.name && updates.name !== this.name) {

tests/integration/tools/atlas/accessLists.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describeWithAtlas("ip access lists", (integration) => {
6767
})) as CallToolResult;
6868
expect(response.content).toBeArray();
6969
expect(response.content).toHaveLength(1);
70-
expect(response.content[0].text).toContain("IP/CIDR ranges added to access list");
70+
expect(response.content[0]?.text).toContain("IP/CIDR ranges added to access list");
7171
});
7272
});
7373

@@ -90,7 +90,7 @@ describeWithAtlas("ip access lists", (integration) => {
9090
expect(response.content).toBeArray();
9191
expect(response.content).toHaveLength(1);
9292
for (const value of values) {
93-
expect(response.content[0].text).toContain(value);
93+
expect(response.content[0]?.text).toContain(value);
9494
}
9595
});
9696
});

tests/integration/tools/atlas/alerts.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describeWithAtlas("alerts", (integration) => {
2323
expect(response.content).toBeArray();
2424
expect(response.content).toHaveLength(1);
2525

26-
const data = parseTable(response.content[0].text as string);
26+
const data = parseTable(response.content[0]?.text as string);
2727
expect(data).toBeArray();
2828

2929
// Since we can't guarantee alerts will exist, we just verify the table structure

tests/integration/tools/atlas/atlasHelpers.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ export function parseTable(text: string): Record<string, string>[] {
7575
.map((cells) => {
7676
const row: Record<string, string> = {};
7777
cells.forEach((cell, index) => {
78-
row[headers[index]] = cell;
78+
if (headers) {
79+
row[headers[index] ?? ""] = cell;
80+
}
7981
});
8082
return row;
8183
});
@@ -87,14 +89,14 @@ async function createProject(apiClient: ApiClient): Promise<Group> {
8789
const projectName: string = `testProj-` + randomId;
8890

8991
const orgs = await apiClient.listOrganizations();
90-
if (!orgs?.results?.length || !orgs.results[0].id) {
92+
if (!orgs?.results?.length || !orgs.results[0]?.id) {
9193
throw new Error("No orgs found");
9294
}
9395

9496
const group = await apiClient.createProject({
9597
body: {
9698
name: projectName,
97-
orgId: orgs.results[0].id,
99+
orgId: orgs.results[0]?.id ?? "",
98100
} as Group,
99101
});
100102

tests/integration/tools/atlas/clusters.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describeWithAtlas("clusters", (integration) => {
8888
})) as CallToolResult;
8989
expect(response.content).toBeArray();
9090
expect(response.content).toHaveLength(2);
91-
expect(response.content[0].text).toContain("has been created");
91+
expect(response.content[0]?.text).toContain("has been created");
9292
});
9393
});
9494

@@ -113,7 +113,7 @@ describeWithAtlas("clusters", (integration) => {
113113
})) as CallToolResult;
114114
expect(response.content).toBeArray();
115115
expect(response.content).toHaveLength(1);
116-
expect(response.content[0].text).toContain(`${clusterName} | `);
116+
expect(response.content[0]?.text).toContain(`${clusterName} | `);
117117
});
118118
});
119119

@@ -135,7 +135,7 @@ describeWithAtlas("clusters", (integration) => {
135135
.callTool({ name: "atlas-list-clusters", arguments: { projectId } })) as CallToolResult;
136136
expect(response.content).toBeArray();
137137
expect(response.content).toHaveLength(2);
138-
expect(response.content[1].text).toContain(`${clusterName} | `);
138+
expect(response.content[1]?.text).toContain(`${clusterName} | `);
139139
});
140140
});
141141

@@ -178,7 +178,7 @@ describeWithAtlas("clusters", (integration) => {
178178
})) as CallToolResult;
179179
expect(response.content).toBeArray();
180180
expect(response.content).toHaveLength(1);
181-
expect(response.content[0].text).toContain(`Connected to cluster "${clusterName}"`);
181+
expect(response.content[0]?.text).toContain(`Connected to cluster "${clusterName}"`);
182182
});
183183
});
184184
});

tests/integration/tools/atlas/dbUsers.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,18 @@ describeWithAtlas("db users", (integration) => {
6565

6666
const elements = getResponseElements(response);
6767
expect(elements).toHaveLength(1);
68-
expect(elements[0].text).toContain("created successfully");
69-
expect(elements[0].text).toContain(userName);
70-
expect(elements[0].text).not.toContain("testpassword");
68+
expect(elements[0]?.text).toContain("created successfully");
69+
expect(elements[0]?.text).toContain(userName);
70+
expect(elements[0]?.text).not.toContain("testpassword");
7171
});
7272

7373
it("should create a database user with generated password", async () => {
7474
const response = await createUserWithMCP();
7575
const elements = getResponseElements(response);
7676
expect(elements).toHaveLength(1);
77-
expect(elements[0].text).toContain("created successfully");
78-
expect(elements[0].text).toContain(userName);
79-
expect(elements[0].text).toContain("with password: `");
77+
expect(elements[0]?.text).toContain("created successfully");
78+
expect(elements[0]?.text).toContain(userName);
79+
expect(elements[0]?.text).toContain("with password: `");
8080
});
8181
});
8282
describe("atlas-list-db-users", () => {
@@ -98,7 +98,7 @@ describeWithAtlas("db users", (integration) => {
9898
.callTool({ name: "atlas-list-db-users", arguments: { projectId } })) as CallToolResult;
9999
expect(response.content).toBeArray();
100100
expect(response.content).toHaveLength(1);
101-
expect(response.content[0].text).toContain(userName);
101+
expect(response.content[0]?.text).toContain(userName);
102102
});
103103
});
104104
});

0 commit comments

Comments
 (0)