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:
parent
39b0ed3c18
commit
5f992994c3
4 changed files with 1588 additions and 1161 deletions
|
|
@ -90,7 +90,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
|
|||
public void Adds_Prompts_To_Server()
|
||||
{
|
||||
var serverOptions = ServiceProvider.GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
var prompts = serverOptions?.Capabilities?.Prompts?.PromptCollection;
|
||||
var prompts = serverOptions.PromptCollection;
|
||||
Assert.NotNull(prompts);
|
||||
Assert.NotEmpty(prompts);
|
||||
}
|
||||
|
|
@ -137,7 +137,7 @@ public partial class McpServerBuilderExtensionsPromptsTests : ClientServerTestBa
|
|||
Assert.False(notificationRead.IsCompleted);
|
||||
|
||||
var serverOptions = ServiceProvider.GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
var serverPrompts = serverOptions.Capabilities?.Prompts?.PromptCollection;
|
||||
var serverPrompts = serverOptions.PromptCollection;
|
||||
Assert.NotNull(serverPrompts);
|
||||
|
||||
var newPrompt = McpServerPrompt.Create([McpServerPrompt(Name = "NewPrompt")] () => "42");
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
|
|||
public void Adds_Resources_To_Server()
|
||||
{
|
||||
var serverOptions = ServiceProvider.GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
var resources = serverOptions?.Capabilities?.Resources?.ResourceCollection;
|
||||
var resources = serverOptions.ResourceCollection;
|
||||
Assert.NotNull(resources);
|
||||
Assert.NotEmpty(resources);
|
||||
}
|
||||
|
|
@ -172,7 +172,7 @@ public partial class McpServerBuilderExtensionsResourcesTests : ClientServerTest
|
|||
Assert.False(notificationRead.IsCompleted);
|
||||
|
||||
var serverOptions = ServiceProvider.GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
var serverResources = serverOptions.Capabilities?.Resources?.ResourceCollection;
|
||||
var serverResources = serverOptions.ResourceCollection;
|
||||
Assert.NotNull(serverResources);
|
||||
|
||||
var newResource = McpServerResource.Create([McpServerResource(Name = "NewResource")] () => "42");
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
|
|||
public void Adds_Tools_To_Server()
|
||||
{
|
||||
var serverOptions = ServiceProvider.GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
var tools = serverOptions.Capabilities?.Tools?.ToolCollection;
|
||||
var tools = serverOptions.ToolCollection;
|
||||
Assert.NotNull(tools);
|
||||
Assert.NotEmpty(tools);
|
||||
}
|
||||
|
|
@ -201,7 +201,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
|
|||
Assert.False(notificationRead.IsCompleted);
|
||||
|
||||
var serverOptions = ServiceProvider.GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
var serverTools = serverOptions.Capabilities?.Tools?.ToolCollection;
|
||||
var serverTools = serverOptions.ToolCollection;
|
||||
Assert.NotNull(serverTools);
|
||||
|
||||
var newTool = McpServerTool.Create([McpServerTool(Name = "NewTool")] () => "42");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,194 @@
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ModelContextProtocol.Protocol;
|
||||
using ModelContextProtocol.Server;
|
||||
|
||||
namespace ModelContextProtocol.Tests.Configuration;
|
||||
|
||||
public class McpServerOptionsSetupTests
|
||||
{
|
||||
#region Prompt Handler Tests
|
||||
[Fact]
|
||||
public void Configure_WithListPromptsHandler_CreatesPromptsCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithListPromptsHandler(async (request, ct) => new ListPromptsResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.ListPromptsHandler);
|
||||
Assert.NotNull(options.Capabilities?.Prompts);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Configure_WithGetPromptHandler_CreatesPromptsCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithGetPromptHandler(async (request, ct) => new GetPromptResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.GetPromptHandler);
|
||||
Assert.NotNull(options.Capabilities?.Prompts);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Resource Handler Tests
|
||||
[Fact]
|
||||
public void Configure_WithListResourceTemplatesHandler_CreatesResourcesCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithListResourceTemplatesHandler(async (request, ct) => new ListResourceTemplatesResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.ListResourceTemplatesHandler);
|
||||
Assert.NotNull(options.Capabilities?.Resources);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Configure_WithListResourcesHandler_CreatesResourcesCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithListResourcesHandler(async (request, ct) => new ListResourcesResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.ListResourcesHandler);
|
||||
Assert.NotNull(options.Capabilities?.Resources);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Configure_WithReadResourceHandler_CreatesResourcesCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithReadResourceHandler(async (request, ct) => new ReadResourceResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.ReadResourceHandler);
|
||||
Assert.NotNull(options.Capabilities?.Resources);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Configure_WithSubscribeToResourcesHandler_And_WithOtherResourcesHandler_EnablesSubscription()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithListResourcesHandler(async (request, ct) => new ListResourcesResult())
|
||||
.WithSubscribeToResourcesHandler(async (request, ct) => new EmptyResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.ListResourcesHandler);
|
||||
Assert.NotNull(options.Handlers.SubscribeToResourcesHandler);
|
||||
Assert.NotNull(options.Capabilities?.Resources);
|
||||
Assert.True(options.Capabilities.Resources.Subscribe);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Configure_WithUnsubscribeFromResourcesHandler_And_WithOtherResourcesHandler_EnablesSubscription()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithListResourcesHandler(async (request, ct) => new ListResourcesResult())
|
||||
.WithUnsubscribeFromResourcesHandler(async (request, ct) => new EmptyResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.ListResourcesHandler);
|
||||
Assert.NotNull(options.Handlers.UnsubscribeFromResourcesHandler);
|
||||
Assert.NotNull(options.Capabilities?.Resources);
|
||||
Assert.True(options.Capabilities.Resources.Subscribe);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Configure_WithSubscribeToResourcesHandler_WithoutOtherResourcesHandler_DoesNotCreateResourcesCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithSubscribeToResourcesHandler(async (request, ct) => new EmptyResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.Null(options.Handlers.SubscribeToResourcesHandler);
|
||||
Assert.Null(options.Capabilities?.Resources);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Configure_WithUnsubscribeFromResourcesHandler_WithoutOtherResourcesHandler_DoesNotCreateResourcesCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithUnsubscribeFromResourcesHandler(async (request, ct) => new EmptyResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.Null(options.Handlers.UnsubscribeFromResourcesHandler);
|
||||
Assert.Null(options.Capabilities?.Resources);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Tool Handler Tests
|
||||
[Fact]
|
||||
public void Configure_WithListToolsHandler_CreatesToolsCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithListToolsHandler(async (request, ct) => new ListToolsResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.ListToolsHandler);
|
||||
Assert.NotNull(options.Capabilities?.Tools);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Configure_WithCallToolHandler_CreatesToolsCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithCallToolHandler(async (request, ct) => new CallToolResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.CallToolHandler);
|
||||
Assert.NotNull(options.Capabilities?.Tools);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Logging Handler Tests
|
||||
[Fact]
|
||||
public void Configure_WithSetLoggingLevelHandler_CreatesLoggingCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithSetLoggingLevelHandler(async (request, ct) => new EmptyResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.SetLoggingLevelHandler);
|
||||
Assert.NotNull(options.Capabilities?.Logging);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Completion Handler Tests
|
||||
[Fact]
|
||||
public void Configure_WithCompleteHandler_CreatesCompletionsCapability()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddMcpServer()
|
||||
.WithCompleteHandler(async (request, ct) => new CompleteResult());
|
||||
|
||||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<McpServerOptions>>().Value;
|
||||
|
||||
Assert.NotNull(options.Handlers.CompleteHandler);
|
||||
Assert.NotNull(options.Capabilities?.Completions);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Loading…
Reference in a new issue