@@ -314,6 +314,19 @@ def mcp_client_params(available_toolboxes: dict, requested_toolboxes: list):
314314 if headers and isinstance (headers , dict ):
315315 for k , v in headers .items ():
316316 headers [k ] = swap_env (v )
317+ optional_headers = available_toolboxes [tb ]['server_params' ].get ('optional_headers' )
318+ # support {{ env SOMETHING }} for header values as well for e.g. tokens
319+ if optional_headers and isinstance (optional_headers , dict ):
320+ for k , v in dict (optional_headers ).items ():
321+ try :
322+ optional_headers [k ] = swap_env (v )
323+ except LookupError as e :
324+ del optional_headers [k ]
325+ if isinstance (headers , dict ):
326+ if isinstance (optional_headers , dict ):
327+ headers .update (optional_headers )
328+ elif isinstance (optional_headers , dict ):
329+ headers = optional_headers
317330 # if None will default to float(5) in client code
318331 timeout = available_toolboxes [tb ]['server_params' ].get ('timeout' )
319332 server_params ['url' ] = available_toolboxes [tb ]['server_params' ].get ('url' )
@@ -329,6 +342,19 @@ def mcp_client_params(available_toolboxes: dict, requested_toolboxes: list):
329342 if headers and isinstance (headers , dict ):
330343 for k , v in headers .items ():
331344 headers [k ] = swap_env (v )
345+ optional_headers = available_toolboxes [tb ]['server_params' ].get ('optional_headers' )
346+ # support {{ env SOMETHING }} for header values as well for e.g. tokens
347+ if optional_headers and isinstance (optional_headers , dict ):
348+ for k , v in dict (optional_headers ).items ():
349+ try :
350+ optional_headers [k ] = swap_env (v )
351+ except LookupError as e :
352+ del optional_headers [k ]
353+ if isinstance (headers , dict ):
354+ if isinstance (optional_headers , dict ):
355+ headers .update (optional_headers )
356+ elif isinstance (optional_headers , dict ):
357+ headers = optional_headers
332358 # if None will default to float(5) in client code
333359 timeout = available_toolboxes [tb ]['server_params' ].get ('timeout' )
334360 server_params ['url' ] = available_toolboxes [tb ]['server_params' ].get ('url' )
0 commit comments