@@ -4,6 +4,7 @@ import * as readline from "node:readline/promises";
4
4
import { stdin , stdout } from "node:process" ;
5
5
import { z } from "zod" ;
6
6
import { PROVIDERS_OR_POLICIES } from "@huggingface/inference" ;
7
+ import type { ServerConfig } from "@huggingface/mcp-client" ;
7
8
import { Agent } from "@huggingface/mcp-client" ;
8
9
import { version as packageVersion } from "../package.json" ;
9
10
import { InputConfigSchema , ServerConfigSchema } from "./lib/types" ;
@@ -107,15 +108,15 @@ async function main() {
107
108
// Check env variables that will use this input
108
109
const inputVars = new Set < string > ( ) ;
109
110
for ( const server of config . servers ) {
110
- if ( server . type === "stdio" && server . config . env ) {
111
- for ( const [ key , value ] of Object . entries ( server . config . env ) ) {
111
+ if ( server . type === "stdio" && server . env ) {
112
+ for ( const [ key , value ] of Object . entries ( server . env ) ) {
112
113
if ( value === envSpecialValue ) {
113
114
inputVars . add ( key ) ;
114
115
}
115
116
}
116
117
}
117
- if ( ( server . type === "http" || server . type === "sse" ) && server . config . options ?. requestInit ?. headers ) {
118
- for ( const [ key , value ] of Object . entries ( server . config . options . requestInit . headers ) ) {
118
+ if ( ( server . type === "http" || server . type === "sse" ) && server . headers ) {
119
+ for ( const [ key , value ] of Object . entries ( server . headers ) ) {
119
120
if ( value . includes ( envSpecialValue ) ) {
120
121
inputVars . add ( key ) ;
121
122
}
@@ -132,54 +133,56 @@ async function main() {
132
133
}
133
134
134
135
// Prompt user for input
136
+ const envVariableKey = inputId . replaceAll ( "-" , "_" ) . toUpperCase ( ) ;
135
137
stdout . write ( ANSI . BLUE ) ;
136
138
stdout . write ( ` • ${ inputId } ` ) ;
137
139
stdout . write ( ANSI . RESET ) ;
138
- stdout . write ( `: ${ description } . (default: load from ${ Array . from ( inputVars ) . join ( ", " ) } ) ` ) ;
140
+ stdout . write ( `: ${ description } . (default: load from ${ envVariableKey } ) ` ) ;
141
+ stdout . write ( "\n" ) ;
139
142
140
143
const userInput = ( await rl . question ( "" ) ) . trim ( ) ;
141
144
142
145
// Inject user input (or env variable) into servers' env
143
146
for ( const server of config . servers ) {
144
- if ( server . type === "stdio" && server . config . env ) {
145
- for ( const [ key , value ] of Object . entries ( server . config . env ) ) {
147
+ if ( server . type === "stdio" && server . env ) {
148
+ for ( const [ key , value ] of Object . entries ( server . env ) ) {
146
149
if ( value === envSpecialValue ) {
147
150
if ( userInput ) {
148
- server . config . env [ key ] = userInput ;
151
+ server . env [ key ] = userInput ;
149
152
} else {
150
- const valueFromEnv = process . env [ key ] || "" ;
151
- server . config . env [ key ] = valueFromEnv ;
153
+ const valueFromEnv = process . env [ envVariableKey ] || "" ;
154
+ server . env [ key ] = valueFromEnv ;
152
155
if ( valueFromEnv ) {
153
156
stdout . write ( ANSI . GREEN ) ;
154
- stdout . write ( `Value successfully loaded from '${ key } '` ) ;
157
+ stdout . write ( `Value successfully loaded from '${ envVariableKey } '` ) ;
155
158
stdout . write ( ANSI . RESET ) ;
156
159
stdout . write ( "\n" ) ;
157
160
} else {
158
161
stdout . write ( ANSI . YELLOW ) ;
159
- stdout . write ( `No value found for '${ key } ' in environment variables. Continuing.` ) ;
162
+ stdout . write ( `No value found for '${ envVariableKey } ' in environment variables. Continuing.` ) ;
160
163
stdout . write ( ANSI . RESET ) ;
161
164
stdout . write ( "\n" ) ;
162
165
}
163
166
}
164
167
}
165
168
}
166
169
}
167
- if ( ( server . type === "http" || server . type === "sse" ) && server . config . options ?. requestInit ?. headers ) {
168
- for ( const [ key , value ] of Object . entries ( server . config . options . requestInit . headers ) ) {
170
+ if ( ( server . type === "http" || server . type === "sse" ) && server . headers ) {
171
+ for ( const [ key , value ] of Object . entries ( server . headers ) ) {
169
172
if ( value . includes ( envSpecialValue ) ) {
170
173
if ( userInput ) {
171
- server . config . options . requestInit . headers [ key ] = value . replace ( envSpecialValue , userInput ) ;
174
+ server . headers [ key ] = value . replace ( envSpecialValue , userInput ) ;
172
175
} else {
173
- const valueFromEnv = process . env [ key ] || "" ;
174
- server . config . options . requestInit . headers [ key ] = value . replace ( envSpecialValue , valueFromEnv ) ;
176
+ const valueFromEnv = process . env [ envVariableKey ] || "" ;
177
+ server . headers [ key ] = value . replace ( envSpecialValue , valueFromEnv ) ;
175
178
if ( valueFromEnv ) {
176
179
stdout . write ( ANSI . GREEN ) ;
177
- stdout . write ( `Value successfully loaded from '${ key } '` ) ;
180
+ stdout . write ( `Value successfully loaded from '${ envVariableKey } '` ) ;
178
181
stdout . write ( ANSI . RESET ) ;
179
182
stdout . write ( "\n" ) ;
180
183
} else {
181
184
stdout . write ( ANSI . YELLOW ) ;
182
- stdout . write ( `No value found for '${ key } ' in environment variables. Continuing.` ) ;
185
+ stdout . write ( `No value found for '${ envVariableKey } ' in environment variables. Continuing.` ) ;
183
186
stdout . write ( ANSI . RESET ) ;
184
187
stdout . write ( "\n" ) ;
185
188
}
@@ -194,21 +197,47 @@ async function main() {
194
197
rl . close ( ) ;
195
198
}
196
199
200
+ const formattedServers : ServerConfig [ ] = config . servers . map ( ( server ) => {
201
+ switch ( server . type ) {
202
+ case "stdio" :
203
+ return {
204
+ type : "stdio" ,
205
+ config : {
206
+ command : server . command ,
207
+ args : server . args ?? [ ] ,
208
+ env : server . env ?? { } ,
209
+ cwd : server . cwd ?? process . cwd ( ) ,
210
+ } ,
211
+ } ;
212
+ case "http" :
213
+ case "sse" :
214
+ return {
215
+ type : server . type ,
216
+ config : {
217
+ url : server . url ,
218
+ requestInit : {
219
+ headers : server . headers ,
220
+ } ,
221
+ } ,
222
+ } ;
223
+ }
224
+ } ) ;
225
+
197
226
const agent = new Agent (
198
227
config . endpointUrl
199
228
? {
200
229
endpointUrl : config . endpointUrl ,
201
230
model : config . model ,
202
231
apiKey : config . apiKey ?? process . env . API_KEY ?? process . env . HF_TOKEN ,
203
- servers : config . servers ,
232
+ servers : formattedServers ,
204
233
prompt,
205
234
}
206
235
: {
207
236
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
208
237
provider : config . provider ! ,
209
238
model : config . model ,
210
239
apiKey : config . apiKey ?? process . env . API_KEY ?? process . env . HF_TOKEN ,
211
- servers : config . servers ,
240
+ servers : formattedServers ,
212
241
prompt,
213
242
}
214
243
) ;
0 commit comments