Skip to content

Comments

feat: add trait-based tool declaration#677

Open
Evian-Zhang wants to merge 2 commits intomodelcontextprotocol:mainfrom
Evian-Zhang:trait-based-tools
Open

feat: add trait-based tool declaration#677
Evian-Zhang wants to merge 2 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
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant