Skip to content

Add IServiceCollection extension methods for registering tools and prompts. #240

Closed

Description

@MikeAlhayek

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.

Activity

  1. halter73 commented on Apr 9, 2025

    @halter73
    Contributor

    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.

  2. MikeAlhayek commented on Apr 9, 2025

    @MikeAlhayek
    ContributorAuthor

    @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.

  3. halter73 commented on Apr 9, 2025

    @halter73
    Contributor

    it feels weird practice to me and could cause issue later of as AddMcpServer() grows.

    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_MultipleTimes test.

    [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>();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions