11import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver" ;
2- import { ApiClient } from "./common/atlas/apiClient.js" ;
3- import defaultConfig from "./config.js" ;
2+ import { ApiClient , ApiClientCredentials } from "./common/atlas/apiClient.js" ;
43import { Implementation } from "@modelcontextprotocol/sdk/types.js" ;
5-
6- // Define the type for configuration used by Session
7- interface SessionConfig {
8- apiBaseUrl ?: string ;
9- apiClientId ?: string ;
10- apiClientSecret ?: string ;
11- [ key : string ] : unknown ;
12- }
4+ import config from "./config.js" ;
135
146export class Session {
157 sessionId ?: string ;
@@ -19,59 +11,31 @@ export class Session {
1911 name : string ;
2012 version : string ;
2113 } ;
22- private credentials ?: { clientId : string ; clientSecret : string } ;
23- private baseUrl : string ;
24- private readonly config : SessionConfig ;
25-
26- constructor ( config : SessionConfig = defaultConfig as SessionConfig ) {
27- this . config = config ;
28- this . baseUrl = this . config . apiBaseUrl ?? "https://cloud.mongodb.com/" ;
29-
30- // Store credentials if available
31- if ( this . config . apiClientId && this . config . apiClientSecret ) {
32- this . credentials = {
33- clientId : this . config . apiClientId ,
34- clientSecret : this . config . apiClientSecret ,
35- } ;
36-
37- // Initialize API client with credentials
38- this . apiClient = new ApiClient ( {
39- baseUrl : this . baseUrl ,
40- credentials : this . credentials ,
41- } ) ;
42- return ;
43- }
4414
45- // Initialize API client without credentials
46- this . apiClient = new ApiClient ( { baseUrl : this . baseUrl } ) ;
15+ constructor ( ) {
16+ const credentials : ApiClientCredentials | undefined =
17+ config . apiClientId && config . apiClientSecret
18+ ? {
19+ clientId : config . apiClientId ,
20+ clientSecret : config . apiClientSecret ,
21+ }
22+ : undefined ;
23+
24+ this . apiClient = new ApiClient ( {
25+ baseUrl : config . apiBaseUrl ,
26+ credentials,
27+ } ) ;
4728 }
4829
49- setAgentRunner ( agentClient : Implementation | undefined ) {
50- if ( agentClient ?. name && agentClient ?. version ) {
30+ setAgentRunner ( agentRunner : Implementation | undefined ) {
31+ if ( agentRunner ?. name && agentRunner ?. version ) {
5132 this . agentRunner = {
52- name : agentClient . name ,
53- version : agentClient . version ,
33+ name : agentRunner . name ,
34+ version : agentRunner . version ,
5435 } ;
5536 }
5637 }
5738
58- ensureAuthenticated ( ) : asserts this is { apiClient : ApiClient } {
59- if ( ! this . apiClient . hasCredentials ( ) ) {
60- if ( ! this . credentials ) {
61- throw new Error (
62- "Not authenticated make sure to configure MCP server with MDB_MCP_API_CLIENT_ID and MDB_MCP_API_CLIENT_SECRET environment variables."
63- ) ;
64- }
65-
66- // Reinitialize API client with the stored credentials
67- // This can happen if the server was configured without credentials but the env variables are later set
68- this . apiClient = new ApiClient ( {
69- baseUrl : this . baseUrl ,
70- credentials : this . credentials ,
71- } ) ;
72- }
73- }
74-
7539 async close ( ) : Promise < void > {
7640 if ( this . serviceProvider ) {
7741 try {
0 commit comments