agent_client_protocol_schema/v1/protocol_level.rs
1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use serde_with::{DefaultOnError, serde_as, skip_serializing_none};
4
5use crate::IntoOption;
6
7use super::{Meta, RequestId};
8
9/// Notification to cancel an ongoing request.
10///
11/// See protocol docs: [Cancellation](https://agentclientprotocol.com/protocol/cancellation)
12#[serde_as]
13#[skip_serializing_none]
14#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
15#[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 #[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#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
87#[serde(untagged)]
88#[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}