@@ -148,19 +148,23 @@ const remoteStainlessHandler = async ({
148148
149149 const codeModeEndpoint = readEnv ( 'CODE_MODE_ENDPOINT_URL' ) ?? 'https://api.stainless.com/api/ai/code-tool' ;
150150
151+ const localClientEnvs = {
152+ ISAACUS_API_KEY : requireValue (
153+ readEnv ( 'ISAACUS_API_KEY' ) ?? client . apiKey ,
154+ 'set ISAACUS_API_KEY environment variable or provide apiKey client option' ,
155+ ) ,
156+ ISAACUS_BASE_URL : readEnv ( 'ISAACUS_BASE_URL' ) ?? client . baseURL ?? undefined ,
157+ } ;
158+ // Merge any upstream client envs from the request header, with upstream values taking precedence.
159+ const mergedClientEnvs = { ...localClientEnvs , ...reqContext . upstreamClientEnvs } ;
160+
151161 // Setting a Stainless API key authenticates requests to the code tool endpoint.
152162 const res = await fetch ( codeModeEndpoint , {
153163 method : 'POST' ,
154164 headers : {
155165 ...( reqContext . stainlessApiKey && { Authorization : reqContext . stainlessApiKey } ) ,
156166 'Content-Type' : 'application/json' ,
157- 'x-stainless-mcp-client-envs' : JSON . stringify ( {
158- ISAACUS_API_KEY : requireValue (
159- readEnv ( 'ISAACUS_API_KEY' ) ?? client . apiKey ,
160- 'set ISAACUS_API_KEY environment variable or provide apiKey client option' ,
161- ) ,
162- ISAACUS_BASE_URL : readEnv ( 'ISAACUS_BASE_URL' ) ?? client . baseURL ?? undefined ,
163- } ) ,
167+ 'x-stainless-mcp-client-envs' : JSON . stringify ( mergedClientEnvs ) ,
164168 } ,
165169 body : JSON . stringify ( {
166170 project_name : 'isaacus' ,
@@ -271,6 +275,9 @@ const localDenoHandler = async ({
271275 printOutput : true ,
272276 spawnOptions : {
273277 cwd : path . dirname ( workerPath ) ,
278+ // Merge any upstream client envs into the Deno subprocess environment,
279+ // with the upstream env vars taking precedence.
280+ env : { ...process . env , ...reqContext . upstreamClientEnvs } ,
274281 } ,
275282 } ) ;
276283
@@ -280,13 +287,17 @@ const localDenoHandler = async ({
280287 reject ( new Error ( `Worker exited with code ${ exitCode } ` ) ) ;
281288 } ) ;
282289
283- const opts : ClientOptions = {
284- baseURL : client . baseURL ,
285- apiKey : client . apiKey ,
286- defaultHeaders : {
287- 'X-Stainless-MCP' : 'true' ,
288- } ,
289- } ;
290+ // Strip null/undefined values so that the worker SDK client can fall back to
291+ // reading from environment variables (including any upstreamClientEnvs).
292+ const opts : ClientOptions = Object . fromEntries (
293+ Object . entries ( {
294+ baseURL : client . baseURL ,
295+ apiKey : client . apiKey ,
296+ defaultHeaders : {
297+ 'X-Stainless-MCP' : 'true' ,
298+ } ,
299+ } ) . filter ( ( [ _ , v ] ) => v != null ) ,
300+ ) as ClientOptions ;
290301
291302 const req = worker . request (
292303 'http://localhost' ,
0 commit comments