Fix McpServerResource registration (#411)

- Add missing filling of ResourceCollection in McpServerOptionsSetup
- Remove AIFunctionMcpServerXx.ToString overrides; for resources, it was changing behavior undesirably, and for the others it was duplicate code.
- Fix notification method used for resource list changes.
- Fix type in WithResources parameter name.
This commit is contained in:
Stephen Toub 2025-05-14 09:08:59 -04:00 • committed by GitHub
commit 8d4f51da50
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 330 additions and 34 deletions

View file

@ -216,9 +216,6 @@ internal sealed class AIFunctionMcpServerPrompt : McpServerPrompt
ProtocolPrompt = prompt;
}
/// <inheritdoc />
public override string ToString() => AIFunction.ToString();
/// <inheritdoc />
public override Prompt ProtocolPrompt { get; }

View file

@ -331,9 +331,6 @@ internal sealed class AIFunctionMcpServerResource : McpServerResource
}
}
/// <inheritdoc />
public override string ToString() => AIFunction.ToString();
/// <inheritdoc />
public override ResourceTemplate ProtocolResourceTemplate { get; }

View file

@ -247,9 +247,6 @@ internal sealed class AIFunctionMcpServerTool : McpServerTool
ProtocolTool = tool;
}
/// <inheritdoc />
public override string ToString() => AIFunction.ToString();
/// <inheritdoc />
public override Tool ProtocolTool { get; }

View file

@ -81,26 +81,19 @@ internal sealed class McpServer : McpEndpoint, IMcpServer
// Now that everything has been configured, subscribe to any necessary notifications.
if (transport is not StreamableHttpServerTransport streamableHttpTransport || streamableHttpTransport.Stateless is false)
{
if (ServerOptions.Capabilities?.Tools?.ToolCollection is { } tools)
{
EventHandler changed = (sender, e) => _ = this.SendNotificationAsync(NotificationMethods.ToolListChangedNotification);
tools.Changed += changed;
_disposables.Add(() => tools.Changed -= changed);
}
Register(ServerOptions.Capabilities?.Tools?.ToolCollection, NotificationMethods.ToolListChangedNotification);
Register(ServerOptions.Capabilities?.Prompts?.PromptCollection, NotificationMethods.PromptListChangedNotification);
Register(ServerOptions.Capabilities?.Resources?.ResourceCollection, NotificationMethods.ResourceListChangedNotification);
if (ServerOptions.Capabilities?.Prompts?.PromptCollection is { } prompts)
void Register<TPrimitive>(McpServerPrimitiveCollection<TPrimitive>? collection, string notificationMethod)
where TPrimitive : IMcpServerPrimitive
{
EventHandler changed = (sender, e) => _ = this.SendNotificationAsync(NotificationMethods.PromptListChangedNotification);
prompts.Changed += changed;
_disposables.Add(() => prompts.Changed -= changed);
}
var resources = ServerOptions.Capabilities?.Resources?.ResourceCollection;
if (resources is not null)
{
EventHandler changed = (sender, e) => _ = this.SendNotificationAsync(NotificationMethods.PromptListChangedNotification);
resources.Changed += changed;
_disposables.Add(() => resources.Changed -= changed);
if (collection is not null)
{
EventHandler changed = (sender, e) => _ = this.SendNotificationAsync(notificationMethod);
collection.Changed += changed;
_disposables.Add(() => collection.Changed -= changed);
}
}
}