Skip to content

Commit a958bb4

Browse files
Copilotstephentoub
andcommitted
Add tests for null collections behavior
Added NullCollectionTests class to verify that handlers are NOT synthesized when collections are explicitly null. These tests ensure we don't unnecessarily create capabilities when nothing is configured. Co-authored-by: stephentoub <[email protected]>
1 parent 77cc43f commit a958bb4

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

tests/ModelContextProtocol.Tests/Server/EmptyCollectionTests.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,72 @@ public async Task EmptyPromptCollection_CanGetPromptAfterAddingDynamically()
158158
Assert.Equal("dynamic prompt content", ((TextContent)result.Messages[0].Content[0]).Text);
159159
}
160160
}
161+
162+
/// <summary>
163+
/// Tests to verify that handlers are NOT synthesized when collections are null.
164+
/// This ensures we don't unnecessarily create capabilities when nothing is configured.
165+
/// </summary>
166+
public class NullCollectionTests : ClientServerTestBase
167+
{
168+
public NullCollectionTests(ITestOutputHelper testOutputHelper)
169+
: base(testOutputHelper)
170+
{
171+
}
172+
173+
protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder)
174+
{
175+
mcpServerBuilder.Services.Configure<McpServerOptions>(options =>
176+
{
177+
options.ServerInfo = new Implementation
178+
{
179+
Name = "null-collection-test",
180+
Version = "1.0.0"
181+
};
182+
183+
// Explicitly leave collections as null - no handlers should be synthesized
184+
options.ResourceCollection = null;
185+
options.ToolCollection = null;
186+
options.PromptCollection = null;
187+
});
188+
}
189+
190+
[Fact]
191+
public void NullCollections_DoNotCreateCapabilities()
192+
{
193+
// Verify that when collections are null (and no handlers are provided),
194+
// the specific capability properties remain null
195+
Assert.Null(Server.ServerCapabilities.Resources);
196+
Assert.Null(Server.ServerCapabilities.Tools);
197+
Assert.Null(Server.ServerCapabilities.Prompts);
198+
}
199+
200+
[Fact]
201+
public async Task NullResourceCollection_ListReturnsEmpty()
202+
{
203+
using var client = await CreateMcpClientForServer();
204+
205+
// When no resource capability is configured, list should return empty
206+
var resources = await client.ListResourcesAsync();
207+
Assert.Empty(resources);
208+
}
209+
210+
[Fact]
211+
public async Task NullToolCollection_ListReturnsEmpty()
212+
{
213+
using var client = await CreateMcpClientForServer();
214+
215+
// When no tool capability is configured, list should return empty
216+
var tools = await client.ListToolsAsync();
217+
Assert.Empty(tools);
218+
}
219+
220+
[Fact]
221+
public async Task NullPromptCollection_ListReturnsEmpty()
222+
{
223+
using var client = await CreateMcpClientForServer();
224+
225+
// When no prompt capability is configured, list should return empty
226+
var prompts = await client.ListPromptsAsync();
227+
Assert.Empty(prompts);
228+
}
229+
}

0 commit comments

Comments
 (0)