@@ -145,18 +145,22 @@ const remoteStainlessHandler = async ({
145145
146146 const codeModeEndpoint = readEnv ( 'CODE_MODE_ENDPOINT_URL' ) ?? 'https://api.stainless.com/api/ai/code-tool' ;
147147
148+ const localClientEnvs = {
149+ FINCH_CLIENT_ID : readEnv ( 'FINCH_CLIENT_ID' ) ?? client . clientID ?? undefined ,
150+ FINCH_CLIENT_SECRET : readEnv ( 'FINCH_CLIENT_SECRET' ) ?? client . clientSecret ?? undefined ,
151+ FINCH_WEBHOOK_SECRET : readEnv ( 'FINCH_WEBHOOK_SECRET' ) ?? client . webhookSecret ?? undefined ,
152+ FINCH_BASE_URL : readEnv ( 'FINCH_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+
148157 // Setting a Stainless API key authenticates requests to the code tool endpoint.
149158 const res = await fetch ( codeModeEndpoint , {
150159 method : 'POST' ,
151160 headers : {
152161 ...( reqContext . stainlessApiKey && { Authorization : reqContext . stainlessApiKey } ) ,
153162 'Content-Type' : 'application/json' ,
154- 'x-stainless-mcp-client-envs' : JSON . stringify ( {
155- FINCH_CLIENT_ID : readEnv ( 'FINCH_CLIENT_ID' ) ?? client . clientID ?? undefined ,
156- FINCH_CLIENT_SECRET : readEnv ( 'FINCH_CLIENT_SECRET' ) ?? client . clientSecret ?? undefined ,
157- FINCH_WEBHOOK_SECRET : readEnv ( 'FINCH_WEBHOOK_SECRET' ) ?? client . webhookSecret ?? undefined ,
158- FINCH_BASE_URL : readEnv ( 'FINCH_BASE_URL' ) ?? client . baseURL ?? undefined ,
159- } ) ,
163+ 'x-stainless-mcp-client-envs' : JSON . stringify ( mergedClientEnvs ) ,
160164 } ,
161165 body : JSON . stringify ( {
162166 project_name : 'finch' ,
@@ -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,16 +283,20 @@ const localDenoHandler = async ({
276283 reject ( new Error ( `Worker exited with code ${ exitCode } ` ) ) ;
277284 } ) ;
278285
279- const opts : ClientOptions = {
280- baseURL : client . baseURL ,
281- accessToken : client . accessToken ,
282- clientID : client . clientID ,
283- clientSecret : client . clientSecret ,
284- webhookSecret : client . webhookSecret ,
285- defaultHeaders : {
286- 'X-Stainless-MCP' : 'true' ,
287- } ,
288- } ;
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+ accessToken : client . accessToken ,
292+ clientID : client . clientID ,
293+ clientSecret : client . clientSecret ,
294+ webhookSecret : client . webhookSecret ,
295+ defaultHeaders : {
296+ 'X-Stainless-MCP' : 'true' ,
297+ } ,
298+ } ) . filter ( ( [ _ , v ] ) => v != null ) ,
299+ ) as ClientOptions ;
289300
290301 const req = worker . request (
291302 'http://localhost' ,
0 commit comments