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§
Sourcefn dispatch(
&self,
method: AgentMethod,
params: Option<Value>,
context: &AcpRequestContext,
) -> Pin<Box<dyn Future<Output = Result<Value, AcpError>> + Send + '_>>
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:
methodwas looked up in the ACP vocabulary (so it is one of the 13 agent methods, not a string);paramsis the raw JSON-RPC params object, orNoneif the request had no params.
The handler is responsible for:
- Decoding
paramsinto the per-method type; - Running the method’s logic;
- Returning either the raw result value
(which the serving adapter puts in the
resultfield) or anAcpError(which becomes theerrorfield).
§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".