Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Tests",
"runtimeExecutable": "npm",
"runtimeArgs": ["test"],
"cwd": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "launch",
Expand Down
29 changes: 24 additions & 5 deletions src/common/atlas/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ApiClientError } from "./apiClientError.js";
import { paths, operations } from "./openapi.js";
import { CommonProperties, TelemetryEvent } from "../../telemetry/types.js";
import { packageInfo } from "../../helpers/packageInfo.js";
import logger, { LogId } from "../../logger.js";

const ATLAS_API_VERSION = "2025-03-12";

Expand Down Expand Up @@ -34,9 +35,7 @@ export class ApiClient {

private getAccessToken = async () => {
if (this.oauth2Client && (!this.accessToken || this.accessToken.expired())) {
this.accessToken = await this.oauth2Client.getToken({
agent: this.options.userAgent,
});
this.accessToken = await this.oauth2Client.getToken({});
}
return this.accessToken?.token.access_token as string | undefined;
};
Expand All @@ -49,7 +48,9 @@ export class ApiClient {

try {
const accessToken = await this.getAccessToken();
request.headers.set("Authorization", `Bearer ${accessToken}`);
if (accessToken) {
request.headers.set("Authorization", `Bearer ${accessToken}`);
}
return request;
} catch {
// ignore not availble tokens, API will return 401
Expand Down Expand Up @@ -81,20 +82,38 @@ export class ApiClient {
auth: {
tokenHost: this.options.baseUrl,
tokenPath: "/api/oauth/token",
revokePath: "/api/oauth/revoke",
},
http: {
headers: {
"User-Agent": this.options.userAgent,
},
},
});
this.client.use(this.authMiddleware);
}
}

public hasCredentials(): boolean {
return !!(this.oauth2Client && this.accessToken);
return !!this.oauth2Client;
}

public async validateAccessToken(): Promise<void> {
await this.getAccessToken();
}

public async close(): Promise<void> {
if (this.accessToken) {
try {
await this.accessToken.revoke("access_token");
} catch (error: unknown) {
const err = error instanceof Error ? error : new Error(String(error));
logger.error(LogId.atlasApiRevokeFailure, "apiClient", `Failed to revoke access token: ${err.message}`);
}
this.accessToken = undefined;
}
}

public async getIpInfo(): Promise<{
currentIpv4Address: string;
}> {
Expand Down
1 change: 1 addition & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const LogId = {
atlasInspectFailure: mongoLogId(1_001_004),
atlasConnectAttempt: mongoLogId(1_001_005),
atlasConnectSucceeded: mongoLogId(1_001_006),
atlasApiRevokeFailure: mongoLogId(1_001_007),

telemetryDisabled: mongoLogId(1_002_001),
telemetryEmitFailure: mongoLogId(1_002_002),
Expand Down
1 change: 1 addition & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class Session extends EventEmitter<{

async close(): Promise<void> {
await this.disconnect();
await this.apiClient.close();
this.emit("close");
}

Expand Down
2 changes: 1 addition & 1 deletion src/tools/mongodb/metadata/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ConnectTool extends MongoDBToolBase {

constructor(session: Session, config: UserConfig, telemetry: Telemetry) {
super(session, config, telemetry);
session.on("close", () => {
session.on("disconnect", () => {
this.updateMetadata();
});
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export function setupIntegrationTest(getUserConfig: () => UserConfig): Integrati
});

afterEach(async () => {
if (mcpServer) {
await mcpServer.session.close();
if (mcpServer && !mcpServer.session.connectedAtlasCluster) {
await mcpServer.session.disconnect();
}
});

Expand Down
Loading