Skip to content

Commit 024e8fc

Browse files
committed
Add invocation_state to ToolContext
Addresses issue #579, #750
1 parent 6dadbce commit 024e8fc

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/strands/tools/decorator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ def inject_special_parameters(
268268
invocation_state: Context for the tool invocation, including agent state.
269269
"""
270270
if self._context_param and self._context_param in self.signature.parameters:
271-
tool_context = ToolContext(tool_use=tool_use, agent=invocation_state["agent"])
271+
tool_context = ToolContext(
272+
tool_use=tool_use, agent=invocation_state["agent"], invocation_state=invocation_state
273+
)
272274
validated_input[self._context_param] = tool_context
273275

274276
# Inject agent if requested (backward compatibility)

src/strands/types/tools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class ToolContext:
132132
tool_use: The complete ToolUse object containing tool invocation details.
133133
agent: The Agent instance executing this tool, providing access to conversation history,
134134
model configuration, and other agent state.
135+
invocation_state: Keyword arguments passed to agent invocation methods (agent(), agent.invoke_async(), etc.).
136+
Provides access to invocation-specific context and parameters.
135137
136138
Note:
137139
This class is intended to be instantiated by the SDK. Direct construction by users
@@ -140,6 +142,7 @@ class ToolContext:
140142

141143
tool_use: ToolUse
142144
agent: "Agent"
145+
invocation_state: dict[str, Any]
143146

144147

145148
ToolChoice = Union[

tests/strands/tools/test_decorator.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ def complex_schema_tool(union_param: Union[List[int], Dict[str, Any], str, None]
10391039
assert "NoneType: None" in result["content"][0]["text"]
10401040

10411041

1042-
async def _run_context_injection_test(context_tool: AgentTool):
1042+
async def _run_context_injection_test(context_tool: AgentTool, additional_context=None):
10431043
"""Common test logic for context injection tests."""
10441044
tool: AgentTool = context_tool
10451045
generator = tool.stream(
@@ -1052,6 +1052,7 @@ async def _run_context_injection_test(context_tool: AgentTool):
10521052
},
10531053
invocation_state={
10541054
"agent": Agent(name="test_agent"),
1055+
**(additional_context or {}),
10551056
},
10561057
)
10571058
tool_results = [value async for value in generator]
@@ -1081,6 +1082,8 @@ def context_tool(message: str, agent: Agent, tool_context: ToolContext) -> dict:
10811082
tool_name = tool_context.tool_use["name"]
10821083
agent_from_tool_context = tool_context.agent
10831084

1085+
assert tool_context.invocation_state["new_value"] == 13
1086+
10841087
return {
10851088
"status": "success",
10861089
"content": [
@@ -1090,7 +1093,12 @@ def context_tool(message: str, agent: Agent, tool_context: ToolContext) -> dict:
10901093
],
10911094
}
10921095

1093-
await _run_context_injection_test(context_tool)
1096+
await _run_context_injection_test(
1097+
context_tool,
1098+
{
1099+
"new_value": 13,
1100+
},
1101+
)
10941102

10951103

10961104
@pytest.mark.asyncio

0 commit comments

Comments
 (0)