Tools registered with WithTools keep a disposed service provider in memory
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 58/100
Línea de trabajo
Start by running the provided reproduction with WithTools both with and without per-app JsonSerializerOptions, then trace CreateAIFunctionFactoryOptions through AIFunctionFactory and the McpJsonUtilities.DefaultOptions cache. Done means disposed service providers become collectible in both cases, confirmed by the WeakReference and GC checks in the reproduction.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Describe the bug
A service provider that creates its tools through WithTools or WithToolsFromAssembly stays in memory for the life of the process, even after it's disposed.
CreateAIFunctionFactoryOptions hands AIFunctionFactory a ConfigureParameterBinding lambda that captures the McpServerToolCreateOptions, and with them the Services these registrations pass in. Microsoft.Extensions.AI caches the descriptor of each tool method in a static ConditionalWeakTable keyed by the serializer options, and that lambda is part of the key of every entry. Unless serializer options are passed, they're McpJsonUtilities.DefaultOptions, which never go away. So each service provider adds entries that no later lookup can match, since the lambda is new every time, and each entry keeps its provider alive. Only the cache's limit of 512 entries puts a cap on it.
An app with a single service provider won't notice. A test project that starts an app per test does. In a dump of ours, this held 67 of the 110 disposed service providers.
To Reproduce
ModelContextProtocol 2.2.0 and Microsoft.Extensions.DependencyInjection 10.0 on net10.0:
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol;
using ModelContextProtocol.Server;
var ownOptions = args.Contains("--own-serializer-options");
List<WeakReference> providers = [];
for (var i = 0; i < 20; i++)
providers.Add(await CreateAndDispose(ownOptions));
for (var i = 0; i < 3; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
Console.WriteLine($"own serializer options: {ownOptions}, {providers.Count(p => p.IsAlive)} of {providers.Count} disposed service providers are still alive");
[MethodImpl(MethodImplOptions.NoInlining)]
static async Task<WeakReference> CreateAndDispose(bool ownOptions)
{
var services = new ServiceCollection();
if (ownOptions)
services.AddMcpServer().WithTools<EchoTools>(new JsonSerializerOptions(McpJsonUtilities.DefaultOptions));
else
services.AddMcpServer().WithTools<EchoTools>();
await using var provider = services.BuildServiceProvider();
_ = provider.GetServices<McpServerTool>().ToList();
return new WeakReference(provider);
}
[McpServerToolType]
public sealed class EchoTools
{
[McpServerTool, Description("Echoes the message back.")]
public string Echo(string message) => message;
}
Output:
own serializer options: False, 20 of 20 disposed service providers are still alive
own serializer options: True, 0 of 20 disposed service providers are still alive
Expected behavior
A disposed service provider can be collected, whether or not serializer options were passed.
Additional context
The path from our dump:
McpJsonUtilities.DefaultOptions
> ConcurrentDictionary<DescriptorKey, ReflectionAIFunctionDescriptor>
> Func<ParameterInfo, ParameterBindingOptions>
> AIFunctionMcpServerTool+<>c__DisplayClass4_0
> McpServerToolCreateOptions
> ServiceProviderEngineScope
Giving each app its own copy of the serializer options, as the second run above does, works around it for now.
- Lenguaje dominante
- C#
- Estrellas
- 4.6k
- Forks
- 849
- Merge medio
- 1 d 16 h
- PR fusionados (30 d)
- 24
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de modelcontextprotocol/csharp-sdk
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
modelcontextprotocol/csharp-sdk#2031 ·
Los mantenedores suelen responder en 1 día
-
Static resources whose URIs differ only in the fragment or user info collapse into onePosiblemente ocupada @halter73 la tomó hace 9 días. AbiertoP2
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
modelcontextprotocol/csharp-sdk#1970 · 4 comentarios · 1 asignado ·
Los mantenedores suelen responder en 1 día
-
POST /mcp with a missing or unparseable Content-Type returns HTTP 500 instead of a 4xxPosiblemente ocupada @halter73 la tomó hace 9 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
modelcontextprotocol/csharp-sdk#1966 · 5 comentarios · 1 asignado ·
Los mantenedores suelen responder en 1 día
-
Reserved URI-template expansion throws for supplementary Unicode charactersPosiblemente ocupada @tarekgh la tomó hace 2 días. AbiertoP3
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
modelcontextprotocol/csharp-sdk#1913 · 1 comentario · 1 asignado ·
Los mantenedores suelen responder en 1 día
-
ClaimsPrincipal parameter injection sample requestPosiblemente ocupada @utsavjain2809 la tomó hace 48 días. Abiertohelp wanted P3
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
modelcontextprotocol/csharp-sdk#1899 ·
Los mantenedores suelen responder en 1 día
Todos los issues de modelcontextprotocol/csharp-sdk
Issues similares
-
account bug effort:S P2
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
nightscout/nocturne#3745 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
grate-devs/grate#969 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
jsboige/CoursIA#27768 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 63/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
martin-stromberg/FinanceManager#562 ·
Los mantenedores suelen responder en 1 día