feat: add trait-based tool declaration#677
Open
Evian-Zhang wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
feat: add trait-based tool declaration#677Evian-Zhang wants to merge 2 commits intomodelcontextprotocol:mainfrom
Evian-Zhang wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Current documentation recommends using
tool_routerandtoolmacros to declare tools. However, for complex business logics, each tool is often organized into separate files/modules. The macro-based approach needs to import all of tool function, tool input, and tool output types, and the name and description of such a tool cannot be put into the corresponding submodule, leading to a not-so-good encapsulation and separation.With the trait-based approach added in this PR, each tool can declare all related stuff in their own module, and the tool router only needs to import the tool struct itself, making the code organization more clear.
How Has This Been Tested?
This has been adopted in our internal mcp server as a best-practice. The doc tests added in this PR will fully cover this trait-based approach. (It is not marked with
ignore, so it will be tested in CI).Breaking Changes
No.
Types of changes
Checklist
Additional context