tower-mcp-types 0.25.0

Standalone MCP protocol types -- no Tower/Tokio required, WASM-safe, re-exported by tower-mcp
Documentation
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Exact wire types for the final MCP Tasks extension (SEP-2663).
//!
//! These types deliberately live outside [`crate::protocol`]'s legacy task
//! types. The 2025-11-25 experimental Tasks feature is not wire-compatible
//! with the final `io.modelcontextprotocol/tasks` extension:
//!
//! - final task timestamps use `ttlMs` and `pollIntervalMs`;
//! - final task creation is a flat `resultType: "task"` result;
//! - `tasks/get` returns a status-discriminated [`DetailedTask`];
//! - `tasks/update` and `tasks/cancel` return complete acknowledgements.
//!
//! Keeping both surfaces distinct lets a runtime choose the correct wire model
//! from the negotiated protocol version without emitting mixed compatibility
//! objects.

use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};

use crate::error::JsonRpcError;
use crate::protocol::{InputRequests, InputResponses, RequestMeta, ResultType, TaskStatus};

/// Extension identifier for final Tasks capability negotiation.
pub const EXTENSION_ID: &str = crate::protocol::TASKS_EXTENSION_ID;

/// Fields shared by every final task state.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TaskMetadata {
    /// Stable, server-generated task identifier.
    pub task_id: String,
    /// Optional human-readable state description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status_message: Option<String>,
    /// ISO 8601 creation timestamp.
    pub created_at: String,
    /// ISO 8601 timestamp of the latest state change.
    pub last_updated_at: String,
    /// Time-to-live from creation in milliseconds, or `null` for unlimited.
    ///
    /// Unlike an omitted optional field, SEP-2663 requires `ttlMs` to be
    /// present. `None` therefore serializes as JSON `null`.
    pub ttl_ms: Option<u64>,
    /// Suggested client polling interval in milliseconds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub poll_interval_ms: Option<u64>,
}

impl TaskMetadata {
    /// Construct the required final task metadata.
    pub fn new(
        task_id: impl Into<String>,
        created_at: impl Into<String>,
        last_updated_at: impl Into<String>,
        ttl_ms: Option<u64>,
    ) -> Self {
        Self {
            task_id: task_id.into(),
            status_message: None,
            created_at: created_at.into(),
            last_updated_at: last_updated_at.into(),
            ttl_ms,
            poll_interval_ms: None,
        }
    }

    /// Set a human-readable status message.
    pub fn with_status_message(mut self, message: impl Into<String>) -> Self {
        self.status_message = Some(message.into());
        self
    }

    /// Set the suggested polling interval.
    pub fn with_poll_interval_ms(mut self, poll_interval_ms: u64) -> Self {
        self.poll_interval_ms = Some(poll_interval_ms);
        self
    }
}

/// Base task state embedded in a final [`CreateTaskResult`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Task {
    /// Shared task fields.
    #[serde(flatten)]
    pub metadata: TaskMetadata,
    /// Current task status.
    pub status: TaskStatus,
}

impl Task {
    /// Construct a base task state.
    pub fn new(metadata: TaskMetadata, status: TaskStatus) -> Self {
        Self { metadata, status }
    }
}

/// Complete status-specific task state returned by `tasks/get` and
/// `notifications/tasks`.
///
/// The internally tagged representation guarantees that required payloads
/// match their status: `input_required` has `inputRequests`, `completed` has
/// `result`, and `failed` has `error`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "snake_case")]
#[non_exhaustive]
pub enum DetailedTask {
    /// Work is currently in progress.
    Working {
        /// Shared task fields.
        #[serde(flatten)]
        metadata: TaskMetadata,
    },
    /// Work is paused until the client fulfils the outstanding requests.
    InputRequired {
        /// Shared task fields.
        #[serde(flatten)]
        metadata: TaskMetadata,
        /// All currently outstanding server-to-client requests.
        #[serde(rename = "inputRequests")]
        input_requests: InputRequests,
    },
    /// Work completed successfully.
    ///
    /// Tool results with `isError: true` still use this variant.
    Completed {
        /// Shared task fields.
        #[serde(flatten)]
        metadata: TaskMetadata,
        /// The exact object the original synchronous request would return.
        result: Map<String, Value>,
    },
    /// Execution failed with a JSON-RPC error.
    Failed {
        /// Shared task fields.
        #[serde(flatten)]
        metadata: TaskMetadata,
        /// Structured JSON-RPC execution error.
        error: JsonRpcError,
    },
    /// Work was cancelled before completion.
    Cancelled {
        /// Shared task fields.
        #[serde(flatten)]
        metadata: TaskMetadata,
    },
}

impl DetailedTask {
    /// Construct a working task.
    pub fn working(metadata: TaskMetadata) -> Self {
        Self::Working { metadata }
    }

    /// Construct an input-required task.
    pub fn input_required(metadata: TaskMetadata, input_requests: InputRequests) -> Self {
        Self::InputRequired {
            metadata,
            input_requests,
        }
    }

    /// Construct a completed task from an object-valued synchronous result.
    pub fn completed(metadata: TaskMetadata, result: Map<String, Value>) -> Self {
        Self::Completed { metadata, result }
    }

    /// Construct a failed task from a structured JSON-RPC error.
    pub fn failed(metadata: TaskMetadata, error: JsonRpcError) -> Self {
        Self::Failed { metadata, error }
    }

    /// Construct a cancelled task.
    pub fn cancelled(metadata: TaskMetadata) -> Self {
        Self::Cancelled { metadata }
    }

    /// Current task status.
    pub fn status(&self) -> TaskStatus {
        match self {
            Self::Working { .. } => TaskStatus::Working,
            Self::InputRequired { .. } => TaskStatus::InputRequired,
            Self::Completed { .. } => TaskStatus::Completed,
            Self::Failed { .. } => TaskStatus::Failed,
            Self::Cancelled { .. } => TaskStatus::Cancelled,
        }
    }

    /// Server-generated identifier of the task this describes.
    pub fn task_id(&self) -> &str {
        &self.metadata().task_id
    }

    /// Shared task metadata.
    pub fn metadata(&self) -> &TaskMetadata {
        match self {
            Self::Working { metadata }
            | Self::InputRequired { metadata, .. }
            | Self::Completed { metadata, .. }
            | Self::Failed { metadata, .. }
            | Self::Cancelled { metadata } => metadata,
        }
    }

    /// Outstanding requests when the task is input-required.
    pub fn input_requests(&self) -> Option<&InputRequests> {
        match self {
            Self::InputRequired { input_requests, .. } => Some(input_requests),
            _ => None,
        }
    }

    /// Completed result object.
    pub fn result(&self) -> Option<&Map<String, Value>> {
        match self {
            Self::Completed { result, .. } => Some(result),
            _ => None,
        }
    }

    /// Failed JSON-RPC error.
    pub fn error(&self) -> Option<&JsonRpcError> {
        match self {
            Self::Failed { error, .. } => Some(error),
            _ => None,
        }
    }
}

/// Flat task handle returned instead of a synchronous result.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateTaskResult {
    #[serde(
        rename = "resultType",
        deserialize_with = "deserialize_task_result_type"
    )]
    result_type: ResultType,
    /// Seed task state. The fields are flattened on the wire.
    #[serde(flatten)]
    pub task: Task,
    /// Optional result metadata.
    #[serde(rename = "_meta", default, skip_serializing_if = "Option::is_none")]
    pub meta: Option<Map<String, Value>>,
}

impl CreateTaskResult {
    /// Construct a flat final task result.
    pub fn new(task: Task) -> Self {
        Self {
            result_type: ResultType::Other(crate::protocol::RESULT_TYPE_TASK.to_string()),
            task,
            meta: None,
        }
    }

    /// The fixed `resultType` discriminator.
    pub fn result_type(&self) -> &ResultType {
        &self.result_type
    }
}

/// Complete result of `tasks/get`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetTaskResult {
    #[serde(
        rename = "resultType",
        deserialize_with = "deserialize_complete_result_type"
    )]
    result_type: ResultType,
    /// Status-specific task state, flattened on the wire.
    #[serde(flatten)]
    pub task: DetailedTask,
    /// Optional result metadata.
    #[serde(rename = "_meta", default, skip_serializing_if = "Option::is_none")]
    pub meta: Option<Map<String, Value>>,
}

impl GetTaskResult {
    /// Construct a complete `tasks/get` result.
    pub fn new(task: DetailedTask) -> Self {
        Self {
            result_type: ResultType::Complete,
            task,
            meta: None,
        }
    }

    /// The fixed `resultType` discriminator.
    pub fn result_type(&self) -> &ResultType {
        &self.result_type
    }
}

/// Complete empty acknowledgement for `tasks/update` or `tasks/cancel`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TaskAcknowledgement {
    #[serde(
        rename = "resultType",
        deserialize_with = "deserialize_complete_result_type"
    )]
    result_type: ResultType,
    /// Optional result metadata.
    #[serde(rename = "_meta", default, skip_serializing_if = "Option::is_none")]
    pub meta: Option<Map<String, Value>>,
}

impl TaskAcknowledgement {
    /// Construct a complete empty acknowledgement.
    pub fn new() -> Self {
        Self {
            result_type: ResultType::Complete,
            meta: None,
        }
    }

    /// The fixed `resultType` discriminator.
    pub fn result_type(&self) -> &ResultType {
        &self.result_type
    }
}

impl Default for TaskAcknowledgement {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of `tasks/update`.
pub type UpdateTaskResult = TaskAcknowledgement;

/// Result of `tasks/cancel`.
pub type CancelTaskResult = TaskAcknowledgement;

/// Parameters for `tasks/get`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetTaskParams {
    /// Task identifier to query.
    pub task_id: String,
    /// Required final-protocol request metadata.
    #[serde(rename = "_meta", default, skip_serializing_if = "Option::is_none")]
    pub meta: Option<RequestMeta>,
}

/// Parameters for `tasks/update`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateTaskParams {
    /// Task identifier to update.
    pub task_id: String,
    /// Responses to currently outstanding task input requests.
    pub input_responses: InputResponses,
    /// Required final-protocol request metadata.
    #[serde(rename = "_meta", default, skip_serializing_if = "Option::is_none")]
    pub meta: Option<RequestMeta>,
}

/// Parameters for `tasks/cancel`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CancelTaskParams {
    /// Task identifier to cancel.
    pub task_id: String,
    /// Required final-protocol request metadata.
    #[serde(rename = "_meta", default, skip_serializing_if = "Option::is_none")]
    pub meta: Option<RequestMeta>,
}

/// Parameters for `notifications/tasks`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskStatusNotificationParams {
    /// Complete status-specific task state.
    #[serde(flatten)]
    pub task: DetailedTask,
    /// Optional notification metadata.
    #[serde(rename = "_meta", default, skip_serializing_if = "Option::is_none")]
    pub meta: Option<Map<String, Value>>,
}

fn deserialize_task_result_type<'de, D>(deserializer: D) -> Result<ResultType, D::Error>
where
    D: Deserializer<'de>,
{
    let result_type = ResultType::deserialize(deserializer)?;
    if result_type.as_str() == crate::protocol::RESULT_TYPE_TASK {
        Ok(result_type)
    } else {
        Err(serde::de::Error::custom(
            "resultType must be \"task\" for CreateTaskResult",
        ))
    }
}

fn deserialize_complete_result_type<'de, D>(deserializer: D) -> Result<ResultType, D::Error>
where
    D: Deserializer<'de>,
{
    let result_type = ResultType::deserialize(deserializer)?;
    if result_type == ResultType::Complete {
        Ok(result_type)
    } else {
        Err(serde::de::Error::custom("resultType must be \"complete\""))
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;
    use crate::protocol::{
        ElicitAction, ElicitResult, InputRequest, InputResponse, ListRootsParams,
    };

    fn metadata() -> TaskMetadata {
        TaskMetadata::new(
            "task-2f68f7c9",
            "2026-07-30T00:00:00Z",
            "2026-07-30T00:00:01Z",
            Some(60_000),
        )
        .with_poll_interval_ms(2_000)
    }

    #[test]
    fn task_metadata_uses_exact_final_field_names_and_required_null_ttl() {
        let value = serde_json::to_value(Task::new(metadata(), TaskStatus::Working)).unwrap();
        assert_eq!(value["ttlMs"], 60_000);
        assert_eq!(value["pollIntervalMs"], 2_000);
        assert!(value.get("ttl").is_none());
        assert!(value.get("pollInterval").is_none());

        let unlimited = TaskMetadata::new(
            "task-unlimited",
            "2026-07-30T00:00:00Z",
            "2026-07-30T00:00:00Z",
            None,
        );
        let value = serde_json::to_value(Task::new(unlimited, TaskStatus::Working)).unwrap();
        assert_eq!(value["ttlMs"], Value::Null);
    }

    #[test]
    fn create_task_result_is_flat_and_rejects_wrong_discriminator() {
        let result = CreateTaskResult::new(Task::new(metadata(), TaskStatus::Working));
        let value = serde_json::to_value(result).unwrap();
        assert_eq!(value["resultType"], "task");
        assert_eq!(value["taskId"], "task-2f68f7c9");
        assert!(value.get("task").is_none());

        let mut invalid = value;
        invalid["resultType"] = json!("complete");
        assert!(serde_json::from_value::<CreateTaskResult>(invalid).is_err());
    }

    #[test]
    fn detailed_task_variants_have_status_specific_payloads() {
        let working =
            serde_json::to_value(GetTaskResult::new(DetailedTask::working(metadata()))).unwrap();
        assert_eq!(working["resultType"], "complete");
        assert_eq!(working["status"], "working");
        assert!(working.get("inputRequests").is_none());
        assert!(working.get("result").is_none());
        assert!(working.get("error").is_none());

        let mut requests = InputRequests::new();
        requests.insert(
            "roots".to_string(),
            InputRequest::ListRoots(ListRootsParams { meta: None }),
        );
        let input_required = serde_json::to_value(GetTaskResult::new(
            DetailedTask::input_required(metadata(), requests),
        ))
        .unwrap();
        assert_eq!(input_required["status"], "input_required");
        assert_eq!(
            input_required["inputRequests"]["roots"]["method"],
            "roots/list"
        );

        let result = json!({
            "content": [{"type": "text", "text": "domain error"}],
            "isError": true
        })
        .as_object()
        .unwrap()
        .clone();
        let completed = serde_json::to_value(GetTaskResult::new(DetailedTask::completed(
            metadata(),
            result,
        )))
        .unwrap();
        assert_eq!(completed["status"], "completed");
        assert_eq!(completed["result"]["isError"], true);

        let failed = serde_json::to_value(GetTaskResult::new(DetailedTask::failed(
            metadata(),
            JsonRpcError::internal_error("worker crashed"),
        )))
        .unwrap();
        assert_eq!(failed["status"], "failed");
        assert_eq!(failed["error"]["code"], -32603);

        let cancelled =
            serde_json::to_value(GetTaskResult::new(DetailedTask::cancelled(metadata()))).unwrap();
        assert_eq!(cancelled["status"], "cancelled");
    }

    #[test]
    fn detailed_task_rejects_missing_required_status_payloads() {
        let base = json!({
            "taskId": "task-1",
            "createdAt": "2026-07-30T00:00:00Z",
            "lastUpdatedAt": "2026-07-30T00:00:00Z",
            "ttlMs": null
        });
        for status in ["input_required", "completed", "failed"] {
            let mut value = base.clone();
            value["status"] = json!(status);
            assert!(
                serde_json::from_value::<DetailedTask>(value).is_err(),
                "{status}"
            );
        }
    }

    #[test]
    fn update_params_are_typed_and_acknowledgements_are_complete() {
        let mut responses = InputResponses::new();
        responses.insert(
            "approval".to_string(),
            InputResponse::Elicit(ElicitResult {
                action: ElicitAction::Accept,
                content: None,
                meta: None,
            }),
        );
        let params = UpdateTaskParams {
            task_id: "task-2f68f7c9".to_string(),
            input_responses: responses,
            meta: None,
        };
        let value = serde_json::to_value(params).unwrap();
        assert_eq!(value["taskId"], "task-2f68f7c9");
        assert_eq!(
            value["inputResponses"]["approval"]["action"],
            json!("accept")
        );

        let ack = serde_json::to_value(TaskAcknowledgement::new()).unwrap();
        assert_eq!(ack, json!({"resultType": "complete"}));
        assert!(
            serde_json::from_value::<TaskAcknowledgement>(json!({
                "resultType": "task"
            }))
            .is_err()
        );
    }

    #[test]
    fn notification_payload_matches_tasks_get_detailed_shape() {
        let notification = TaskStatusNotificationParams {
            task: DetailedTask::cancelled(metadata()),
            meta: None,
        };
        let value = serde_json::to_value(notification).unwrap();
        assert_eq!(value["status"], "cancelled");
        assert_eq!(value["taskId"], "task-2f68f7c9");
        assert_eq!(value["ttlMs"], 60_000);
    }
}