strands.tools.loader
¶
Tool loading utilities.
_TOOL_MODULE_PREFIX = '_strands_tool_'
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
AgentTool
¶
Bases: ABC
Abstract base class for all SDK tools.
This class defines the interface that all tool implementations must follow. Each tool must provide its name, specification, and implement a stream method that executes the tool's functionality.
Source code in strands/types/tools.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
is_dynamic
property
¶
Whether the tool was dynamically loaded during runtime.
Dynamic tools may have different lifecycle management.
Returns:
| Type | Description |
|---|---|
bool
|
True if loaded dynamically, False otherwise. |
supports_hot_reload
property
¶
Whether the tool supports automatic reloading when modified.
Returns:
| Type | Description |
|---|---|
bool
|
False by default. |
tool_name
abstractmethod
property
¶
The unique name of the tool used for identification and invocation.
tool_spec
abstractmethod
property
¶
Tool specification that describes its functionality and parameters.
tool_type
abstractmethod
property
¶
The type of the tool implementation (e.g., 'python', 'javascript', 'lambda').
Used for categorization and appropriate handling.
__init__()
¶
Initialize the base agent tool with default dynamic state.
Source code in strands/types/tools.py
227 228 229 | |
get_display_properties()
¶
Get properties to display in UI representations of this tool.
Subclasses can extend this to include additional properties.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of property names and their string values. |
Source code in strands/types/tools.py
295 296 297 298 299 300 301 302 303 304 305 306 | |
mark_dynamic()
¶
Mark this tool as dynamically loaded.
Source code in strands/types/tools.py
291 292 293 | |
stream(tool_use, invocation_state, **kwargs)
abstractmethod
¶
Stream tool events and return the final result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_use
|
ToolUse
|
The tool use request containing tool ID and parameters. |
required |
invocation_state
|
dict[str, Any]
|
Caller-provided kwargs that were passed to the agent when it was invoked (agent(), agent.invoke_async(), etc.). |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Yields:
| Type | Description |
|---|---|
ToolGenerator
|
Tool events with the last being the tool result. |
Source code in strands/types/tools.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
DecoratedFunctionTool
¶
Bases: AgentTool, Generic[P, R]
An AgentTool that wraps a function that was decorated with @tool.
This class adapts Python functions decorated with @tool to the AgentTool interface. It handles both direct function calls and tool use invocations, maintaining the function's original behavior while adding tool capabilities.
The class is generic over the function's parameter types (P) and return type (R) to maintain type safety.
Source code in strands/tools/decorator.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | |
supports_hot_reload
property
¶
Check if this tool supports automatic reloading when modified.
Returns:
| Type | Description |
|---|---|
bool
|
Always true for function-based tools. |
tool_name
property
¶
Get the name of the tool.
Returns:
| Type | Description |
|---|---|
str
|
The tool name as a string. |
tool_spec
property
¶
Get the tool specification.
Returns:
| Type | Description |
|---|---|
ToolSpec
|
The tool specification dictionary containing metadata for Agent integration. |
tool_type
property
¶
Get the type of the tool.
Returns:
| Type | Description |
|---|---|
str
|
The string "function" indicating this is a function-based tool. |
__call__(*args, **kwargs)
¶
Call the original function with the provided arguments.
This method enables the decorated function to be called directly with its original signature, preserving the normal function call behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
args
|
Positional arguments to pass to the function. |
()
|
**kwargs
|
kwargs
|
Keyword arguments to pass to the function. |
{}
|
Returns:
| Type | Description |
|---|---|
R
|
The result of the original function call. |
Source code in strands/tools/decorator.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |
__get__(instance, obj_type=None)
¶
Descriptor protocol implementation for proper method binding.
This method enables the decorated function to work correctly when used as a class method. It binds the instance to the function call when accessed through an instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Any
|
The instance through which the descriptor is accessed, or None when accessed through the class. |
required |
obj_type
|
Optional[Type]
|
The class through which the descriptor is accessed. |
None
|
Returns:
| Type | Description |
|---|---|
DecoratedFunctionTool[P, R]
|
A new DecoratedFunctionTool with the instance bound to the function if accessed through an instance, |
DecoratedFunctionTool[P, R]
|
otherwise returns self. |
Example
class MyClass:
@tool
def my_tool():
...
instance = MyClass()
# instance of DecoratedFunctionTool that works as you'd expect
tool = instance.my_tool
Source code in strands/tools/decorator.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
__init__(tool_name, tool_spec, tool_func, metadata)
¶
Initialize the decorated function tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
The name to use for the tool (usually the function name). |
required |
tool_spec
|
ToolSpec
|
The tool specification containing metadata for Agent integration. |
required |
tool_func
|
Callable[P, R]
|
The original function being decorated. |
required |
metadata
|
FunctionToolMetadata
|
The FunctionToolMetadata object with extracted function information. |
required |
Source code in strands/tools/decorator.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | |
get_display_properties()
¶
Get properties to display in UI representations.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Function properties (e.g., function name). |
Source code in strands/tools/decorator.py
651 652 653 654 655 656 657 658 659 660 | |
stream(tool_use, invocation_state, **kwargs)
async
¶
Stream the tool with a tool use specification.
This method handles tool use streams from a Strands Agent. It validates the input, calls the function, and formats the result according to the expected tool result format.
Key operations:
- Extract tool use ID and input parameters
- Validate input against the function's expected parameters
- Call the function with validated input
- Format the result as a standard tool result
- Handle and format any errors that occur
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_use
|
ToolUse
|
The tool use specification from the Agent. |
required |
invocation_state
|
dict[str, Any]
|
Caller-provided kwargs that were passed to the agent when it was invoked (agent(), agent.invoke_async(), etc.). |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Yields:
| Type | Description |
|---|---|
ToolGenerator
|
Tool events with the last being the tool result. |
Source code in strands/tools/decorator.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | |
PythonAgentTool
¶
Bases: AgentTool
Tool implementation for Python-based tools.
This class handles tools implemented as Python functions, providing a simple interface for executing Python code as SDK tools.
Source code in strands/tools/tools.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
supports_hot_reload
property
¶
Check if this tool supports automatic reloading when modified.
Returns:
| Type | Description |
|---|---|
bool
|
Always true for function-based tools. |
tool_name
property
¶
Get the name of the tool.
Returns:
| Type | Description |
|---|---|
str
|
The name of the tool. |
tool_spec
property
¶
Get the tool specification for this Python-based tool.
Returns:
| Type | Description |
|---|---|
ToolSpec
|
The tool specification. |
tool_type
property
¶
Identifies this as a Python-based tool implementation.
Returns:
| Type | Description |
|---|---|
str
|
"python". |
__init__(tool_name, tool_spec, tool_func)
¶
Initialize a Python-based tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Unique identifier for the tool. |
required |
tool_spec
|
ToolSpec
|
Tool specification defining parameters and behavior. |
required |
tool_func
|
ToolFunc
|
Python function to execute when the tool is invoked. |
required |
Source code in strands/tools/tools.py
168 169 170 171 172 173 174 175 176 177 178 179 180 | |
stream(tool_use, invocation_state, **kwargs)
async
¶
Stream the Python function with the given tool use request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_use
|
ToolUse
|
The tool use request. |
required |
invocation_state
|
dict[str, Any]
|
Context for the tool invocation, including agent state. |
required |
**kwargs
|
Any
|
Additional keyword arguments for future extensibility. |
{}
|
Yields:
| Type | Description |
|---|---|
ToolGenerator
|
Tool events with the last being the tool result. |
Source code in strands/tools/tools.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
ToolLoader
¶
Handles loading of tools from different sources.
Source code in strands/tools/loader.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
load_python_tool(tool_path, tool_name)
staticmethod
¶
DEPRECATED: Load a Python tool module and return a single AgentTool for backwards compatibility.
Use load_python_tools to retrieve all tools defined in a .py file (returns a list).
This function will emit a DeprecationWarning and return the first discovered tool.
Source code in strands/tools/loader.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
load_python_tools(tool_path, tool_name)
staticmethod
¶
DEPRECATED: Load a Python tool module and return all discovered function-based tools as a list.
This method always returns a list of AgentTool (possibly length 1). It is the canonical API for retrieving multiple tools from a single Python file.
Source code in strands/tools/loader.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
load_tool(tool_path, tool_name)
classmethod
¶
DEPRECATED: Load a single tool based on its file extension for backwards compatibility.
Use load_tools to retrieve all tools defined in a file (returns a list).
This function will emit a DeprecationWarning and return the first discovered tool.
Source code in strands/tools/loader.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
load_tools(tool_path, tool_name)
classmethod
¶
DEPRECATED: Load tools from a file based on its file extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_path
|
str
|
Path to the tool file. |
required |
tool_name
|
str
|
Name of the tool. |
required |
Returns:
| Type | Description |
|---|---|
list[AgentTool]
|
A single Tool instance. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the tool file does not exist. |
ValueError
|
If the tool file has an unsupported extension. |
Exception
|
For other errors during tool loading. |
Source code in strands/tools/loader.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
load_tool_from_string(tool_string)
¶
Load tools follows strands supported input string formats.
This function can load a tool based on a string in the following ways:
1. Local file path to a module based tool: ./path/to/module/tool.py
2. Module import path
2.1. Path to a module based tool: strands_tools.file_read
2.2. Path to a module with multiple AgentTool instances (@tool decorated): tests.fixtures.say_tool
2.3. Path to a module and a specific function: tests.fixtures.say_tool:say
Source code in strands/tools/loader.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
load_tools_from_file_path(tool_path)
¶
Load module from specified path, and then load tools from that module.
This function attempts to load the passed in path as a python module, and if it succeeds, then it tries to import strands tool(s) from that module.
Source code in strands/tools/loader.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
load_tools_from_module(module, module_name)
¶
Load tools from a module.
First checks if the passed in module has instances of DecoratedToolFunction classes as atributes to the module. If so, then it returns them as a list of tools. If not, then it attempts to load the module as a module based tool.
Source code in strands/tools/loader.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
load_tools_from_module_path(module_tool_path)
¶
Load strands tool from a module path.
Example module paths: my.module.path my.module.path:tool_name
Source code in strands/tools/loader.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |