Skip to content

Use TryAddEnumerable for McpServerOptionsSetup #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ModelContextProtocol.Server;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.Extensions.DependencyInjection;

Expand All @@ -17,7 +18,7 @@ public static class McpServerServiceCollectionExtensions
public static IMcpServerBuilder AddMcpServer(this IServiceCollection services, Action<McpServerOptions>? configureOptions = null)
{
services.AddOptions();
services.AddTransient<IConfigureOptions<McpServerOptions>, McpServerOptionsSetup>();
services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<McpServerOptions>, McpServerOptionsSetup>());
if (configureOptions is not null)
{
services.Configure(configureOptions);
Expand Down
51 changes: 51 additions & 0 deletions tests/ModelContextProtocol.Tests/SseIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using ModelContextProtocol.Server;
using ModelContextProtocol.Tests.Utils;
using ModelContextProtocol.Utils.Json;
using TestServerWithHosting.Tools;

namespace ModelContextProtocol.Tests;

Expand Down Expand Up @@ -95,6 +96,56 @@ public async Task ConnectAndReceiveNotification_InMemoryServer()
Assert.Equal("Hello from server!", message);
}

[Fact]
public async Task AddMcpServer_CanBeCalled_MultipleTimes()
{
var firstOptionsCallbackCallCount = 0;
var secondOptionsCallbackCallCount = 0;

Builder.Services.AddMcpServer(options =>
{
Interlocked.Increment(ref firstOptionsCallbackCallCount);
})
.WithTools<EchoTool>();

Builder.Services.AddMcpServer(options =>
{
Interlocked.Increment(ref secondOptionsCallbackCallCount);
})
.WithTools<SampleLlmTool>();


await using var app = Builder.Build();
app.MapMcp();
await app.StartAsync(TestContext.Current.CancellationToken);

using var httpClient = CreateHttpClient();
await using var mcpClient = await ConnectMcpClient(httpClient);

// Options can be lazily initialized, but they must be instantiated by the time an MCP client can finish connecting.
// Callbacks can be called multiple times if configureOptionsAsync is configured, because that uses the IOptionsFactory,
// but that's not the case in this test.
Assert.Equal(1, firstOptionsCallbackCallCount);
Assert.Equal(1, secondOptionsCallbackCallCount);

var tools = await mcpClient.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);

Assert.Equal(2, tools.Count);
Assert.Contains(tools, tools => tools.Name == "Echo");
Assert.Contains(tools, tools => tools.Name == "sampleLLM");

var echoResponse = await mcpClient.CallToolAsync(
"Echo",
new Dictionary<string, object?>
{
["message"] = "from client!"
},
cancellationToken: TestContext.Current.CancellationToken);
var textContent = Assert.Single(echoResponse.Content, c => c.Type == "text");

Assert.Equal("hello from client!", textContent.Text);
}

private static void MapAbsoluteEndpointUriMcp(IEndpointRouteBuilder endpoints)
{
var loggerFactory = endpoints.ServiceProvider.GetRequiredService<ILoggerFactory>();
Expand Down