Skip to main content

AgentExecutor

Trait AgentExecutor 

Source
pub trait AgentExecutor:
    Send
    + Sync
    + 'static {
    // Required method
    fn execute<'a>(
        &'a self,
        ctx: &'a RequestContext,
        queue: &'a dyn EventQueueWriter,
    ) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>>;

    // Provided methods
    fn cancel<'a>(
        &'a self,
        ctx: &'a RequestContext,
        _queue: &'a dyn EventQueueWriter,
    ) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>> { ... }
    fn on_shutdown<'a>(
        &'a self,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> { ... }
}
Expand description

Trait for implementing A2A agent execution logic.

Implementors process incoming messages by writing events (status updates, artifacts) to the provided EventQueueWriter. The executor runs in a spawned task and should signal completion by writing a terminal status update and returning Ok(()).

§Object safety

This trait is object-safe: methods return Pin<Box<dyn Future>> so that executors can be used as Arc<dyn AgentExecutor>. This eliminates the need for generic parameters on RequestHandler, RestDispatcher, and JsonRpcDispatcher, simplifying the entire server API surface.

§Example

use std::pin::Pin;
use std::future::Future;
use a2a_protocol_server::executor::AgentExecutor;
use a2a_protocol_server::request_context::RequestContext;
use a2a_protocol_server::streaming::EventQueueWriter;
use a2a_protocol_types::error::A2aResult;

struct MyAgent;

impl AgentExecutor for MyAgent {
    fn execute<'a>(
        &'a self,
        ctx: &'a RequestContext,
        queue: &'a dyn EventQueueWriter,
    ) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>> {
        Box::pin(async move {
            // Write status updates and artifacts to `queue`.
            Ok(())
        })
    }
}

Required Methods§

Source

fn execute<'a>( &'a self, ctx: &'a RequestContext, queue: &'a dyn EventQueueWriter, ) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>>

Executes agent logic for the given request.

Write StreamResponse events to queue as the agent progresses. The method should return Ok(()) after writing the final event, or Err(...) on failure.

§Errors

Returns an A2aError if execution fails.

Provided Methods§

Source

fn cancel<'a>( &'a self, ctx: &'a RequestContext, _queue: &'a dyn EventQueueWriter, ) -> Pin<Box<dyn Future<Output = A2aResult<()>> + Send + 'a>>

Cancels an in-progress task.

The default implementation returns an error indicating the task is not cancelable. Override this to support task cancellation.

§Errors

Returns an A2aError if cancellation fails or is not supported.

Source

fn on_shutdown<'a>(&'a self) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>

Called during handler shutdown to allow cleanup of external resources (database connections, file handles, etc.).

The default implementation is a no-op.

Implementors§