Skip to content

feat: add trait-based tool declaration#677

Merged
DaleSeo merged 6 commits intomodelcontextprotocol:mainfrom
Evian-Zhang:trait-based-tools
Feb 25, 2026
Merged

feat: add trait-based tool declaration#677
DaleSeo merged 6 commits intomodelcontextprotocol:mainfrom
Evian-Zhang:trait-based-tools

Conversation

@Evian-Zhang
Copy link
Contributor

Motivation and Context

Current documentation recommends using tool_router and tool macros 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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

@Evian-Zhang Evian-Zhang requested a review from a team as a code owner February 21, 2026 08:06
@github-actions github-actions bot added T-documentation Documentation improvements T-core Core library changes T-handler Handler implementation changes labels Feb 21, 2026
Copy link
Member

@DaleSeo DaleSeo left a comment

Choose a reason for hiding this comment

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

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.

@github-actions github-actions bot added the T-macros Macro changes label Feb 25, 2026
@Evian-Zhang
Copy link
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 sync_tool_wrapper and async_tool_wrapper.

Copy link
Member

@DaleSeo DaleSeo left a comment

Choose a reason for hiding this comment

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

Thanks for the quick turnaround on the feedback, @Evian-Zhang!

@DaleSeo DaleSeo merged commit 6c336a9 into modelcontextprotocol:main Feb 25, 2026
11 checks passed
This was referenced Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-core Core library changes T-documentation Documentation improvements T-handler Handler implementation changes T-macros Macro changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants