Skip to content

Host multiple MCP servers with different HTTP endpoints in one process using keyed services #1202

Open

Description

@flensrocker

Is your feature request related to a problem? Please describe.

In my "modulith" (ASP.NET Core) application I want to provide multiple MCP servers/endpoints, so that every endpoint delivers a subset of tools, so that MCP clients are not "overwhelmed" by the amount of tools.

Describe the solution you'd like

I would like to register multiple MCP server as keyed services in dependency injection and call MapMcp multiple times with different routes providing the key used for the keyed services.

builder.AddKeyedMcpServer("key", ...) // etc.

app.MapKeyedMcp("key", "/mcp/key", ...) // etc.

Describe alternatives you've considered

I haven't considered alternatives, because keyed services seems natural for me. Only viable alternative would be "going microservices" and split the application into multiple containers.

Activity

  1. relusion commented on Feb 2, 2026

    @relusion

    I've built a library that addresses this use case.
    (https://github.com/relusion/Mcp.AspNetCore.MultiHost)

    It takes a slightly different approach than keyed services - each MCP host gets its own isolated DI container with configurable service bridging from the main application.

    Usage example:

    builder.Services.AddMcpMultiHost(options =>
    {
        options.AddHost("admin", host =>
        {
            host.WithRoutePrefix("/mcp/admin")
                .ConfigureMcpServer(mcp => mcp
                    .WithHttpTransport()
                    .WithTools<AdminTools>());
        });
    
        options.AddHost("user", host =>
        {
            host.WithRoutePrefix("/mcp/user")
                .ConfigureMcpServer(mcp => mcp
                    .WithHttpTransport()
                    .WithTools<UserTools>());
        });
    });
    
    app.MapMcpMultiHost();

    Features:

    • Per-host DI isolation (tools in /mcp/admin are not visible in /mcp/user)
    • Service bridging (share logging, config, custom services between hosts)
    • Optional discovery endpoint (/mcp/_hosts)
    • Works with existing MCP SDK public APIs

    Feedback’s welcome... Thanks.

  2. halter73 commented on Feb 10, 2026

    @halter73
    Contributor

    Thanks for sharing @relusion. Mcp.AspNetCore.MultiHost looks cool! I'm glad you built on top of the MCP SDK. It seems really useful for scenarios where you want a unique set of DI services per endpoint. Let us know if you need anything for your scenarios.

    I would like to register multiple MCP server as keyed services in dependency injection and call MapMcp multiple times with different routes providing the key used for the keyed services.

    If you just need a specific set of tools per endpoint which are resolved using keyed services, that shouldn't be too hard to do without an extra library.

    This is pretty close to something we already demonstrate in the AspNetCoreMcpPerSessionTools sample, but instead of calling ConcurrentDictionary<string, McpServerTool[]>.TryGetValue(toolCategory, out var tools) in ConfigureSessionOptions, you'd call httpContext.RequestServices.GetKeyedServices<McpServerTool>(toolCategory).

    // Map MCP with route parameter for tool category filtering
    app.MapMcp("/{toolCategory?}");

    options.ConfigureSessionOptions = async (httpContext, mcpOptions, cancellationToken) =>
    {
    // Determine tool category from route parameters
    var toolCategory = httpContext.Request.RouteValues["toolCategory"]?.ToString()?.ToLower() ?? "all";
    // Get pre-populated tools for the requested category
    if (toolDictionary.TryGetValue(toolCategory, out var tools))

    We don't currently have helper methods to register McpServerTools as keyed services like WithTools, but you can register them manually using a combination of AddKeyedSingleton and McpServerTool.Create. If you use the Func version of AddKeyedSingleton, the McpServerToolCreateOptions has a Services property where you can pass in the IServiceProvider allowing you to inject services into your tool method/Delegate.

    https://github.com/modelcontextprotocol/csharp-sdk/tree/9c1538fd98f171faa61b003e62e94582dbe4a3ed/samples/AspNetCoreMcpPerSessionTools

  3. leanserver commented on Mar 19, 2026

    @leanserver

    @halter73 Is this still supported? Despite the examples, I am not seeing any of the mentioned APIs on ToolCapabilities in the latest package.

    Thanks!

  4. halter73 commented on Mar 22, 2026

    @halter73
    Contributor

    What's supported is what's demonstrated in the AspNetCoreMcpPerSessionTools. Higher level stuff like AddMcpMultiHost or AddKeyedMcp could definitely be built using the low level features demonstrated by AspNetCoreMcpPerSessionTools, but those have never been supported APIs in the main C# SDK.

  5. added this to the 2.x milestone on Jul 27, 2026
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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions