Skip to content

Commit 66ec84f

Browse files
committed
address comments
1 parent 3c1d3fc commit 66ec84f

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

src/common/atlas/accessListUtils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { ApiClient } from "./apiClient.js";
22
import logger, { LogId } from "../logger.js";
33
import { ApiClientError } from "./apiClientError.js";
44

5+
export const DEFAULT_ACCESS_LIST_COMMENT = "Added by MongoDB MCP Server to enable tool access";
6+
57
export async function makeCurrentIpAccessListEntry(
68
apiClient: ApiClient,
79
projectId: string,
8-
comment: string = "Added by Atlas MCP"
10+
comment: string = DEFAULT_ACCESS_LIST_COMMENT
911
) {
1012
const { currentIpv4Address } = await apiClient.getIpInfo();
1113
return {
@@ -23,7 +25,7 @@ export async function makeCurrentIpAccessListEntry(
2325
*/
2426
export async function ensureCurrentIpInAccessList(apiClient: ApiClient, projectId: string): Promise<void> {
2527
// Get the current public IP
26-
const entry = await makeCurrentIpAccessListEntry(apiClient, projectId, "Added by MCP pre-run access list helper");
28+
const entry = await makeCurrentIpAccessListEntry(apiClient, projectId, DEFAULT_ACCESS_LIST_COMMENT);
2729
try {
2830
await apiClient.createProjectIpAccessList({
2931
params: { path: { groupId: projectId } },
@@ -36,7 +38,12 @@ export async function ensureCurrentIpInAccessList(apiClient: ApiClient, projectI
3638
);
3739
} catch (err) {
3840
if (err instanceof ApiClientError && err.response?.status === 409) {
39-
// 409 Conflict: entry already exists, ignore
41+
// 409 Conflict: entry already exists, log info
42+
logger.debug(
43+
LogId.atlasIpAccessListAdded,
44+
"accessListUtils",
45+
`IP address ${entry.ipAddress} is already present in the access list for project ${projectId}.`
46+
);
4047
return;
4148
}
4249
logger.debug(

src/tools/atlas/create/createAccessList.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { z } from "zod";
22
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
33
import { AtlasToolBase } from "../atlasTool.js";
44
import { ToolArgs, OperationType } from "../../tool.js";
5-
import { makeCurrentIpAccessListEntry } from "../../../common/atlas/accessListUtils.js";
6-
7-
const DEFAULT_COMMENT = "Added by Atlas MCP";
5+
import { makeCurrentIpAccessListEntry, DEFAULT_ACCESS_LIST_COMMENT } from "../../../common/atlas/accessListUtils.js";
86

97
export class CreateAccessListTool extends AtlasToolBase {
108
public name = "atlas-create-access-list";
@@ -18,7 +16,11 @@ export class CreateAccessListTool extends AtlasToolBase {
1816
.optional(),
1917
cidrBlocks: z.array(z.string().cidr()).describe("CIDR blocks to allow access from").optional(),
2018
currentIpAddress: z.boolean().describe("Add the current IP address").default(false),
21-
comment: z.string().describe("Comment for the access list entries").default(DEFAULT_COMMENT).optional(),
19+
comment: z
20+
.string()
21+
.describe("Comment for the access list entries")
22+
.default(DEFAULT_ACCESS_LIST_COMMENT)
23+
.optional(),
2224
};
2325

2426
protected async execute({
@@ -35,22 +37,22 @@ export class CreateAccessListTool extends AtlasToolBase {
3537
const ipInputs = (ipAddresses || []).map((ipAddress) => ({
3638
groupId: projectId,
3739
ipAddress,
38-
comment: comment || DEFAULT_COMMENT,
40+
comment: comment || DEFAULT_ACCESS_LIST_COMMENT,
3941
}));
4042

4143
if (currentIpAddress) {
4244
const input = await makeCurrentIpAccessListEntry(
4345
this.session.apiClient,
4446
projectId,
45-
comment || DEFAULT_COMMENT
47+
comment || DEFAULT_ACCESS_LIST_COMMENT
4648
);
4749
ipInputs.push(input);
4850
}
4951

5052
const cidrInputs = (cidrBlocks || []).map((cidrBlock) => ({
5153
groupId: projectId,
5254
cidrBlock,
53-
comment: comment || DEFAULT_COMMENT,
55+
comment: comment || DEFAULT_ACCESS_LIST_COMMENT,
5456
}));
5557

5658
const inputs = [...ipInputs, ...cidrInputs];

tests/unit/accessListUtils.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it, expect, vi } from "vitest";
22
import { ApiClient } from "../../src/common/atlas/apiClient.js";
3-
import { ensureCurrentIpInAccessList } from "../../src/common/atlas/accessListUtils.js";
3+
import { ensureCurrentIpInAccessList, DEFAULT_ACCESS_LIST_COMMENT } from "../../src/common/atlas/accessListUtils.js";
44
import { ApiClientError } from "../../src/common/atlas/apiClientError.js";
55

66
describe("accessListUtils", () => {
@@ -13,9 +13,7 @@ describe("accessListUtils", () => {
1313
// eslint-disable-next-line @typescript-eslint/unbound-method
1414
expect(apiClient.createProjectIpAccessList).toHaveBeenCalledWith({
1515
params: { path: { groupId: "projectId" } },
16-
body: [
17-
{ groupId: "projectId", ipAddress: "127.0.0.1", comment: "Added by MCP pre-run access list helper" },
18-
],
16+
body: [{ groupId: "projectId", ipAddress: "127.0.0.1", comment: DEFAULT_ACCESS_LIST_COMMENT }],
1917
});
2018
});
2119

@@ -35,9 +33,7 @@ describe("accessListUtils", () => {
3533
// eslint-disable-next-line @typescript-eslint/unbound-method
3634
expect(apiClient.createProjectIpAccessList).toHaveBeenCalledWith({
3735
params: { path: { groupId: "projectId" } },
38-
body: [
39-
{ groupId: "projectId", ipAddress: "127.0.0.1", comment: "Added by MCP pre-run access list helper" },
40-
],
36+
body: [{ groupId: "projectId", ipAddress: "127.0.0.1", comment: DEFAULT_ACCESS_LIST_COMMENT }],
4137
});
4238
});
4339
});

0 commit comments

Comments
 (0)