Skip to content

Commit ada39ad

Browse files
fixed tests for resource list changes.
1 parent 0179cfe commit ada39ad

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/ModelContextProtocol/Protocol/Types/ReadResourceRequestParams.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ public class ReadResourceRequestParams : RequestParams
1010
/// The URI of the resource to read. The URI can use any protocol; it is up to the server how to interpret it.
1111
/// </summary>
1212
[System.Text.Json.Serialization.JsonPropertyName("uri")]
13-
public string? Uri { get; init; }
13+
public required string Uri { get; init; }
1414
}

src/ModelContextProtocol/Server/McpServer.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public McpServer(ITransport transport, McpServerOptions options, ILoggerFactory?
7171

7272
RegisterListChange(capabilities?.Tools, NotificationMethods.ToolListChangedNotification);
7373
RegisterListChange(capabilities?.Prompts, NotificationMethods.PromptListChangedNotification);
74-
RegisterListChange(capabilities?.Resources, RequestMethods.ResourcesList);
74+
RegisterListChange(capabilities?.Resources, NotificationMethods.ResourceListChangedNotification);
7575

7676
// And initialize the session.
7777
InitializeSession(transport);
@@ -185,14 +185,9 @@ private void SetResourcesHandler(McpServerOptions options)
185185

186186
var listResourcesHandler = resourcesCapability.ListResourcesHandler;
187187
var listResourceTemplatesHandler = resourcesCapability.ListResourceTemplatesHandler;
188+
var readResourceHandler = resourcesCapability.ReadResourceHandler;
188189
var resourceCollection = resourcesCapability.ResourceCollection;
189190

190-
if ((listResourcesHandler is not { } && listResourceTemplatesHandler is not { }) ||
191-
resourcesCapability.ReadResourceHandler is not { } readResourceHandler)
192-
{
193-
throw new McpException("Resources capability was enabled, but ListResources and/or ReadResource handlers were not specified.");
194-
}
195-
196191
var originalListResourcesHandler = listResourcesHandler;
197192
listResourcesHandler = async (request, cancellationToken) =>
198193
{
@@ -208,12 +203,19 @@ await originalListResourcesHandler(request, cancellationToken).ConfigureAwait(fa
208203
return result;
209204
};
210205

206+
var isMissingListResourceHandlers = originalListResourcesHandler is not { } && listResourceTemplatesHandler is not { };
207+
if (resourceCollection is not { IsEmpty: false } && (isMissingListResourceHandlers || readResourceHandler is not { }))
208+
{
209+
throw new McpException("Resources capability was enabled, but ListResources, ListResourceTemplates, and/or ReadResource handlers were not specified.");
210+
}
211+
211212
RequestHandlers.Set(
212213
RequestMethods.ResourcesList,
213214
(request, cancellationToken) => listResourcesHandler(new(this, request), cancellationToken),
214215
McpJsonUtilities.JsonContext.Default.ListResourcesRequestParams,
215216
McpJsonUtilities.JsonContext.Default.ListResourcesResult);
216217

218+
readResourceHandler ??= static (_, _) => Task.FromResult(new ReadResourceResult());
217219
RequestHandlers.Set(
218220
RequestMethods.ResourcesRead,
219221
(request, cancellationToken) => readResourceHandler(new(this, request), cancellationToken),

tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsResourcesTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public void Adds_Resources_To_Server()
5050
var resources = serverOptions?.Capabilities?.Resources?.ResourceCollection;
5151
Assert.NotNull(resources);
5252
Assert.Equal(2, resources.Count);
53-
Assert.Equal("test.txt", resources["test"].Name);
54-
Assert.Equal("test2.txt", resources["test2"].Name);
53+
Assert.Equal("test", resources["test"].Name);
54+
Assert.Equal("test2", resources["test2"].Name);
5555
}
5656

5757
[Fact]
@@ -79,7 +79,7 @@ public async Task Can_Be_Notified_Of_ResourceList_Changes()
7979
.GetRequiredService<IOptions<McpServerOptions>>()
8080
.Value;
8181
TaskCompletionSource<JsonRpcNotification> changeReceived = new();
82-
client.RegisterNotificationHandler(
82+
await using var _ = client.RegisterNotificationHandler(
8383
NotificationMethods.ResourceListChangedNotification,
8484
(notification, token) =>
8585
{

0 commit comments

Comments
 (0)