feat: add trait-based tool declaration#677
Merged
DaleSeo merged 6 commits intomodelcontextprotocol:mainfrom Feb 25, 2026
Merged
feat: add trait-based tool declaration#677DaleSeo merged 6 commits intomodelcontextprotocol:mainfrom
DaleSeo merged 6 commits intomodelcontextprotocol:mainfrom
Conversation
DaleSeo
reviewed
Feb 24, 2026
Member
DaleSeo
left a comment
There was a problem hiding this comment.
Thanks for your contribution, @Evian-Zhang! This trait-based approach seems like a solid solution to a real ergonomic issue.
For a follow-up, it would be great to see some unit tests that cover both the happy path and error conversion for sync_tool_wrapper and async_tool_wrapper. The documentation tests show usage well, but having dedicated tests would give us more confidence in handling edge cases.
…s and return types
Contributor
Author
|
Thank you for your great suggestions! I have updated code following your advice. I have added tests for both the happy path and error conversion for |
DaleSeo
approved these changes
Feb 25, 2026
Member
DaleSeo
left a comment
There was a problem hiding this comment.
Thanks for the quick turnaround on the feedback, @Evian-Zhang!
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