loopctl 0.3.0

A trait-based framework for building agent loops with pluggable LLM clients, tools, and memory
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
//! Integration tests for the MCP client adapter.
//!
//! Every test spins up a tiny in-process rmcp server (built with
//! `#[tool_router]` / `#[tool]`) and connects an `McpToolProvider` to it over a
//! `tokio::io::duplex`. No subprocess, no network.

#![cfg(feature = "mcp")]
// `ToolRouter<T>` fields are read by rmcp's `#[tool_handler]`-generated dispatch
// methods; the dead-code analysis can't see macro-generated reads.
#![allow(dead_code)]
// Integration tests are a separate crate and do not inherit `lib.rs`'s
// `cfg_attr(test, allow(...))`. Apply the same test-code relaxations the lib
// uses: assertions legitimately `unwrap`/`expect`/`panic`/index for clarity.
#![allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::indexing_slicing,
    clippy::missing_panics_doc,
    clippy::missing_errors_doc
)]

use std::time::Duration;

use loopctl::mcp::{McpClient, McpToolProvider};
use loopctl::message::ToolContent as MessageToolContent;
use loopctl::message::ToolContentPart;
use loopctl::tool::{Tool, ToolContext, ToolRegistry};
use rmcp::handler::server::router::tool::ToolRouter;
use rmcp::model::CallToolResult;
use rmcp::model::ContentBlock;
use rmcp::model::Tool as RmcpTool;
use rmcp::model::ToolAnnotations;
use rmcp::{ErrorData as McpErrorData, ServerHandler, ServiceExt, tool, tool_handler, tool_router};
use serde_json::Value;
use serde_json::json;

/// Buffer size matching the adapter's own duplex.
const DUPLEX_BUFFER: usize = 4096;

/// Connect a pure client to `server` over an in-memory duplex, returning the
/// client and the server's background join handle (so tests can assert on
/// shutdown).
async fn connect_in_process<S>(server: S) -> (McpClient, tokio::task::JoinHandle<()>)
where
    S: ServerHandler + Clone + Send + 'static,
{
    let (server_end, client_end) = tokio::io::duplex(DUPLEX_BUFFER);
    let server_handle = tokio::spawn(async move {
        let running = server.serve(server_end).await.expect("server serve");
        let _ = running.waiting().await.ok();
    });
    let client = ().serve(client_end).await.map(McpClient::from_service).expect("client serve");
    (client, server_handle)
}

/// Echo server: an `echo` tool and a `status` tool, both argument-free so the
/// test servers need no `schemars::JsonSchema` derive (rmcp's `#[tool]` macro
/// would otherwise require it for typed `Parameters<T>`).
#[derive(Clone)]
struct EchoServer {
    router: ToolRouter<Self>,
}

#[tool_router]
impl EchoServer {
    fn new() -> Self {
        Self {
            router: Self::tool_router(),
        }
    }

    #[tool(description = "Echo a fixed message back")]
    async fn echo(&self) -> String {
        "hi".to_string()
    }

    #[tool(description = "Report server status")]
    async fn status(&self) -> String {
        "ok".to_string()
    }
}
#[allow(clippy::unused_async_trait_impl)] // FIXME(rmcp): drop when tool_handler emits awaits
#[tool_handler]
impl ServerHandler for EchoServer {}

#[tokio::test]
async fn discovery_lists_server_tools() {
    let (client, _server) = connect_in_process(EchoServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let tools = provider.tools();
    assert_eq!(tools.len(), 2, "two server tools discovered");
    let names: Vec<&str> = tools.iter().map(Tool::name).collect();
    assert!(names.contains(&"echo"), "echo present: {names:?}");
    assert!(names.contains(&"status"), "status present: {names:?}");
}

#[tokio::test]
async fn echo_round_trip_returns_text() {
    let (client, _server) = connect_in_process(EchoServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let echo = provider
        .tools()
        .iter()
        .find(|t| t.name() == "echo")
        .expect("echo tool");
    let ctx = ToolContext::default();
    let out = echo.call(json!({}), &ctx).await.expect("call ok");
    assert!(!out.is_error, "not a soft error");
    assert_eq!(out.text_content(), "hi");
    assert!(matches!(out.payload, MessageToolContent::Text(_)));
}

#[tokio::test]
async fn name_prefix_namespaces_exposed_name_only() {
    let (client, _server) = connect_in_process(EchoServer::new()).await;
    let provider = McpToolProvider::connect(client, Some("git".into()))
        .await
        .expect("connect");
    let status = provider
        .tools()
        .iter()
        .find(|t| t.name() == "git__status")
        .expect("prefixed status tool");
    assert_eq!(status.schema().tool, "git__status");
    // The forwarded call uses the un-prefixed server name; the status tool
    // answers "ok", proving the call reached the right server-side tool.
    let ctx = ToolContext::default();
    let out = status.call(json!({}), &ctx).await.expect("call ok");
    assert_eq!(out.text_content(), "ok");
}

#[tokio::test]
async fn register_into_populates_registry() {
    let (client, _server) = connect_in_process(EchoServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let mut registry = ToolRegistry::new();
    provider.register_into(&mut registry);
    assert_eq!(registry.len(), 2);
    assert!(registry.contains("echo"));
    assert!(registry.contains("status"));
    assert_eq!(registry.all_schemas().len(), 2);
}

#[tokio::test]
async fn is_read_only_defaults_false_and_never_concurrency_safe() {
    let (client, _server) = connect_in_process(EchoServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    for tool in provider.tools() {
        assert!(
            !tool.is_read_only(),
            "unannotated tool {} should not be read-only",
            tool.name()
        );
        assert!(
            !tool.is_concurrency_safe(),
            "MCP tool {} should never claim concurrency-safe",
            tool.name()
        );
    }
}

/// A server whose tool returns a soft error (`isError = true`) with text.
#[derive(Clone)]
struct SoftErrorServer {
    router: ToolRouter<Self>,
}

#[tool_router]
impl SoftErrorServer {
    fn new() -> Self {
        Self {
            router: Self::tool_router(),
        }
    }

    #[tool(description = "Always reports a soft error")]
    async fn fail(&self) -> Result<CallToolResult, McpErrorData> {
        Ok(CallToolResult::error(vec![ContentBlock::text("boom")]))
    }
}
#[allow(clippy::unused_async_trait_impl)] // FIXME(rmcp): drop when tool_handler emits awaits
#[tool_handler]
impl ServerHandler for SoftErrorServer {}

#[tokio::test]
async fn soft_error_returns_ok_with_is_error_set() {
    let (client, _server) = connect_in_process(SoftErrorServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let fail = provider
        .tools()
        .iter()
        .find(|t| t.name() == "fail")
        .expect("fail tool");
    let ctx = ToolContext::default();
    let out = fail.call(json!({}), &ctx).await.expect("soft error is Ok");
    assert!(out.is_error, "is_error flag set");
    assert_eq!(out.text_content(), "boom");
}

/// A server whose tool returns an empty error (`isError = true`, no content).
#[derive(Clone)]
struct EmptyErrorServer {
    router: ToolRouter<Self>,
}

#[tool_router]
impl EmptyErrorServer {
    fn new() -> Self {
        Self {
            router: Self::tool_router(),
        }
    }

    #[tool(description = "Reports an error with no content")]
    async fn fail(&self) -> Result<CallToolResult, McpErrorData> {
        Ok(CallToolResult::error(vec![]))
    }
}
#[allow(clippy::unused_async_trait_impl)] // FIXME(rmcp): drop when tool_handler emits awaits
#[tool_handler]
impl ServerHandler for EmptyErrorServer {}

/// A server whose tool rejects the call at the JSON-RPC level, returning
/// `Err(ErrorData)` from `call_tool` — exercises the protocol-error bridge
/// (`ServiceError` → `ToolError::Execution`), distinct from the soft `isError`
/// path and the empty-error path.
#[derive(Clone)]
struct ProtocolErrorServer {
    router: ToolRouter<Self>,
}

#[tool_router]
impl ProtocolErrorServer {
    fn new() -> Self {
        Self {
            router: Self::tool_router(),
        }
    }

    #[tool(description = "Rejects the call with a JSON-RPC error")]
    async fn reject(&self) -> Result<CallToolResult, McpErrorData> {
        Err(McpErrorData::invalid_params("not allowed", None))
    }
}
#[allow(clippy::unused_async_trait_impl)] // FIXME(rmcp): drop when tool_handler emits awaits
#[tool_handler]
impl ServerHandler for ProtocolErrorServer {}

#[tokio::test]
async fn empty_error_becomes_hard_toolerror() {
    let (client, _server) = connect_in_process(EmptyErrorServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let fail = provider
        .tools()
        .iter()
        .find(|t| t.name() == "fail")
        .expect("fail tool");
    let ctx = ToolContext::default();
    let err = fail
        .call(json!({}), &ctx)
        .await
        .expect_err("empty error must be a hard Err");
    match err {
        loopctl::tool::ToolError::Execution(msg) => {
            assert!(
                msg.contains("fail"),
                "empty-error message should name the tool: {msg}"
            );
        }
        other => panic!("expected ToolError::Execution, got {other:?}"),
    }
}

#[tokio::test]
async fn protocol_error_rejection_becomes_hard_toolerror() {
    let (client, _server) = connect_in_process(ProtocolErrorServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let reject = provider
        .tools()
        .iter()
        .find(|t| t.name() == "reject")
        .expect("reject tool");
    let ctx = ToolContext::default();
    let err = reject
        .call(json!({}), &ctx)
        .await
        .expect_err("a JSON-RPC rejection must be a hard Err");
    match err {
        loopctl::tool::ToolError::Execution(msg) => {
            assert!(
                msg.contains("MCP tools/call failed"),
                "error should carry the tools/call failure context: {msg}"
            );
        }
        other => panic!("expected ToolError::Execution, got {other:?}"),
    }
}

/// A server with multipart-returning tools.
#[derive(Clone)]
struct MultipartServer {
    router: ToolRouter<Self>,
}

#[tool_router]
impl MultipartServer {
    fn new() -> Self {
        Self {
            router: Self::tool_router(),
        }
    }

    #[tool(description = "Returns two text parts")]
    async fn two(&self) -> Result<CallToolResult, McpErrorData> {
        Ok(CallToolResult::success(vec![
            ContentBlock::text("a"),
            ContentBlock::text("b"),
        ]))
    }

    #[tool(description = "Returns text plus image")]
    async fn mixed(&self) -> Result<CallToolResult, McpErrorData> {
        Ok(CallToolResult::success(vec![
            ContentBlock::text("caption"),
            ContentBlock::image("Zm9v", "image/png"),
        ]))
    }
}
#[allow(clippy::unused_async_trait_impl)] // FIXME(rmcp): drop when tool_handler emits awaits
#[tool_handler]
impl ServerHandler for MultipartServer {}

#[tokio::test]
async fn multipart_text_joins_with_newline() {
    let (client, _server) = connect_in_process(MultipartServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let two = provider
        .tools()
        .iter()
        .find(|t| t.name() == "two")
        .expect("two tool");
    let ctx = ToolContext::default();
    let out = two.call(json!({}), &ctx).await.expect("call ok");
    assert!(
        matches!(out.payload, MessageToolContent::Multipart(_)),
        "expected multipart"
    );
    assert_eq!(out.text_content(), "a\nb");
}

#[tokio::test]
async fn multipart_mixed_preserves_part_kinds_in_order() {
    let (client, _server) = connect_in_process(MultipartServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let mixed = provider
        .tools()
        .iter()
        .find(|t| t.name() == "mixed")
        .expect("mixed tool");
    let ctx = ToolContext::default();
    let out = mixed.call(json!({}), &ctx).await.expect("call ok");
    let MessageToolContent::Multipart(parts) = out.payload else {
        panic!("expected multipart, got {:?}", out.payload);
    };
    assert_eq!(parts.len(), 2, "two parts");
    assert!(
        matches!(parts.first(), Some(ToolContentPart::Text { text }) if text == "caption"),
        "first is the caption text"
    );
    assert!(
        matches!(parts.get(1), Some(ToolContentPart::Image { .. })),
        "second is the image"
    );
}

/// A server whose tool returns an audio block — the "stringify, don't drop"
/// fallback.
#[derive(Clone)]
struct AudioServer {
    router: ToolRouter<Self>,
}

#[tool_router]
impl AudioServer {
    fn new() -> Self {
        Self {
            router: Self::tool_router(),
        }
    }

    #[tool(description = "Returns an audio block")]
    async fn beep(&self) -> Result<CallToolResult, McpErrorData> {
        Ok(CallToolResult::success(vec![ContentBlock::audio(
            "AAAA",
            "audio/wav",
        )]))
    }
}
#[allow(clippy::unused_async_trait_impl)] // FIXME(rmcp): drop when tool_handler emits awaits
#[tool_handler]
impl ServerHandler for AudioServer {}

#[tokio::test]
async fn unsupported_content_type_does_not_vanish() {
    let (client, _server) = connect_in_process(AudioServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let beep = provider
        .tools()
        .iter()
        .find(|t| t.name() == "beep")
        .expect("beep tool");
    let ctx = ToolContext::default();
    let out = beep.call(json!({}), &ctx).await.expect("call ok");
    let text = out.text_content();
    assert!(
        text.contains("unsupported MCP content type"),
        "audio must surface as a note, got {text:?}"
    );
}

#[tokio::test]
async fn refresh_replaces_snapshot_in_place() {
    let (client, _server) = connect_in_process(EchoServer::new()).await;
    let mut provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let before = provider.tools().len();

    let mut registry = ToolRegistry::new();
    provider.register_into(&mut registry);
    let registry_len_before = registry.len();

    provider.refresh().await.expect("refresh");
    assert_eq!(
        provider.tools().len(),
        before,
        "same server, same count after refresh"
    );
    assert_eq!(
        registry.len(),
        registry_len_before,
        "refresh must not mutate an already-populated registry"
    );
}

/// A server with two tools whose server-side names collide after prefixing.
#[derive(Clone)]
struct CollisionServer {
    router: ToolRouter<Self>,
}

#[tool_router]
impl CollisionServer {
    fn new() -> Self {
        Self {
            router: Self::tool_router(),
        }
    }

    #[tool(name = "status", description = "first status")]
    async fn status_a(&self) -> String {
        "a".to_string()
    }

    #[tool(name = "status", description = "second status")]
    async fn status_b(&self) -> String {
        "b".to_string()
    }
}
#[allow(clippy::unused_async_trait_impl)] // FIXME(rmcp): drop when tool_handler emits awaits
#[tool_handler]
impl ServerHandler for CollisionServer {}

#[tokio::test]
async fn intra_batch_name_collision_keeps_one_no_panic() {
    let (client, _server) = connect_in_process(CollisionServer::new()).await;
    let provider = McpToolProvider::connect(client, Some("git".into()))
        .await
        .expect("connect");
    let names: Vec<&str> = provider.tools().iter().map(Tool::name).collect();
    assert_eq!(
        names.iter().filter(|&&n| n == "git__status").count(),
        1,
        "exactly one colliding name kept: {names:?}"
    );
}

#[tokio::test]
async fn drop_provider_cancels_background_server() {
    let (client, server_handle) = connect_in_process(EchoServer::new()).await;
    {
        let provider = McpToolProvider::connect(client, None)
            .await
            .expect("connect");
        assert!(!provider.tools().is_empty());
    }
    let resolved = tokio::time::timeout(Duration::from_secs(2), server_handle)
        .await
        .expect("server shuts down within 2s of provider drop");
    resolved.expect("server task did not panic");
}

#[tokio::test]
async fn re_register_with_overlapping_name_overwrites() {
    let (client, _server) = connect_in_process(EchoServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let mut registry = ToolRegistry::new();
    provider.register_into(&mut registry);
    let len_after_first = registry.len();
    provider.register_into(&mut registry);
    assert_eq!(registry.len(), len_after_first);
}

#[tokio::test]
async fn schema_passed_through_preserves_properties() {
    // `plain` is advertised by `AnnotatedServer` with a non-trivial input
    // schema (type/properties/required); the bridge must carry it verbatim.
    let (client, _server) = connect_in_process(AnnotatedServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let plain = provider
        .tools()
        .iter()
        .find(|t| t.name() == "plain")
        .expect("plain tool");
    let adapted = plain.schema().input_schema;
    assert!(
        adapted.get("properties").is_some(),
        "adapted schema preserves properties: {adapted}"
    );
    let required = adapted.get("required").and_then(Value::as_array);
    assert!(
        required.is_some_and(|arr| arr.iter().any(|v| v == "q")),
        "required field `q` carried through: {adapted}"
    );
}

#[tokio::test]
async fn output_schema_carried_through_accessor() {
    // `plain` declares an outputSchema; the adapter carries it verbatim and
    // exposes it via McpTool::output_schema(). Pins the round-1 accessor that
    // had no coverage.
    let (client, _server) = connect_in_process(AnnotatedServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let plain = provider
        .tools()
        .iter()
        .find(|t| t.name() == "plain")
        .expect("plain tool");
    let carried = plain.output_schema().expect("outputSchema carried through");
    assert_eq!(
        carried.get("type").and_then(Value::as_str),
        Some("string"),
        "outputSchema carried verbatim: {carried}"
    );
    // The annotated tool declares no outputSchema → None.
    let annotated = provider
        .tools()
        .iter()
        .find(|t| t.name() == "annotated")
        .expect("annotated tool");
    assert!(
        annotated.output_schema().is_none(),
        "absent outputSchema → None"
    );
}

/// An `RmcpTool` with explicit annotations, used to exercise the annotation
/// bridge. Built via rmcp's constructors because `Tool`/`ToolAnnotations` are
/// `#[non_exhaustive]`.
fn annotated_tool(read_only: bool) -> RmcpTool {
    let annotations = ToolAnnotations::default()
        .read_only(read_only)
        .destructive(false);
    RmcpTool::new("annotated", "annotated tool", serde_json::Map::new())
        .with_annotations(annotations)
}

/// An `RmcpTool` with **no** annotations, a non-trivial input schema, and a
/// declared `outputSchema` — exercises the spec-default path (absent
/// `readOnlyHint` → false; absent `destructiveHint` → true), verbatim schema
/// passthrough, and `outputSchema` carriage.
fn plain_tool() -> RmcpTool {
    let mut schema = serde_json::Map::new();
    schema.insert(
        "type".to_string(),
        serde_json::Value::String("object".to_string()),
    );
    schema.insert(
        "properties".to_string(),
        serde_json::json!({"q": {"type": "string"}}),
    );
    schema.insert(
        "required".to_string(),
        serde_json::Value::Array(vec![serde_json::Value::String("q".to_string())]),
    );
    let mut output_schema = serde_json::Map::new();
    output_schema.insert(
        "type".to_string(),
        serde_json::Value::String("string".to_string()),
    );
    RmcpTool::new("plain", "an unannotated tool", schema)
        .with_raw_output_schema(std::sync::Arc::new(output_schema))
}

/// A server that advertises an annotated tool and an unannotated tool by
/// overriding `list_tools` (the `#[tool]` macro does not annotate, so a manual
/// override is the way to exercise the annotation bridge). The `#[tool_handler]`
/// macro sees the manual `list_tools` and skips generating its own, so the
/// override wins; it still generates `call_tool`/`get_info`.
#[derive(Clone)]
struct AnnotatedServer {
    router: ToolRouter<Self>,
}

#[tool_router]
impl AnnotatedServer {
    fn new() -> Self {
        Self {
            router: Self::tool_router(),
        }
    }
}

impl ServerHandler for AnnotatedServer {
    fn list_tools(
        &self,
        _request: Option<rmcp::model::PaginatedRequestParams>,
        _ctx: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl Future<Output = Result<rmcp::model::ListToolsResult, McpErrorData>> {
        std::future::ready(Ok(rmcp::model::ListToolsResult {
            next_cursor: None,
            tools: vec![annotated_tool(true), plain_tool()],
            ..Default::default()
        }))
    }
}

#[tokio::test]
async fn read_only_hint_honored_from_live_server() {
    let (client, _server) = connect_in_process(AnnotatedServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let lookup = provider
        .tools()
        .iter()
        .find(|t| t.name() == "annotated")
        .expect("annotated tool discovered");
    assert!(lookup.is_read_only(), "annotated read-only honored");
}

#[tokio::test]
async fn absent_destructive_hint_defaults_to_destructive_per_spec() {
    // The MCP spec: an absent destructiveHint means "assume destructive".
    // `plain` carries no annotations, so is_destructive_hint() must be true.
    let (client, _server) = connect_in_process(AnnotatedServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let plain = provider
        .tools()
        .iter()
        .find(|t| t.name() == "plain")
        .expect("plain tool discovered");
    assert!(
        plain.is_destructive_hint(),
        "absent destructiveHint must default to destructive (true) per spec"
    );
    assert!(
        !plain.is_read_only(),
        "absent readOnlyHint must default to non-read-only (false)"
    );
    // And the explicitly-annotated tool keeps its explicit value.
    let annotated = provider
        .tools()
        .iter()
        .find(|t| t.name() == "annotated")
        .expect("annotated tool discovered");
    assert!(
        !annotated.is_destructive_hint(),
        "explicit destructiveHint=false must be honored"
    );
}

/// A server whose only tool sleeps before answering — long relative to the
/// test's shortened call timeout, short relative to the default.
#[derive(Clone)]
struct SlowServer {
    router: ToolRouter<Self>,
}

#[tool_router]
impl SlowServer {
    fn new() -> Self {
        Self {
            router: Self::tool_router(),
        }
    }

    #[tool(description = "Sleeps before answering")]
    async fn slow(&self) -> String {
        tokio::time::sleep(Duration::from_millis(150)).await;
        "finally".to_string()
    }
}
#[allow(clippy::unused_async_trait_impl)] // FIXME(rmcp): drop when tool_handler emits awaits
#[tool_handler]
impl ServerHandler for SlowServer {}

#[tokio::test]
async fn call_timeout_cuts_a_slow_tool_with_a_soft_error() {
    let (client, _server) = connect_in_process(SlowServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect")
        .with_call_timeout(Duration::from_millis(10));
    let slow = provider
        .tools()
        .iter()
        .find(|t| t.name() == "slow")
        .expect("slow tool");
    let ctx = ToolContext::default();

    let out = slow
        .call(json!({}), &ctx)
        .await
        .expect("timeout is a soft error");
    assert!(out.is_error, "the timeout must surface as is_error");
    let text = out.text_content();
    assert!(
        text.contains("slow") && text.contains("timed out"),
        "the soft error must name the tool and the timeout: {text:?}"
    );
}

#[tokio::test]
async fn default_timeout_lets_a_quick_tool_through() {
    let (client, _server) = connect_in_process(SlowServer::new()).await;
    let provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect");
    let slow = provider
        .tools()
        .iter()
        .find(|t| t.name() == "slow")
        .expect("slow tool");
    let ctx = ToolContext::default();

    let out = slow
        .call(json!({}), &ctx)
        .await
        .expect("call ok under the default timeout");
    assert!(!out.is_error, "a 150ms call must survive the 60s default");
    assert_eq!(out.text_content(), "finally");
}

#[tokio::test]
async fn refresh_preserves_the_overridden_call_timeout() {
    let (client, _server) = connect_in_process(SlowServer::new()).await;
    let mut provider = McpToolProvider::connect(client, None)
        .await
        .expect("connect")
        .with_call_timeout(Duration::from_millis(10));

    provider.refresh().await.expect("refresh");
    let slow = provider
        .tools()
        .iter()
        .find(|t| t.name() == "slow")
        .expect("slow tool after refresh");
    let ctx = ToolContext::default();

    let out = slow.call(json!({}), &ctx).await.expect("call resolves");
    assert!(
        out.is_error && out.text_content().contains("timed out"),
        "the refreshed tool must still be bounded by the override: {}",
        out.text_content()
    );
}