Closed
Description
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:
- Add the following code in
Program.cs
:builder.Services.AddDbContext<CoreDbContext>();
At this point, the lifetime ofCoreDbContext
isScoped
. - Add the following parameter to the
McpServerTool
method:
Task<string> GetKnowledge(
IMcpServer thisServer,
CoreDbContext db,
[Description("question")] string question)
{
}
- 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)