Skip to content

Commit 82b62cf

Browse files
committed
Fix ListResourceTemplatesHandler issue
1 parent c6ccf02 commit 82b62cf

File tree

2 files changed

+195
-1
lines changed

2 files changed

+195
-1
lines changed

src/ModelContextProtocol/McpServerOptionsSetup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private static void OverwriteWithSetHandlers(McpServerHandlers handlers, McpServ
9191
}
9292

9393
ResourcesCapability? resourcesCapability = options.Capabilities?.Resources;
94-
if (handlers.ListResourcesHandler is not null || handlers.ReadResourceHandler is not null)
94+
if (handlers.ListResourceTemplatesHandler is not null || handlers.ListResourcesHandler is not null || handlers.ReadResourceHandler is not null)
9595
{
9696
resourcesCapability ??= new();
9797
optionsHandlers.ListResourceTemplatesHandler = handlers.ListResourceTemplatesHandler ?? optionsHandlers.ListResourceTemplatesHandler;
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Options;
3+
using ModelContextProtocol.Protocol;
4+
using ModelContextProtocol.Server;
5+
6+
namespace ModelContextProtocol.Tests.Configuration;
7+
8+
public class McpServerOptionsSetupTests
9+
{
10+
#region Prompt Handler Tests
11+
[Fact]
12+
public void Configure_WithListPromptsHandler_CreatesPromptsCapability()
13+
{
14+
var services = new ServiceCollection();
15+
services.AddMcpServer()
16+
.WithListPromptsHandler(async (request, ct) => new ListPromptsResult());
17+
18+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
19+
20+
Assert.NotNull(options.Handlers?.ListPromptsHandler);
21+
Assert.NotNull(options.Capabilities?.Prompts);
22+
}
23+
24+
[Fact]
25+
public void Configure_WithGetPromptHandler_CreatesPromptsCapability()
26+
{
27+
var services = new ServiceCollection();
28+
services.AddMcpServer()
29+
.WithGetPromptHandler(async (request, ct) => new GetPromptResult());
30+
31+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
32+
33+
Assert.NotNull(options.Handlers?.GetPromptHandler);
34+
Assert.NotNull(options.Capabilities?.Prompts);
35+
}
36+
#endregion
37+
38+
#region Resource Handler Tests
39+
[Fact]
40+
public void Configure_WithListResourceTemplatesHandler_CreatesResourcesCapability()
41+
{
42+
var services = new ServiceCollection();
43+
services.AddMcpServer()
44+
.WithListResourceTemplatesHandler(async (request, ct) => new ListResourceTemplatesResult());
45+
46+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
47+
48+
Assert.NotNull(options.Handlers?.ListResourceTemplatesHandler);
49+
Assert.NotNull(options.Capabilities?.Resources);
50+
}
51+
52+
[Fact]
53+
public void Configure_WithListResourcesHandler_CreatesResourcesCapability()
54+
{
55+
var services = new ServiceCollection();
56+
services.AddMcpServer()
57+
.WithListResourcesHandler(async (request, ct) => new ListResourcesResult());
58+
59+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
60+
61+
Assert.NotNull(options.Handlers?.ListResourcesHandler);
62+
Assert.NotNull(options.Capabilities?.Resources);
63+
}
64+
65+
[Fact]
66+
public void Configure_WithReadResourceHandler_CreatesResourcesCapability()
67+
{
68+
var services = new ServiceCollection();
69+
services.AddMcpServer()
70+
.WithReadResourceHandler(async (request, ct) => new ReadResourceResult());
71+
72+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
73+
74+
Assert.NotNull(options.Handlers?.ReadResourceHandler);
75+
Assert.NotNull(options.Capabilities?.Resources);
76+
}
77+
78+
[Fact]
79+
public void Configure_WithSubscribeToResourcesHandler_And_WithOtherResourcesHandler_EnablesSubscription()
80+
{
81+
var services = new ServiceCollection();
82+
services.AddMcpServer()
83+
.WithListResourcesHandler(async (request, ct) => new ListResourcesResult())
84+
.WithSubscribeToResourcesHandler(async (request, ct) => new EmptyResult());
85+
86+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
87+
88+
Assert.NotNull(options.Handlers?.ListResourcesHandler);
89+
Assert.NotNull(options.Handlers?.SubscribeToResourcesHandler);
90+
Assert.NotNull(options.Capabilities?.Resources);
91+
Assert.True(options.Capabilities.Resources.Subscribe);
92+
}
93+
94+
[Fact]
95+
public void Configure_WithUnsubscribeFromResourcesHandler_And_WithOtherResourcesHandler_EnablesSubscription()
96+
{
97+
var services = new ServiceCollection();
98+
services.AddMcpServer()
99+
.WithListResourcesHandler(async (request, ct) => new ListResourcesResult())
100+
.WithUnsubscribeFromResourcesHandler(async (request, ct) => new EmptyResult());
101+
102+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
103+
104+
Assert.NotNull(options.Handlers?.ListResourcesHandler);
105+
Assert.NotNull(options.Handlers?.UnsubscribeFromResourcesHandler);
106+
Assert.NotNull(options.Capabilities?.Resources);
107+
Assert.True(options.Capabilities.Resources.Subscribe);
108+
}
109+
110+
[Fact]
111+
public void Configure_WithSubscribeToResourcesHandler_WithoutOtherResourcesHandler_DoesNotCreateResourcesCapability()
112+
{
113+
var services = new ServiceCollection();
114+
services.AddMcpServer()
115+
.WithSubscribeToResourcesHandler(async (request, ct) => new EmptyResult());
116+
117+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
118+
119+
Assert.Null(options.Handlers?.SubscribeToResourcesHandler);
120+
Assert.Null(options.Capabilities?.Resources);
121+
}
122+
123+
[Fact]
124+
public void Configure_WithUnsubscribeFromResourcesHandler_WithoutOtherResourcesHandler_DoesNotCreateResourcesCapability()
125+
{
126+
var services = new ServiceCollection();
127+
services.AddMcpServer()
128+
.WithUnsubscribeFromResourcesHandler(async (request, ct) => new EmptyResult());
129+
130+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
131+
132+
Assert.Null(options.Handlers?.UnsubscribeFromResourcesHandler);
133+
Assert.Null(options.Capabilities?.Resources);
134+
}
135+
#endregion
136+
137+
#region Tool Handler Tests
138+
[Fact]
139+
public void Configure_WithListToolsHandler_CreatesToolsCapability()
140+
{
141+
var services = new ServiceCollection();
142+
services.AddMcpServer()
143+
.WithListToolsHandler(async (request, ct) => new ListToolsResult());
144+
145+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
146+
147+
Assert.NotNull(options.Handlers?.ListToolsHandler);
148+
Assert.NotNull(options.Capabilities?.Tools);
149+
}
150+
151+
[Fact]
152+
public void Configure_WithCallToolHandler_CreatesToolsCapability()
153+
{
154+
var services = new ServiceCollection();
155+
services.AddMcpServer()
156+
.WithCallToolHandler(async (request, ct) => new CallToolResult());
157+
158+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
159+
160+
Assert.NotNull(options.Handlers?.CallToolHandler);
161+
Assert.NotNull(options.Capabilities?.Tools);
162+
}
163+
#endregion
164+
165+
#region Logging Handler Tests
166+
[Fact]
167+
public void Configure_WithSetLoggingLevelHandler_CreatesLoggingCapability()
168+
{
169+
var services = new ServiceCollection();
170+
services.AddMcpServer()
171+
.WithSetLoggingLevelHandler(async (request, ct) => new EmptyResult());
172+
173+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
174+
175+
Assert.NotNull(options.Handlers?.SetLoggingLevelHandler);
176+
Assert.NotNull(options.Capabilities?.Logging);
177+
}
178+
#endregion
179+
180+
#region Completion Handler Tests
181+
[Fact]
182+
public void Configure_WithCompleteHandler_CreatesCompletionsCapability()
183+
{
184+
var services = new ServiceCollection();
185+
services.AddMcpServer()
186+
.WithCompleteHandler(async (request, ct) => new CompleteResult());
187+
188+
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
189+
190+
Assert.NotNull(options.Handlers?.CompleteHandler);
191+
Assert.NotNull(options.Capabilities?.Completions);
192+
}
193+
#endregion
194+
}

0 commit comments

Comments
 (0)