We currently synthesize handlers for tools, prompts, resources if the relevant collection has been initialized, but only if it's not empty. If instead it's empty, with the idea that entries will be added later, no handlers are synthesized.
Repro:
usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.Hosting;usingMicrosoft.Extensions.Logging;usingModelContextProtocol.Client;usingModelContextProtocol.Protocol;usingModelContextProtocol.Server;usingSystem.IO.Pipelines;PipeclientToServerPipe=new(),serverToClientPipe=new();McpServerResourceCollectionresourceCollection=[];// Uncomment this line and then both resources will work// resourceCollection.Add(McpServerResource.Create(() => "something", new() { UriTemplate = "something://something/1" }));varbuilder=Host.CreateApplicationBuilder(args);builder.Logging.AddConsole(o =>o.LogToStandardErrorThreshold=LogLevel.Trace);builder.Services.AddMcpServer(options =>{options.ServerInfo=newImplementation{Name="observable-game-state",Version="1.0.0",Title="Observable Game State Server"};options.ResourceCollection=resourceCollection;}).WithStreamServerTransport(clientToServerPipe.Reader.AsStream(),serverToClientPipe.Writer.AsStream());_=builder.Build().RunAsync();awaitusingMcpClientclient=awaitMcpClient.CreateAsync(newStreamClientTransport(clientToServerPipe.Writer.AsStream(),serverToClientPipe.Reader.AsStream()));Console.WriteLine("First:");foreach(varresourceinawaitclient.ListResourcesAsync()){Console.WriteLine($"Resource: {resource.Uri}");}resourceCollection.Add(McpServerResource.Create(()=>"something",new(){UriTemplate="something://something/2"}));Console.WriteLine("Second:");foreach(varresourceinawaitclient.ListResourcesAsync()){Console.WriteLine($"Resource: {resource.Uri}");}That check is happening here:
| if(resourcesis{IsEmpty:false}) |
We should instead always synthesize these handlers if this collection has been initialized, even if it's empty.
We currently synthesize handlers for tools, prompts, resources if the relevant collection has been initialized, but only if it's not empty. If instead it's empty, with the idea that entries will be added later, no handlers are synthesized.
Repro:
That check is happening here:
csharp-sdk/src/ModelContextProtocol.Core/Server/McpServerImpl.cs
Line 298 in b40f06c
We should instead always synthesize these handlers if this collection has been initialized, even if it's empty.