Skip to content

Support discovery of tools that use DI with McpTool attribute #80

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

Closed
erinnmclaughlin opened this issue Mar 24, 2025 · 1 comment
Closed
Labels
enhancement New feature or request

Comments

@erinnmclaughlin
Copy link

Is your feature request related to a problem? Please describe.
Currently, discovery of tools using [McpToolType] and [McpTool] is only supported for public static methods (ref). It would be useful to also support instance methods when the class is registered in DI.

Describe the solution you'd like

Program.cs

var builder = Host.CreateApplicationBuilder(args);
builder.AddHttpClient<TodoApi>(c => c.BaseAddress = new Uri("...")); // for example
builder.AddMcpServer().WithStdioServerTransport().WithTools();

await builder.Build().RunAsync();

TodoApi.cs

// it'd be nice to this was automatically added to the MCP tool list.
[McpToolType]
public class TodoApi(HttpClient httpClient)
{
    [McpTool("getTodoList")]
    [Description("Gets the user's todo list.")]
    public async Task<TodoList[]> GetTodoListAsync(CancellationToken ct)
    {
       var todos = await httpClient.GetFromJsonAsync<TodoList[]>("todos", ct);
       return todos ?? [];
    }
}

Describe alternatives you've considered
There is currently an option to manually configure the ServerCapabilities.Tools, so this is a workaround.

@erinnmclaughlin erinnmclaughlin added the enhancement New feature or request label Mar 24, 2025
@erinnmclaughlin
Copy link
Author

Good timing - closing as a duplicate of #79

NeverMorewd added a commit to NeverMorewd/csharp-sdk that referenced this issue Mar 26, 2025
stephentoub added a commit that referenced this issue Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant