Skip to content

.NET: [Bug]: Microsoft.Agents.AI.Hosting Add AddAIAgent Tool call is not adding the service provider to the agent. #4453

Description

Description

Problem:
When I register an AIAgent in an Aspnet app with the default .AddAIAgent("name", Instructions, Description, null) and then register a tool using WithAITool(...) and that tools requires the serviceProvider I recieve and Empty one instead of my app's Service Provider.

Expected:
I should recieve the ServiceProvider of my app.

Steps:

  • Create a new asp net app
  • Register An AIAgent with A tool
  • Tool is called but with an empty Service Provider

Code Sample

builder
      .AddAIAgent("architect-helper", Instructions, Description, null)
      .WithAITool(AIFunctionFactory.Create(RagTools.GetSuggestedArchitecture))

//Tool
   public static async Task<List<DocumentationArticle>> GetSuggestedArchitecture(
       IServiceProvider serviceProvider,
       string appArchitecture)
   {
      // By default I get an empty service provider so this will fail
        using var scope = serviceProvider.CreateAsyncScope();
   }

/// This workaround works:
builder.AddAIAgent(
name: "architect-helper",
(sp, key) =>
{
    var chatClient = sp.GetRequiredService<IChatClient>();

    AIAgent agent = new ChatClientAgent(chatClient, Instructions,
        name: key,
        description: Description,
        tools: [
            AIFunctionFactory.Create(RagTools.GetSuggestedArchitecture),
        ],
        //Pass the service provider to the agent so it can resolve dependencies when executing tools
        services:sp
        );
    return agent;
})

Error Messages / Stack Traces

Package Versions

Microsoft.Agents.AI.Hosting:1.0.0-preview.260225.1

.NET Version

10

Additional Context

Also would be nice to have a way to have a tool call that resolves dependencies and even creates the scope for you?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETUsage: [Issues, PRs], Target: .NetbugUsage: [Issues], Target: all issues (Legacy, prefer issue type: bug)hostingUsage: [Issues, PRs], Target: all hosting related solutions

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions