-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathMcpServerBuilderExtensionsResourcesTests.cs
More file actions
453 lines (378 loc) · 20.5 KB
/
McpServerBuilderExtensionsResourcesTests.cs
File metadata and controls
453 lines (378 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using ModelContextProtocol.Tests.Utils;
using System.Collections;
using System.ComponentModel;
using System.Threading.Channels;
using static ModelContextProtocol.Tests.Configuration.McpServerBuilderExtensionsPromptsTests;
namespace ModelContextProtocol.Tests.Configuration;
public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTestBase
{
public McpServerBuilderExtensionsResourcesTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}
protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder)
{
mcpServerBuilder
.WithListResourcesHandler(async (request, cancellationToken) =>
{
var cursor = request.Params?.Cursor;
switch (cursor)
{
case null:
return new()
{
NextCursor = "abc",
Resources = [new()
{
Name = "Resource1",
Uri = "test://resource1",
}],
};
case "abc":
return new()
{
NextCursor = "def",
Resources = [new()
{
Name = "Resource2",
Uri = "test://resource2",
}],
};
case "def":
return new()
{
NextCursor = null,
Resources = [new()
{
Name = "Resource3",
Uri = "test://resource3",
}],
};
default:
throw new McpProtocolException($"Unexpected cursor: '{cursor}'", McpErrorCode.InvalidParams);
}
})
.WithListResourceTemplatesHandler(async (request, cancellationToken) =>
{
var cursor = request.Params?.Cursor;
switch (cursor)
{
case null:
return new()
{
NextCursor = "abc",
ResourceTemplates = [new()
{
Name = "ResourceTemplate1",
UriTemplate = "test://resourceTemplate/{id}",
}],
};
case "abc":
return new()
{
NextCursor = null,
ResourceTemplates = [new()
{
Name = "ResourceTemplate2",
UriTemplate = "test://resourceTemplate2/{id}",
}],
};
default:
throw new McpProtocolException($"Unexpected cursor: '{cursor}'", McpErrorCode.InvalidParams);
}
})
.WithReadResourceHandler(async (request, cancellationToken) =>
{
switch (request.Params?.Uri)
{
case "test://Resource1":
case "test://Resource2":
case "test://Resource3":
case "test://ResourceTemplate1":
case "test://ResourceTemplate2":
return new ReadResourceResult
{
Contents = [new TextResourceContents { Text = request.Params?.Uri ?? "(null)", Uri = request.Params?.Uri ?? "(null)" }]
};
}
throw new McpProtocolException($"Resource not found: {request.Params?.Uri}", McpErrorCode.ResourceNotFound);
})
.WithResources<SimpleResources>();
}
[Fact]
public void Adds_Resources_To_Server()
{
var serverOptions = ServiceProvider.GetRequiredService<IOptions<McpServerOptions>>().Value;
var resources = serverOptions.ResourceCollection;
Assert.NotNull(resources);
Assert.NotEmpty(resources);
}
[Fact]
public async Task Can_List_And_Call_Registered_Resources()
{
await using McpClient client = await CreateMcpClientForServer();
Assert.NotNull(client.ServerCapabilities.Resources);
var resources = await client.ListResourcesAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(7, resources.Count);
var resource = resources.First(t => t.Name == "some_neat_direct_resource");
Assert.Equal("Some neat direct resource", resource.Description);
var result = await resource.ReadAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(result);
Assert.Single(result.Contents);
Assert.Equal("This is a neat resource", Assert.IsType<TextResourceContents>(result.Contents[0]).Text);
}
[Fact]
public async Task Can_List_And_Call_Registered_ResourceTemplates()
{
await using McpClient client = await CreateMcpClientForServer();
var resources = await client.ListResourceTemplatesAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(3, resources.Count);
var resource = resources.First(t => t.Name == "some_neat_templated_resource");
Assert.Equal("Some neat resource with parameters", resource.Description);
var result = await resource.ReadAsync(new Dictionary<string, object?>() { ["name"] = "hello" }, cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(result);
Assert.Single(result.Contents);
Assert.Equal("This is a neat resource with parameters: hello", Assert.IsType<TextResourceContents>(result.Contents[0]).Text);
}
[Fact]
public async Task Can_Be_Notified_Of_Resource_Changes()
{
await using McpClient client = await CreateMcpClientForServer();
var resources = await client.ListResourcesAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(7, resources.Count);
Channel<JsonRpcNotification> listChanged = Channel.CreateUnbounded<JsonRpcNotification>();
var notificationRead = listChanged.Reader.ReadAsync(TestContext.Current.CancellationToken);
Assert.False(notificationRead.IsCompleted);
var serverOptions = ServiceProvider.GetRequiredService<IOptions<McpServerOptions>>().Value;
var serverResources = serverOptions.ResourceCollection;
Assert.NotNull(serverResources);
var newResource = McpServerResource.Create([McpServerResource(Name = "NewResource")] () => "42");
await using (client.RegisterNotificationHandler("notifications/resources/list_changed", (notification, cancellationToken) =>
{
listChanged.Writer.TryWrite(notification);
return default;
}))
{
serverResources.Add(newResource);
await notificationRead;
resources = await client.ListResourcesAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(8, resources.Count);
Assert.Contains(resources, t => t.Name == "NewResource");
notificationRead = listChanged.Reader.ReadAsync(TestContext.Current.CancellationToken);
Assert.False(notificationRead.IsCompleted);
serverResources.Remove(newResource);
await notificationRead;
}
resources = await client.ListResourcesAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(7, resources.Count);
Assert.DoesNotContain(resources, t => t.Name == "NewResource");
}
[Fact]
public async Task AttributeProperties_Propagated()
{
await using McpClient client = await CreateMcpClientForServer();
var resources = await client.ListResourcesAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(resources);
Assert.NotEmpty(resources);
McpClientResource resource = resources.First(t => t.Name == "some_neat_direct_resource");
Assert.Equal("This is a title", resource.Title);
Assert.NotNull(resource.ProtocolResource.Icons);
Assert.NotEmpty(resource.ProtocolResource.Icons);
var resourceIcon = Assert.Single(resource.ProtocolResource.Icons);
Assert.Equal("https://example.com/direct-resource-icon.svg", resourceIcon.Source);
Assert.Null(resourceIcon.Theme);
var resourceTemplates = await client.ListResourceTemplatesAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(resourceTemplates);
Assert.NotEmpty(resourceTemplates);
McpClientResourceTemplate resourceTemplate = resourceTemplates.First(t => t.Name == "some_neat_templated_resource");
Assert.Equal("This is another title", resourceTemplate.Title);
Assert.NotNull(resourceTemplate.ProtocolResourceTemplate.Icons);
Assert.NotEmpty(resourceTemplate.ProtocolResourceTemplate.Icons);
var templateIcon = Assert.Single(resourceTemplate.ProtocolResourceTemplate.Icons);
Assert.Equal("https://example.com/templated-resource-icon.svg", templateIcon.Source);
Assert.Null(templateIcon.Theme);
}
[Fact]
public async Task Throws_When_Resource_Fails()
{
await using McpClient client = await CreateMcpClientForServer();
await Assert.ThrowsAsync<McpProtocolException>(async () => await client.ReadResourceAsync(
$"resource://mcp/{nameof(SimpleResources.ThrowsException)}",
cancellationToken: TestContext.Current.CancellationToken));
}
[Fact]
public async Task Logs_Resource_Uri_On_Successful_Read()
{
await using McpClient client = await CreateMcpClientForServer();
var result = await client.ReadResourceAsync(
"resource://mcp/some_neat_direct_resource",
cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(result);
var infoLog = Assert.Single(MockLoggerProvider.LogMessages, m => m.Message == "ReadResource \"resource://mcp/some_neat_direct_resource\" completed.");
Assert.Equal(LogLevel.Information, infoLog.LogLevel);
}
[Fact]
public async Task Logs_Resource_Uri_When_Resource_Throws()
{
await using McpClient client = await CreateMcpClientForServer();
await Assert.ThrowsAsync<McpProtocolException>(async () => await client.ReadResourceAsync(
"resource://mcp/throws_exception",
cancellationToken: TestContext.Current.CancellationToken));
var errorLog = Assert.Single(MockLoggerProvider.LogMessages, m => m.LogLevel == LogLevel.Error);
Assert.Equal("ReadResource \"resource://mcp/throws_exception\" threw an unhandled exception.", errorLog.Message);
Assert.IsType<InvalidOperationException>(errorLog.Exception);
}
[Fact]
public async Task Logs_Resource_Error_When_Resource_Throws_OperationCanceledException()
{
await using McpClient client = await CreateMcpClientForServer();
await Assert.ThrowsAsync<McpProtocolException>(async () => await client.ReadResourceAsync(
"resource://mcp/throws_operation_canceled_exception",
cancellationToken: TestContext.Current.CancellationToken));
Assert.Contains(MockLoggerProvider.LogMessages, m =>
m.LogLevel == LogLevel.Error &&
m.Message == "ReadResource \"resource://mcp/throws_operation_canceled_exception\" threw an unhandled exception." &&
m.Exception is OperationCanceledException);
Assert.Contains(MockLoggerProvider.LogMessages, m =>
m.LogLevel == LogLevel.Warning &&
m.Message.Contains("request handler failed"));
}
[Fact]
public async Task Logs_Resource_Error_When_Resource_Throws_McpProtocolException()
{
await using McpClient client = await CreateMcpClientForServer();
await Assert.ThrowsAsync<McpProtocolException>(async () => await client.ReadResourceAsync(
"resource://mcp/throws_mcp_protocol_exception",
cancellationToken: TestContext.Current.CancellationToken));
Assert.Contains(MockLoggerProvider.LogMessages, m =>
m.LogLevel == LogLevel.Error &&
m.Message == "ReadResource \"resource://mcp/throws_mcp_protocol_exception\" threw an unhandled exception." &&
m.Exception is McpProtocolException);
Assert.Contains(MockLoggerProvider.LogMessages, m =>
m.LogLevel == LogLevel.Warning &&
m.Message.Contains("request handler failed"));
}
[Fact]
public async Task Throws_Exception_On_Unknown_Resource()
{
await using McpClient client = await CreateMcpClientForServer();
var e = await Assert.ThrowsAsync<McpProtocolException>(async () => await client.ReadResourceAsync(
"test:///NotRegisteredResource",
cancellationToken: TestContext.Current.CancellationToken));
Assert.Contains("Resource not found", e.Message);
Assert.Equal(McpErrorCode.ResourceNotFound, e.ErrorCode);
}
[Fact]
public void WithResources_InvalidArgs_Throws()
{
IMcpServerBuilder builder = new ServiceCollection().AddMcpServer();
Assert.Throws<ArgumentNullException>("resourceTemplates", () => builder.WithResources((IEnumerable<McpServerResource>)null!));
Assert.Throws<ArgumentNullException>("resourceTemplateTypes", () => builder.WithResources((IEnumerable<Type>)null!));
Assert.Throws<ArgumentNullException>("target", () => builder.WithResources<object>(target: null!));
IMcpServerBuilder nullBuilder = null!;
Assert.Throws<ArgumentNullException>("builder", () => nullBuilder.WithResources<object>());
Assert.Throws<ArgumentNullException>("builder", () => nullBuilder.WithResources(new object()));
Assert.Throws<ArgumentNullException>("builder", () => nullBuilder.WithResources(Array.Empty<Type>()));
Assert.Throws<ArgumentNullException>("builder", () => nullBuilder.WithResourcesFromAssembly());
}
[Fact]
public async Task WithResources_TargetInstance_UsesTarget()
{
ServiceCollection sc = new();
var target = new ResourceWithId(new ObjectWithId() { Id = "42" });
sc.AddMcpServer().WithResources(target);
McpServerResource resource = sc.BuildServiceProvider().GetServices<McpServerResource>().First(t => t.ProtocolResource?.Name == "returns_string");
var result = await resource.ReadAsync(new RequestContext<ReadResourceRequestParams>(McpServer.Create(new TestServerTransport(), new McpServerOptions()), new JsonRpcRequest { Method = "test", Id = new RequestId("1") })
{
Params = new()
{
Uri = "returns://string"
}
}, TestContext.Current.CancellationToken);
Assert.Equal(target.ReturnsString(), (result?.Contents[0] as TextResourceContents)?.Text);
}
[Fact]
public async Task WithResources_TargetInstance_UsesEnumerableImplementation()
{
ServiceCollection sc = new();
sc.AddMcpServer().WithResources(new MyResourceProvider());
var resources = sc.BuildServiceProvider().GetServices<McpServerResource>().ToArray();
Assert.Equal(2, resources.Length);
Assert.Contains(resources, t => t.ProtocolResource?.Name == "Returns42");
Assert.Contains(resources, t => t.ProtocolResource?.Name == "Returns43");
}
private sealed class MyResourceProvider : IEnumerable<McpServerResource>
{
public IEnumerator<McpServerResource> GetEnumerator()
{
yield return McpServerResource.Create(() => "42", new() { Name = "Returns42" });
yield return McpServerResource.Create(() => "43", new() { Name = "Returns43" });
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
[Fact]
public void Empty_Enumerables_Is_Allowed()
{
IMcpServerBuilder builder = new ServiceCollection().AddMcpServer();
builder.WithResources(resourceTemplates: Array.Empty<McpServerResource>()); // no exception
builder.WithResources(resourceTemplateTypes: Array.Empty<Type>()); // no exception
builder.WithResources<object>(); // no exception even though no resources exposed
builder.WithResourcesFromAssembly(typeof(AIFunction).Assembly); // no exception even though no resources exposed
}
[Fact]
public void Register_Resources_From_Current_Assembly()
{
ServiceCollection sc = new();
sc.AddMcpServer().WithResourcesFromAssembly();
IServiceProvider services = sc.BuildServiceProvider();
Assert.Contains(services.GetServices<McpServerResource>(), t => t.ProtocolResource?.Uri == $"resource://mcp/some_neat_direct_resource");
Assert.Contains(services.GetServices<McpServerResource>(), t => t.ProtocolResourceTemplate?.UriTemplate == $"resource://mcp/some_neat_templated_resource{{?name}}");
}
[Fact]
public void Register_Resources_From_Multiple_Sources()
{
ServiceCollection sc = new();
sc.AddMcpServer()
.WithResources<SimpleResources>()
.WithResources<MoreResources>()
.WithResources([McpServerResource.Create(() => "42", new() { UriTemplate = "myResources:///returns42/{something}" })]);
IServiceProvider services = sc.BuildServiceProvider();
Assert.Contains(services.GetServices<McpServerResource>(), t => t.ProtocolResource?.Uri == $"resource://mcp/some_neat_direct_resource");
Assert.Contains(services.GetServices<McpServerResource>(), t => t.ProtocolResourceTemplate?.UriTemplate == $"resource://mcp/some_neat_templated_resource{{?name}}");
Assert.Contains(services.GetServices<McpServerResource>(), t => t.ProtocolResourceTemplate?.UriTemplate == $"resource://mcp/another_neat_direct_resource");
Assert.Contains(services.GetServices<McpServerResource>(), t => t.ProtocolResourceTemplate.UriTemplate == "myResources:///returns42/{something}");
}
[McpServerResourceType]
public sealed class SimpleResources
{
[McpServerResource(Title = "This is a title", IconSource = "https://example.com/direct-resource-icon.svg"), Description("Some neat direct resource")]
public static string SomeNeatDirectResource() => "This is a neat resource";
[McpServerResource(Title = "This is another title", IconSource = "https://example.com/templated-resource-icon.svg"), Description("Some neat resource with parameters")]
public static string SomeNeatTemplatedResource(string name) => $"This is a neat resource with parameters: {name}";
[McpServerResource]
public static string ThrowsException() => throw new InvalidOperationException("uh oh");
[McpServerResource]
public static string ThrowsOperationCanceledException() => throw new OperationCanceledException("Resource was canceled");
[McpServerResource]
public static string ThrowsMcpProtocolException() => throw new McpProtocolException("Resource protocol error", McpErrorCode.InvalidParams);
}
[McpServerResourceType]
public sealed class MoreResources
{
[McpServerResource, Description("Another neat direct resource")]
public static string AnotherNeatDirectResource() => "This is a neat resource";
}
[McpServerResourceType]
public sealed class ResourceWithId(ObjectWithId id)
{
[McpServerResource(UriTemplate = "returns://string")]
public string ReturnsString() => $"Id: {id.Id}";
}
}