22import os
33import signal
44import traceback
5- from typing import Any , Dict , List , Optional
5+ from typing import Optional
66
77import typer
88from rich import print
@@ -40,8 +40,8 @@ async def run_agent(
4040
4141 config , prompt = _load_agent_config (agent_path )
4242
43- inputs : List [ Dict [ str , Any ]] = config .get ("inputs" , [])
44- servers : List [ Dict [ str , Any ]] = config .get ("servers" , [])
43+ inputs = config .get ("inputs" , [])
44+ servers = config .get ("servers" , [])
4545
4646 abort_event = asyncio .Event ()
4747 exit_event = asyncio .Event ()
@@ -82,38 +82,45 @@ def _sigint_handler() -> None:
8282 env_special_value = "${input:" + input_id + "}" # Special value to indicate env variable injection
8383
8484 # Check env variables that will use this input
85- input_vars = list (
86- {
87- key
88- for server in servers
89- for key , value in server .get ("config" , {}).get ("env" , {}).items ()
90- if value == env_special_value
91- }
92- )
85+ input_vars = set ()
86+ for server in servers :
87+ # Check stdio's "env" and http/sse's "headers" mappings
88+ env_or_headers = (
89+ server ["config" ].get ("env" , {})
90+ if server ["type" ] == "stdio"
91+ else server ["config" ].get ("options" , {}).get ("requestInit" , {}).get ("headers" , {})
92+ )
93+ for key , value in env_or_headers .items ():
94+ if env_special_value in value :
95+ input_vars .add (key )
9396
9497 if not input_vars :
9598 print (f"[yellow]Input { input_id } defined in config but not used by any server.[/yellow]" )
9699 continue
97100
98101 # Prompt user for input
99102 print (
100- f"[blue] • { input_id } [/blue]: { description } . (default: load from { ', ' .join (input_vars )} )." ,
103+ f"[blue] • { input_id } [/blue]: { description } . (default: load from { ', ' .join (sorted ( input_vars ) )} )." ,
101104 end = " " ,
102105 )
103106 user_input = (await _async_prompt (exit_event = exit_event )).strip ()
104107 if exit_event .is_set ():
105108 return
106109
107- # Inject user input (or env variable) into servers' env
110+ # Inject user input (or env variable) into stdio's env or http/sse's headers
108111 for server in servers :
109- env = server .get ("config" , {}).get ("env" , {})
110- for key , value in env .items ():
111- if value == env_special_value :
112+ env_or_headers = (
113+ server ["config" ].get ("env" , {})
114+ if server ["type" ] == "stdio"
115+ else server ["config" ].get ("options" , {}).get ("requestInit" , {}).get ("headers" , {})
116+ )
117+ for key , value in env_or_headers .items ():
118+ if env_special_value in value :
112119 if user_input :
113- env [key ] = user_input
120+ env_or_headers [key ] = env_or_headers [ key ]. replace ( env_special_value , user_input )
114121 else :
115122 value_from_env = os .getenv (key , "" )
116- env [key ] = value_from_env
123+ env_or_headers [key ] = env_or_headers [ key ]. replace ( env_special_value , value_from_env )
117124 if value_from_env :
118125 print (f"[green]Value successfully loaded from '{ key } '[/green]" )
119126 else :
@@ -125,10 +132,10 @@ def _sigint_handler() -> None:
125132
126133 # Main agent loop
127134 async with Agent (
128- provider = config .get ("provider" ),
135+ provider = config .get ("provider" ), # type: ignore[arg-type]
129136 model = config .get ("model" ),
130- base_url = config .get ("endpointUrl" ),
131- servers = servers ,
137+ base_url = config .get ("endpointUrl" ), # type: ignore[arg-type]
138+ servers = servers , # type: ignore[arg-type]
132139 prompt = prompt ,
133140 ) as agent :
134141 await agent .load_tools ()
0 commit comments