strands.tools
¶
Agent tool interfaces and utilities.
This module provides the core functionality for creating, managing, and executing tools through agents.
strands.tools.tools
¶
Core tool implementations.
This module provides the base classes for all tool implementations in the SDK, including function-based tools and Python module-based tools, as well as utilities for validating tool uses and normalizing tool schemas.
FunctionTool
¶
Bases: AgentTool
Tool implementation for function-based tools created with @tool.
This class adapts Python functions decorated with @tool to the AgentTool interface.
Source code in strands/tools/tools.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 |
|
original_function
property
¶
Get the original function (without wrapper).
Returns:
Type | Description |
---|---|
Callable
|
Undecorated function. |
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 function-based tool.
Returns:
Type | Description |
---|---|
ToolSpec
|
The tool specification. |
tool_type
property
¶
Get the type of the tool.
Returns:
Type | Description |
---|---|
str
|
The string "function" indicating this is a function-based tool. |
__init__(func, tool_name=None)
¶
Initialize a function-based tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[[ToolUse, Unpack[Any]], ToolResult]
|
The decorated function. |
required |
tool_name
|
Optional[str]
|
Optional tool name (defaults to function name). |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If func is not decorated with @tool. |
Source code in strands/tools/tools.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
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/tools.py
246 247 248 249 250 251 252 253 254 |
|
invoke(tool, *args, **kwargs)
¶
Execute the function with the given tool use request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool
|
ToolUse
|
The tool use request containing the tool name, ID, and input parameters. |
required |
*args
|
Any
|
Additional positional arguments to pass to the function. |
()
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the function. |
{}
|
Returns:
Type | Description |
---|---|
ToolResult
|
A ToolResult containing the status and content from the function execution. |
Source code in strands/tools/tools.py
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 |
|
InvalidToolUseNameException
¶
Bases: Exception
Exception raised when a tool use has an invalid name.
Source code in strands/tools/tools.py
19 20 21 22 |
|
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
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 |
|
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, callback)
¶
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 |
callback
|
Callable[[ToolUse, Any, dict[str, Any]], ToolResult]
|
Python function to execute when the tool is invoked. |
required |
Source code in strands/tools/tools.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
|
invoke(tool, *args, **kwargs)
¶
Execute the Python function with the given tool use request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool
|
ToolUse
|
The tool use request. |
required |
*args
|
Any
|
Additional positional arguments to pass to the underlying callback function. |
()
|
**kwargs
|
dict[str, Any]
|
Additional keyword arguments to pass to the underlying callback function. |
{}
|
Returns:
Type | Description |
---|---|
ToolResult
|
A ToolResult containing the status and content from the callback execution. |
Source code in strands/tools/tools.py
311 312 313 314 315 316 317 318 319 320 321 322 |
|
normalize_schema(schema)
¶
Normalize a JSON schema to match expectations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Dict[str, Any]
|
The schema to normalize. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
The normalized schema. |
Source code in strands/tools/tools.py
66 67 68 69 70 71 72 73 74 75 76 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 106 107 108 109 110 111 |
|
normalize_tool_spec(tool_spec)
¶
Normalize a complete tool specification by transforming its inputSchema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_spec
|
ToolSpec
|
The tool specification to normalize. |
required |
Returns:
Type | Description |
---|---|
ToolSpec
|
The normalized tool specification. |
Source code in strands/tools/tools.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
validate_tool_use(tool)
¶
Validate a tool use request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool
|
ToolUse
|
The tool use to validate. |
required |
Source code in strands/tools/tools.py
25 26 27 28 29 30 31 |
|
validate_tool_use_name(tool)
¶
Validate the name of a tool use.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool
|
ToolUse
|
The tool use to validate. |
required |
Raises:
Type | Description |
---|---|
InvalidToolUseNameException
|
If the tool name is invalid. |
Source code in strands/tools/tools.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
strands.tools.decorator
¶
Tool decorator for SDK.
This module provides the @tool decorator that transforms Python functions into SDK Agent tools with automatic metadata extraction and validation.
The @tool decorator performs several functions:
- Extracts function metadata (name, description, parameters) from docstrings and type hints
- Generates a JSON schema for input validation
- Handles two different calling patterns:
- Standard function calls (func(arg1, arg2))
- Tool use calls (agent.my_tool(param1="hello", param2=123))
- Provides error handling and result formatting
- Works with both standalone functions and class methods
Example
from strands import Agent, tool
@tool
def my_tool(param1: str, param2: int = 42) -> dict:
'''
Tool description - explain what it does.
#Args:
param1: Description of first parameter.
param2: Description of second parameter (default: 42).
#Returns:
A dictionary with the results.
'''
result = do_something(param1, param2)
return {
"status": "success",
"content": [{"text": f"Result: {result}"}]
}
agent = Agent(tools=[my_tool])
agent.my_tool(param1="hello", param2=123)
FunctionToolMetadata
¶
Helper class to extract and manage function metadata for tool decoration.
This class handles the extraction of metadata from Python functions including:
- Function name and description from docstrings
- Parameter names, types, and descriptions
- Return type information
- Creation of Pydantic models for input validation
The extracted metadata is used to generate a tool specification that can be used by Strands Agent to understand and validate tool usage.
Source code in strands/tools/decorator.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 106 107 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 150 151 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 |
|
__init__(func)
¶
Initialize with the function to process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[..., Any]
|
The function to extract metadata from. Can be a standalone function or a class method. |
required |
Source code in strands/tools/decorator.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
extract_metadata()
¶
Extract metadata from the function to create a tool specification.
This method analyzes the function to create a standardized tool specification that Strands Agent can use to understand and interact with the tool.
The specification includes:
- name: The function name (or custom override)
- description: The function's docstring
- inputSchema: A JSON schema describing the expected parameters
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
A dictionary containing the tool specification. |
Source code in strands/tools/decorator.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
validate_input(input_data)
¶
Validate input data using the Pydantic model.
This method ensures that the input data meets the expected schema before it's passed to the actual function. It converts the data to the correct types when possible and raises informative errors when not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data
|
Dict[str, Any]
|
A dictionary of parameter names and values to validate. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
A dictionary with validated and converted parameter values. |
Raises:
Type | Description |
---|---|
ValueError
|
If the input data fails validation, with details about what failed. |
Source code in strands/tools/decorator.py
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 |
|
tool(func=None, **tool_kwargs)
¶
Decorator that transforms a Python function into a Strands tool.
This decorator seamlessly enables a function to be called both as a regular Python function and as a Strands tool. It extracts metadata from the function's signature, docstring, and type hints to generate an OpenAPI-compatible tool specification.
When decorated, a function:
- Still works as a normal function when called directly with arguments
- Processes tool use API calls when provided with a tool use dictionary
- Validates inputs against the function's type hints and parameter spec
- Formats return values according to the expected Strands tool result format
- Provides automatic error handling and reporting
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Optional[Callable[..., Any]]
|
The function to decorate. |
None
|
**tool_kwargs
|
Any
|
Additional tool specification options to override extracted values.
E.g., |
{}
|
Returns:
Type | Description |
---|---|
Callable[[T], T]
|
The decorated function with attached tool specifications. |
Example
@tool
def my_tool(name: str, count: int = 1) -> str:
'''Does something useful with the provided parameters.
"Args:
name: The name to process
count: Number of times to process (default: 1)
"Returns:
A message with the result
'''
return f"Processed {name} {count} times"
agent = Agent(tools=[my_tool])
agent.my_tool(name="example", count=3)
# Returns: {
# "toolUseId": "123",
# "status": "success",
# "content": [{"text": "Processed example 3 times"}]
# }
Source code in strands/tools/decorator.py
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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
|
strands.tools.executor
¶
Tool execution functionality for the event loop.
run_tools(handler, tool_uses, event_loop_metrics, request_state, invalid_tool_use_ids, tool_results, cycle_trace, parent_span=None, parallel_tool_executor=None)
¶
Execute tools either in parallel or sequentially.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler
|
Callable[[ToolUse], ToolResult]
|
Tool handler processing function. |
required |
tool_uses
|
List[ToolUse]
|
List of tool uses to execute. |
required |
event_loop_metrics
|
EventLoopMetrics
|
Metrics collection object. |
required |
request_state
|
Any
|
Current request state. |
required |
invalid_tool_use_ids
|
List[str]
|
List of invalid tool use IDs. |
required |
tool_results
|
List[ToolResult]
|
List to populate with tool results. |
required |
cycle_trace
|
Trace
|
Parent trace for the current cycle. |
required |
parent_span
|
Optional[Span]
|
Parent span for the current cycle. |
None
|
parallel_tool_executor
|
Optional[ParallelToolExecutorInterface]
|
Optional executor for parallel processing. |
None
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if any tool failed, False otherwise. |
Source code in strands/tools/executor.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 75 76 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 106 107 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 |
|
validate_and_prepare_tools(message, tool_uses, tool_results, invalid_tool_use_ids)
¶
Validate tool uses and prepare them for execution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message
|
Message
|
Current message. |
required |
tool_uses
|
List[ToolUse]
|
List to populate with tool uses. |
required |
tool_results
|
List[ToolResult]
|
List to populate with tool results for invalid tools. |
required |
invalid_tool_use_ids
|
List[str]
|
List to populate with invalid tool use IDs. |
required |
Source code in strands/tools/executor.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
strands.tools.loader
¶
Tool loading utilities.
ToolLoader
¶
Handles loading of tools from different sources.
Source code in strands/tools/loader.py
106 107 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 150 151 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 |
|
load_python_tool(tool_path, tool_name)
staticmethod
¶
Load a Python tool module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_path
|
str
|
Path to the Python tool file. |
required |
tool_name
|
str
|
Name of the tool. |
required |
Returns:
Type | Description |
---|---|
AgentTool
|
Tool instance. |
Raises:
Type | Description |
---|---|
AttributeError
|
If required attributes are missing from the tool module. |
ImportError
|
If there are issues importing the tool module. |
TypeError
|
If the tool function is not callable. |
ValueError
|
If function in module is not a valid tool. |
Exception
|
For other errors during tool loading. |
Source code in strands/tools/loader.py
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 150 151 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 |
|
load_tool(tool_path, tool_name)
classmethod
¶
Load a tool 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 |
---|---|
AgentTool
|
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
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 |
|
load_function_tool(func)
¶
Load a function as a tool if it's decorated with @tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Any
|
The function to load. |
required |
Returns:
Type | Description |
---|---|
Optional[FunctionTool]
|
FunctionTool if successful, None otherwise. |
Source code in strands/tools/loader.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
scan_directory_for_tools(directory)
¶
Scan a directory for Python modules containing function-based tools.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
Path
|
The directory to scan. |
required |
Returns:
Type | Description |
---|---|
Dict[str, FunctionTool]
|
Dictionary mapping tool names to FunctionTool instances. |
Source code in strands/tools/loader.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 |
|
scan_module_for_tools(module)
¶
Scan a module for function-based tools.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module
|
Any
|
The module to scan. |
required |
Returns:
Type | Description |
---|---|
List[FunctionTool]
|
List of FunctionTool instances found in the module. |
Source code in strands/tools/loader.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
strands.tools.registry
¶
Tool registry.
This module provides the central registry for all tools available to the agent, including discovery, validation, and invocation capabilities.
ToolRegistry
¶
Central registry for all tools available to the agent.
This class manages tool registration, validation, discovery, and invocation.
Source code in strands/tools/registry.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 75 76 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 106 107 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 150 151 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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 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 |
|
NewToolDict
¶
Bases: TypedDict
Dictionary type for adding or updating a tool in the configuration.
Attributes:
Name | Type | Description |
---|---|---|
spec |
ToolSpec
|
The tool specification that defines the tool's interface and behavior. |
Source code in strands/tools/registry.py
538 539 540 541 542 543 544 545 |
|
__init__()
¶
Initialize the tool registry.
Source code in strands/tools/registry.py
31 32 33 34 35 |
|
discover_tool_modules()
¶
Discover available tool modules in all tools directories.
Returns:
Type | Description |
---|---|
Dict[str, Path]
|
Dictionary mapping tool names to their full paths. |
Source code in strands/tools/registry.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
|
get_all_tools_config()
¶
Dynamically generate tool configuration by combining built-in and dynamic tools.
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dictionary containing all tool configurations. |
Source code in strands/tools/registry.py
140 141 142 143 144 145 146 147 148 149 150 151 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 |
|
get_tools_dirs()
¶
Get all tool directory paths.
Returns:
Type | Description |
---|---|
List[Path]
|
A list of Path objects for current working directory's "./tools/". |
Source code in strands/tools/registry.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
initialize_tool_config()
¶
Initialize tool configuration from tool handler with optional filtering.
Returns:
Type | Description |
---|---|
ToolConfig
|
Tool config. |
Source code in strands/tools/registry.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
|
initialize_tools(load_tools_from_directory=True)
¶
Initialize all tools by discovering and loading them dynamically from all tool directories.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
load_tools_from_directory
|
bool
|
Whether to reload tools if changes are made at runtime. |
True
|
Source code in strands/tools/registry.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 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 |
|
load_tool_from_filepath(tool_name, tool_path)
¶
Load a tool from a file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_name
|
str
|
Name of the tool. |
required |
tool_path
|
str
|
Path to the tool file. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the tool file is not found. |
ValueError
|
If the tool cannot be loaded. |
Source code in strands/tools/registry.py
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 |
|
process_tools(tools)
¶
Process tools list that can contain tool names, paths, imported modules, or functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tools
|
List[Any]
|
List of tool specifications. Can be:
|
required |
Returns:
Type | Description |
---|---|
List[str]
|
List of tool names that were processed. |
Source code in strands/tools/registry.py
37 38 39 40 41 42 43 44 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 75 76 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 106 107 108 109 110 |
|
register_tool(tool)
¶
Register a tool function with the given name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool
|
AgentTool
|
The tool to register. |
required |
Source code in strands/tools/registry.py
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 |
|
reload_tool(tool_name)
¶
Reload a specific tool module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_name
|
str
|
Name of the tool to reload. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the tool file cannot be found. |
ImportError
|
If there are issues importing the tool module. |
ValueError
|
If the tool specification is invalid or required components are missing. |
Exception
|
For other errors during tool reloading. |
Source code in strands/tools/registry.py
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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
|
validate_tool_spec(tool_spec)
¶
Validate tool specification against required schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_spec
|
ToolSpec
|
Tool specification to validate. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the specification is invalid. |
Source code in strands/tools/registry.py
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 |
|
strands.tools.thread_pool_executor
¶
Thread pool execution management for parallel tool calls.
ThreadPoolExecutorWrapper
¶
Bases: ParallelToolExecutorInterface
Wrapper around ThreadPoolExecutor to implement the strands.types.event_loop.ParallelToolExecutorInterface.
This class adapts Python's standard ThreadPoolExecutor to conform to the SDK's ParallelToolExecutorInterface, allowing it to be used for parallel tool execution within the agent event loop. It provides methods for submitting tasks, monitoring their completion, and shutting down the executor.
Attributes:
Name | Type | Description |
---|---|---|
thread_pool |
The underlying ThreadPoolExecutor instance. |
Source code in strands/tools/thread_pool_executor.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 |
|
__init__(thread_pool)
¶
Initialize with a ThreadPoolExecutor instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
thread_pool
|
ThreadPoolExecutor
|
The ThreadPoolExecutor to wrap. |
required |
Source code in strands/tools/thread_pool_executor.py
21 22 23 24 25 26 27 |
|
as_completed(futures, timeout=None)
¶
Return an iterator over the futures as they complete.
The returned iterator yields futures as they complete (finished or cancelled).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
futures
|
Iterable[Future]
|
The futures to iterate over. |
required |
timeout
|
Optional[int]
|
The maximum number of seconds to wait. None means no limit. |
None
|
Returns:
Type | Description |
---|---|
Iterator[Future]
|
An iterator yielding futures as they complete. |
Raises:
Type | Description |
---|---|
TimeoutError
|
If the timeout is reached. |
Source code in strands/tools/thread_pool_executor.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
shutdown(wait=True)
¶
Shutdown the thread pool executor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wait
|
bool
|
If True, waits until all running futures have finished executing. |
True
|
Source code in strands/tools/thread_pool_executor.py
63 64 65 66 67 68 69 |
|
submit(fn, /, *args, **kwargs)
¶
Submit a callable to be executed with the given arguments.
This method schedules the callable to be executed as fn(args, *kwargs) and returns a Future instance representing the execution of the callable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn
|
Callable[..., Any]
|
The callable to execute. |
required |
*args
|
Any
|
Positional arguments for the callable. |
()
|
**kwargs
|
Any
|
Keyword arguments for the callable. |
{}
|
Returns:
Type | Description |
---|---|
Future
|
A Future instance representing the execution of the callable. |
Source code in strands/tools/thread_pool_executor.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
strands.tools.watcher
¶
Tool watcher for hot reloading tools during development.
This module provides functionality to watch tool directories for changes and automatically reload tools when they are modified.
ToolWatcher
¶
Watches tool directories for changes and reloads tools when they are modified.
Source code in strands/tools/watcher.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 75 76 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 106 107 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 |
|
MasterChangeHandler
¶
Bases: FileSystemEventHandler
Master handler that delegates to all registered handlers.
Source code in strands/tools/watcher.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
__init__(dir_path)
¶
Initialize a master change handler for a specific directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir_path
|
str
|
The directory path to watch. |
required |
Source code in strands/tools/watcher.py
72 73 74 75 76 77 78 |
|
on_modified(event)
¶
Delegate file modification events to all registered handlers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
Any
|
The file system event that triggered this handler. |
required |
Source code in strands/tools/watcher.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
ToolChangeHandler
¶
Bases: FileSystemEventHandler
Handler for tool file changes.
Source code in strands/tools/watcher.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
__init__(tool_registry)
¶
Initialize a tool change handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_registry
|
ToolRegistry
|
The tool registry to update when tools change. |
required |
Source code in strands/tools/watcher.py
44 45 46 47 48 49 50 |
|
on_modified(event)
¶
Reload tool if file modification detected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event
|
Any
|
The file system event that triggered this handler. |
required |
Source code in strands/tools/watcher.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
__init__(tool_registry)
¶
Initialize a tool watcher for the given tool registry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_registry
|
ToolRegistry
|
The tool registry to report changes. |
required |
Source code in strands/tools/watcher.py
32 33 34 35 36 37 38 39 |
|
start()
¶
Start watching all tools directories for changes.
Source code in strands/tools/watcher.py
98 99 100 101 102 103 104 105 106 107 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 |
|
strands.tools.mcp
¶
Model Context Protocol (MCP) integration.
This package provides integration with the Model Context Protocol (MCP), allowing agents to use tools provided by MCP servers.
- Docs: https://www.anthropic.com/news/model-context-protocol
strands.tools.mcp.mcp_agent_tool
¶
MCP Agent Tool module for adapting Model Context Protocol tools to the agent framework.
This module provides the MCPAgentTool class which serves as an adapter between MCP (Model Context Protocol) tools and the agent framework's tool interface. It allows MCP tools to be seamlessly integrated and used within the agent ecosystem.
MCPAgentTool
¶
Bases: AgentTool
Adapter class that wraps an MCP tool and exposes it as an AgentTool.
This class bridges the gap between the MCP protocol's tool representation and the agent framework's tool interface, allowing MCP tools to be used seamlessly within the agent framework.
Source code in strands/tools/mcp/mcp_agent_tool.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 75 76 77 78 79 80 81 82 83 84 85 |
|
tool_name
property
¶
Get the name of the tool.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The name of the MCP tool |
tool_spec
property
¶
Get the specification of the tool.
This method converts the MCP tool specification to the agent framework's ToolSpec format, including the input schema and description.
Returns:
Name | Type | Description |
---|---|---|
ToolSpec |
ToolSpec
|
The tool specification in the agent framework format |
tool_type
property
¶
Get the type of the tool.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The type of the tool, always "python" for MCP tools |
__init__(mcp_tool, mcp_client)
¶
Initialize a new MCPAgentTool instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mcp_tool
|
Tool
|
The MCP tool to adapt |
required |
mcp_client
|
MCPClient
|
The MCP server connection to use for tool invocation |
required |
Source code in strands/tools/mcp/mcp_agent_tool.py
29 30 31 32 33 34 35 36 37 38 39 |
|
invoke(tool, *args, **kwargs)
¶
Invoke the MCP tool.
This method delegates the tool invocation to the MCP server connection, passing the tool use ID, tool name, and input arguments.
Source code in strands/tools/mcp/mcp_agent_tool.py
76 77 78 79 80 81 82 83 84 85 |
|
strands.tools.mcp.mcp_client
¶
Model Context Protocol (MCP) server connection management module.
This module provides the MCPClient class which handles connections to MCP servers. It manages the lifecycle of MCP connections, including initialization, tool discovery, tool invocation, and proper cleanup of resources. The connection runs in a background thread to avoid blocking the main application thread while maintaining communication with the MCP service.
MCPClient
¶
Represents a connection to a Model Context Protocol (MCP) server.
This class implements a context manager pattern for efficient connection management, allowing reuse of the same connection for multiple tool calls to reduce latency. It handles the creation, initialization, and cleanup of MCP connections.
The connection runs in a background thread to avoid blocking the main application thread while maintaining communication with the MCP service.
Source code in strands/tools/mcp/mcp_client.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 75 76 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 106 107 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 150 151 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 |
|
__enter__()
¶
Context manager entry point which initializes the MCP server connection.
Source code in strands/tools/mcp/mcp_client.py
72 73 74 |
|
__exit__(exc_type, exc_val, exc_tb)
¶
Context manager exit point that cleans up resources.
Source code in strands/tools/mcp/mcp_client.py
76 77 78 |
|
__init__(transport_callable)
¶
Initialize a new MCP Server connection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
transport_callable
|
Callable[[], MCPTransport]
|
A callable that returns an MCPTransport (read_stream, write_stream) tuple |
required |
Source code in strands/tools/mcp/mcp_client.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
call_tool_sync(tool_use_id, name, arguments=None, read_timeout_seconds=None)
¶
Synchronously calls a tool on the MCP server.
This method calls the asynchronous call_tool method on the MCP session and converts the result to the ToolResult format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_use_id
|
str
|
Unique identifier for this tool use |
required |
name
|
str
|
Name of the tool to call |
required |
arguments
|
dict[str, Any] | None
|
Optional arguments to pass to the tool |
None
|
read_timeout_seconds
|
timedelta | None
|
Optional timeout for the tool call |
None
|
Returns:
Name | Type | Description |
---|---|---|
ToolResult |
ToolResult
|
The result of the tool call |
Source code in strands/tools/mcp/mcp_client.py
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 |
|
list_tools_sync()
¶
Synchronously retrieves the list of available tools from the MCP server.
This method calls the asynchronous list_tools method on the MCP session and adapts the returned tools to the AgentTool interface.
Returns:
Type | Description |
---|---|
List[MCPAgentTool]
|
List[AgentTool]: A list of available tools adapted to the AgentTool interface |
Source code in strands/tools/mcp/mcp_client.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
start()
¶
Starts the background thread and waits for initialization.
This method starts the background thread that manages the MCP connection and blocks until the connection is ready or times out.
Returns:
Name | Type | Description |
---|---|---|
self |
MCPClient
|
The MCPClient instance |
Raises:
Type | Description |
---|---|
Exception
|
If the MCP connection fails to initialize within the timeout period |
Source code in strands/tools/mcp/mcp_client.py
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 106 107 108 |
|
stop(exc_type, exc_val, exc_tb)
¶
Signals the background thread to stop and waits for it to complete, ensuring proper cleanup of all resources.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exc_type
|
Optional[BaseException]
|
Exception type if an exception was raised in the context |
required |
exc_val
|
Optional[BaseException]
|
Exception value if an exception was raised in the context |
required |
exc_tb
|
Optional[TracebackType]
|
Exception traceback if an exception was raised in the context |
required |
Source code in strands/tools/mcp/mcp_client.py
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 |
|
strands.tools.mcp.mcp_types
¶
Type definitions for MCP integration.