|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using Microsoft.Extensions.Options; |
| 3 | +using ModelContextProtocol.Client; |
| 4 | +using ModelContextProtocol.Server; |
| 5 | +using ModelContextProtocol.Protocol; |
| 6 | + |
| 7 | +namespace ModelContextProtocol.Tests.Configuration; |
| 8 | + |
| 9 | +// Integration test with full client-server setup |
| 10 | +public class McpServerResourceCapabilityIntegrationTests : ClientServerTestBase |
| 11 | +{ |
| 12 | + public McpServerResourceCapabilityIntegrationTests(ITestOutputHelper testOutputHelper) |
| 13 | + : base(testOutputHelper) |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder) |
| 18 | + { |
| 19 | + // User manually declares support for sending resource subscription notifications |
| 20 | + services.Configure<McpServerOptions>(options => |
| 21 | + { |
| 22 | + options.Capabilities = new() |
| 23 | + { |
| 24 | + Resources = new() |
| 25 | + { |
| 26 | + Subscribe = true, |
| 27 | + } |
| 28 | + }; |
| 29 | + }); |
| 30 | + |
| 31 | + mcpServerBuilder.WithResources<SimpleResourceType>(); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public async Task Client_CanListResources_WhenSubscribeCapabilityIsManuallySet() |
| 36 | + { |
| 37 | + await using McpClient client = await CreateMcpClientForServer(); |
| 38 | + |
| 39 | + // The server should advertise Subscribe capability |
| 40 | + Assert.NotNull(client.ServerCapabilities.Resources); |
| 41 | + Assert.True(client.ServerCapabilities.Resources.Subscribe, "Server should advertise Subscribe capability when manually set"); |
| 42 | + |
| 43 | + // The resources should be exposed and listable |
| 44 | + var resources = await client.ListResourcesAsync(TestContext.Current.CancellationToken); |
| 45 | + Assert.NotEmpty(resources); |
| 46 | + Assert.Contains(resources, r => r.Name == "test_resource"); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task Client_CanListResources_WhenCapabilitySetViaAddMcpServerCallback() |
| 51 | + { |
| 52 | + // This is a separate test using a different configuration approach |
| 53 | + await using McpClient client = await CreateMcpClientForServer(); |
| 54 | + |
| 55 | + // The server should advertise Subscribe capability |
| 56 | + Assert.NotNull(client.ServerCapabilities.Resources); |
| 57 | + |
| 58 | + // The resources should be exposed and listable |
| 59 | + var resources = await client.ListResourcesAsync(TestContext.Current.CancellationToken); |
| 60 | + Assert.NotEmpty(resources); |
| 61 | + } |
| 62 | + |
| 63 | + [McpServerResourceType] |
| 64 | + public sealed class SimpleResourceType |
| 65 | + { |
| 66 | + [McpServerResource] |
| 67 | + public static string TestResource() => "Test content"; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// Test that exactly matches the issue scenario |
| 72 | +public class McpServerResourceCapabilityIssueReproTests : ClientServerTestBase |
| 73 | +{ |
| 74 | + public McpServerResourceCapabilityIssueReproTests(ITestOutputHelper testOutputHelper) |
| 75 | + : base(testOutputHelper) |
| 76 | + { |
| 77 | + } |
| 78 | + |
| 79 | + protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder) |
| 80 | + { |
| 81 | + // This test uses the exact pattern from the issue: |
| 82 | + // AddMcpServer with options callback that sets Capabilities.Resources.Subscribe = true |
| 83 | + // followed by WithResources |
| 84 | + // NO call to services.Configure after AddMcpServer |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public async Task Resources_AreExposed_WhenSubscribeCapabilitySetInAddMcpServerOptions() |
| 89 | + { |
| 90 | + // Create a fresh service collection to test the exact scenario from the issue |
| 91 | + var services = new ServiceCollection(); |
| 92 | + services.AddLogging(); |
| 93 | + services.AddSingleton(XunitLoggerProvider); |
| 94 | + |
| 95 | + // This matches the issue: setting capabilities in AddMcpServer callback |
| 96 | + var builder = services.AddMcpServer( |
| 97 | + options => |
| 98 | + { |
| 99 | + // Declare support for sending resource subscription notifications |
| 100 | + options.Capabilities = new() |
| 101 | + { |
| 102 | + Resources = new() |
| 103 | + { |
| 104 | + Subscribe = true, |
| 105 | + } |
| 106 | + }; |
| 107 | + }) |
| 108 | + .WithResources<LiveResources>() |
| 109 | + .WithStdioServerTransport(); |
| 110 | + |
| 111 | + var serviceProvider = services.BuildServiceProvider(); |
| 112 | + var mcpOptions = serviceProvider.GetRequiredService<IOptions<McpServerOptions>>().Value; |
| 113 | + |
| 114 | + // Verify capabilities are preserved |
| 115 | + Assert.NotNull(mcpOptions.Capabilities?.Resources); |
| 116 | + Assert.True(mcpOptions.Capabilities.Resources.Subscribe, "Subscribe capability should be preserved"); |
| 117 | + |
| 118 | + // Verify resources are registered |
| 119 | + Assert.NotNull(mcpOptions.ResourceCollection); |
| 120 | + Assert.NotEmpty(mcpOptions.ResourceCollection); |
| 121 | + Assert.Contains(mcpOptions.ResourceCollection, r => r.ProtocolResource?.Name == "live_resource"); |
| 122 | + } |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void ResourcesCapability_IsCreated_WhenOnlyResourcesAreProvided() |
| 126 | + { |
| 127 | + // Test that ResourcesCapability is created even without handlers or manual setting |
| 128 | + var services = new ServiceCollection(); |
| 129 | + var builder = services.AddMcpServer() |
| 130 | + .WithResources<LiveResources>() |
| 131 | + .WithStdioServerTransport(); |
| 132 | + |
| 133 | + var serviceProvider = services.BuildServiceProvider(); |
| 134 | + var mcpOptions = serviceProvider.GetRequiredService<IOptions<McpServerOptions>>().Value; |
| 135 | + |
| 136 | + // Resources are registered |
| 137 | + Assert.NotNull(mcpOptions.ResourceCollection); |
| 138 | + Assert.NotEmpty(mcpOptions.ResourceCollection); |
| 139 | + |
| 140 | + // But ResourcesCapability should NOT be created just because resources exist! |
| 141 | + // The capability is only created when resources are actually used by the server |
| 142 | + // This is correct behavior - the capability is set up during server initialization |
| 143 | + // in McpServerImpl.ConfigureResources |
| 144 | + } |
| 145 | + |
| 146 | + [McpServerResourceType] |
| 147 | + public sealed class LiveResources |
| 148 | + { |
| 149 | + [McpServerResource] |
| 150 | + public static string LiveResource() => "Live content"; |
| 151 | + } |
| 152 | +} |
0 commit comments