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:
parent
ce9f73f43f
commit
8d4f51da50
2 changed files with 330 additions and 34 deletions
|
|
@ -334,16 +334,16 @@ public static partial class McpServerBuilderExtensions
|
|||
|
||||
/// <summary>Adds <see cref="McpServerResource"/> instances to the service collection backing <paramref name="builder"/>.</summary>
|
||||
/// <param name="builder">The builder instance.</param>
|
||||
/// <param name="resourcetemplates">The <see cref="McpServerResource"/> instances to add to the server.</param>
|
||||
/// <param name="resourceTemplates">The <see cref="McpServerResource"/> instances to add to the server.</param>
|
||||
/// <returns>The builder provided in <paramref name="builder"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="resourcetemplates"/> is <see langword="null"/>.</exception>
|
||||
public static IMcpServerBuilder WithResources(this IMcpServerBuilder builder, IEnumerable<McpServerResource> resourcetemplates)
|
||||
/// <exception cref="ArgumentNullException"><paramref name="resourceTemplates"/> is <see langword="null"/>.</exception>
|
||||
public static IMcpServerBuilder WithResources(this IMcpServerBuilder builder, IEnumerable<McpServerResource> resourceTemplates)
|
||||
{
|
||||
Throw.IfNull(builder);
|
||||
Throw.IfNull(resourcetemplates);
|
||||
Throw.IfNull(resourceTemplates);
|
||||
|
||||
foreach (var resourceTemplate in resourcetemplates)
|
||||
foreach (var resourceTemplate in resourceTemplates)
|
||||
{
|
||||
if (resourceTemplate is not null)
|
||||
{
|
||||
|
|
@ -409,7 +409,7 @@ public static partial class McpServerBuilderExtensions
|
|||
/// of the containing class will be constructed for each invocation of the resource.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Resource templates registered through this method can be discovered by clients using the <c>list_resourcetemplates</c> request
|
||||
/// Resource templates registered through this method can be discovered by clients using the <c>list_resourceTemplates</c> request
|
||||
/// and invoked using the <c>read_resource</c> request.
|
||||
/// </para>
|
||||
/// <para>
|
||||
|
|
|
|||
|
|
@ -10,10 +10,12 @@ namespace Microsoft.Extensions.DependencyInjection;
|
|||
/// <param name="serverHandlers">The server handlers configuration options.</param>
|
||||
/// <param name="serverTools">Tools individually registered.</param>
|
||||
/// <param name="serverPrompts">Prompts individually registered.</param>
|
||||
/// <param name="serverResources">Resources individually registered.</param>
|
||||
internal sealed class McpServerOptionsSetup(
|
||||
IOptions<McpServerHandlers> serverHandlers,
|
||||
IEnumerable<McpServerTool> serverTools,
|
||||
IEnumerable<McpServerPrompt> serverPrompts) : IConfigureOptions<McpServerOptions>
|
||||
IEnumerable<McpServerPrompt> serverPrompts,
|
||||
IEnumerable<McpServerResource> serverResources) : IConfigureOptions<McpServerOptions>
|
||||
{
|
||||
/// <summary>
|
||||
/// Configures the given McpServerOptions instance by setting server information
|
||||
|
|
@ -58,6 +60,23 @@ internal sealed class McpServerOptionsSetup(
|
|||
options.Capabilities.Prompts.PromptCollection = promptCollection;
|
||||
}
|
||||
|
||||
// Collect all of the provided resources into a resources collection. If the options already has
|
||||
// a collection, add to it, otherwise create a new one. We want to maintain the identity
|
||||
// of an existing collection in case someone has provided their own derived type, wants
|
||||
// change notifications, etc.
|
||||
McpServerPrimitiveCollection<McpServerResource> resourceCollection = options.Capabilities?.Resources?.ResourceCollection ?? [];
|
||||
foreach (var resource in serverResources)
|
||||
{
|
||||
resourceCollection.TryAdd(resource);
|
||||
}
|
||||
|
||||
if (!resourceCollection.IsEmpty)
|
||||
{
|
||||
options.Capabilities ??= new();
|
||||
options.Capabilities.Resources ??= new();
|
||||
options.Capabilities.Resources.ResourceCollection = resourceCollection;
|
||||
}
|
||||
|
||||
// Apply custom server handlers.
|
||||
serverHandlers.Value.OverwriteWithSetHandlers(options);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue