You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add IServiceCollection extension methods for registering tools and prompts. #240
Is your feature request related to a problem? Please describe.
I’m working on implementing an MCP server within [OrchardCore CMS](https://github.com/OrchardCMS/OrchardCore). OrchardCore is a modular framework that allows features and modules to dynamically register services as needed. This modularity enables me to register MCP-related services conditionally, based on which features are enabled.
Describe the solution you'd like
I’d like to register Tools, Prompts and Resource using an IServiceCollection extension. This approach allows multiple features to independently register their own tools, resources, prompts, and more—promoting modularity and extensibility across the application.
Most of our documentation shows WithToolsFromAssembly (which is effectively an IServiceCollection extension method), but you do not have to use that. Have you looked at the McpServerBuilderExtensions?
You can get access to an IMcpServerBuilder from the IServiceCollection by calling builder.Services.AddMcpServer() as shown in our README.
This approach allows multiple features to independently register their own tools, resources, prompts, and more
This should still be possible with AddMcpServer(). Like most Add extension methods to IServiceCollection it's safe to call multiple times to get access to an IMcpServerBuilder from multiple projects.
@halter73 thank you! Is there a downside from offering IServiceCollection extensions. Although it is safe to call AddMcpServer() multiple times, it feel weird practice to me and could cause issue later of as AddMcpServer() grows.
And it will not cause issues later. It's a bug for an IServiceCollection Add extension methods to not to be idempotent in cases where core services are getting added like AddOptions and AddMcpServer. That's why I recently added the AddMcpServer_CanBeCalled_MultipleTimes test.
Activity
halter73 commentedon Apr 9, 2025
Most of our documentation shows
WithToolsFromAssembly(which is effectively an IServiceCollection extension method), but you do not have to use that. Have you looked at the McpServerBuilderExtensions?You can get access to an IMcpServerBuilder from the IServiceCollection by calling
builder.Services.AddMcpServer()as shown in our README.This should still be possible with
AddMcpServer(). Like mostAddextension methods toIServiceCollectionit's safe to call multiple times to get access to an IMcpServerBuilder from multiple projects.MikeAlhayek commentedon Apr 9, 2025
@halter73 thank you! Is there a downside from offering
IServiceCollectionextensions. Although it is safe to callAddMcpServer()multiple times, it feel weird practice to me and could cause issue later of asAddMcpServer()grows.halter73 commentedon Apr 9, 2025
It's an extremely common practice. Look how often we wantonly call AddOptions() in dotnet itself.
And it will not cause issues later. It's a bug for an IServiceCollection Add extension methods to not to be idempotent in cases where core services are getting added like AddOptions and AddMcpServer. That's why I recently added the
AddMcpServer_CanBeCalled_MultipleTimestest.csharp-sdk/tests/ModelContextProtocol.AspNetCore.Tests/SseIntegrationTests.cs
Lines 99 to 115 in 0730038