@@ -21,13 +21,20 @@ import { defaultCreateAtlasLocalClient } from "../common/atlasLocal.js";
2121import type { Client } from "@mongodb-js/atlas-local" ;
2222import { VectorSearchEmbeddingsManager } from "../common/search/vectorSearchEmbeddingsManager.js" ;
2323
24+ type CreateSessionConfigFn = ( userConfig : UserConfig ) => Promise < UserConfig > ;
25+
2426export type TransportRunnerConfig = {
2527 userConfig : UserConfig ;
2628 createConnectionManager ?: ConnectionManagerFactoryFn ;
2729 connectionErrorHandler ?: ConnectionErrorHandler ;
2830 createAtlasLocalClient ?: AtlasLocalClientFactoryFn ;
2931 additionalLoggers ?: LoggerBase [ ] ;
3032 telemetryProperties ?: Partial < CommonProperties > ;
33+ /**
34+ * Hook which allows library consumers to fetch configuration from external sources (e.g., secrets managers, APIs)
35+ * or modify the existing configuration before the session is created.
36+ */
37+ createSessionConfig ?: CreateSessionConfigFn ;
3138} ;
3239
3340export abstract class TransportRunnerBase {
@@ -38,6 +45,7 @@ export abstract class TransportRunnerBase {
3845 private readonly connectionErrorHandler : ConnectionErrorHandler ;
3946 private readonly atlasLocalClient : Promise < Client | undefined > ;
4047 private readonly telemetryProperties : Partial < CommonProperties > ;
48+ private readonly createSessionConfig ?: CreateSessionConfigFn ;
4149
4250 protected constructor ( {
4351 userConfig,
@@ -46,12 +54,14 @@ export abstract class TransportRunnerBase {
4654 createAtlasLocalClient = defaultCreateAtlasLocalClient ,
4755 additionalLoggers = [ ] ,
4856 telemetryProperties = { } ,
57+ createSessionConfig,
4958 } : TransportRunnerConfig ) {
5059 this . userConfig = userConfig ;
5160 this . createConnectionManager = createConnectionManager ;
5261 this . connectionErrorHandler = connectionErrorHandler ;
5362 this . atlasLocalClient = createAtlasLocalClient ( ) ;
5463 this . telemetryProperties = telemetryProperties ;
64+ this . createSessionConfig = createSessionConfig ;
5565 const loggers : LoggerBase [ ] = [ ...additionalLoggers ] ;
5666 if ( this . userConfig . loggers . includes ( "stderr" ) ) {
5767 loggers . push ( new ConsoleLogger ( Keychain . root ) ) ;
@@ -76,32 +86,36 @@ export abstract class TransportRunnerBase {
7686 }
7787
7888 protected async setupServer ( ) : Promise < Server > {
89+ // Call the config provider hook if provided, allowing consumers to
90+ // fetch or modify configuration before session initialization
91+ const userConfig = this . createSessionConfig ? await this . createSessionConfig ( this . userConfig ) : this . userConfig ;
92+
7993 const mcpServer = new McpServer ( {
8094 name : packageInfo . mcpServerName ,
8195 version : packageInfo . version ,
8296 } ) ;
8397
8498 const logger = new CompositeLogger ( this . logger ) ;
85- const exportsManager = ExportsManager . init ( this . userConfig , logger ) ;
99+ const exportsManager = ExportsManager . init ( userConfig , logger ) ;
86100 const connectionManager = await this . createConnectionManager ( {
87101 logger,
88- userConfig : this . userConfig ,
102+ userConfig,
89103 deviceId : this . deviceId ,
90104 } ) ;
91105
92106 const session = new Session ( {
93- apiBaseUrl : this . userConfig . apiBaseUrl ,
94- apiClientId : this . userConfig . apiClientId ,
95- apiClientSecret : this . userConfig . apiClientSecret ,
107+ apiBaseUrl : userConfig . apiBaseUrl ,
108+ apiClientId : userConfig . apiClientId ,
109+ apiClientSecret : userConfig . apiClientSecret ,
96110 atlasLocalClient : await this . atlasLocalClient ,
97111 logger,
98112 exportsManager,
99113 connectionManager,
100114 keychain : Keychain . root ,
101- vectorSearchEmbeddingsManager : new VectorSearchEmbeddingsManager ( this . userConfig , connectionManager ) ,
115+ vectorSearchEmbeddingsManager : new VectorSearchEmbeddingsManager ( userConfig , connectionManager ) ,
102116 } ) ;
103117
104- const telemetry = Telemetry . create ( session , this . userConfig , this . deviceId , {
118+ const telemetry = Telemetry . create ( session , userConfig , this . deviceId , {
105119 commonProperties : this . telemetryProperties ,
106120 } ) ;
107121
@@ -111,14 +125,14 @@ export abstract class TransportRunnerBase {
111125 mcpServer,
112126 session,
113127 telemetry,
114- userConfig : this . userConfig ,
128+ userConfig,
115129 connectionErrorHandler : this . connectionErrorHandler ,
116130 elicitation,
117131 } ) ;
118132
119133 // We need to create the MCP logger after the server is constructed
120134 // because it needs the server instance
121- if ( this . userConfig . loggers . includes ( "mcp" ) ) {
135+ if ( userConfig . loggers . includes ( "mcp" ) ) {
122136 logger . addLogger ( new McpLogger ( result , Keychain . root ) ) ;
123137 }
124138
0 commit comments