feat: expose internals through tool decorator param StrandsContext #557
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
Previously,
@tool
-decorated functions could not access the same contextual information available to module-based tools, specifically thetool_use_id
and other framework-provided data. This limitation made it impossible for decorated tools to implement features like global storage indexing or request tracking that required access to tool execution metadata.Solution
This PR introduces
StrandsContext
, a typed dictionary that provides@tool
-decorated functions with access to framework-level context data in a future-proof and backward-compatible way.Name very much open for suggestions
Key Features
Backward Compatibility
All existing
@tool
functions continue to work unchanged. Existingagent
parameter injection remains fully functional with no breaking changes to current tool implementations.Future-Proof Design
Single reserved parameter name (
strands_context
) eliminates risk of future naming conflicts. ExtensibleTypedDict
structure allows adding new context fields without breaking changes. Defensive architecture prevents accidental parameter collisions as the framework evolves.Type Safety
Full TypeScript-like typing with IDE autocomplete support.
StrandsContext
provides structured access totool_use: ToolUse
for complete tool invocation details includingtoolUseId
, andinvocation_state: dict[str, Any]
for agent state and execution context.Usage Example
Convenience Features
For common use cases, we maintain convenient parameter injection. The
agent
parameter continues to work as before for easy agent access.strands_context
provides comprehensive access when needed. Tools can use either approach based on their requirements.Future Roadmap
Strongly Typed InvocationState: We're planning to replace
invocation_state: dict[str, Any]
with a properInvocationState
TypedDict in an upcoming release. This will provide better type safety for framework-provided state, IDE autocomplete for known invocation context fields, and clearer documentation of available context data.The current
StrandsContext
design accommodates this future enhancement without requiring changes to existing tool implementations.Related Issues
#194
Documentation PR
Documentation PR to be raised before merging after naming is aligned.
Type of Change
New feature
Breaking change
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepare
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.