@@ -130,7 +130,7 @@ func Load(ctx context.Context, path string, runConfig config.RuntimeConfig) (*te
130130 if ! ok {
131131 return nil , fmt .Errorf ("agent '%s' not found in configuration" , name )
132132 }
133- agentTools , err := getToolsForAgent (& a , parentDir , sharedTools , models [0 ])
133+ agentTools , err := getToolsForAgent (ctx , & a , parentDir , sharedTools , models [0 ], env )
134134 if err != nil {
135135 return nil , fmt .Errorf ("failed to get tools: %w" , err )
136136 }
@@ -184,7 +184,7 @@ func getModelsForAgent(ctx context.Context, cfg *latest.Config, a *latest.AgentC
184184}
185185
186186// getToolsForAgent returns the tool definitions for an agent based on its configuration
187- func getToolsForAgent (a * latest.AgentConfig , parentDir string , sharedTools map [string ]tools.ToolSet , model provider.Provider ) ([]tools.ToolSet , error ) {
187+ func getToolsForAgent (ctx context. Context , a * latest.AgentConfig , parentDir string , sharedTools map [string ]tools.ToolSet , model provider. Provider , env environment .Provider ) ([]tools.ToolSet , error ) {
188188 var t []tools.ToolSet
189189
190190 if len (a .SubAgents ) > 0 {
@@ -278,17 +278,34 @@ func getToolsForAgent(a *latest.AgentConfig, parentDir string, sharedTools map[s
278278 // This improves shareability of agent configs.
279279 args = append (args , "--catalog=https://desktop.docker.com/mcp/catalog/v2/catalog.yaml" )
280280
281+ envVars , err := environment .ExpandAll (ctx , environment .ToValues (toolset .Env ), env )
282+ if err != nil {
283+ return nil , fmt .Errorf ("failed to expand the tool's environment variables: %w" , err )
284+ }
285+
281286 // TODO(dga): If the server's docker image had the right annotations, we could run it directly with `docker run` or with the MCP gateway as a go library.
282- t = append (t , mcp .NewToolsetCommand ("docker" , args , os . Environ () , toolset .Tools , cleanUp ))
287+ t = append (t , mcp .NewToolsetCommand ("docker" , args , envVars , toolset .Tools , cleanUp ))
283288
284289 case toolset .Type == "mcp" && toolset .Command != "" :
285- t = append (t , mcp .NewToolsetCommand (toolset .Command , toolset .Args , os .Environ (), toolset .Tools , nil ))
290+ envVars , err := environment .ExpandAll (ctx , environment .ToValues (toolset .Env ), env )
291+ if err != nil {
292+ return nil , fmt .Errorf ("failed to expand the tool's environment variables: %w" , err )
293+ }
294+
295+ t = append (t , mcp .NewToolsetCommand (toolset .Command , toolset .Args , envVars , toolset .Tools , nil ))
286296
287297 case toolset .Type == "mcp" && toolset .Remote .URL != "" :
288- // Expand headers.
298+ // TODO: the tool can have env variables too
299+
300+ // Expand env vars in headers.
289301 headers := map [string ]string {}
290302 for k , v := range toolset .Remote .Headers {
291- headers [k ] = environment .Expand (v , os .Environ ())
303+ expanded , err := environment .Expand (ctx , v , env )
304+ if err != nil {
305+ return nil , fmt .Errorf ("failed to expand header '%s': %w" , k , err )
306+ }
307+
308+ headers [k ] = expanded
292309 }
293310
294311 mcpc , err := mcp .NewToolsetRemote (toolset .Remote .URL , toolset .Remote .TransportType , headers , toolset .Tools )
0 commit comments