Skip to main content

agent_client_protocol_schema/v1/
protocol_level.rs

1use serde::{Deserialize, Serialize};
2use serde_with::{DefaultOnError, serde_as, skip_serializing_none};
3
4use crate::IntoOption;
5
6use super::{Meta, RequestId};
7
8/// Notification to cancel an ongoing request.
9///
10/// See protocol docs: [Cancellation](https://agentclientprotocol.com/protocol/cancellation)
11#[serde_as]
12#[skip_serializing_none]
13#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
14#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
15#[cfg_attr(feature = "schemars", schemars(extend("x-side" = "protocol", "x-method" = CANCEL_REQUEST_METHOD_NAME)))]
16#[serde(rename_all = "camelCase")]
17#[non_exhaustive]
18pub struct CancelRequestNotification {
19    /// The ID of the request to cancel.
20    pub request_id: RequestId,
21    /// The _meta property is reserved by ACP to allow clients and agents to attach additional
22    /// metadata to their interactions. Implementations MUST NOT make assumptions about values at
23    /// these keys.
24    ///
25    /// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
26    #[serde_as(deserialize_as = "DefaultOnError")]
27    #[cfg_attr(feature = "schemars", schemars(extend("x-deserialize-default-on-error" = true)))]
28    #[serde(default)]
29    #[serde(rename = "_meta")]
30    pub meta: Option<Meta>,
31}
32
33impl CancelRequestNotification {
34    /// Builds [`CancelRequestNotification`] with the required notification fields set; optional fields start unset or empty.
35    #[must_use]
36    pub fn new(request_id: impl Into<RequestId>) -> Self {
37        Self {
38            request_id: request_id.into(),
39            meta: None,
40        }
41    }
42
43    /// The _meta property is reserved by ACP to allow clients and agents to attach additional
44    /// metadata to their interactions. Implementations MUST NOT make assumptions about values at
45    /// these keys.
46    ///
47    /// See protocol docs: [Extensibility](https://agentclientprotocol.com/protocol/extensibility)
48    #[must_use]
49    pub fn meta(mut self, meta: impl IntoOption<Meta>) -> Self {
50        self.meta = meta.into_option();
51        self
52    }
53}
54
55// Method schema
56
57/// Names of all methods that agents handle.
58///
59/// Provides a centralized definition of method names used in the protocol.
60#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
61#[non_exhaustive]
62pub struct GeneralMethodNames {
63    /// Method name for protocol-level request cancellation notifications.
64    pub cancel_request: &'static str,
65}
66
67/// Constant containing all agent method names.
68pub const PROTOCOL_LEVEL_METHOD_NAMES: GeneralMethodNames = GeneralMethodNames {
69    cancel_request: CANCEL_REQUEST_METHOD_NAME,
70};
71
72/// Method name for general cancel notification
73pub(crate) const CANCEL_REQUEST_METHOD_NAME: &str = "$/cancel_request";
74
75/// General protocol-level notifications that all sides are expected to
76/// implement.
77///
78/// Notifications whose methods start with '$/' are messages which
79/// are protocol implementation dependent and might not be implementable in all
80/// clients or agents. For example if the implementation uses a single threaded
81/// synchronous programming language then there is little it can do to react to
82/// a `$/cancel_request` notification. If an agent or client receives
83/// notifications starting with '$/' it is free to ignore the notification.
84///
85/// Notifications do not expect a response.
86#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
87#[derive(Clone, Debug, Serialize, Deserialize)]
88#[serde(untagged)]
89#[cfg_attr(feature = "schemars", schemars(inline))]
90#[non_exhaustive]
91pub enum ProtocolLevelNotification {
92    /// Cancels an ongoing request.
93    ///
94    /// This is a notification sent by the side that sent a request to cancel that request.
95    ///
96    /// Upon receiving this notification, the receiver:
97    ///
98    /// 1. MAY cancel the corresponding request activity and all nested activities
99    /// 2. MAY send any pending notifications.
100    /// 3. MUST send one of these responses for the original request:
101    ///   - Valid response with appropriate data (partial results or cancellation marker)
102    ///   - Error response with code `-32800` (Cancelled)
103    ///
104    /// See protocol docs: [Cancellation](https://agentclientprotocol.com/protocol/cancellation)
105    CancelRequestNotification(CancelRequestNotification),
106}
107
108impl ProtocolLevelNotification {
109    /// Returns the corresponding method name of the notification.
110    #[must_use]
111    pub fn method(&self) -> &str {
112        match self {
113            Self::CancelRequestNotification(..) => PROTOCOL_LEVEL_METHOD_NAMES.cancel_request,
114        }
115    }
116}