invokeAgentRuntime method

Future<InvokeAgentRuntimeResponse> invokeAgentRuntime({
  1. required String agentRuntimeArn,
  2. required Uint8List payload,
  3. String? accept,
  4. String? accountId,
  5. String? baggage,
  6. String? contentType,
  7. String? mcpProtocolVersion,
  8. String? mcpSessionId,
  9. String? qualifier,
  10. String? runtimeSessionId,
  11. String? runtimeUserId,
  12. String? traceId,
  13. String? traceParent,
  14. String? traceState,
})

Sends a request to an agent or tool hosted in an Amazon Bedrock AgentCore Runtime and receives responses in real-time.

To invoke an agent, you can specify either the AgentCore Runtime ARN or the agent ID with an account ID, and provide a payload containing your request. When you use the agent ID instead of the full ARN, you don't need to URL-encode the identifier. You can optionally specify a qualifier to target a specific endpoint of the agent.

This operation supports streaming responses, allowing you to receive partial responses as they become available. We recommend using pagination to ensure that the operation returns quickly and successfully when processing large responses.

For example code, see Invoke an AgentCore Runtime agent.

If you're integrating your agent with OAuth, you can't use the Amazon Web Services SDK to call InvokeAgentRuntime. Instead, make a HTTPS request to InvokeAgentRuntime. For an example, see Authenticate and authorize with Inbound Auth and Outbound Auth.

To use this operation, you must have the bedrock-agentcore:InvokeAgentRuntime permission. If you are making a call to InvokeAgentRuntime on behalf of a user ID with the X-Amzn-Bedrock-AgentCore-Runtime-User-Id header, You require permissions to both actions (bedrock-agentcore:InvokeAgentRuntime and bedrock-agentcore:InvokeAgentRuntimeForUser).

May throw AccessDeniedException. May throw InternalServerException. May throw ResourceNotFoundException. May throw RetryableConflictException. May throw RuntimeClientError. May throw ServiceQuotaExceededException. May throw ThrottlingException. May throw ValidationException.

Parameter agentRuntimeArn : The identifier of the agent runtime to invoke. You can specify either the full Amazon Web Services Resource Name (ARN) or the agent ID. If you use the agent ID, you must also provide the accountId query parameter.

Parameter payload : The input data to send to the agent runtime. The format of this data depends on the specific agent configuration and must match the specified content type. For most agents, this is a JSON object containing the user's request.

Parameter accept : The desired MIME type for the response from the agent runtime. This tells the agent runtime what format to use for the response data. Common values include application/json for JSON data.

Parameter accountId : The identifier of the Amazon Web Services account for the agent runtime resource. This parameter is required when you specify an agent ID instead of the full ARN for agentRuntimeArn.

Parameter baggage : Additional context information for distributed tracing.

Parameter contentType : The MIME type of the input data in the payload. This tells the agent runtime how to interpret the payload data. Common values include application/json for JSON data.

Parameter mcpProtocolVersion : The version of the MCP protocol being used.

Parameter mcpSessionId : The identifier of the MCP session.

Parameter qualifier : The qualifier to use for the agent runtime. This is an endpoint name that points to a specific version. If not specified, Amazon Bedrock AgentCore uses the default endpoint of the agent runtime.

Parameter runtimeSessionId : The identifier of the runtime session.

Parameter runtimeUserId : The identifier of the runtime user.

Parameter traceId : The trace identifier for request tracking.

Parameter traceParent : The parent trace information for distributed tracing.

Parameter traceState : The trace state information for distributed tracing.

Implementation

Future<InvokeAgentRuntimeResponse> invokeAgentRuntime({
  required String agentRuntimeArn,
  required Uint8List payload,
  String? accept,
  String? accountId,
  String? baggage,
  String? contentType,
  String? mcpProtocolVersion,
  String? mcpSessionId,
  String? qualifier,
  String? runtimeSessionId,
  String? runtimeUserId,
  String? traceId,
  String? traceParent,
  String? traceState,
}) async {
  final headers = <String, String>{
    if (accept != null) 'Accept': accept.toString(),
    if (baggage != null) 'baggage': baggage.toString(),
    if (contentType != null) 'Content-Type': contentType.toString(),
    if (mcpProtocolVersion != null)
      'Mcp-Protocol-Version': mcpProtocolVersion.toString(),
    if (mcpSessionId != null) 'Mcp-Session-Id': mcpSessionId.toString(),
    if (runtimeSessionId != null)
      'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id':
          runtimeSessionId.toString(),
    if (runtimeUserId != null)
      'X-Amzn-Bedrock-AgentCore-Runtime-User-Id': runtimeUserId.toString(),
    if (traceId != null) 'X-Amzn-Trace-Id': traceId.toString(),
    if (traceParent != null) 'traceparent': traceParent.toString(),
    if (traceState != null) 'tracestate': traceState.toString(),
  };
  final $query = <String, List<String>>{
    if (accountId != null) 'accountId': [accountId],
    if (qualifier != null) 'qualifier': [qualifier],
  };
  final response = await _protocol.sendRaw(
    payload: payload,
    method: 'POST',
    requestUri:
        '/runtimes/${Uri.encodeComponent(agentRuntimeArn)}/invocations',
    queryParams: $query,
    headers: headers,
    exceptionFnMap: _exceptionFns,
  );
  return InvokeAgentRuntimeResponse(
    response: await response.stream.toBytes(),
    contentType:
        _s.extractHeaderStringValue(response.headers, 'Content-Type')!,
    baggage: _s.extractHeaderStringValue(response.headers, 'baggage'),
    mcpProtocolVersion:
        _s.extractHeaderStringValue(response.headers, 'Mcp-Protocol-Version'),
    mcpSessionId:
        _s.extractHeaderStringValue(response.headers, 'Mcp-Session-Id'),
    runtimeSessionId: _s.extractHeaderStringValue(
        response.headers, 'X-Amzn-Bedrock-AgentCore-Runtime-Session-Id'),
    traceId: _s.extractHeaderStringValue(response.headers, 'X-Amzn-Trace-Id'),
    traceParent: _s.extractHeaderStringValue(response.headers, 'traceparent'),
    traceState: _s.extractHeaderStringValue(response.headers, 'tracestate'),
    statusCode: response.statusCode,
  );
}