-
Notifications
You must be signed in to change notification settings - Fork 97
chore: Improve access list and connect experienece [MCP-5] #372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
3d079c6
43cf255
a34de86
46e2e5f
b373956
39cc667
ccb61ac
1dda709
cdb37b9
0f5cb6b
4389e6c
3c1d3fc
66ec84f
e2f0d21
25e2b58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { ApiClient } from "./apiClient.js"; | ||
import logger, { LogId } from "../logger.js"; | ||
import { ApiClientError } from "./apiClientError.js"; | ||
|
||
export async function makeCurrentIpAccessListEntry( | ||
apiClient: ApiClient, | ||
projectId: string, | ||
comment: string = "Added by Atlas MCP" | ||
) { | ||
const { currentIpv4Address } = await apiClient.getIpInfo(); | ||
return { | ||
groupId: projectId, | ||
ipAddress: currentIpv4Address, | ||
comment, | ||
}; | ||
} | ||
|
||
/** | ||
* Ensures the current public IP is in the access list for the given Atlas project. | ||
* If the IP is already present, this is a no-op. | ||
* @param apiClient The Atlas API client instance | ||
* @param projectId The Atlas project ID | ||
*/ | ||
export async function ensureCurrentIpInAccessList(apiClient: ApiClient, projectId: string): Promise<void> { | ||
// Get the current public IP | ||
const entry = await makeCurrentIpAccessListEntry(apiClient, projectId, "Added by MCP pre-run access list helper"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Is that comment helpful externally? If I was a user and saw that, I wouldn't know what it means. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated a bit more, but I think it's helpful to know where the new access list entry came from |
||
try { | ||
await apiClient.createProjectIpAccessList({ | ||
params: { path: { groupId: projectId } }, | ||
body: [entry], | ||
}); | ||
logger.debug( | ||
LogId.atlasIpAccessListAdded, | ||
"atlas-connect-cluster", | ||
`IP access list created: ${JSON.stringify(entry)}` | ||
); | ||
} catch (err) { | ||
if (err instanceof ApiClientError && err.response?.status === 409) { | ||
// 409 Conflict: entry already exists, ignore | ||
return; | ||
} | ||
throw err; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { z } from "zod"; | |
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; | ||
import { AtlasToolBase } from "../atlasTool.js"; | ||
import { ToolArgs, OperationType } from "../../tool.js"; | ||
import { makeCurrentIpAccessListEntry } from "../../../common/atlas/accessListUtils.js"; | ||
|
||
const DEFAULT_COMMENT = "Added by Atlas MCP"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is duplicated now - should we move it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, updated! |
||
|
||
|
@@ -38,12 +39,11 @@ export class CreateAccessListTool extends AtlasToolBase { | |
})); | ||
|
||
if (currentIpAddress) { | ||
const currentIp = await this.session.apiClient.getIpInfo(); | ||
const input = { | ||
groupId: projectId, | ||
ipAddress: currentIp.currentIpv4Address, | ||
comment: comment || DEFAULT_COMMENT, | ||
}; | ||
const input = await makeCurrentIpAccessListEntry( | ||
this.session.apiClient, | ||
projectId, | ||
comment || DEFAULT_COMMENT | ||
); | ||
ipInputs.push(input); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { describe, it, expect, vi } from "vitest"; | ||
import { ApiClient } from "../../src/common/atlas/apiClient.js"; | ||
import { ensureCurrentIpInAccessList } from "../../src/common/atlas/accessListUtils.js"; | ||
import { ApiClientError } from "../../src/common/atlas/apiClientError.js"; | ||
|
||
describe("accessListUtils", () => { | ||
it("should add the current IP to the access list", async () => { | ||
const apiClient = { | ||
getIpInfo: vi.fn().mockResolvedValue({ currentIpv4Address: "127.0.0.1" } as never), | ||
createProjectIpAccessList: vi.fn().mockResolvedValue(undefined as never), | ||
} as unknown as ApiClient; | ||
await ensureCurrentIpInAccessList(apiClient, "projectId"); | ||
expect(apiClient.createProjectIpAccessList).toHaveBeenCalledWith({ | ||
params: { path: { groupId: "projectId" } }, | ||
body: [ | ||
{ groupId: "projectId", ipAddress: "127.0.0.1", comment: "Added by MCP pre-run access list helper" }, | ||
], | ||
}); | ||
}); | ||
|
||
it("should not fail if the current IP is already in the access list", async () => { | ||
const apiClient = { | ||
getIpInfo: vi.fn().mockResolvedValue({ currentIpv4Address: "127.0.0.1" } as never), | ||
createProjectIpAccessList: vi | ||
.fn() | ||
.mockRejectedValue( | ||
ApiClientError.fromError( | ||
{ status: 409, statusText: "Conflict" } as Response, | ||
{ message: "Conflict" } as never | ||
) as never | ||
), | ||
} as unknown as ApiClient; | ||
await ensureCurrentIpInAccessList(apiClient, "projectId"); | ||
expect(apiClient.createProjectIpAccessList).toHaveBeenCalledWith({ | ||
params: { path: { groupId: "projectId" } }, | ||
body: [ | ||
{ groupId: "projectId", ipAddress: "127.0.0.1", comment: "Added by MCP pre-run access list helper" }, | ||
], | ||
}); | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.