Skip to main content

A2aRequestHandler

Trait A2aRequestHandler 

Source
pub trait A2aRequestHandler:
    Send
    + Sync
    + 'static {
    // Required method
    fn dispatch(
        &self,
        method: A2aMethod,
        params: Option<Value>,
        context: &A2aRequestContext,
    ) -> Pin<Box<dyn Future<Output = Result<Value, A2aError>> + Send + '_>>;
}
Expand description

The consumer’s extension point for A2A methods.

The serving adapter calls dispatch once per JSON-RPC request. The handler receives the method (already validated against the A2A vocabulary) and the raw serde_json::Value params. The handler returns the raw result value, or an A2aError.

§Object safety

The trait is object-safe. A consumer who wants to swap handlers at runtime holds an Arc<dyn A2aRequestHandler>; the serving adapter accepts either an Arc<dyn> or a concrete H: A2aRequestHandler.

§Default impls

None of the methods have a default impl. A consumer who wants to leave a method unimplemented can return A2aError::MethodNotImplemented from dispatch; the serving adapter maps that to JSON-RPC -32601 and the caller’s client knows the method is not supported.

Required Methods§

Source

fn dispatch( &self, method: A2aMethod, params: Option<Value>, context: &A2aRequestContext, ) -> Pin<Box<dyn Future<Output = Result<Value, A2aError>> + Send + '_>>

Handles a routed A2A method.

The serving adapter guarantees:

  • method was looked up in the A2A vocabulary (so it is one of the 11 methods, not a string);
  • params is the raw JSON-RPC params object, or None if the request had no params.

The handler is responsible for:

  • Decoding params into the per-method type;
  • Running the method’s logic;
  • Returning either the raw result value (which the serving adapter puts in the result field) or an A2aError (which becomes the error field).
§Errors

Any A2aError. The serving adapter answers with the right JSON-RPC code; the handler never has to know the code.

§Example

A minimal handler that implements only SendMessage and returns A2aError::MethodNotImplemented for the other ten methods. The full reference impl is in the a2a_server example.

use std::pin::Pin;
use std::sync::Arc;

use futures_core::Future;
use serde_json::{json, Value};

use protocol_droid::adapters::secondary::a2a::A2aMethod;
    use protocol_droid::application::a2a::{
    A2aError, A2aRequestContext, A2aRequestHandler,
};

struct EchoAgent;

impl A2aRequestHandler for EchoAgent {
    fn dispatch(
        &self,
        method: A2aMethod,
        params: Option<Value>,
        _context: &A2aRequestContext,
    ) -> Pin<
        Box<dyn Future<Output = Result<Value, A2aError>> + Send + '_>,
    > {
        Box::pin(async move {
            match method {
                A2aMethod::SendMessage => {
                    // Decode the params, run the
                    // agent's logic, return the
                    // result as JSON.
                    Ok(json!({"id": "t-1", "status":
                        {"state": "completed"}}))
                }
                other => Err(A2aError::MethodNotImplemented {
                    method: other,
                }),
            }
        })
    }
}

let _handler: Arc<dyn A2aRequestHandler> = Arc::new(EchoAgent);

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§