Tools registered with WithTools keep a disposed service provider in memory
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 58/100
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- C#
- Sterne
- 4.6k
- Forks
- 849
- Ø Merge
- 1 T. 16 Std.
- Gemergte PRs (30 T.)
- 24
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus modelcontextprotocol/csharp-sdk
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
modelcontextprotocol/csharp-sdk#2031 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Static resources whose URIs differ only in the fragment or user info collapse into oneEvtl. vergeben @halter73 hat das vor 9 Tagen übernommen. OffenP2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
modelcontextprotocol/csharp-sdk#1970 · 4 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
POST /mcp with a missing or unparseable Content-Type returns HTTP 500 instead of a 4xxEvtl. vergeben @halter73 hat das vor 9 Tagen übernommen. Offenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
modelcontextprotocol/csharp-sdk#1966 · 5 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
Reserved URI-template expansion throws for supplementary Unicode charactersEvtl. vergeben @tarekgh hat das vor 2 Tagen übernommen. OffenP3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
modelcontextprotocol/csharp-sdk#1913 · 1 Kommentar · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 1 Tag
-
ClaimsPrincipal parameter injection sample requestEvtl. vergeben @utsavjain2809 hat das vor 48 Tagen übernommen. Offenhelp wanted P3
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
modelcontextprotocol/csharp-sdk#1899 ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in modelcontextprotocol/csharp-sdk
Ähnliche Issues
-
account bug effort:S P2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
nightscout/nocturne#3745 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
grate-devs/grate#969 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
jsboige/CoursIA#27768 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 63/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
martin-stromberg/FinanceManager#562 ·
Maintainer antworten meist innerhalb von 1 Tag