Add elicitation capability (#467)

* Add elicitation capability
This commit is contained in:
Stephen Toub 2025-05-29 22:03:03 -04:00 • committed by GitHub
commit 5cc7c00456
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -92,21 +92,21 @@ public class McpServerTests : LoggedTest
}
[Fact]
public async Task RequestSamplingAsync_Should_Throw_Exception_If_Client_Does_Not_Support_Sampling()
public async Task SampleAsync_Should_Throw_Exception_If_Client_Does_Not_Support_Sampling()
{
// Arrange
await using var transport = new TestServerTransport();
await using var server = McpServerFactory.Create(transport, _options, LoggerFactory);
SetClientCapabilities(server, new ClientCapabilities());
var action = async () => await server.RequestSamplingAsync(new CreateMessageRequestParams { Messages = [] }, CancellationToken.None);
var action = async () => await server.SampleAsync(new CreateMessageRequestParams { Messages = [] }, CancellationToken.None);
// Act & Assert
await Assert.ThrowsAsync<InvalidOperationException>(action);
}
[Fact]
public async Task RequestSamplingAsync_Should_SendRequest()
public async Task SampleAsync_Should_SendRequest()
{
// Arrange
await using var transport = new TestServerTransport();
@ -116,7 +116,7 @@ public class McpServerTests : LoggedTest
var runTask = server.RunAsync(TestContext.Current.CancellationToken);
// Act
var result = await server.RequestSamplingAsync(new CreateMessageRequestParams { Messages = [] }, CancellationToken.None);
var result = await server.SampleAsync(new CreateMessageRequestParams { Messages = [] }, CancellationToken.None);
Assert.NotNull(result);
Assert.NotEmpty(transport.SentMessages);
@ -161,6 +161,40 @@ public class McpServerTests : LoggedTest
await runTask;
}
[Fact]
public async Task ElicitAsync_Should_Throw_Exception_If_Client_Does_Not_Support_Elicitation()
{
// Arrange
await using var transport = new TestServerTransport();
await using var server = McpServerFactory.Create(transport, _options, LoggerFactory);
SetClientCapabilities(server, new ClientCapabilities());
// Act & Assert
await Assert.ThrowsAsync<InvalidOperationException>(async () => await server.ElicitAsync(new ElicitRequestParams(), CancellationToken.None));
}
[Fact]
public async Task ElicitAsync_Should_SendRequest()
{
// Arrange
await using var transport = new TestServerTransport();
await using var server = McpServerFactory.Create(transport, _options, LoggerFactory);
SetClientCapabilities(server, new ClientCapabilities { Elicitation = new ElicitationCapability() });
var runTask = server.RunAsync(TestContext.Current.CancellationToken);
// Act
var result = await server.ElicitAsync(new ElicitRequestParams(), CancellationToken.None);
// Assert
Assert.NotNull(result);
Assert.NotEmpty(transport.SentMessages);
Assert.IsType<JsonRpcRequest>(transport.SentMessages[0]);
Assert.Equal(RequestMethods.ElicitationCreate, ((JsonRpcRequest)transport.SentMessages[0]).Method);
await transport.DisposeAsync();
await runTask;
}
[Fact]
public async Task Can_Handle_Ping_Requests()
{