feat: add class based tool loader to enable easier integation with existing codebases #162
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.
Description
Class-based Tool Loader for Existing Codebases
While the
@tool
annotation works great for new projects, we often face the challenge of integrating existing codebases that already have established repository patterns and service implementations. This PR addresses this challenge by introducing a class-based tool loader that dramatically simplifies the process of exposing existing functionality to agents.The core of this implementation is the
load_tools_from_instance
function, which automatically converts public methods from existing classes into AgentTools. It handles instance, static, and class methods seamlessly, while providing automatic name disambiguation when dealing with multiple instances. This means teams can leverage their existing codebase without having to modify their code with decorators or make significant structural changes.The implementation is flexible and forward-looking. While it currently provides the essential functionality needed to quickly onboard existing codebases, it sets the foundation for future enhancements. We can build upon this base to add features like base class support, enhanced method descriptions, conditional loading via annotations, and custom filtering mechanisms.
This approach significantly reduces the barrier to entry for teams wanting to integrate with Strands SDK. Instead of requiring extensive refactoring or decoration of existing code, teams can now expose their business logic to agents with minimal effort. The included test files demonstrate both simple use cases with the WeatherTimeTool and more complex scenarios involving multiple instances with static methods, showing the versatility of this approach.
The primary goal is to accelerate adoption in existing codebases, making it easier for teams to start using agents with their current implementations while maintaining the flexibility to evolve their integration over time.
For example, consider an existing code base with a class DynamoDBUserRepository:
this feature would enable
Documentation PR
Type of Change
Testing
All
hatch fmt --linter
hatch fmt --formatter
hatch test --all
Getting throttled with integ tests. But running inisolation we get
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.