Skip to content

Unable to resolve service for type 'ModelContextProtocol.Server.IMcpServer' #564

Open
@scholtz

Description

@scholtz

Describe the bug
I follow the main repo readme, and i am getting the "Unable to resolve service for type 'ModelContextProtocol.Server.IMcpServer'" error when trying to access the IMcpServer.

To Reproduce
Steps to reproduce the behavior:

  1. build mcp server
  2. connect from mcp tool
  3. see the exception in the mcp server (on first usage)

Startup.cs:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;

var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{
    // Configure all logs to go to stderr
    consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});
builder.Services
    .AddMcpServer()
    .WithHttpTransport()
    .WithToolsFromAssembly();
await builder.Build().RunAsync();

mcptool.cs

[McpServerTool(Name = "SummarizeContentFromUrl"), Description("Summarizes content downloaded from a specific URI")]
public static async Task<string> SummarizeDownloadedContent(
    IMcpServer thisServer,
    HttpClient httpClient,
    [Description("The url from which to download the content to summarize")] string url,
    CancellationToken cancellationToken)
{
    string content = await httpClient.GetStringAsync(url);

    ChatMessage[] messages =
    [
        new(ChatRole.User, "Briefly summarize the following downloaded content:"),
        new(ChatRole.User, content),
    ];
    
    ChatOptions options = new()
    {
        MaxOutputTokens = 256,
        Temperature = 0.3f,
    };

    return $"Summary: {await thisServer.AsSamplingChatClient().GetResponseAsync(messages, options, cancellationToken)}";
}

Expected behavior
A clear and concise description of what you expected to happen.

Logs

System.InvalidOperationException: Unable to resolve service for type 'ModelContextProtocol.Server.IMcpServer' while attempting to activate 'MyClass..'
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.Extensions.DependencyInjection.McpServerBuilderExtensions.CreateTarget(IServiceProvider services, Type type)
   at Microsoft.Extensions.DependencyInjection.McpServerBuilderExtensions.<>c__DisplayClass3_1.<WithTools>b__2(RequestContext`1 r)
   at ModelContextProtocol.Server.AIFunctionMcpServerTool.<>c__DisplayClass4_0.<Create>b__0(AIFunctionArguments args)
   at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunction.InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken)
   at Microsoft.Extensions.AI.AIFunctionFactory.ReflectionAIFunction.InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken)
   at ModelContextProtocol.Server.AIFunctionMcpServerTool.InvokeAsync(RequestContext`1 request, CancellationToken cancellationToken)
ModelContextProtocol.Server.McpServer: Information: Server (MyClass 1.0.0.0), Client (Visual Studio Code (via mcp-remote 0.1.17) 1.101.2) method 'tools/call' request handler completed.

Additional context

Please either update the documentation on how to properly register IMcpServer dependency, or remove it from it and create example on how to fetch the current session id by the tool.

Is there any way how can I help to fix this issue?

Thanks

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions