Skip to content

Commit 8bc1800

Browse files
authored
feat: Add documentation about ToolContext (#224)
Adding barebones docs as a follow-up to strands-agents/sdk-python/pull/557 Co-authored-by: Mackenzie Zastrow <[email protected]>
1 parent 9a62bd2 commit 8bc1800

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/user-guide/concepts/tools/python-tools.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ async def async_example():
115115
asyncio.run(async_example())
116116
```
117117

118+
### ToolContext
119+
120+
Tools can access their execution context by setting `context=True` and including a `tool_context` parameter. The `ToolContext` provides access to the invoking agent and current tool use data:
121+
122+
```python
123+
from strands import tool, Agent, ToolContext
124+
125+
@tool(context=True)
126+
def get_self_name(tool_context: ToolContext) -> str:
127+
return f"The agent name is {tool_context.agent.name}"
128+
129+
@tool(context=True)
130+
def get_tool_use_id(tool_context: ToolContext) -> str:
131+
return f"Tool use is {tool_context.tool_use["toolUseId"]}"
132+
133+
agent = Agent(tools=[get_self_name, get_tool_use_id], name="Best agent")
134+
agent("What is your name?")
135+
agent("What is the tool use id?")
136+
```
137+
118138
## Class-Based Tools
119139

120140
Class-based tools allow you to create tools that maintain state and leverage object-oriented programming patterns. This approach is useful when your tools need to share resources, maintain context between invocations, follow object-oriented design principles, customize tools before passing them to an agent, or create different tool configurations for different agents.

0 commit comments

Comments
 (0)