Skip to content

Resolving scoped service is not supported #198

Closed
@gong626035906

Description

@gong626035906

Describe the bug
When I try to inject a service with a Scoped lifetime in the Tool method, an error will occur. I wonder if all injections must be of the Singleton type?

To Reproduce
Steps to reproduce the behavior:

  1. Add the following code in Program.cs: builder.Services.AddDbContext<CoreDbContext>(); At this point, the lifetime of CoreDbContext is Scoped.
  2. Add the following parameter to the McpServerTool method:
Task<string> GetKnowledge(
        IMcpServer thisServer,
        CoreDbContext db,
        [Description("question")] string question)
{
}
  1. An exception will be thrown when the LLM calls the method.

Expected behavior
Inject CoreDbContext correctly

Logs

System.InvalidOperationException: Cannot resolve scoped service 'Zenative.Server.Database.CoreDbContext' from root provider.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(ServiceCallSite callSite, IServiceScope scope, IServiceScope rootScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.Extensions.AI.TemporaryAIFunctionFactory.ReflectionAIFunction.InvokeCoreAsync(IEnumerable`1 arguments, CancellationToken cancellationToken)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions