@@ -144,19 +144,23 @@ 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+ CAS_PARSER_API_KEY : requireValue (
149+ readEnv ( 'CAS_PARSER_API_KEY' ) ?? client . apiKey ,
150+ 'set CAS_PARSER_API_KEY environment variable or provide apiKey client option' ,
151+ ) ,
152+ CAS_PARSER_BASE_URL : readEnv ( 'CAS_PARSER_BASE_URL' ) ?? client . baseURL ?? undefined ,
153+ } ;
154+ // Merge any upstream client envs from the request header, with upstream values taking precedence.
155+ const mergedClientEnvs = { ...localClientEnvs , ...reqContext . upstreamClientEnvs } ;
156+
147157 // Setting a Stainless API key authenticates requests to the code tool endpoint.
148158 const res = await fetch ( codeModeEndpoint , {
149159 method : 'POST' ,
150160 headers : {
151161 ...( reqContext . stainlessApiKey && { Authorization : reqContext . stainlessApiKey } ) ,
152162 'Content-Type' : 'application/json' ,
153- 'x-stainless-mcp-client-envs' : JSON . stringify ( {
154- CAS_PARSER_API_KEY : requireValue (
155- readEnv ( 'CAS_PARSER_API_KEY' ) ?? client . apiKey ,
156- 'set CAS_PARSER_API_KEY environment variable or provide apiKey client option' ,
157- ) ,
158- CAS_PARSER_BASE_URL : readEnv ( 'CAS_PARSER_BASE_URL' ) ?? client . baseURL ?? undefined ,
159- } ) ,
163+ 'x-stainless-mcp-client-envs' : JSON . stringify ( mergedClientEnvs ) ,
160164 } ,
161165 body : JSON . stringify ( {
162166 project_name : 'cas-parser' ,
@@ -267,6 +271,9 @@ const localDenoHandler = async ({
267271 printOutput : true ,
268272 spawnOptions : {
269273 cwd : path . dirname ( workerPath ) ,
274+ // Merge any upstream client envs into the Deno subprocess environment,
275+ // with the upstream env vars taking precedence.
276+ env : { ...process . env , ...reqContext . upstreamClientEnvs } ,
270277 } ,
271278 } ) ;
272279
@@ -276,13 +283,17 @@ const localDenoHandler = async ({
276283 reject ( new Error ( `Worker exited with code ${ exitCode } ` ) ) ;
277284 } ) ;
278285
279- const opts : ClientOptions = {
280- baseURL : client . baseURL ,
281- apiKey : client . apiKey ,
282- defaultHeaders : {
283- 'X-Stainless-MCP' : 'true' ,
284- } ,
285- } ;
286+ // Strip null/undefined values so that the worker SDK client can fall back to
287+ // reading from environment variables (including any upstreamClientEnvs).
288+ const opts : ClientOptions = Object . fromEntries (
289+ Object . entries ( {
290+ baseURL : client . baseURL ,
291+ apiKey : client . apiKey ,
292+ defaultHeaders : {
293+ 'X-Stainless-MCP' : 'true' ,
294+ } ,
295+ } ) . filter ( ( [ _ , v ] ) => v != null ) ,
296+ ) as ClientOptions ;
286297
287298 const req = worker . request (
288299 'http://localhost' ,
0 commit comments