@@ -144,22 +144,25 @@ const remoteStainlessHandler = async ({
144144
145145 const codeModeEndpoint = readEnv ( 'CODE_MODE_ENDPOINT_URL' ) ?? 'https://api.stainless.com/api/ai/code-tool' ;
146146
147+ const localClientEnvs = {
148+ IMAGEKIT_PRIVATE_KEY : requireValue (
149+ readEnv ( 'IMAGEKIT_PRIVATE_KEY' ) ?? client . privateKey ,
150+ 'set IMAGEKIT_PRIVATE_KEY environment variable or provide privateKey client option' ,
151+ ) ,
152+ OPTIONAL_IMAGEKIT_IGNORES_THIS : readEnv ( 'OPTIONAL_IMAGEKIT_IGNORES_THIS' ) ?? client . password ?? undefined ,
153+ IMAGEKIT_WEBHOOK_SECRET : readEnv ( 'IMAGEKIT_WEBHOOK_SECRET' ) ?? client . webhookSecret ?? undefined ,
154+ IMAGE_KIT_BASE_URL : readEnv ( 'IMAGE_KIT_BASE_URL' ) ?? client . baseURL ?? undefined ,
155+ } ;
156+ // Merge any upstream client envs from the request header, with upstream values taking precedence.
157+ const mergedClientEnvs = { ...localClientEnvs , ...reqContext . upstreamClientEnvs } ;
158+
147159 // Setting a Stainless API key authenticates requests to the code tool endpoint.
148160 const res = await fetch ( codeModeEndpoint , {
149161 method : 'POST' ,
150162 headers : {
151163 ...( reqContext . stainlessApiKey && { Authorization : reqContext . stainlessApiKey } ) ,
152164 'Content-Type' : 'application/json' ,
153- 'x-stainless-mcp-client-envs' : JSON . stringify ( {
154- IMAGEKIT_PRIVATE_KEY : requireValue (
155- readEnv ( 'IMAGEKIT_PRIVATE_KEY' ) ?? client . privateKey ,
156- 'set IMAGEKIT_PRIVATE_KEY environment variable or provide privateKey client option' ,
157- ) ,
158- OPTIONAL_IMAGEKIT_IGNORES_THIS :
159- readEnv ( 'OPTIONAL_IMAGEKIT_IGNORES_THIS' ) ?? client . password ?? undefined ,
160- IMAGEKIT_WEBHOOK_SECRET : readEnv ( 'IMAGEKIT_WEBHOOK_SECRET' ) ?? client . webhookSecret ?? undefined ,
161- IMAGE_KIT_BASE_URL : readEnv ( 'IMAGE_KIT_BASE_URL' ) ?? client . baseURL ?? undefined ,
162- } ) ,
165+ 'x-stainless-mcp-client-envs' : JSON . stringify ( mergedClientEnvs ) ,
163166 } ,
164167 body : JSON . stringify ( {
165168 project_name : 'imagekit' ,
@@ -270,6 +273,9 @@ const localDenoHandler = async ({
270273 printOutput : true ,
271274 spawnOptions : {
272275 cwd : path . dirname ( workerPath ) ,
276+ // Merge any upstream client envs into the Deno subprocess environment,
277+ // with the upstream env vars taking precedence.
278+ env : { ...process . env , ...reqContext . upstreamClientEnvs } ,
273279 } ,
274280 } ) ;
275281
@@ -279,15 +285,19 @@ const localDenoHandler = async ({
279285 reject ( new Error ( `Worker exited with code ${ exitCode } ` ) ) ;
280286 } ) ;
281287
282- const opts : ClientOptions = {
283- baseURL : client . baseURL ,
284- privateKey : client . privateKey ,
285- password : client . password ,
286- webhookSecret : client . webhookSecret ,
287- defaultHeaders : {
288- 'X-Stainless-MCP' : 'true' ,
289- } ,
290- } ;
288+ // Strip null/undefined values so that the worker SDK client can fall back to
289+ // reading from environment variables (including any upstreamClientEnvs).
290+ const opts : ClientOptions = Object . fromEntries (
291+ Object . entries ( {
292+ baseURL : client . baseURL ,
293+ privateKey : client . privateKey ,
294+ password : client . password ,
295+ webhookSecret : client . webhookSecret ,
296+ defaultHeaders : {
297+ 'X-Stainless-MCP' : 'true' ,
298+ } ,
299+ } ) . filter ( ( [ _ , v ] ) => v != null ) ,
300+ ) as ClientOptions ;
291301
292302 const req = worker . request (
293303 'http://localhost' ,
0 commit comments