generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 536
feat: expose tool_use and agent through ToolContext to decorated tools #557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dbschmigelski
merged 18 commits into
strands-agents:main
from
dbschmigelski:feat/decorator/tool_param
Aug 14, 2025
+312
−15
Merged
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ec53f88
feat: expose internals through tool decorator param StrandsContext
dbschmigelski 84d1410
fix: simplify _is_special_param
dbschmigelski f379fb0
fix: clean integ test
dbschmigelski 5def990
fix: linting
dbschmigelski 9307a0e
fix: bug referencing wrong variable
dbschmigelski b099a2a
WIP
dbschmigelski ba59f6f
fix: update tests to pass agent in to direct tool call
dbschmigelski df69ea6
feat: add InvocationState TypedDict
dbschmigelski 29b344b
feat: add InvocationState TypedDict
dbschmigelski 932a76e
event_loop_cycle_id is UUID
dbschmigelski 76e9f8d
fix kwargs
dbschmigelski fea469d
fix: typing
dbschmigelski 6886a4f
feat: expose opt in functionalisty for tool decorator
dbschmigelski a9172d6
linting
dbschmigelski f664e4d
litning
dbschmigelski 72d506e
Update src/strands/tools/decorator.py
dbschmigelski 84d8bde
Apply suggestion from @zastrowm
dbschmigelski 087981a
tests: add aadditional unit tests
dbschmigelski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #!/usr/bin/env python3 | ||
dbschmigelski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Integration test for ToolContext functionality with real agent interactions. | ||
| """ | ||
|
|
||
| from strands import Agent, ToolContext, tool | ||
| from strands.types.tools import ToolResult | ||
dbschmigelski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @tool(context="custom_context_field") | ||
| def good_story(message: str, custom_context_field: ToolContext) -> dict: | ||
| """Tool that writes a good story""" | ||
| tool_use_id = custom_context_field.tool_use["toolUseId"] | ||
| return { | ||
| "status": "success", | ||
| "content": [{"text": f"Context tool processed with ID: {tool_use_id}"}], | ||
| } | ||
|
|
||
|
|
||
| @tool(context=True) | ||
| def bad_story(message: str, tool_context: ToolContext) -> dict: | ||
dbschmigelski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Tool that writes a bad story""" | ||
| tool_use_id = tool_context.tool_use["toolUseId"] | ||
| return { | ||
| "status": "success", | ||
| "content": [{"text": f"Context tool processed with ID: {tool_use_id}"}], | ||
| } | ||
|
|
||
|
|
||
| def _validate_tool_result_content(agent: Agent): | ||
| first_tool_result: ToolResult = [ | ||
| block["toolResult"] for message in agent.messages for block in message["content"] if "toolResult" in block | ||
| ][0] | ||
|
|
||
| assert first_tool_result["status"] == "success" | ||
| assert ( | ||
| first_tool_result["content"][0]["text"] == f"Context tool processed with ID: {first_tool_result['toolUseId']}" | ||
| ) | ||
|
|
||
|
|
||
| def test_strands_context_integration_context_true(): | ||
| """Test ToolContext functionality with real agent interactions.""" | ||
|
|
||
| agent = Agent(tools=[good_story]) | ||
| agent("using a tool, write a good story") | ||
|
|
||
| _validate_tool_result_content(agent) | ||
|
|
||
|
|
||
| def test_strands_context_integration_context_custom(): | ||
| """Test ToolContext functionality with real agent interactions.""" | ||
|
|
||
| agent = Agent(tools=[bad_story]) | ||
| agent("using a tool, write a bad story") | ||
|
|
||
| _validate_tool_result_content(agent) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.