Skip to content

Commit 754ce8d

Browse files
committed
address comment: make dynamic config part of session
1 parent 23fff05 commit 754ce8d

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/session.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,29 @@ export class Session {
99
apiClient: ApiClient;
1010
agentClientName?: string;
1111
agentClientVersion?: string;
12+
private credentials?: { clientId: string; clientSecret: string };
13+
private baseUrl: string;
1214

1315
constructor() {
14-
// Initialize API client with credentials if available
16+
this.baseUrl = config.apiBaseUrl ?? "https://cloud.mongodb.com/";
17+
18+
// Store credentials if available
1519
if (config.apiClientId && config.apiClientSecret) {
20+
this.credentials = {
21+
clientId: config.apiClientId,
22+
clientSecret: config.apiClientSecret,
23+
};
24+
25+
// Initialize API client with credentials
1626
this.apiClient = new ApiClient({
17-
baseUrl: config.apiBaseUrl,
18-
credentials: {
19-
clientId: config.apiClientId,
20-
clientSecret: config.apiClientSecret,
21-
},
27+
baseUrl: this.baseUrl,
28+
credentials: this.credentials,
2229
});
2330
return;
2431
}
2532

2633
// Initialize API client without credentials
27-
this.apiClient = new ApiClient({ baseUrl: config.apiBaseUrl });
34+
this.apiClient = new ApiClient({ baseUrl: this.baseUrl });
2835
}
2936

3037
setAgentClientData(agentClient: Implementation | undefined) {
@@ -33,20 +40,18 @@ export class Session {
3340
}
3441

3542
ensureAuthenticated(): asserts this is { apiClient: ApiClient } {
36-
if (!this.apiClient || !this.apiClient.hasCredentials()) {
37-
if (!config.apiClientId || !config.apiClientSecret) {
43+
if (!this.apiClient.hasCredentials()) {
44+
if (!this.credentials) {
3845
throw new Error(
3946
"Not authenticated make sure to configure MCP server with MDB_MCP_API_CLIENT_ID and MDB_MCP_API_CLIENT_SECRET environment variables."
4047
);
4148
}
4249

43-
// Initialize or reinitialize API client with credentials
50+
// Reinitialize API client with the stored credentials
51+
// This can happen if the server was configured without credentials but the env variables are later set
4452
this.apiClient = new ApiClient({
45-
baseUrl: config.apiBaseUrl,
46-
credentials: {
47-
clientId: config.apiClientId,
48-
clientSecret: config.apiClientSecret,
49-
},
53+
baseUrl: this.baseUrl,
54+
credentials: this.credentials,
5055
});
5156
}
5257
}

0 commit comments

Comments
 (0)