Skip to content

Commit 16046d5

Browse files
committed
feat: add currentIp option atlas-create-access-list tool
1 parent c8f9257 commit 16046d5

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/common/atlas/apiClient.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,30 @@ export class ApiClient {
252252
}
253253
}
254254

255+
async getIpInfo() {
256+
await this.validateToken();
257+
258+
const endpoint = "api/private/ipinfo";
259+
const url = new URL(endpoint, config.apiBaseUrl);
260+
const response = await fetch(url, {
261+
method: "GET",
262+
headers: {
263+
Accept: "application/json",
264+
Authorization: `Bearer ${this.token?.access_token}`,
265+
"User-Agent": config.userAgent,
266+
},
267+
});
268+
269+
if (!response.ok) {
270+
throw await ApiClientError.fromResponse(response);
271+
}
272+
273+
const responseBody = await response.json();
274+
return responseBody as {
275+
currentIpv4Address: string;
276+
};
277+
}
278+
255279
async listProjects(options?: FetchOptions<operations["listProjects"]>) {
256280
const { data } = await this.client.GET(`/api/atlas/v2/groups`, options);
257281
return data;

src/tools/atlas/createAccessList.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class CreateAccessListTool extends AtlasToolBase {
1515
.describe("IP addresses to allow access from")
1616
.optional(),
1717
cidrBlocks: z.array(z.string().cidr()).describe("CIDR blocks to allow access from").optional(),
18+
currentIpAddress: z.boolean().describe("Add the current IP address").default(false),
1819
comment: z.string().describe("Comment for the access list entries").default(DEFAULT_COMMENT).optional(),
1920
};
2021

@@ -23,11 +24,12 @@ export class CreateAccessListTool extends AtlasToolBase {
2324
ipAddresses,
2425
cidrBlocks,
2526
comment,
27+
currentIpAddress,
2628
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
2729
await this.ensureAuthenticated();
2830

29-
if (!ipAddresses?.length && !cidrBlocks?.length) {
30-
throw new Error("Either ipAddresses or cidrBlocks must be provided.");
31+
if (!ipAddresses?.length && !cidrBlocks?.length && !currentIpAddress) {
32+
throw new Error("One of ipAddresses, cidrBlocks, currentIpAddress must be provided.");
3133
}
3234

3335
const ipInputs = (ipAddresses || []).map((ipAddress) => ({
@@ -36,6 +38,16 @@ export class CreateAccessListTool extends AtlasToolBase {
3638
comment: comment || DEFAULT_COMMENT,
3739
}));
3840

41+
if (currentIpAddress) {
42+
const currentIp = await this.apiClient!.getIpInfo();
43+
const input = {
44+
groupId: projectId,
45+
ipAddress: currentIp.currentIpv4Address,
46+
comment: comment || DEFAULT_COMMENT,
47+
};
48+
ipInputs.push(input);
49+
}
50+
3951
const cidrInputs = (cidrBlocks || []).map((cidrBlock) => ({
4052
groupId: projectId,
4153
cidrBlock,

src/tools/atlas/tools.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { CreateAccessListTool } from "./createAccessList.js";
1111
import { InspectAccessListTool } from "./inspectAccessList.js";
1212
import { ListDBUsersTool } from "./listDBUsers.js";
1313
import { CreateDBUserTool } from "./createDBUser.js";
14+
import { InspectCurrentIPTool } from "./inspectCurrentIP.js";
1415

1516
export function registerAtlasTools(server: McpServer, state: State, apiClient: ApiClient) {
1617
const tools: ToolBase[] = [
@@ -23,6 +24,7 @@ export function registerAtlasTools(server: McpServer, state: State, apiClient: A
2324
new InspectAccessListTool(state, apiClient),
2425
new ListDBUsersTool(state, apiClient),
2526
new CreateDBUserTool(state, apiClient),
27+
new InspectCurrentIPTool(state, apiClient),
2628
];
2729

2830
for (const tool of tools) {

0 commit comments

Comments
 (0)