@@ -142,19 +142,23 @@ const remoteStainlessHandler = async ({
142142
143143 const codeModeEndpoint = readEnv ( 'CODE_MODE_ENDPOINT_URL' ) ?? 'https://api.stainless.com/api/ai/code-tool' ;
144144
145+ const localClientEnvs = {
146+ HUB_API_KEY : requireValue (
147+ readEnv ( 'HUB_API_KEY' ) ?? client . apiKey ,
148+ 'set HUB_API_KEY environment variable or provide apiKey client option' ,
149+ ) ,
150+ FORMBRICKS_HUB_BASE_URL : readEnv ( 'FORMBRICKS_HUB_BASE_URL' ) ?? client . baseURL ?? undefined ,
151+ } ;
152+ // Merge any upstream client envs from the request header, with upstream values taking precedence.
153+ const mergedClientEnvs = { ...localClientEnvs , ...reqContext . upstreamClientEnvs } ;
154+
145155 // Setting a Stainless API key authenticates requests to the code tool endpoint.
146156 const res = await fetch ( codeModeEndpoint , {
147157 method : 'POST' ,
148158 headers : {
149159 ...( reqContext . stainlessApiKey && { Authorization : reqContext . stainlessApiKey } ) ,
150160 'Content-Type' : 'application/json' ,
151- 'x-stainless-mcp-client-envs' : JSON . stringify ( {
152- HUB_API_KEY : requireValue (
153- readEnv ( 'HUB_API_KEY' ) ?? client . apiKey ,
154- 'set HUB_API_KEY environment variable or provide apiKey client option' ,
155- ) ,
156- FORMBRICKS_HUB_BASE_URL : readEnv ( 'FORMBRICKS_HUB_BASE_URL' ) ?? client . baseURL ?? undefined ,
157- } ) ,
161+ 'x-stainless-mcp-client-envs' : JSON . stringify ( mergedClientEnvs ) ,
158162 } ,
159163 body : JSON . stringify ( {
160164 project_name : 'hub' ,
@@ -265,6 +269,9 @@ const localDenoHandler = async ({
265269 printOutput : true ,
266270 spawnOptions : {
267271 cwd : path . dirname ( workerPath ) ,
272+ // Merge any upstream client envs into the Deno subprocess environment,
273+ // with the upstream env vars taking precedence.
274+ env : { ...process . env , ...reqContext . upstreamClientEnvs } ,
268275 } ,
269276 } ) ;
270277
@@ -274,13 +281,17 @@ const localDenoHandler = async ({
274281 reject ( new Error ( `Worker exited with code ${ exitCode } ` ) ) ;
275282 } ) ;
276283
277- const opts : ClientOptions = {
278- baseURL : client . baseURL ,
279- apiKey : client . apiKey ,
280- defaultHeaders : {
281- 'X-Stainless-MCP' : 'true' ,
282- } ,
283- } ;
284+ // Strip null/undefined values so that the worker SDK client can fall back to
285+ // reading from environment variables (including any upstreamClientEnvs).
286+ const opts : ClientOptions = Object . fromEntries (
287+ Object . entries ( {
288+ baseURL : client . baseURL ,
289+ apiKey : client . apiKey ,
290+ defaultHeaders : {
291+ 'X-Stainless-MCP' : 'true' ,
292+ } ,
293+ } ) . filter ( ( [ _ , v ] ) => v != null ) ,
294+ ) as ClientOptions ;
284295
285296 const req = worker . request (
286297 'http://localhost' ,
0 commit comments