Fix resource subscription handling (#89)

* Fix resource subscription handling
Add resource subscription tests

* Fix typo

* Typo

* small fixes

* Fix concurrency issue with test

* Align with specification schemas
This commit is contained in:
Peder Holdgaard Pedersen 2025-03-17 17:54:29 +01:00 • committed by GitHub
commit 34a6f1c665
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 210 additions and 36 deletions

View file

@ -227,7 +227,7 @@ public static class McpClientExtensions
{
Throw.IfNull(client);
return client.SendRequestAsync<dynamic>(
return client.SendRequestAsync<EmptyResult>(
CreateRequest("resources/subscribe", new() { ["uri"] = uri }),
cancellationToken);
}
@ -242,7 +242,7 @@ public static class McpClientExtensions
{
Throw.IfNull(client);
return client.SendRequestAsync<dynamic>(
return client.SendRequestAsync<EmptyResult>(
CreateRequest("resources/unsubscribe", new() { ["uri"] = uri }),
cancellationToken);
}

View file

@ -107,7 +107,7 @@ public static partial class McpServerBuilderExtensions
/// </summary>
/// <param name="builder">The builder instance.</param>
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithSubscribeToResourcesHandler(this IMcpServerBuilder builder, Func<RequestContext<string>, CancellationToken, Task> handler)
public static IMcpServerBuilder WithSubscribeToResourcesHandler(this IMcpServerBuilder builder, Func<RequestContext<SubscribeRequestParams>, CancellationToken, Task<EmptyResult>> handler)
{
Throw.IfNull(builder);
@ -120,7 +120,7 @@ public static partial class McpServerBuilderExtensions
/// </summary>
/// <param name="builder">The builder instance.</param>
/// <param name="handler">The handler.</param>
public static IMcpServerBuilder WithUnsubscribeFromResourcesHandler(this IMcpServerBuilder builder, Func<RequestContext<string>, CancellationToken, Task> handler)
public static IMcpServerBuilder WithUnsubscribeFromResourcesHandler(this IMcpServerBuilder builder, Func<RequestContext<UnsubscribeRequestParams>, CancellationToken, Task<EmptyResult>> handler)
{
Throw.IfNull(builder);

View file

@ -126,13 +126,13 @@ public record ResourcesCapability
/// Gets or sets the handler for subscribe to resources messages.
/// </summary>
[JsonIgnore]
public Func<RequestContext<string>, CancellationToken, Task>? SubscribeToResourcesHandler { get; init; }
public Func<RequestContext<SubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? SubscribeToResourcesHandler { get; init; }
/// <summary>
/// Gets or sets the handler for unsubscribe from resources messages.
/// </summary>
[JsonIgnore]
public Func<RequestContext<string>, CancellationToken, Task>? UnsubscribeFromResourcesHandler { get; init; }
public Func<RequestContext<UnsubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? UnsubscribeFromResourcesHandler { get; init; }
}
/// <summary>

View file

@ -0,0 +1,16 @@
using System.Text.Json.Serialization;
namespace McpDotNet.Protocol.Types;
/// <summary>
/// The server's response to a completion/complete request
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public class CompleteResult
{
/// <summary>
/// The completion object containing the completion values.
/// </summary>
[JsonPropertyName("completion")]
public Completion Completion { get; set; } = new Completion();
}

View file

@ -0,0 +1,10 @@
namespace McpDotNet.Protocol.Types;
/// <summary>
/// An empty result object.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public class EmptyResult
{
}

View file

@ -66,15 +66,3 @@ public class Reference
return true;
}
}
/// <summary>
/// The server's response to a completion/complete request
/// </summary>
public class CompleteResult
{
/// <summary>
/// The completion object containing the completion values.
/// </summary>
[JsonPropertyName("completion")]
public Completion Completion { get; set; } = new Completion();
}

View file

@ -0,0 +1,14 @@
namespace McpDotNet.Protocol.Types;
/// <summary>
/// Sent from the server as the payload of "notifications/resources/updated" notifications whenever a subscribed resource changes.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public class ResourceUpdatedNotificationParams
{
/// <summary>
/// The URI of the resource to subscribe to. The URI can use any protocol; it is up to the server how to interpret it.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("uri")]
public string? Uri { get; init; }
}

View file

@ -0,0 +1,14 @@
namespace McpDotNet.Protocol.Types;
/// <summary>
/// Sent from the client to request updated notifications from the server whenever a particular primitive changes.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public class SubscribeRequestParams
{
/// <summary>
/// The URI of the resource to subscribe to. The URI can use any protocol; it is up to the server how to interpret it.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("uri")]
public string? Uri { get; init; }
}

View file

@ -0,0 +1,14 @@
namespace McpDotNet.Protocol.Types;
/// <summary>
/// Sent from the client to request not receiving updated notifications from the server whenever a primitive resource changes.
/// <see href="https://github.com/modelcontextprotocol/specification/blob/main/schema/2024-11-05/schema.json">See the schema for details</see>
/// </summary>
public class UnsubscribeRequestParams
{
/// <summary>
/// The URI of the resource to unsubscribe fro. The URI can use any protocol; it is up to the server how to interpret it.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("uri")]
public string? Uri { get; init; }
}

View file

@ -163,7 +163,8 @@ internal sealed class McpServer : McpJsonRpcEndpoint, IMcpServer
throw new McpServerException("Resources capability was enabled with subscribe support, but SubscribeToResources and/or UnsubscribeFromResources handlers were not specified.");
}
// TODO: Implement Subscribe support
SetRequestHandler<SubscribeRequestParams, EmptyResult>("resources/subscribe", request => subscribeHandler(new(this, request), cancellationToken));
SetRequestHandler<UnsubscribeRequestParams, EmptyResult>("resources/unsubscribe", request => unsubscribeHandler(new(this, request), cancellationToken));
}
private void SetPromptsHandler(McpServerOptions options)

View file

@ -45,12 +45,12 @@ public sealed class McpServerHandlers
/// <summary>
/// Gets or sets the handler for subscribe to resources messages.
/// </summary>
public Func<RequestContext<string>, CancellationToken, Task>? SubscribeToResourcesHandler { get; set; }
public Func<RequestContext<SubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? SubscribeToResourcesHandler { get; set; }
/// <summary>
/// Gets or sets the handler for unsubscribe from resources messages.
/// </summary>
public Func<RequestContext<string>, CancellationToken, Task>? UnsubscribeFromResourcesHandler { get; set; }
public Func<RequestContext<UnsubscribeRequestParams>, CancellationToken, Task<EmptyResult>>? UnsubscribeFromResourcesHandler { get; set; }
/// <summary>
/// Overwrite any handlers in McpServerOptions with non-null handlers from this instance.
@ -77,25 +77,29 @@ public sealed class McpServerHandlers
ResourcesCapability? resourcesCapability = options.Capabilities?.Resources;
if (ListResourcesHandler is not null ||
ReadResourceHandler is not null ||
SubscribeToResourcesHandler is not null ||
UnsubscribeFromResourcesHandler is not null)
ReadResourceHandler is not null)
{
resourcesCapability = resourcesCapability is null ?
new()
{
ListResourcesHandler = ListResourcesHandler,
ReadResourceHandler = ReadResourceHandler,
SubscribeToResourcesHandler = SubscribeToResourcesHandler,
UnsubscribeFromResourcesHandler = UnsubscribeFromResourcesHandler,
ReadResourceHandler = ReadResourceHandler
} :
resourcesCapability with
{
ListResourcesHandler = ListResourcesHandler ?? resourcesCapability.ListResourcesHandler,
ReadResourceHandler = ReadResourceHandler ?? resourcesCapability.ReadResourceHandler,
ReadResourceHandler = ReadResourceHandler ?? resourcesCapability.ReadResourceHandler
};
if (SubscribeToResourcesHandler is not null || UnsubscribeFromResourcesHandler is not null)
{
resourcesCapability = resourcesCapability with
{
SubscribeToResourcesHandler = SubscribeToResourcesHandler ?? resourcesCapability.SubscribeToResourcesHandler,
UnsubscribeFromResourcesHandler = UnsubscribeFromResourcesHandler ?? resourcesCapability.UnsubscribeFromResourcesHandler,
Subscribe = true
};
}
}
ToolsCapability? toolsCapability = options.Capabilities?.Tools;