Skip to main content

agent_client_protocol_schema/v2/
protocol_level.rs

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