protocol-droid 0.3.0

A Model Context Protocol toolkit that speaks every published spec revision, with one canonical IR behind many per-revision codecs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! A2A push-notification method envelopes —
//! the four methods that read and write a
//! task's push-notification configurations.
//!
//! # What these are
//!
//! [`CreateTaskPushNotificationConfigRequest`]
//! and
//! [`CreateTaskPushNotificationConfigResponse`]
//! are the typed shapes for the
//! `CreateTaskPushNotificationConfig` JSON-RPC
//! method — register a new webhook for a task.
//!
//! [`GetTaskPushNotificationConfigRequest`]
//! and
//! [`GetTaskPushNotificationConfigResponse`]
//! are the typed shapes for the
//! `GetTaskPushNotificationConfig` JSON-RPC
//! method — fetch a single config by id.
//!
//! [`ListTaskPushNotificationConfigsRequest`]
//! and
//! [`ListTaskPushNotificationConfigsResponse`]
//! are the typed shapes for the
//! `ListTaskPushNotificationConfigs`
//! JSON-RPC method — list all configs for a
//! task.
//!
//! [`DeleteTaskPushNotificationConfigRequest`]
//! and
//! [`DeleteTaskPushNotificationConfigResponse`]
//! are the typed shapes for the
//! `DeleteTaskPushNotificationConfig`
//! JSON-RPC method — remove a config by id.
//!
//! # Why the response is `TaskPushNotificationConfig`
//! directly, not a wrapper
//!
//! Each of the three read/write methods
//! returns a [`TaskPushNotificationConfig`]
//! value. The shape is the inner value
//! directly — no wrapper object — so the
//! wire form is the spec's
//! `{"id": "...", "taskId": "...", "url": "..."}`.
//! A newtype with `transparent` is the
//! cheapest shape that keeps the type
//! distinct from a raw
//! `TaskPushNotificationConfig` (and lets
//! the consumer pattern-match on the
//! response type if they want to).
//!
//! # Why the delete response is `{}`
//!
//! The spec uses `google.protobuf.Empty` for
//! the delete response, which serialises to
//! `{}` on the wire. A handler that wants to
//! signal success does not have to write a
//! payload; the JSON-RPC layer's
//! `result: null` or `result: {}` is the
//! success signal.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use super::push_notification::TaskPushNotificationConfig;

// --- CreateTaskPushNotificationConfig ---------------------------

/// A typed `CreateTaskPushNotificationConfig`
/// request. The wire form is the
/// [`TaskPushNotificationConfig`] object
/// directly.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CreateTaskPushNotificationConfigRequest(pub TaskPushNotificationConfig);

impl From<TaskPushNotificationConfig> for CreateTaskPushNotificationConfigRequest {
    fn from(config: TaskPushNotificationConfig) -> Self {
        Self(config)
    }
}

impl std::ops::Deref for CreateTaskPushNotificationConfigRequest {
    type Target = TaskPushNotificationConfig;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// A typed `CreateTaskPushNotificationConfig`
/// response. The wire form is the
/// [`TaskPushNotificationConfig`] object
/// directly.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CreateTaskPushNotificationConfigResponse(pub TaskPushNotificationConfig);

impl From<TaskPushNotificationConfig> for CreateTaskPushNotificationConfigResponse {
    fn from(config: TaskPushNotificationConfig) -> Self {
        Self(config)
    }
}

impl std::ops::Deref for CreateTaskPushNotificationConfigResponse {
    type Target = TaskPushNotificationConfig;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

// --- GetTaskPushNotificationConfig -------------------------------

/// A typed `GetTaskPushNotificationConfig`
/// request. Wire form:
/// ```json
/// { "id": "config-1", "taskId": "task-1" }
/// ```
///
/// Both fields are required.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetTaskPushNotificationConfigRequest {
    /// The config's id.
    pub id: String,
    /// The parent task's id.
    pub task_id: String,
}

impl GetTaskPushNotificationConfigRequest {
    /// Starts a new request with `id` and
    /// `task_id`.
    #[must_use]
    pub fn new(id: impl Into<String>, task_id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            task_id: task_id.into(),
        }
    }
}

/// A typed `GetTaskPushNotificationConfig`
/// response. The wire form is the
/// [`TaskPushNotificationConfig`] object
/// directly.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct GetTaskPushNotificationConfigResponse(pub TaskPushNotificationConfig);

impl From<TaskPushNotificationConfig> for GetTaskPushNotificationConfigResponse {
    fn from(config: TaskPushNotificationConfig) -> Self {
        Self(config)
    }
}

impl std::ops::Deref for GetTaskPushNotificationConfigResponse {
    type Target = TaskPushNotificationConfig;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

// --- ListTaskPushNotificationConfigs -----------------------------

/// A typed `ListTaskPushNotificationConfigs`
/// request. Wire form:
/// ```json
/// {
///   "taskId": "task-1",
///   "pageSize": 50,
///   "pageToken": "..."
/// }
/// ```
///
/// `taskId` is required; `pageSize` and
/// `pageToken` are optional.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ListTaskPushNotificationConfigsRequest {
    /// The parent task's id. Required.
    pub task_id: String,
    /// The maximum number of configurations
    /// to return. A request with no
    /// page size lets the agent choose.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_size: Option<u32>,
    /// A page token received from a
    /// previous
    /// `ListTaskPushNotificationConfigsResponse`
    /// call. The agent uses it to continue
    /// from the last page.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
}

impl ListTaskPushNotificationConfigsRequest {
    /// Starts a new request with `task_id`.
    /// Use [`with_page_size`](Self::with_page_size)
    /// and
    /// [`with_page_token`](Self::with_page_token)
    /// to set the pagination fields.
    #[must_use]
    pub fn new(task_id: impl Into<String>) -> Self {
        Self {
            task_id: task_id.into(),
            page_size: None,
            page_token: None,
        }
    }

    /// Sets the page size.
    #[must_use]
    pub const fn with_page_size(mut self, page_size: u32) -> Self {
        self.page_size = Some(page_size);
        self
    }

    /// Sets the page token.
    #[must_use]
    pub fn with_page_token(mut self, page_token: impl Into<String>) -> Self {
        self.page_token = Some(page_token.into());
        self
    }
}

/// A typed `ListTaskPushNotificationConfigs`
/// response. Wire form:
/// ```json
/// {
///   "configs": [TaskPushNotificationConfig, ...],
///   "nextPageToken": "..."
/// }
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ListTaskPushNotificationConfigsResponse {
    /// The configurations. An empty list is
    /// a task that has no configs.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub configs: Vec<TaskPushNotificationConfig>,
    /// A token to retrieve the next page of
    /// results, or empty if there are no
    /// more results in the list. A response
    /// with no `nextPageToken` is the
    /// last page.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub next_page_token: Option<String>,
}

impl ListTaskPushNotificationConfigsResponse {
    /// Starts a new response with `configs`.
    /// Use
    /// [`with_next_page_token`](Self::with_next_page_token)
    /// to attach a token.
    #[must_use]
    pub fn new(configs: impl IntoIterator<Item = TaskPushNotificationConfig>) -> Self {
        Self {
            configs: configs.into_iter().collect(),
            next_page_token: None,
        }
    }

    /// Sets the next page token.
    #[must_use]
    pub fn with_next_page_token(mut self, next_page_token: impl Into<String>) -> Self {
        self.next_page_token = Some(next_page_token.into());
        self
    }
}

// --- DeleteTaskPushNotificationConfig ----------------------------

/// A typed `DeleteTaskPushNotificationConfig`
/// request. Wire form:
/// ```json
/// { "id": "config-1", "taskId": "task-1" }
/// ```
///
/// Both fields are required.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteTaskPushNotificationConfigRequest {
    /// The config's id.
    pub id: String,
    /// The parent task's id.
    pub task_id: String,
}

impl DeleteTaskPushNotificationConfigRequest {
    /// Starts a new request with `id` and
    /// `task_id`.
    #[must_use]
    pub fn new(id: impl Into<String>, task_id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            task_id: task_id.into(),
        }
    }
}

/// A typed `DeleteTaskPushNotificationConfig`
/// response. The wire form is `{}` (the
/// spec's `google.protobuf.Empty`).
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeleteTaskPushNotificationConfigResponse {
    /// A catch-all for the metadata. The
    /// spec does not name a field, but a
    /// server that wants to include a
    /// trailing note (e.g. `"deleted_at"`:
    /// `"2025-01-01T00:00:00Z"`) can use
    /// this map.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub metadata: BTreeMap<String, serde_json::Value>,
}

#[cfg(test)]
mod tests {
    use super::{
        CreateTaskPushNotificationConfigRequest, CreateTaskPushNotificationConfigResponse,
        DeleteTaskPushNotificationConfigRequest, DeleteTaskPushNotificationConfigResponse,
        GetTaskPushNotificationConfigRequest, GetTaskPushNotificationConfigResponse,
        ListTaskPushNotificationConfigsRequest, ListTaskPushNotificationConfigsResponse,
    };
    use crate::domain::a2a::push_notification::TaskPushNotificationConfig;

    type TestError = Box<dyn std::error::Error>;

    fn sample_config() -> TaskPushNotificationConfig {
        TaskPushNotificationConfig::new("c-1", "t-1", "https://example.com/wh")
    }

    // --- Create -----------------------------------------------------

    #[test]
    fn create_request_serialises_as_config_object() -> Result<(), TestError> {
        let request = CreateTaskPushNotificationConfigRequest::from(sample_config());
        let value = serde_json::to_value(&request)?;
        let obj = value
            .as_object()
            .ok_or("a CreateTaskPushNotificationConfigRequest must serialise to a JSON object")?;
        assert_eq!(
            obj.get("id").and_then(serde_json::Value::as_str),
            Some("c-1")
        );
        assert_eq!(
            obj.get("taskId").and_then(serde_json::Value::as_str),
            Some("t-1")
        );
        // The wire form is the inner
        // config — no wrapper.
        assert!(!obj.contains_key("config"));
        Ok(())
    }

    #[test]
    fn create_response_from_config_via_into_works() {
        let config = sample_config();
        let response: CreateTaskPushNotificationConfigResponse = config.clone().into();
        assert_eq!(response.0, config);
    }

    // --- Get --------------------------------------------------------

    #[test]
    fn get_request_serialises_with_id_and_task_id() -> Result<(), TestError> {
        let request = GetTaskPushNotificationConfigRequest::new("c-1", "t-1");
        let value = serde_json::to_value(&request)?;
        let obj = value
            .as_object()
            .ok_or("a GetTaskPushNotificationConfigRequest must serialise to a JSON object")?;
        assert_eq!(
            obj.get("id").and_then(serde_json::Value::as_str),
            Some("c-1")
        );
        assert_eq!(
            obj.get("taskId").and_then(serde_json::Value::as_str),
            Some("t-1")
        );
        Ok(())
    }

    #[test]
    fn get_response_serialises_as_config_object() -> Result<(), TestError> {
        let response = GetTaskPushNotificationConfigResponse::from(sample_config());
        let value = serde_json::to_value(&response)?;
        let obj = value
            .as_object()
            .ok_or("a GetTaskPushNotificationConfigResponse must serialise to a JSON object")?;
        assert_eq!(
            obj.get("id").and_then(serde_json::Value::as_str),
            Some("c-1")
        );
        Ok(())
    }

    // --- List -------------------------------------------------------

    #[test]
    fn list_request_serialises_with_task_id_only() -> Result<(), TestError> {
        let request = ListTaskPushNotificationConfigsRequest::new("t-1");
        let value = serde_json::to_value(&request)?;
        let obj = value
            .as_object()
            .ok_or("a ListTaskPushNotificationConfigsRequest must serialise to a JSON object")?;
        assert_eq!(
            obj.get("taskId").and_then(serde_json::Value::as_str),
            Some("t-1")
        );
        assert!(!obj.contains_key("pageSize"));
        assert!(!obj.contains_key("pageToken"));
        Ok(())
    }

    #[test]
    fn list_request_round_trips_with_pagination() -> Result<(), TestError> {
        let request = ListTaskPushNotificationConfigsRequest::new("t-1")
            .with_page_size(50)
            .with_page_token("next-page");
        let json = serde_json::to_string(&request)?;
        let parsed: ListTaskPushNotificationConfigsRequest = serde_json::from_str(&json)?;
        assert_eq!(parsed, request);
        Ok(())
    }

    #[test]
    fn list_response_with_no_next_page_token_omits_the_field() -> Result<(), TestError> {
        // A response that is the last
        // page has no `nextPageToken` —
        // the absence is the signal.
        let response = ListTaskPushNotificationConfigsResponse::new([sample_config()]);
        let value = serde_json::to_value(&response)?;
        let obj = value
            .as_object()
            .ok_or("a ListTaskPushNotificationConfigsResponse must serialise to a JSON object")?;
        assert!(!obj.contains_key("nextPageToken"));
        let configs = obj
            .get("configs")
            .and_then(serde_json::Value::as_array)
            .ok_or("`configs` must be a JSON array")?;
        assert_eq!(configs.len(), 1);
        Ok(())
    }

    #[test]
    fn list_response_round_trips_with_token_and_configs() -> Result<(), TestError> {
        let response = ListTaskPushNotificationConfigsResponse::new([
            sample_config(),
            TaskPushNotificationConfig::new("c-2", "t-1", "https://example.com/wh2"),
        ])
        .with_next_page_token("next");
        let json = serde_json::to_string(&response)?;
        let parsed: ListTaskPushNotificationConfigsResponse = serde_json::from_str(&json)?;
        assert_eq!(parsed, response);
        Ok(())
    }

    // --- Delete -----------------------------------------------------

    #[test]
    fn delete_request_serialises_with_id_and_task_id() -> Result<(), TestError> {
        let request = DeleteTaskPushNotificationConfigRequest::new("c-1", "t-1");
        let value = serde_json::to_value(&request)?;
        let obj = value
            .as_object()
            .ok_or("a DeleteTaskPushNotificationConfigRequest must serialise to a JSON object")?;
        assert_eq!(
            obj.get("id").and_then(serde_json::Value::as_str),
            Some("c-1")
        );
        assert_eq!(
            obj.get("taskId").and_then(serde_json::Value::as_str),
            Some("t-1")
        );
        Ok(())
    }

    #[test]
    fn delete_response_with_no_metadata_is_an_empty_object() -> Result<(), TestError> {
        // The spec uses
        // `google.protobuf.Empty` for the
        // delete response, which
        // serialises to `{}` on the
        // wire. A response that adds no
        // metadata is exactly `{}`.
        let response = DeleteTaskPushNotificationConfigResponse::default();
        let value = serde_json::to_value(&response)?;
        let obj = value
            .as_object()
            .ok_or("a DeleteTaskPushNotificationConfigResponse must serialise to a JSON object")?;
        assert!(obj.is_empty(), "expected `{{}}`, got {value}");
        Ok(())
    }
}