Decouple Handlers and Collections from protocol types (#765)

* Decouple Handlers and Collections (e.g. ToolsHandler and ToolsCollection) from Capabilities by promoting them to the options types.

Also, group all handlers in container classes, for server, I decided to move McpServerHandlers down to Mcp.Core and move the NotificationHandlers there too.

Also, Fix ListResourceTemplatesHandler issue.

* Add remarks for empty protocol capability types

* Keep JsonIgnore properties and mark them as Obsolete

* Continue supporting Obsolete members

* Update Obsolete messages to indicate the member will be removed

* Add EditorBrowsable Never to the remaining obsolete members
This commit is contained in:
David Cantú 2025-09-24 09:42:03 -05:00 • committed by GitHub
commit 5f992994c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 1588 additions and 1161 deletions

View file

@ -251,9 +251,7 @@ public abstract class HttpServerIntegrationTests : LoggedTest, IClassFixture<Sse
int samplingHandlerCalls = 0;
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
McpClientOptions options = new();
options.Capabilities = new();
options.Capabilities.Sampling ??= new();
options.Capabilities.Sampling.SamplingHandler = async (_, _, _) =>
options.Handlers.SamplingHandler = async (_, _, _) =>
{
samplingHandlerCalls++;
return new CreateMessageResult

View file

@ -161,29 +161,26 @@ public abstract class MapMcpTests(ITestOutputHelper testOutputHelper) : KestrelI
await app.StartAsync(TestContext.Current.CancellationToken);
var sampleCount = 0;
var clientOptions = new McpClientOptions
var clientOptions = new McpClientOptions()
{
Capabilities = new()
Handlers = new()
{
Sampling = new()
SamplingHandler = async (parameters, _, _) =>
{
SamplingHandler = async (parameters, _, _) =>
{
Assert.NotNull(parameters?.Messages);
var message = Assert.Single(parameters.Messages);
Assert.Equal(Role.User, message.Role);
Assert.Equal("Test prompt for sampling", Assert.IsType<TextContentBlock>(message.Content).Text);
Assert.NotNull(parameters?.Messages);
var message = Assert.Single(parameters.Messages);
Assert.Equal(Role.User, message.Role);
Assert.Equal("Test prompt for sampling", Assert.IsType<TextContentBlock>(message.Content).Text);
sampleCount++;
return new CreateMessageResult
{
Model = "test-model",
Role = Role.Assistant,
Content = new TextContentBlock { Text = "Sampling response from client" },
};
},
},
},
sampleCount++;
return new CreateMessageResult
{
Model = "test-model",
Role = Role.Assistant,
Content = new TextContentBlock { Text = "Sampling response from client" },
};
}
}
};
await using var mcpClient = await ConnectAsync(clientOptions: clientOptions);

View file

@ -102,9 +102,7 @@ public class StatelessServerTests(ITestOutputHelper outputHelper) : KestrelInMem
await StartAsync();
var mcpClientOptions = new McpClientOptions();
mcpClientOptions.Capabilities = new();
mcpClientOptions.Capabilities.Sampling ??= new();
mcpClientOptions.Capabilities.Sampling.SamplingHandler = (_, _, _) =>
mcpClientOptions.Handlers.SamplingHandler = (_, _, _) =>
{
throw new UnreachableException();
};
@ -122,9 +120,7 @@ public class StatelessServerTests(ITestOutputHelper outputHelper) : KestrelInMem
await StartAsync();
var mcpClientOptions = new McpClientOptions();
mcpClientOptions.Capabilities = new();
mcpClientOptions.Capabilities.Roots ??= new();
mcpClientOptions.Capabilities.Roots.RootsHandler = (_, _) =>
mcpClientOptions.Handlers.RootsHandler = (_, _) =>
{
throw new UnreachableException();
};
@ -142,9 +138,7 @@ public class StatelessServerTests(ITestOutputHelper outputHelper) : KestrelInMem
await StartAsync();
var mcpClientOptions = new McpClientOptions();
mcpClientOptions.Capabilities = new();
mcpClientOptions.Capabilities.Elicitation ??= new();
mcpClientOptions.Capabilities.Elicitation.ElicitationHandler = (_, _) =>
mcpClientOptions.Handlers.ElicitationHandler = (_, _) =>
{
throw new UnreachableException();
};