tower-mcp 0.22.2

Tower-native Model Context Protocol (MCP) implementation
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! Unit tests for [`Resource`](super::Resource), [`ResourceTemplate`](super::ResourceTemplate), and their builders.

use super::*;
use std::time::Duration;
use tower::timeout::TimeoutLayer;

#[tokio::test]
async fn test_builder_resource() {
    let resource = ResourceBuilder::new("file:///test.txt")
        .name("Test File")
        .description("A test file")
        .text("Hello, World!");

    assert_eq!(resource.uri, "file:///test.txt");
    assert_eq!(resource.name, "Test File");
    assert_eq!(resource.description.as_deref(), Some("A test file"));

    let result = resource.read().await;
    assert_eq!(result.contents.len(), 1);
    assert_eq!(result.contents[0].text.as_deref(), Some("Hello, World!"));
}

#[cfg(feature = "stateless")]
#[tokio::test]
async fn test_mrtr_builder_preserves_input_required_outcome() {
    let resource = ResourceBuilder::new("test://continue")
        .mrtr_handler(|_ctx| async move {
            Ok(RequestOutcome::input_required(
                crate::protocol::InputRequiredResult::new().with_request_state("signed-state"),
            ))
        })
        .build();

    let outcome = resource
        .read_outcome_with_context(RequestContext::new(crate::protocol::RequestId::Number(1)))
        .await
        .unwrap();
    assert_eq!(
        outcome
            .as_input_required()
            .and_then(|result| result.request_state.as_deref()),
        Some("signed-state")
    );
}

// =========================================================================
// Clone vs. handler kind (#1340)
// =========================================================================
//
// `Resource::clone` (see the hand-written `impl Clone for Resource` above)
// is exercised incidentally by `read()`/`read_with_context()`, which clone
// `self` before dispatching -- so a plain-handler resource's clone was
// already covered by every test that calls `.read()`. `read_outcome_with_context`,
// which the router actually calls, does not clone `self`, only the handler
// behind it, so nothing here had exercised `Resource::clone` for an MRTR
// handler at all. `Tool::clone` dropping a field (#1298) is the reason
// this gets a dedicated, named test rather than staying implicit.

#[tokio::test]
async fn resource_clone_carries_the_plain_handler() {
    let resource = ResourceBuilder::new("memory://cloneable")
        .name("Cloneable")
        .handler(|| async {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri: "memory://cloneable".to_string(),
                    mime_type: Some("text/plain".to_string()),
                    text: Some("original".to_string()),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        })
        .build();

    let cloned = resource.clone();
    let result = cloned.read().await;
    assert_eq!(result.contents[0].text.as_deref(), Some("original"));
}

#[cfg(feature = "stateless")]
#[tokio::test]
async fn resource_clone_carries_the_mrtr_handler() {
    let resource = ResourceBuilder::new("test://cloneable-continue")
        .mrtr_handler(|_ctx| async move {
            Ok(RequestOutcome::input_required(
                crate::protocol::InputRequiredResult::new().with_request_state("cloned-state"),
            ))
        })
        .build();

    let cloned = resource.clone();
    let outcome = cloned
        .read_outcome_with_context(RequestContext::new(crate::protocol::RequestId::Number(1)))
        .await
        .unwrap();
    assert_eq!(
        outcome
            .as_input_required()
            .and_then(|result| result.request_state.as_deref()),
        Some("cloned-state"),
        "a dropped mrtr_handler after clone would fall through to the \
         absent `service` and panic on `.expect(...)` instead of \
         returning this"
    );
}

#[cfg(feature = "stateless")]
#[tokio::test]
async fn mrtr_resource_composes_middleware() {
    let resource = ResourceBuilder::new("test://layered-continue")
        .mrtr_handler(|_ctx| async move {
            Ok(RequestOutcome::input_required(
                crate::protocol::InputRequiredResult::new().with_request_state("layered-state"),
            ))
        })
        .layer(TimeoutLayer::new(Duration::from_secs(1)))
        .build();

    let outcome = resource
        .read_outcome_with_context(RequestContext::new(crate::protocol::RequestId::Number(2)))
        .await
        .unwrap();
    assert_eq!(
        outcome
            .as_input_required()
            .and_then(|result| result.request_state.as_deref()),
        Some("layered-state")
    );
}

#[cfg(feature = "stateless")]
#[tokio::test]
async fn test_mrtr_template_preserves_context_and_input_required_outcome() {
    let template = ResourceTemplateBuilder::new("test://items/{id}").mrtr_handler(
        |ctx, uri, variables| async move {
            assert_eq!(ctx.request_state(), Some("prior-state"));
            assert_eq!(uri, "test://items/42");
            assert_eq!(variables.get("id").map(String::as_str), Some("42"));
            Ok(RequestOutcome::input_required(
                crate::protocol::InputRequiredResult::new().with_request_state("next-state"),
            ))
        },
    );
    let variables = template.match_uri("test://items/42").unwrap();
    let mut ctx = RequestContext::new(crate::protocol::RequestId::Number(1));
    ctx.extensions_mut().insert(crate::mrtr::MrtrRequest::new(
        None,
        Some("prior-state".into()),
    ));

    let outcome = template
        .read_outcome_with_context(ctx, "test://items/42", variables)
        .await
        .unwrap();
    assert_eq!(
        outcome
            .as_input_required()
            .and_then(|result| result.request_state.as_deref()),
        Some("next-state")
    );
}

#[tokio::test]
async fn test_json_resource() {
    let resource = ResourceBuilder::new("file:///config.json")
        .name("Config")
        .json(serde_json::json!({"key": "value"}));

    assert_eq!(resource.mime_type.as_deref(), Some("application/json"));

    let result = resource.read().await;
    assert!(result.contents[0].text.as_ref().unwrap().contains("key"));
}

#[tokio::test]
async fn test_handler_resource() {
    let resource = ResourceBuilder::new("memory://counter")
        .name("Counter")
        .handler(|| async {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri: "memory://counter".to_string(),
                    mime_type: Some("text/plain".to_string()),
                    text: Some("42".to_string()),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        })
        .build();

    let result = resource.read().await;
    assert_eq!(result.contents[0].text.as_deref(), Some("42"));
}

#[tokio::test]
async fn test_handler_resource_with_layer() {
    let resource = ResourceBuilder::new("file:///with-timeout.txt")
        .name("Resource with Timeout")
        .handler(|| async {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri: "file:///with-timeout.txt".to_string(),
                    mime_type: Some("text/plain".to_string()),
                    text: Some("content".to_string()),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        })
        .layer(TimeoutLayer::new(Duration::from_secs(30)))
        .build();

    let result = resource.read().await;
    assert_eq!(result.contents[0].text.as_deref(), Some("content"));
}

#[tokio::test]
async fn test_handler_resource_with_timeout_error() {
    let resource = ResourceBuilder::new("file:///slow.txt")
        .name("Slow Resource")
        .handler(|| async {
            // Sleep much longer than timeout to ensure timeout fires reliably in CI
            tokio::time::sleep(Duration::from_secs(1)).await;
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri: "file:///slow.txt".to_string(),
                    mime_type: Some("text/plain".to_string()),
                    text: Some("content".to_string()),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        })
        .layer(TimeoutLayer::new(Duration::from_millis(50)))
        .build();

    let result = resource.read().await;
    // read() still renders an error as content, since its signature has
    // no room for a Result. The timeout is a genuine middleware
    // failure (#1280), so it is sanitized to -32603 rather than
    // leaking the raw middleware error.
    assert!(result.contents[0].text.as_ref().unwrap().contains("-32603"));
}

#[tokio::test]
async fn test_context_aware_handler() {
    let resource = ResourceBuilder::new("file:///ctx.txt")
        .name("Context Resource")
        .handler_with_context(|_ctx: RequestContext| async {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri: "file:///ctx.txt".to_string(),
                    mime_type: Some("text/plain".to_string()),
                    text: Some("context aware".to_string()),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        })
        .build();

    let result = resource.read().await;
    assert_eq!(result.contents[0].text.as_deref(), Some("context aware"));
}

#[tokio::test]
async fn test_context_aware_handler_with_layer() {
    let resource = ResourceBuilder::new("file:///ctx-layer.txt")
        .name("Context Resource with Layer")
        .handler_with_context(|_ctx: RequestContext| async {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri: "file:///ctx-layer.txt".to_string(),
                    mime_type: Some("text/plain".to_string()),
                    text: Some("context with layer".to_string()),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        })
        .layer(TimeoutLayer::new(Duration::from_secs(30)))
        .build();

    let result = resource.read().await;
    assert_eq!(
        result.contents[0].text.as_deref(),
        Some("context with layer")
    );
}

#[tokio::test]
async fn test_trait_resource() {
    struct TestResource;

    impl McpResource for TestResource {
        const URI: &'static str = "test://resource";
        const NAME: &'static str = "Test";
        const DESCRIPTION: Option<&'static str> = Some("A test resource");
        const MIME_TYPE: Option<&'static str> = Some("text/plain");

        async fn read(&self) -> Result<ReadResourceResult> {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri: Self::URI.to_string(),
                    mime_type: Self::MIME_TYPE.map(|s| s.to_string()),
                    text: Some("test content".to_string()),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        }
    }

    let resource = TestResource.into_resource();
    assert_eq!(resource.uri, "test://resource");
    assert_eq!(resource.name, "Test");

    let result = resource.read().await;
    assert_eq!(result.contents[0].text.as_deref(), Some("test content"));
}

#[test]
fn test_resource_definition() {
    let resource = ResourceBuilder::new("file:///test.txt")
        .name("Test")
        .description("Description")
        .mime_type("text/plain")
        .text("content");

    let def = resource.definition();
    assert_eq!(def.uri, "file:///test.txt");
    assert_eq!(def.name, "Test");
    assert_eq!(def.description.as_deref(), Some("Description"));
    assert_eq!(def.mime_type.as_deref(), Some("text/plain"));
}

#[test]
fn test_resource_request_new() {
    let ctx = RequestContext::new(crate::protocol::RequestId::Number(1));
    let req = ResourceRequest::new(ctx, "file:///test.txt".to_string());
    assert_eq!(req.uri, "file:///test.txt");
}

#[test]
fn test_resource_catch_error_clone() {
    let handler = FnHandler {
        handler: || async {
            Ok::<_, Error>(ReadResourceResult {
                contents: vec![],
                meta: None,
                ..Default::default()
            })
        },
    };
    let service = ResourceHandlerService::new(handler);
    let catch_error = ResourceCatchError::new(service);
    let _clone = catch_error.clone();
}

#[test]
fn test_resource_catch_error_debug() {
    let handler = FnHandler {
        handler: || async {
            Ok::<_, Error>(ReadResourceResult {
                contents: vec![],
                meta: None,
                ..Default::default()
            })
        },
    };
    let service = ResourceHandlerService::new(handler);
    let catch_error = ResourceCatchError::new(service);
    let debug = format!("{:?}", catch_error);
    assert!(debug.contains("ResourceCatchError"));
}

// =========================================================================
// Resource Template Tests
// =========================================================================

#[test]
fn test_compile_uri_template_simple() {
    let CompiledTemplate {
        pattern: regex,
        variables: vars,
        ..
    } = compile_uri_template("file:///{path}").unwrap();
    assert_eq!(vars, vec!["path"]);
    assert!(regex.is_match("file:///README.md"));
    assert!(!regex.is_match("file:///foo/bar")); // no slashes in simple expansion
}

#[test]
fn test_compile_uri_template_multiple_vars() {
    let CompiledTemplate {
        pattern: regex,
        variables: vars,
        ..
    } = compile_uri_template("api://v1/{resource}/{id}").unwrap();
    assert_eq!(vars, vec!["resource", "id"]);
    assert!(regex.is_match("api://v1/users/123"));
    assert!(regex.is_match("api://v1/posts/abc"));
    assert!(!regex.is_match("api://v1/users")); // missing id
}

#[test]
fn test_compile_uri_template_reserved_expansion() {
    let CompiledTemplate {
        pattern: regex,
        variables: vars,
        ..
    } = compile_uri_template("file:///{+path}").unwrap();
    assert_eq!(vars, vec!["path"]);
    assert!(regex.is_match("file:///README.md"));
    assert!(regex.is_match("file:///foo/bar/baz.txt")); // slashes allowed
}

#[test]
fn test_compile_uri_template_special_chars() {
    let CompiledTemplate {
        pattern: regex,
        variables: vars,
        ..
    } = compile_uri_template("http://example.com/api?query={q}").unwrap();
    assert_eq!(vars, vec!["q"]);
    assert!(regex.is_match("http://example.com/api?query=hello"));
}

#[test]
fn test_resource_template_match_uri() {
    let template = ResourceTemplateBuilder::new("db://users/{id}")
        .name("User Records")
        .handler(|uri: String, vars: HashMap<String, String>| async move {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri,
                    mime_type: None,
                    text: Some(format!("User {}", vars.get("id").unwrap())),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        });

    // Test matching
    let vars = template.match_uri("db://users/123").unwrap();
    assert_eq!(vars.get("id"), Some(&"123".to_string()));

    // Test non-matching
    assert!(template.match_uri("db://posts/123").is_none());
    assert!(template.match_uri("db://users").is_none());
}

#[test]
fn test_resource_template_match_multiple_vars() {
    let template = ResourceTemplateBuilder::new("api://{version}/{resource}/{id}")
        .name("API Resources")
        .handler(|uri: String, _vars: HashMap<String, String>| async move {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri,
                    mime_type: None,
                    text: None,
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        });

    let vars = template.match_uri("api://v2/users/abc-123").unwrap();
    assert_eq!(vars.get("version"), Some(&"v2".to_string()));
    assert_eq!(vars.get("resource"), Some(&"users".to_string()));
    assert_eq!(vars.get("id"), Some(&"abc-123".to_string()));
}

#[tokio::test]
async fn test_resource_template_read() {
    let template = ResourceTemplateBuilder::new("file:///{path}")
        .name("Files")
        .mime_type("text/plain")
        .handler(|uri: String, vars: HashMap<String, String>| async move {
            let path = vars.get("path").unwrap().clone();
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri,
                    mime_type: Some("text/plain".to_string()),
                    text: Some(format!("Contents of {}", path)),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        });

    let vars = template.match_uri("file:///README.md").unwrap();
    let result = template.read("file:///README.md", vars).await.unwrap();

    assert_eq!(result.contents.len(), 1);
    assert_eq!(result.contents[0].uri, "file:///README.md");
    assert_eq!(
        result.contents[0].text.as_deref(),
        Some("Contents of README.md")
    );
}

#[test]
fn test_resource_template_definition() {
    let template = ResourceTemplateBuilder::new("db://records/{id}")
        .name("Database Records")
        .description("Access database records by ID")
        .mime_type("application/json")
        .handler(|uri: String, _vars: HashMap<String, String>| async move {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri,
                    mime_type: None,
                    text: None,
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        });

    let def = template.definition();
    assert_eq!(def.uri_template, "db://records/{id}");
    assert_eq!(def.name, "Database Records");
    assert_eq!(
        def.description.as_deref(),
        Some("Access database records by ID")
    );
    assert_eq!(def.mime_type.as_deref(), Some("application/json"));
}

#[test]
fn test_resource_template_reserved_path() {
    let template = ResourceTemplateBuilder::new("file:///{+path}")
        .name("Files with subpaths")
        .handler(|uri: String, _vars: HashMap<String, String>| async move {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri,
                    mime_type: None,
                    text: None,
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        });

    // Reserved expansion should match slashes
    let vars = template.match_uri("file:///src/lib/utils.rs").unwrap();
    assert_eq!(vars.get("path"), Some(&"src/lib/utils.rs".to_string()));
}

#[test]
fn test_resource_annotations() {
    use crate::protocol::{ContentAnnotations, ContentRole};

    let annotations = ContentAnnotations {
        audience: Some(vec![ContentRole::User]),
        priority: Some(0.8),
        last_modified: None,
    };

    let resource = ResourceBuilder::new("file:///important.txt")
        .name("Important File")
        .annotations(annotations.clone())
        .text("content");

    let def = resource.definition();
    assert!(def.annotations.is_some());
    let ann = def.annotations.unwrap();
    assert_eq!(ann.priority, Some(0.8));
    assert_eq!(ann.audience.unwrap(), vec![ContentRole::User]);
}

#[test]
fn test_resource_template_annotations() {
    use crate::protocol::{ContentAnnotations, ContentRole};

    let annotations = ContentAnnotations {
        audience: Some(vec![ContentRole::Assistant]),
        priority: Some(0.5),
        last_modified: None,
    };

    let template = ResourceTemplateBuilder::new("db://users/{id}")
        .name("Users")
        .annotations(annotations)
        .handler(|uri: String, _vars: HashMap<String, String>| async move {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri,
                    mime_type: None,
                    text: Some("data".to_string()),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        });

    let def = template.definition();
    assert!(def.annotations.is_some());
    let ann = def.annotations.unwrap();
    assert_eq!(ann.priority, Some(0.5));
    assert_eq!(ann.audience.unwrap(), vec![ContentRole::Assistant]);
}

#[test]
fn test_resource_no_annotations_by_default() {
    let resource = ResourceBuilder::new("file:///test.txt")
        .name("Test")
        .text("content");

    let def = resource.definition();
    assert!(def.annotations.is_none());
}

#[test]
fn test_try_handler_success() {
    let result = ResourceTemplateBuilder::new("db://users/{id}")
        .name("Users")
        .try_handler(|uri: String, _vars: HashMap<String, String>| async move {
            Ok(ReadResourceResult {
                contents: vec![ResourceContent {
                    uri,
                    mime_type: None,
                    text: Some("ok".to_string()),
                    blob: None,
                    meta: None,
                }],
                meta: None,
                ..Default::default()
            })
        });

    assert!(result.is_ok());
    let template = result.unwrap();
    assert_eq!(template.uri_template, "db://users/{id}");
}

#[test]
fn test_compile_uri_template_returns_result() {
    // Valid templates should succeed
    assert!(compile_uri_template("file:///{path}").is_ok());
    assert!(compile_uri_template("api://v1/{resource}/{id}").is_ok());
    assert!(compile_uri_template("file:///{+path}").is_ok());
    assert!(compile_uri_template("no-vars").is_ok());
    assert!(compile_uri_template("").is_ok());
}