Class RequestContext

java.lang.Object
org.a2aproject.sdk.server.agentexecution.RequestContext

public class RequestContext extends Object
Container for request parameters and task state provided to AgentExecutor.

This class encapsulates all the information an agent needs to process a request: the user's message, existing task state (for continuing conversations), configuration, and server call context. It's the primary way agents access request data.

Key Components

  • Message: The user's input message with parts (text, images, etc.)
  • Task: Existing task state for continuing conversations (null for new conversations)
  • TaskId/ContextId: Identifiers for the task and conversation (auto-generated if not provided)
  • Configuration: Request settings (blocking mode, push notifications, etc.)
  • Related Tasks: Other tasks in the same conversation context

Common Usage Patterns


 public void execute(RequestContext context, AgentEmitter emitter) {
     // Check if this is a new conversation or continuation
     Task existingTask = context.getTask();
     if (existingTask == null) {
         // New conversation - initialize
     } else {
         // Continuing conversation - access history
         List<Message> history = existingTask.history();
     }

     // Extract user input
     String userMessage = context.getUserInput("\n");

     // Access configuration if needed
     MessageSendConfiguration config = context.getConfiguration();
     boolean returnImmediately = config != null && Boolean.TRUE.equals(config.returnImmediately());

     // Process and respond...
 }
 

Text Extraction Helper

The getUserInput(String) method is a convenient way to extract text from message parts:

 // Get all text parts joined with newlines
 String text = context.getUserInput("\n");

 // Get all text parts joined with spaces
 String text = context.getUserInput(" ");
 
See Also:
  • Method Details

    • getTaskId

      public String getTaskId()
      Returns the task identifier.

      This is auto-generated (UUID) by the builder if not provided by the client in the message parameters. This value is never null.

      Returns:
      the task ID (never null)
    • getContextId

      public String getContextId()
      Returns the conversation context identifier.

      Conversation contexts group related tasks together (e.g., multiple tasks in the same user session). This is auto-generated (UUID) by the builder if not provided by the client in the message parameters. This value is never null.

      Returns:
      the context ID (never null)
    • getTask

      public @Nullable Task getTask()
      Returns the existing task state, if this is a continuation of a conversation.

      For new conversations, this is null. For continuing conversations, contains the full task state including history, artifacts, and status.

      Common Pattern:

      
       if (context.getTask() == null) {
           // New conversation - initialize state
       } else {
           // Continuing - access previous messages
           List<Message> history = context.getTask().history();
       }
       
      Returns:
      the existing task, or null if this is a new conversation
    • getRelatedTasks

      public List<Task> getRelatedTasks()
      Returns other tasks in the same conversation context.

      Useful for multi-task conversations where the agent needs to access state from related tasks.

      Returns:
      unmodifiable list of related tasks (empty if none)
    • getMessage

      public @Nullable Message getMessage()
      Returns the user's message.

      Contains the message parts (text, images, etc.) sent by the client. Use getUserInput(String) for convenient text extraction.

      Returns:
      the message, or null if not available
      See Also:
    • getConfiguration

      public @Nullable MessageSendConfiguration getConfiguration()
      Returns the request configuration.

      Contains settings like blocking mode, push notification config, etc.

      Returns:
      the configuration, or null if not provided
    • getMetadata

      public @Nullable Map<String,Object> getMetadata()
      Returns the request metadata.
      Returns:
      the metadata, or null if not available
    • getCallContext

      public @Nullable ServerCallContext getCallContext()
      Returns the server call context.

      Contains transport-specific information like authentication, headers, etc. Most agents don't need this.

      Returns:
      the call context, or null if not available
    • getTenant

      public @Nullable String getTenant()
      Returns the tenant identifier from the request parameters.

      The tenant is used in multi-tenant environments to identify which customer or organization the request belongs to.

      Returns:
      the tenant identifier, or null if no params or tenant not set
    • getUserInput

      public String getUserInput()
      Extracts all text content from the message and joins with the newline delimiter.

      This is a convenience method for getting text input from messages that may contain multiple text parts. Non-text parts (images, etc.) are ignored.

      Examples:

      
       // Join with newlines (common for multi-paragraph input)
       String text = context.getUserInput();
       
      Returns:
      all text parts joined with newline, or empty string if no message
    • getUserInput

      public String getUserInput(@Nullable String delimiter)
      Extracts all text content from the message and joins with the specified delimiter.

      This is a convenience method for getting text input from messages that may contain multiple text parts. Non-text parts (images, etc.) are ignored.

      Examples:

      
       // Join with newlines (common for multi-paragraph input)
       String text = context.getUserInput();
      
       // Join with spaces (common for single-line input)
       String text = context.getUserInput(" ");
      
       // Default delimiter is newline
       String text = context.getUserInput(null);  // uses "\n"
       
      Parameters:
      delimiter - the string to insert between text parts (null defaults to "\n")
      Returns:
      all text parts joined with delimiter, or empty string if no message
    • attachRelatedTask

      public void attachRelatedTask(Task task)
      Attaches a related task to this context.

      This is primarily used by the framework to populate related tasks after construction. Agent implementations should use getRelatedTasks() to access related tasks.

      Parameters:
      task - the task to attach