@@ -25,31 +25,35 @@ interface Props<T extends string> {
25
25
26
26
export async function bundler < T extends string > ( props : Props < T > ) : Promise < {
27
27
run : ( ) => void ;
28
- server : Server ;
28
+ createServer : ( envList : Record < string , string > ) => Promise < Server > ;
29
29
} > {
30
- const adapter = new Client ( {
31
- name : props . name ,
32
- version : props . version ,
33
- } ) ;
34
- await connectMcp ( {
35
- adapter,
36
- mcpServers : props . mcpServers ,
37
- } ) ;
38
-
39
- const server = createServer ( {
40
- name : props . name ,
41
- version : props . version ,
42
- adapter,
43
- } ) ;
44
30
return {
45
31
run : ( ) => {
46
32
buildCli ( {
47
33
name : props . name ,
48
34
version : props . version ,
49
- adapter,
35
+ mcpServers : props . mcpServers ,
36
+ env : props . env ,
50
37
} ) . parse ( process . argv ) ;
51
38
} ,
52
- server,
39
+ createServer : async ( envList : Record < string , string > ) => {
40
+ const adapter = new Client ( {
41
+ name : props . name ,
42
+ version : props . version ,
43
+ } ) ;
44
+ await connectMcp ( {
45
+ adapter,
46
+ mcpServers : props . mcpServers ,
47
+ envMapper : props . env ,
48
+ env : envList ,
49
+ } ) ;
50
+
51
+ return createServer ( {
52
+ name : props . name ,
53
+ version : props . version ,
54
+ adapter,
55
+ } ) ;
56
+ } ,
53
57
} ;
54
58
}
55
59
@@ -61,10 +65,14 @@ export async function bundler<T extends string>(props: Props<T>): Promise<{
61
65
export async function connectMcp ( props : {
62
66
adapter : Client ;
63
67
mcpServers : Record < string , McpConnection > ;
68
+ envMapper : Record < string , string > ;
69
+ env : Record < string , string > ;
64
70
} ) {
65
71
const transports = await Promise . all ( Object . entries ( props . mcpServers ) . map ( async ( [ name , setting ] ) => {
66
72
// InMemory
67
73
if ( setting instanceof Server ) {
74
+ const env = extractEnvFromName ( name , props . env , props . envMapper ) ;
75
+ Object . assign ( process . env , env ) ;
68
76
const [ clientTransport , serverTransport ] = InMemoryTransport . createLinkedPair ( ) ;
69
77
await setting . connect ( serverTransport ) ;
70
78
return clientTransport ;
@@ -77,7 +85,10 @@ export async function connectMcp(props: {
77
85
78
86
// Stdio
79
87
if ( "command" in setting ) {
80
- return new StdioClientTransport ( setting ) ;
88
+ return new StdioClientTransport ( {
89
+ ...setting ,
90
+ env : extractEnvFromName ( name , props . env , props . envMapper ) ,
91
+ } ) ;
81
92
}
82
93
83
94
setting satisfies never ;
@@ -88,3 +99,17 @@ export async function connectMcp(props: {
88
99
await props . adapter . connect ( transport ) ;
89
100
}
90
101
}
102
+
103
+ /**
104
+ * Extracts the environment variables from the name.
105
+ *
106
+ * @param name - The name of the MCP server.
107
+ * @param env - The environment variables. { ENV_KEY: "ENV_TARGET" }
108
+ * ENV_TARGET is the target environment variable name, so example: "slack", "notion", "github", etc.
109
+ * @param envMapper - The environment variable mapper. { ENV_KEY: "ENV_VALUE" }
110
+ * ENV_VALUE is the value of the environment variable, so example: "gh_12309wqje123", "nt_12309wqje123", etc.
111
+ * @returns The environment variables.
112
+ */
113
+ const extractEnvFromName = ( name : string , env : Record < string , string > , envMapper : Record < string , string > ) => {
114
+ return Object . fromEntries ( Object . entries ( env ) . filter ( ( [ , value ] ) => value === name ) . map ( ( [ key ] ) => [ key , envMapper [ key ] ] ) ) ;
115
+ }
0 commit comments