Skip to content

Commit 34934d5

Browse files
committed
Add stateless test
1 parent 96a5c09 commit 34934d5

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/ModelContextProtocol/Server/McpServerExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ private static void ThrowIfElicitationUnsupported(IMcpServer server)
258258
{
259259
if (server.ServerOptions.KnownClientInfo is not null)
260260
{
261-
throw new InvalidOperationException("Elicititation requests are not supported in stateless mode.");
261+
throw new InvalidOperationException("Elicitation is not supported in stateless mode.");
262262
}
263263

264264
throw new InvalidOperationException("Client does not support elicitation requests.");

tests/ModelContextProtocol.AspNetCore.Tests/StatelessServerTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,26 @@ public async Task RootsRequest_Fails_WithInvalidOperationException()
136136
Assert.Equal("Server to client requests are not supported in stateless mode.", toolContent.Text);
137137
}
138138

139+
[Fact]
140+
public async Task ElicitRequest_Fails_WithInvalidOperationException()
141+
{
142+
await StartAsync();
143+
144+
var mcpClientOptions = new McpClientOptions();
145+
mcpClientOptions.Capabilities = new();
146+
mcpClientOptions.Capabilities.Elicitation ??= new();
147+
mcpClientOptions.Capabilities.Elicitation.ElicitationHandler = (_, _) =>
148+
{
149+
throw new UnreachableException();
150+
};
151+
152+
await using var client = await ConnectMcpClientAsync(mcpClientOptions);
153+
154+
var toolResponse = await client.CallToolAsync("testElicitationErrors", cancellationToken: TestContext.Current.CancellationToken);
155+
var toolContent = Assert.Single(toolResponse.Content);
156+
Assert.Equal("Server to client requests are not supported in stateless mode.", toolContent.Text);
157+
}
158+
139159
[Fact]
140160
public async Task UnsolicitedNotification_Fails_WithInvalidOperationException()
141161
{
@@ -206,6 +226,21 @@ public static async Task<string> TestRootsErrors(IMcpServer server)
206226
return ex.Message;
207227
}
208228

229+
[McpServerTool(Name = "testElicitationErrors")]
230+
public static async Task<string> TestElicitationErrors(IMcpServer server)
231+
{
232+
const string expectedElicitationErrorMessage = "Elicitation is not supported in stateless mode.";
233+
234+
// Even when the client has elicitation support, it should not be advertised in stateless mode.
235+
Assert.Null(server.ClientCapabilities);
236+
237+
var requestElicitationEx = Assert.Throws<InvalidOperationException>(() => server.ElicitAsync(new()));
238+
Assert.Equal(expectedElicitationErrorMessage, requestElicitationEx.Message);
239+
240+
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() => server.SendRequestAsync(new JsonRpcRequest { Method = RequestMethods.ElicitationCreate }));
241+
return ex.Message;
242+
}
243+
209244
[McpServerTool(Name = "testScope")]
210245
public static string? TestScope(ScopedService scopedService) => scopedService.State;
211246

0 commit comments

Comments
 (0)