Skip to main content

AcpRequestHandler

Trait AcpRequestHandler 

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

The consumer’s extension point for ACP methods.

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

§Object safety

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

§Default impls

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

§Example

A minimal handler that implements only initialize and returns AcpError::MethodNotImplemented for the other twelve methods. The full reference impl is in a future acp_agent example.

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

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

use protocol_droid::domain::acp::method::AgentMethod;
use protocol_droid::application::acp::{
    AcpError, AcpRequestContext, AcpRequestHandler,
};

struct InitializeOnlyAgent;

impl AcpRequestHandler for InitializeOnlyAgent {
    fn dispatch(
        &self,
        method: AgentMethod,
        _params: Option<Value>,
        _context: &AcpRequestContext,
    ) -> Pin<
        Box<dyn Future<Output = Result<Value, AcpError>> + Send + '_>,
    > {
        Box::pin(async move {
            match method {
                AgentMethod::Initialize => {
                    // Run the agent's initialize
                    // handshake, return the
                    // response as JSON.
                    Ok(json!({"protocolVersion": 1, "agentInfo": {}}))
                }
                other => Err(AcpError::MethodNotImplemented {
                    method: other,
                }),
            }
        })
    }
}

let _handler: Arc<dyn AcpRequestHandler> = Arc::new(InitializeOnlyAgent);

Required Methods§

Source

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

Handles a routed ACP method.

The serving adapter guarantees:

  • method was looked up in the ACP vocabulary (so it is one of the 13 agent 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 AcpError (which becomes the error field).
§Errors

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

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§