Dependency Injection Goals #387
Replies: 1 comment
-
Good topic for discussion 👍 Regarding the use of static classes/methods, although you can do param injection without specific This means the parameters on the actual tool method are always by (my) convention only parameters that are provided by the MCP client (other than possible other special types that you elude to). Personally I prefer this as it's easier to parse but I appreciate one class per tool method might not suit everyone!
[McpServerToolType]
public class CreateFile(IMcpServer thisServer, IProjectFilesService filesService)
{
[McpServerTool(Name = nameof(CreateFile)), Description("Create a new file in a project")]
public string Execute(
[Description("The project Id")] string projectId,
[Description("The relative path for the new file")] string filePath,
[Description("The contents of the new file")] string contents)
{
return filesService.CreateNewFile(projectId, filePath, contents);
}
} |
Beta Was this translation helpful? Give feedback.
-
Pre-submission Checklist
Discussion Topic
I'm curious what the goals are for the MCP Server around dependency injection support. I'm going to focus on tools specifically as well. It's possible the documentation simply needs to be updated.
Currently the docs are focused on static classes and static methods. Example: ...
but if we read the docs on the method to add tools we can see that we can do instances as well.
I would love to see a dedicated Server doc page so the main README.md can stay clean and simple. I'm simply spoiled by the tradition of great docs from Microsoft and this is a very young and early project.
Special Types
Currently we can add an
IMcpServer server
to a tool call and get that injected which is really nice. We can also addCancellationToken ct = default
as a parameter that will be provided. Is there an appetite to add the other "Special Types" to the supported list? I am most interested in addingClaimsPrincipal
to the list of supported options.Today it shows up as a parameter to be provided:
For Static Tools, I can clearly use the
IMcpServer.Services
to grab dependencies from the tool call method, and I can do constructor dependency injection with tools that are non-static - but is there an appetite to support[FromServices]
for method injection in static tools? The question is really one from a consistency with other ASP.Net type tooling.Thank you for your consideration. :)
Beta Was this translation helpful? Give feedback.
All reactions