Tools¶
Tools and utilities for Bedrock AgentCore SDK including browser and code interpreter tools.
bedrock_agentcore.tools.code_interpreter_client
¶
Client for interacting with the Code Interpreter sandbox service.
This module provides a client for the AWS Code Interpreter sandbox, allowing applications to start, stop, and invoke code execution in a managed sandbox environment.
CodeInterpreter
¶
Client for interacting with the AWS Code Interpreter sandbox service.
This client handles the session lifecycle and method invocation for Code Interpreter sandboxes, providing an interface to execute code in a secure, managed environment.
Attributes:
Name | Type | Description |
---|---|---|
data_plane_service_name |
str
|
AWS service name for the data plane. |
client |
The boto3 client for interacting with the service. |
|
identifier |
str
|
The code interpreter identifier. |
session_id |
str
|
The active session ID. |
Source code in bedrock_agentcore/tools/code_interpreter_client.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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
identifier
property
writable
¶
Get the current code interpreter identifier.
Returns:
Type | Description |
---|---|
Optional[str]
|
Optional[str]: The current identifier or None if not set. |
session_id
property
writable
¶
Get the current session ID.
Returns:
Type | Description |
---|---|
Optional[str]
|
Optional[str]: The current session ID or None if not set. |
__init__(region)
¶
Initialize a Code Interpreter client for the specified AWS region.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
region
|
str
|
The AWS region to use for the Code Interpreter service. |
required |
Source code in bedrock_agentcore/tools/code_interpreter_client.py
33 34 35 36 37 38 39 40 41 42 43 44 |
|
invoke(method, params=None)
¶
Invoke a method in the code interpreter sandbox.
If no session is active, this method automatically starts a new session before invoking the requested method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
str
|
The name of the method to invoke in the sandbox. |
required |
params
|
Optional[Dict]
|
Parameters to pass to the method. Defaults to None. |
None
|
request_id
|
Optional[str]
|
A custom request ID. If not provided, a unique ID is generated. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
The response from the code interpreter service. |
Source code in bedrock_agentcore/tools/code_interpreter_client.py
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 |
|
start(identifier=DEFAULT_IDENTIFIER, name=None, session_timeout_seconds=DEFAULT_TIMEOUT)
¶
Start a code interpreter sandbox session.
This method initializes a new code interpreter session with the provided parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
identifier
|
Optional[str]
|
The code interpreter sandbox identifier to use. Defaults to DEFAULT_IDENTIFIER. |
DEFAULT_IDENTIFIER
|
name
|
Optional[str]
|
A name for this session. If not provided, a name will be generated using a UUID. |
None
|
session_timeout_seconds
|
Optional[int]
|
The timeout for the session in seconds. Defaults to DEFAULT_TIMEOUT. |
DEFAULT_TIMEOUT
|
description
|
Optional[str]
|
A description for this session. Defaults to an empty string. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The session ID of the newly created session. |
Source code in bedrock_agentcore/tools/code_interpreter_client.py
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 |
|
stop()
¶
Stop the current code interpreter session if one is active.
This method stops any active session and clears the session state. If no session is active, this method does nothing.
Returns:
Name | Type | Description |
---|---|---|
bool |
True if no session was active or the session was successfully stopped. |
Source code in bedrock_agentcore/tools/code_interpreter_client.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
code_session(region)
¶
Context manager for creating and managing a code interpreter session.
This context manager handles creating a client, starting a session, and ensuring the session is properly cleaned up when the context exits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
region
|
str
|
The AWS region to use for the Code Interpreter service. |
required |
Yields:
Name | Type | Description |
---|---|---|
CodeInterpreterClient |
CodeInterpreter
|
An initialized and started code interpreter client. |
Example
with code_session('us-west-2') as client: ... result = client.invoke('listFiles') ... # Process result here
Source code in bedrock_agentcore/tools/code_interpreter_client.py
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 |
|
bedrock_agentcore.tools.browser_client
¶
Client for interacting with the Browser sandbox service.
This module provides a client for the AWS Browser sandbox, allowing applications to start, stop, and automate browser interactions in a managed sandbox environment using Playwright.
BrowserClient
¶
Client for interacting with the AWS Browser sandbox service.
This client handles the session lifecycle and browser automation for Browser sandboxes, providing an interface to perform web automation tasks in a secure, managed environment.
Attributes:
Name | Type | Description |
---|---|---|
region |
str
|
The AWS region being used. |
data_plane_service_name |
str
|
AWS service name for the data plane. |
client |
The boto3 client for interacting with the service. |
|
identifier |
str
|
The browser identifier. |
session_id |
str
|
The active session ID. |
Source code in bedrock_agentcore/tools/browser_client.py
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 |
|
identifier
property
writable
¶
Get the current browser identifier.
Returns:
Type | Description |
---|---|
Optional[str]
|
Optional[str]: The current identifier or None if not set. |
session_id
property
writable
¶
Get the current session ID.
Returns:
Type | Description |
---|---|
Optional[str]
|
Optional[str]: The current session ID or None if not set. |
__init__(region)
¶
Initialize a Browser client for the specified AWS region.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
region
|
str
|
The AWS region to use for the Browser service. |
required |
Source code in bedrock_agentcore/tools/browser_client.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
generate_live_view_url(expires=DEFAULT_LIVE_VIEW_PRESIGNED_URL_TIMEOUT)
¶
Generate a pre-signed URL for viewing the browser session.
Creates a pre-signed URL that can be used to view the current browser session. If no session is active, a new session will be started.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expires
|
int
|
The number of seconds until the pre-signed URL expires. Defaults to DEFAULT_LIVE_VIEW_PRESIGNED_URL_TIMEOUT (300 seconds). |
DEFAULT_LIVE_VIEW_PRESIGNED_URL_TIMEOUT
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The pre-signed URL for viewing the browser session. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the URL generation fails. |
Source code in bedrock_agentcore/tools/browser_client.py
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 |
|
generate_ws_headers()
¶
Generate the WebSocket headers needed for connecting to the browser sandbox.
This method creates properly signed WebSocket headers for connecting to the browser automation endpoint.
Returns:
Type | Description |
---|---|
Tuple[str, Dict[str, str]]
|
Tuple[str, Dict[str, str]]: A tuple containing the WebSocket URL and the headers dictionary. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If no AWS credentials are found. |
Source code in bedrock_agentcore/tools/browser_client.py
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 |
|
release_control()
¶
Release control of the browser session by enabling the automation stream.
This method enables external automation capabilities of the browser session, relinquishing exclusive control. If no session exists, a warning is logged and the method returns without taking action.
Source code in bedrock_agentcore/tools/browser_client.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
start(identifier=DEFAULT_IDENTIFIER, name=None, session_timeout_seconds=DEFAULT_SESSION_TIMEOUT)
¶
Start a browser sandbox session.
This method initializes a new browser session with the provided parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
identifier
|
Optional[str]
|
The browser sandbox identifier to use. Defaults to DEFAULT_IDENTIFIER. |
DEFAULT_IDENTIFIER
|
name
|
Optional[str]
|
A name for this session. If not provided, a name will be generated using a UUID. |
None
|
session_timeout_seconds
|
Optional[int]
|
The timeout for the session in seconds. Defaults to DEFAULT_TIMEOUT. |
DEFAULT_SESSION_TIMEOUT
|
description
|
Optional[str]
|
A description for this session. Defaults to an empty string. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The session ID of the newly created session. |
Source code in bedrock_agentcore/tools/browser_client.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 120 121 122 123 124 125 126 127 128 129 130 |
|
stop()
¶
Stop the current browser session if one is active.
This method stops any active session and clears the session state. If no session is active, this method does nothing.
Returns:
Name | Type | Description |
---|---|---|
bool |
True if no session was active or the session was successfully stopped. |
Source code in bedrock_agentcore/tools/browser_client.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
take_control()
¶
Take control of the browser session by disabling the automation stream.
This method disables external automation capabilities of the browser session, giving this client exclusive control. If no session is active, a new session will be started.
Raises:
Type | Description |
---|---|
RuntimeError
|
If a session could not be found or started. |
Source code in bedrock_agentcore/tools/browser_client.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
browser_session(region)
¶
Context manager for creating and managing a browser sandbox session.
This context manager handles creating a client, starting a session, and ensuring the session is properly cleaned up when the context exits.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
region
|
str
|
The AWS region to use for the Browser service. |
required |
Yields:
Name | Type | Description |
---|---|---|
BrowserClient |
BrowserClient
|
An initialized and started browser client. |
Example
with browser_session('us-west-2') as client: ... browser = client.get_browser_obj() ... page = browser.new_page() ... page.goto('https://example.com')
Source code in bedrock_agentcore/tools/browser_client.py
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 |
|