pub trait AgentExecutor:
Send
+ Sync
+ 'static {
// Required method
fn execute<'a>(
&'a self,
ctx: &'a RequestContext,
queue: &'a (dyn EventQueueWriter + 'static),
) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>;
// Provided methods
fn cancel<'a>(
&'a self,
ctx: &'a RequestContext,
queue: &'a (dyn EventQueueWriter + 'static),
) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>> { ... }
fn resumes_after_hand_off(&self) -> bool { ... }
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(())
})
}
}§Ergonomic helpers
Use boxed_future to reduce
boilerplate, or the agent_executor! macro
for a fully declarative approach:
use a2a_protocol_server::agent_executor;
struct EchoAgent;
agent_executor!(EchoAgent, |_ctx, _queue| async {
Ok(())
});Required Methods§
Sourcefn execute<'a>(
&'a self,
ctx: &'a RequestContext,
queue: &'a (dyn EventQueueWriter + 'static),
) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>
fn execute<'a>( &'a self, ctx: &'a RequestContext, queue: &'a (dyn EventQueueWriter + 'static), ) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + 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§
Sourcefn cancel<'a>(
&'a self,
ctx: &'a RequestContext,
queue: &'a (dyn EventQueueWriter + 'static),
) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>
fn cancel<'a>( &'a self, ctx: &'a RequestContext, queue: &'a (dyn EventQueueWriter + 'static), ) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>
Cancels an in-progress task.
The default implementation cancels. It emits the terminal
Canceled status so subscribers see it, best-effort: a task with no
live subscribers has no queue receivers, and that must not fail the
cancel, because the handler persists the state either way. Cancellation
in A2A is cooperative — by the time this runs the handler has already
triggered the running task’s cancellation token, which a running
execute is expected to observe. The ctx passed here is a fresh
context for the cancel call: its own
RequestContext::cancellation_token is not cancelled.
Override this when the task holds something that must be released —
a reserved slot, a parked message, an open handle. Overriding is about
releasing state, not about opting in to cancellation: an override that
returns Ok without writing a terminal state has the Canceled status
written for it, after it returns, so the task’s open streams still end
on it.
The handler also runs this hook when its own shutdown stops a task.
The task then ends Failed with failure class transient instead,
and a Canceled written here is written as that failure: nobody
cancelled it, and its caller may send it again.
Before 0.7 the default refused with TaskNotCancelable, which left
Working tasks uncancelable out of the box and reported it as the
task’s fault. Every reference SDK requires agents to support cancel.
§Errors
Returns an A2aError if an
override fails to release what the task holds. The default
implementation does not return an error.
Sourcefn resumes_after_hand_off(&self) -> bool
fn resumes_after_hand_off(&self) -> bool
Whether a turn this executor did not finish may be run again from its input, so that a graceful shutdown hands it back instead of failing it (ADR 0054, audit D-21).
The default is false: shutdown ends a turn still running after the
completion window Failed, failure class transient. Answer true
only when running a turn twice is safe: its tool calls carry
idempotency keys (ADR 0026), its other side effects are checked before
they are made. Then, on a handler with turn claims (a shared task
store), a turn that returns after shutdown fired its token, without a
terminal or interrupted state, is left working, with a status
message whose metadata carries HAND_OFF_METADATA_KEY, and its claim
is released; the cancel hook is not run. Its caller sends the turn’s
message again to the same task id, which another replica admits and
runs with the caller’s own credentials. Nothing re-runs it otherwise:
the reaper leaves a handed-off task alone for HAND_OFF_WINDOW and
then ends it Failed/transient, as it ends any task nobody runs.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".