Add [McpServerTool] support for instance methods#100
Add [McpServerTool] support for instance methods#100stephentoub merged 2 commits intomodelcontextprotocol:mainfrom
Conversation
By popular demand, enables [McpServerTool] on instance methods. The instance is constructed per invocation, using ActivatorUtilities.CreateInstance when possible so that constructor arguments are satisfied from DI.
There was a problem hiding this comment.
LGTM. It might be worth adding tests around tool lifecycle to verify that it's created and disposed per-invocation, but I understand that we're moving quickly.
Edit: I see Can_Call_Registered_Tool_With_Instance_Method would fail if it did not create an instance per-invocation, but verifying it gets disposed would be nice. And possibly verify that we don't try to resolve the tool directly from the container.
|
@halter73, can you also review dotnet/extensions#6193 ? I'd like to hold off on merging this until I know that's going in. |
|
The tool is currently getting its dependencies resolved from the application service provider. Should it not create a new service scope per tool invocation (and dispose it again afterwards)? |
|
By popular demand, enables [McpServerTool] on instance methods. The instance is constructed per invocation, using ActivatorUtilities.CreateInstance when possible so that constructor arguments are satisfied from DI.
Ports dotnet/extensions#6193 to the temporary AIFunctionFactory polyfill.