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.
CLIENT_SESSION_NOT_RUNNING_ERROR_MESSAGE = 'the client session is not running. Ensure the agent is used within the MCP client context manager. For more information see: https://strandsagents.com/latest/user-guide/concepts/tools/mcp-tools/#mcpclientinitializationerror'
module-attribute
¶
ImageFormat = Literal['png', 'jpeg', 'gif', 'webp']
module-attribute
¶
Supported image formats.
MCPTransport = AbstractAsyncContextManager[MessageStream | _MessageStreamWithGetSessionIdCallback]
module-attribute
¶
MIME_TO_FORMAT = {'image/jpeg': 'jpeg', 'image/jpg': 'jpeg', 'image/png': 'png', 'image/gif': 'gif', 'image/webp': 'webp'}
module-attribute
¶
T = TypeVar('T')
module-attribute
¶
ToolResultStatus = Literal['success', 'error']
module-attribute
¶
Status of a tool execution result.
_NON_FATAL_ERROR_PATTERNS = ['unknown request id']
module-attribute
¶
_ToolMatcher = str | Pattern[str] | _ToolFilterCallback
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 | |
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
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 | |
tool_name
property
¶
Get the name of the tool.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The agent-facing name of the tool (may be disambiguated) |
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, description, and optional output schema.
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, name_override=None, timeout=None)
¶
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 |
name_override
|
str | None
|
Optional name to use for the agent tool (for disambiguation) If None, uses the original MCP tool name |
None
|
timeout
|
timedelta | None
|
Optional timeout duration for tool execution |
None
|
Source code in strands/tools/mcp/mcp_agent_tool.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
stream(tool_use, invocation_state, **kwargs)
async
¶
Stream the MCP tool.
This method delegates the tool stream to the MCP server connection, passing the tool use ID, tool name, and input arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_use
|
ToolUse
|
The tool use request containing tool ID and parameters. |
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/mcp/mcp_agent_tool.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
MCPClient
¶
Bases: ToolProvider
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. When structured content is available from MCP tools, it will be returned as the last item in the content array of the ToolResult.
Warning
This class implements the experimental ToolProvider interface and its methods are subject to change.
Source code in strands/tools/mcp/mcp_client.py
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 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 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 | |
__enter__()
¶
Context manager entry point which initializes the MCP server connection.
TODO: Refactor to lazy initialization pattern following idiomatic Python. Heavy work in enter is non-idiomatic - should move connection logic to first method call instead.
Source code in strands/tools/mcp/mcp_client.py
145 146 147 148 149 150 151 | |
__exit__(exc_type, exc_val, exc_tb)
¶
Context manager exit point that cleans up resources.
Source code in strands/tools/mcp/mcp_client.py
153 154 155 | |
__init__(transport_callable, *, startup_timeout=30, tool_filters=None, prefix=None, elicitation_callback=None)
¶
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 |
startup_timeout
|
int
|
Timeout after which MCP server initialization should be cancelled. Defaults to 30. |
30
|
tool_filters
|
ToolFilters | None
|
Optional filters to apply to tools. |
None
|
prefix
|
str | None
|
Optional prefix for tool names. |
None
|
elicitation_callback
|
Optional[ElicitationFnT]
|
Optional callback function to handle elicitation requests from the MCP server. |
None
|
Source code in strands/tools/mcp/mcp_client.py
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 | |
add_consumer(consumer_id, **kwargs)
¶
Add a consumer to this tool provider.
Synchronous to prevent GC deadlocks when called from Agent finalizers.
Source code in strands/tools/mcp/mcp_client.py
257 258 259 260 261 262 263 | |
call_tool_async(tool_use_id, name, arguments=None, read_timeout_seconds=None)
async
¶
Asynchronously 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 MCPToolResult 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 |
|---|---|---|
MCPToolResult |
MCPToolResult
|
The result of the tool call |
Source code in strands/tools/mcp/mcp_client.py
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 | |
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. If the MCP tool returns structured content, it will be included as the last item in the content array of the returned ToolResult.
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 |
|---|---|---|
MCPToolResult |
MCPToolResult
|
The result of the tool call |
Source code in strands/tools/mcp/mcp_client.py
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 | |
get_prompt_sync(prompt_id, args)
¶
Synchronously retrieves a prompt from the MCP server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt_id
|
str
|
The ID of the prompt to retrieve |
required |
args
|
dict[str, Any]
|
Optional arguments to pass to the prompt |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GetPromptResult |
GetPromptResult
|
The prompt response from the MCP server |
Source code in strands/tools/mcp/mcp_client.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
list_prompts_sync(pagination_token=None)
¶
Synchronously retrieves the list of available prompts from the MCP server.
This method calls the asynchronous list_prompts method on the MCP session and returns the raw ListPromptsResult with pagination support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pagination_token
|
Optional[str]
|
Optional token for pagination |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ListPromptsResult |
ListPromptsResult
|
The raw MCP response containing prompts and pagination info |
Source code in strands/tools/mcp/mcp_client.py
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 | |
list_tools_sync(pagination_token=None, prefix=None, tool_filters=None)
¶
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pagination_token
|
str | None
|
Optional token for pagination |
None
|
prefix
|
str | None
|
Optional prefix to apply to tool names. If None, uses constructor default. If explicitly provided (including empty string), overrides constructor default. |
None
|
tool_filters
|
ToolFilters | None
|
Optional filters to apply to tools. If None, uses constructor default. If explicitly provided (including empty dict), overrides constructor default. |
None
|
Returns:
| Type | Description |
|---|---|
PaginatedList[MCPAgentTool]
|
List[AgentTool]: A list of available tools adapted to the AgentTool interface |
Source code in strands/tools/mcp/mcp_client.py
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 | |
load_tools(**kwargs)
async
¶
Load and return tools from the MCP server.
This method implements the ToolProvider interface by loading tools from the MCP server and caching them for reuse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional arguments for future compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
Sequence[AgentTool]
|
List of AgentTool instances from the MCP server. |
Source code in strands/tools/mcp/mcp_client.py
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 | |
remove_consumer(consumer_id, **kwargs)
¶
Remove a consumer from this tool provider.
This method is idempotent - calling it multiple times with the same ID has no additional effect after the first call.
Synchronous to prevent GC deadlocks when called from Agent finalizers. Uses existing synchronous stop() method for safe cleanup.
Source code in strands/tools/mcp/mcp_client.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
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
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 | |
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.
This method is defensive and can handle partial initialization states that may occur if start() fails partway through initialization.
Resources to cleanup: - _background_thread: Thread running the async event loop - _background_thread_session: MCP ClientSession (auto-closed by context manager) - _background_thread_event_loop: AsyncIO event loop in background thread - _close_future: AsyncIO future to signal thread shutdown - _close_exception: Exception that caused the background thread shutdown; None if a normal shutdown occurred. - _init_future: Future for initialization synchronization
Cleanup order: 1. Signal close future to background thread (if session initialized) 2. Wait for background thread to complete 3. Reset all state for reuse
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
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 | |
MCPClientInitializationError
¶
Bases: Exception
Raised when the MCP server fails to initialize properly.
Source code in strands/types/exceptions.py
49 50 51 52 | |
MCPToolResult
¶
Bases: ToolResult
Result of an MCP tool execution.
Extends the base ToolResult with MCP-specific structured content support. The structuredContent field contains optional JSON data returned by MCP tools that provides structured results beyond the standard text/image/document content.
Attributes:
| Name | Type | Description |
|---|---|---|
structuredContent |
NotRequired[dict[str, Any]]
|
Optional JSON object containing structured data returned by the MCP tool. This allows MCP tools to return complex data structures that can be processed programmatically by agents or other tools. |
metadata |
NotRequired[dict[str, Any]]
|
Optional arbitrary metadata returned by the MCP tool. This field allows MCP servers to attach custom metadata to tool results (e.g., token usage, performance metrics, or business-specific tracking information). |
Source code in strands/tools/mcp/mcp_types.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
PaginatedList
¶
Bases: list, Generic[T]
A generic list-like object that includes a pagination token.
This maintains backwards compatibility by inheriting from list, so existing code that expects List[T] will continue to work.
Source code in strands/types/collections.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
__init__(data, token=None)
¶
Initialize a PaginatedList with data and an optional pagination token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
List[T]
|
The list of items to store. |
required |
token
|
Optional[str]
|
Optional pagination token for retrieving additional items. |
None
|
Source code in strands/types/collections.py
15 16 17 18 19 20 21 22 23 | |
ToolFilters
¶
Bases: TypedDict
Filters for controlling which MCP tools are loaded and available.
Tools are filtered in this order: 1. If 'allowed' is specified, only tools matching these patterns are included 2. Tools matching 'rejected' patterns are then excluded
Source code in strands/tools/mcp/mcp_client.py
52 53 54 55 56 57 58 59 60 61 | |
ToolProvider
¶
Bases: ABC
Interface for providing tools with lifecycle management.
Provides a way to load a collection of tools and clean them up when done, with lifecycle managed by the agent.
Source code in strands/experimental/tools/tool_provider.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 | |
add_consumer(consumer_id, **kwargs)
abstractmethod
¶
Add a consumer to this tool provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
consumer_id
|
Any
|
Unique identifier for the consumer. |
required |
**kwargs
|
Any
|
Additional arguments for future compatibility. |
{}
|
Source code in strands/experimental/tools/tool_provider.py
29 30 31 32 33 34 35 36 37 | |
load_tools(**kwargs)
abstractmethod
async
¶
Load and return the tools in this provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Additional arguments for future compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
Sequence[AgentTool]
|
List of tools that are ready to use. |
Source code in strands/experimental/tools/tool_provider.py
17 18 19 20 21 22 23 24 25 26 27 | |
remove_consumer(consumer_id, **kwargs)
abstractmethod
¶
Remove a consumer from this tool provider.
This method must be idempotent - calling it multiple times with the same ID should have no additional effect after the first call.
Provider may clean up resources when no consumers remain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
consumer_id
|
Any
|
Unique identifier for the consumer. |
required |
**kwargs
|
Any
|
Additional arguments for future compatibility. |
{}
|
Source code in strands/experimental/tools/tool_provider.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
ToolProviderException
¶
Bases: Exception
Exception raised when a tool provider fails to load or cleanup tools.
Source code in strands/types/exceptions.py
80 81 82 83 | |
ToolResultContent
¶
Bases: TypedDict
Content returned by a tool execution.
Attributes:
| Name | Type | Description |
|---|---|---|
document |
DocumentContent
|
Document content returned by the tool. |
image |
ImageContent
|
Image content returned by the tool. |
json |
Any
|
JSON-serializable data returned by the tool. |
text |
str
|
Text content returned by the tool. |
Source code in strands/types/tools.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
_ToolFilterCallback
¶
Bases: Protocol
Source code in strands/tools/mcp/mcp_client.py
45 46 | |
mcp_instrumentation()
¶
Apply OpenTelemetry instrumentation patches to MCP components.
This function instruments three key areas of MCP communication: 1. Client-side: Injects tracing context into tool call requests 2. Transport-level: Extracts context from incoming messages 3. Session-level: Manages bidirectional context flow
The patches enable distributed tracing by: - Adding OpenTelemetry context to the _meta field of MCP requests - Extracting and activating context on the server side - Preserving context across async message processing boundaries
This function is idempotent - multiple calls will not accumulate wrappers.
Source code in strands/tools/mcp/mcp_instrumentation.py
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 | |