Skip to content

Conversation

@stephentoub
Copy link
Contributor

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.

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.
Copy link
Contributor

@halter73 halter73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@stephentoub
Copy link
Contributor Author

@halter73, can you also review dotnet/extensions#6193 ? I'd like to hold off on merging this until I know that's going in.

@stephentoub stephentoub merged commit b61cdef into modelcontextprotocol:main Mar 26, 2025
8 checks passed
@stephentoub stephentoub deleted the mcpservertoolinstance branch March 26, 2025 13:34
@halllo
Copy link

halllo commented Mar 30, 2025

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)?

@stephentoub
Copy link
Contributor Author

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)?

@halter73 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants