pmcp 2.22.2

High-quality Rust SDK for Model Context Protocol (MCP) with full TypeScript SDK compatibility
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
//! E3 — `garde` field validation on hand-written typed tools (Phase 128, plan 04).
//!
//! Run:
//!
//! ```text
//! RUSTFLAGS="" cargo test -p pmcp --features "full" --test typed_tool_garde -- --test-threads=1
//! RUSTFLAGS="" PROPTEST_CASES=64 cargo test -p pmcp --features "full" --test typed_tool_garde -- --ignored property_
//! ```
//!
//! Naming rule: every test fn is prefixed `typed_tool_garde_` so a positional
//! filter (`cargo test typed_tool_garde`) resolves this binary's contents, and the
//! single property arm is prefixed `property_` so `make test-property`'s
//! `--ignored property_` selector reaches it. This file lives in ROOT `tests/`
//! precisely because that selector only scans root targets.
//!
//! What every refusal row asserts: that the rendered message does NOT contain the
//! rejected value. A refusal that names the declared bound is the goal; a refusal
//! that quotes the caller's argument is the leak this plan closes (T-128-17,
//! T-128-17a, T-128-17b).
#![cfg(feature = "validation")]

use garde::Validate;
use pmcp::{RequestHandlerExtra, ToolHandler, TypedSyncTool, TypedTool};
use serde::Deserialize;
use serde_json::{json, Value};
use std::collections::HashMap;

/// The token the D1 path (`schema_validation::safe_pointer`) redacts a
/// caller-chosen pointer segment with. Duplicated here as a STRING LITERAL on
/// purpose: the const is crate-private, and an integration test asserting the
/// literal is what pins the two paths to the same wire text.
const REDACTED_SEGMENT: &str = "<redacted>";

fn extra(id: &str) -> RequestHandlerExtra {
    RequestHandlerExtra::new(id.to_string(), Default::default())
}

/// Unwrap a refusal's message, failing loudly on any other error variant.
///
/// A refusal is a TOOL-LEVEL rejection (`isError: true`), not a protocol error:
/// the model reads a protocol error as a server fault and has nothing to correct.
fn refusal(err: pmcp::Error) -> String {
    match err {
        pmcp::Error::ToolRejected { message, .. } => message,
        other => panic!("expected Error::ToolRejected, got {other:?}"),
    }
}

fn object_schema() -> Value {
    json!({"type": "object"})
}

/// `{"buckets": {<key>: {"label": "too-long-label"}}}`, built explicitly because
/// the map KEY is the caller-controlled part under test.
fn buckets_payload(key: &str) -> Value {
    let mut buckets = serde_json::Map::new();
    buckets.insert(key.to_string(), json!({"label": "too-long-label"}));
    json!({"buckets": Value::Object(buckets)})
}

// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------

/// A declared `length` rule: the canonical E3 shape.
#[derive(Debug, Deserialize, Validate)]
struct TitleArgs {
    #[garde(length(chars, max = 10))]
    title: String,
}

/// No `garde` derive at all — the "existing constructor, no validation" row.
#[derive(Debug, Deserialize)]
struct PlainArgs {
    title: String,
}

/// A `#[garde(dive)]` into a map: garde 0.23 extends the `Path` with the map's
/// KEYS (`garde-0.23.0/src/validate.rs:293`), which are caller-chosen.
#[derive(Debug, Deserialize, Validate)]
struct BucketsArgs {
    #[garde(dive)]
    buckets: HashMap<String, Bucket>,
}

#[derive(Debug, Deserialize, Validate)]
struct Bucket {
    #[garde(length(chars, max = 4))]
    label: String,
}

/// A wrong-type deserialization failure: `serde_json`'s `Display` quotes the value.
#[derive(Debug, Deserialize, Validate)]
struct CountArgs {
    #[garde(range(max = 100))]
    count: u32,
}

/// An unknown-enum-variant deserialization failure.
#[derive(Debug, Deserialize, Validate)]
struct ModeArgs {
    #[garde(skip)]
    #[allow(dead_code)]
    mode: Mode,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
#[allow(dead_code)]
enum Mode {
    Fast,
    Slow,
}

/// An invalid-string deserialization failure (`char` wants exactly one character).
#[derive(Debug, Deserialize, Validate)]
struct InitialArgs {
    #[garde(skip)]
    #[allow(dead_code)]
    initial: char,
}

/// The PHI-shaped payload every refusal row asserts the absence of.
const SECRET: &str = "Jane Doe DOB 1970-01-01";

// ---------------------------------------------------------------------------
// TypedTool — the async type
// ---------------------------------------------------------------------------

#[tokio::test]
async fn typed_tool_garde_violation_refuses_and_never_echoes_the_value() {
    let tool = TypedTool::new_validated_with_schema(
        "validated_title",
        object_schema(),
        |args: TitleArgs, _extra| Box::pin(async move { Ok(json!({"title": args.title})) }),
    );

    let err = tool
        .handle(json!({"title": SECRET}), extra("r-1"))
        .await
        .expect_err("an over-long title must be refused");
    let message = refusal(err);

    assert!(
        message.contains("title"),
        "the refusal must name the DECLARED field: {message}"
    );
    assert!(
        message.contains("10"),
        "the refusal must carry the declared bound: {message}"
    );
    assert!(
        !message.contains(SECRET),
        "the refusal leaked the rejected value: {message}"
    );
}

#[tokio::test]
async fn typed_tool_garde_conforming_value_runs_the_handler() {
    let tool = TypedTool::new_validated_with_schema(
        "validated_title",
        object_schema(),
        |args: TitleArgs, _extra| Box::pin(async move { Ok(json!({"title": args.title})) }),
    );

    let out = tool
        .handle(json!({"title": "0123456789"}), extra("r-2"))
        .await
        .expect("a 10-character title is exactly at the declared maximum");
    assert_eq!(out, json!({"title": "0123456789"}));
}

#[tokio::test]
async fn typed_tool_garde_existing_constructor_runs_no_validation() {
    // `TitleArgs` DOES implement `garde::Validate`, and the plain constructor must
    // still not validate: the stored validator is `None`.
    let tool = TypedTool::new_with_schema(
        "unvalidated_title",
        object_schema(),
        |args: TitleArgs, _extra| Box::pin(async move { Ok(json!({"title": args.title})) }),
    );

    let out = tool
        .handle(json!({"title": SECRET}), extra("r-3"))
        .await
        .expect("the plain constructor must not validate");
    assert_eq!(out, json!({"title": SECRET}));
}

#[tokio::test]
async fn typed_tool_garde_type_without_derive_is_unaffected() {
    let tool =
        TypedTool::new_with_schema("plain_title", object_schema(), |args: PlainArgs, _extra| {
            Box::pin(async move { Ok(json!({"len": args.title.len()})) })
        });

    let out = tool
        .handle(json!({"title": SECRET}), extra("r-4"))
        .await
        .expect("a type with no garde derive behaves exactly as before");
    assert_eq!(out, json!({"len": SECRET.len()}));
}

#[tokio::test]
async fn typed_tool_garde_map_dive_key_is_redacted_not_echoed() {
    let tool = TypedTool::new_validated_with_schema(
        "validated_buckets",
        object_schema(),
        |_args: BucketsArgs, _extra| Box::pin(async move { Ok(json!({"ok": true})) }),
    );

    let err = tool
        .handle(buckets_payload(SECRET), extra("r-5"))
        .await
        .expect_err("the inner label violates its declared maximum");
    let message = refusal(err);

    assert!(
        !message.contains(SECRET),
        "the caller-chosen map key reached the refusal: {message}"
    );
    assert!(
        message.contains(REDACTED_SEGMENT),
        "the caller-chosen path segment must carry the fixed redaction token: {message}"
    );
    assert!(
        message.contains("buckets") && message.contains("label"),
        "the DECLARED segments must survive the projection: {message}"
    );
}

#[tokio::test]
async fn typed_tool_garde_dotted_map_key_is_redacted_as_one_segment() {
    // `garde::Path`'s `Display` joins components with `.`, so a key CONTAINING a
    // dot is indistinguishable from nesting once rendered. The projection walks the
    // components instead, so the whole key is redacted as a single segment rather
    // than split into halves that each look like an identifier.
    let dotted = "ssn.value";
    let tool = TypedTool::new_validated_with_schema(
        "validated_buckets",
        object_schema(),
        |_args: BucketsArgs, _extra| Box::pin(async move { Ok(json!({"ok": true})) }),
    );

    let err = tool
        .handle(buckets_payload(dotted), extra("r-6"))
        .await
        .expect_err("the inner label violates its declared maximum");
    let message = refusal(err);

    assert!(
        !message.contains(dotted),
        "a dotted caller key must not survive as text: {message}"
    );
    assert_eq!(
        message.matches(REDACTED_SEGMENT).count(),
        1,
        "the dotted key must be ONE redacted segment, not two: {message}"
    );
}

// ---------------------------------------------------------------------------
// The deserialization route in front of garde (T-128-17a)
// ---------------------------------------------------------------------------

#[tokio::test]
async fn typed_tool_garde_unvalidated_deserialize_message_is_byte_identical() {
    let payload = json!({"count": SECRET});
    let expected_inner = serde_json::from_value::<CountArgs>(payload.clone())
        .expect_err("a string is not a u32")
        .to_string();

    let tool = TypedTool::new_with_schema(
        "legacy_count",
        object_schema(),
        |_args: CountArgs, _extra| Box::pin(async move { Ok(json!({})) }),
    );

    let message = refusal(
        tool.handle(payload, extra("r-7"))
            .await
            .expect_err("deserialization must fail"),
    );

    assert_eq!(
        message,
        format!("Invalid arguments for tool 'legacy_count': {expected_inner}"),
        "the UNVALIDATED path's message must stay byte-identical to today's"
    );
    assert!(
        message.contains(SECRET),
        "today's message does echo the value — that is the asymmetry being pinned"
    );
}

#[tokio::test]
async fn typed_tool_garde_validated_deserialize_redacts_a_wrong_type() {
    let tool = TypedTool::new_validated_with_schema(
        "validated_count",
        object_schema(),
        |_args: CountArgs, _extra| Box::pin(async move { Ok(json!({})) }),
    );

    let message = refusal(
        tool.handle(json!({"count": SECRET}), extra("r-8"))
            .await
            .expect_err("a string is not a u32"),
    );

    assert!(
        !message.contains(SECRET),
        "the validated path leaked the rejected value: {message}"
    );
    assert!(
        message.contains("validated_count"),
        "the refusal must still name the tool: {message}"
    );
}

#[tokio::test]
async fn typed_tool_garde_validated_deserialize_redacts_an_unknown_enum_variant() {
    let tool = TypedTool::new_validated_with_schema(
        "validated_mode",
        object_schema(),
        |_args: ModeArgs, _extra| Box::pin(async move { Ok(json!({})) }),
    );

    let message = refusal(
        tool.handle(json!({"mode": SECRET}), extra("r-9"))
            .await
            .expect_err("an unknown variant must fail"),
    );

    assert!(
        !message.contains(SECRET),
        "the validated path leaked the unknown variant text: {message}"
    );
}

#[tokio::test]
async fn typed_tool_garde_validated_deserialize_redacts_an_invalid_string() {
    let tool = TypedTool::new_validated_with_schema(
        "validated_initial",
        object_schema(),
        |_args: InitialArgs, _extra| Box::pin(async move { Ok(json!({})) }),
    );

    let message = refusal(
        tool.handle(json!({"initial": SECRET}), extra("r-10"))
            .await
            .expect_err("a multi-character string is not a char"),
    );

    assert!(
        !message.contains(SECRET),
        "the validated path leaked the invalid string: {message}"
    );
}

// ---------------------------------------------------------------------------
// TypedSyncTool — the same behaviours on the sync type
// ---------------------------------------------------------------------------

#[tokio::test]
async fn typed_tool_garde_sync_violation_refuses_and_never_echoes_the_value() {
    let tool = TypedSyncTool::new_validated_with_schema(
        "validated_sync_title",
        object_schema(),
        |args: TitleArgs, _extra| Ok(json!({"title": args.title})),
    );

    let message = refusal(
        tool.handle(json!({"title": SECRET}), extra("s-1"))
            .await
            .expect_err("an over-long title must be refused"),
    );

    assert!(message.contains("title"), "{message}");
    assert!(
        !message.contains(SECRET),
        "the sync refusal leaked the rejected value: {message}"
    );
}

#[tokio::test]
async fn typed_tool_garde_sync_conforming_value_runs_the_handler() {
    let tool = TypedSyncTool::new_validated_with_schema(
        "validated_sync_title",
        object_schema(),
        |args: TitleArgs, _extra| Ok(json!({"title": args.title})),
    );

    let out = tool
        .handle(json!({"title": "0123456789"}), extra("s-2"))
        .await
        .expect("a 10-character title is exactly at the declared maximum");
    assert_eq!(out, json!({"title": "0123456789"}));
}

#[tokio::test]
async fn typed_tool_garde_sync_existing_constructor_runs_no_validation() {
    let tool = TypedSyncTool::new_with_schema(
        "unvalidated_sync_title",
        object_schema(),
        |args: TitleArgs, _extra| Ok(json!({"title": args.title})),
    );

    let out = tool
        .handle(json!({"title": SECRET}), extra("s-3"))
        .await
        .expect("the plain constructor must not validate");
    assert_eq!(out, json!({"title": SECRET}));
}

#[tokio::test]
async fn typed_tool_garde_sync_map_dive_key_is_redacted_not_echoed() {
    let tool = TypedSyncTool::new_validated_with_schema(
        "validated_sync_buckets",
        object_schema(),
        |_args: BucketsArgs, _extra| Ok(json!({"ok": true})),
    );

    let message = refusal(
        tool.handle(buckets_payload(SECRET), extra("s-4"))
            .await
            .expect_err("the inner label violates its declared maximum"),
    );

    assert!(!message.contains(SECRET), "{message}");
    assert!(message.contains(REDACTED_SEGMENT), "{message}");
}

#[tokio::test]
async fn typed_tool_garde_sync_validated_deserialize_is_redacted() {
    let tool = TypedSyncTool::new_validated_with_schema(
        "validated_sync_count",
        object_schema(),
        |_args: CountArgs, _extra| Ok(json!({})),
    );

    let message = refusal(
        tool.handle(json!({"count": SECRET}), extra("s-5"))
            .await
            .expect_err("a string is not a u32"),
    );
    assert!(!message.contains(SECRET), "{message}");
}

#[tokio::test]
async fn typed_tool_garde_sync_unvalidated_deserialize_message_is_byte_identical() {
    let payload = json!({"count": SECRET});
    let expected_inner = serde_json::from_value::<CountArgs>(payload.clone())
        .expect_err("a string is not a u32")
        .to_string();

    let tool = TypedSyncTool::new_with_schema(
        "legacy_sync_count",
        object_schema(),
        |_args: CountArgs, _extra| Ok(json!({})),
    );

    let message = refusal(
        tool.handle(payload, extra("s-6"))
            .await
            .expect_err("deserialization must fail"),
    );
    assert_eq!(
        message,
        format!("Invalid arguments for tool 'legacy_sync_count': {expected_inner}")
    );
}

// ---------------------------------------------------------------------------
// `new_validated` — the schema-generating constructor
// ---------------------------------------------------------------------------

#[cfg(feature = "schema-generation")]
mod schema_generating {
    use super::{extra, refusal, SECRET};
    use garde::Validate;
    use pmcp::{ToolHandler, TypedSyncTool, TypedTool};
    use schemars::JsonSchema;
    use serde::Deserialize;
    use serde_json::json;

    #[derive(Debug, Deserialize, JsonSchema, Validate)]
    struct TitleArgs {
        #[garde(length(chars, max = 10))]
        title: String,
    }

    #[tokio::test]
    async fn typed_tool_garde_new_validated_generates_a_schema_and_validates() {
        let tool = TypedTool::new_validated("generated", |args: TitleArgs, _extra| {
            Box::pin(async move { Ok(json!({"title": args.title})) })
        });

        assert_eq!(
            tool.metadata().expect("metadata").input_schema["type"],
            json!("object"),
            "new_validated must still generate the schema `new` would"
        );

        let message = refusal(
            tool.handle(json!({"title": SECRET}), extra("g-1"))
                .await
                .expect_err("an over-long title must be refused"),
        );
        assert!(!message.contains(SECRET), "{message}");
    }

    #[tokio::test]
    async fn typed_tool_garde_sync_new_validated_generates_a_schema_and_validates() {
        let tool = TypedSyncTool::new_validated("generated_sync", |args: TitleArgs, _extra| {
            Ok(json!({"title": args.title}))
        });

        assert!(tool.metadata().expect("metadata").input_schema.is_object());

        let message = refusal(
            tool.handle(json!({"title": SECRET}), extra("g-2"))
                .await
                .expect_err("an over-long title must be refused"),
        );
        assert!(!message.contains(SECRET), "{message}");
    }
}

// ---------------------------------------------------------------------------
// Property arm — the CLAUDE.md ALWAYS-property leg for E3
// ---------------------------------------------------------------------------

/// Over arbitrary strings longer than the declared maximum, the rendered refusal
/// never contains the input.
///
/// Selected by `make test-property` (`cargo test --features "full" -- --ignored
/// property_`), which only scans ROOT test targets — hence this file's location.
#[test]
#[ignore = "property arm — selected by `make test-property` (--ignored property_)"]
fn property_typed_tool_garde_refusal_never_contains_the_input() {
    use proptest::prelude::*;

    const TOOL: &str = "validated_property_tool";

    let runtime = tokio::runtime::Builder::new_current_thread()
        .build()
        .expect("current-thread runtime");

    proptest!(|(chars in proptest::collection::vec(any::<char>(), 11..40))| {
        let input: String = chars.into_iter().collect();

        // The refusal's fixed text is known WITHOUT the input, so a coincidental
        // substring collision is discarded rather than reported as a leak.
        let template = format!("Invalid arguments for tool '{TOOL}': /title: ");
        prop_assume!(!template.contains(input.as_str()));

        let tool = TypedTool::new_validated_with_schema(
            TOOL,
            object_schema(),
            |args: TitleArgs, _extra| Box::pin(async move { Ok(json!({"title": args.title})) }),
        );

        let message = refusal(
            runtime
                .block_on(tool.handle(json!({"title": input.clone()}), extra("p-1")))
                .expect_err("an over-long title must be refused"),
        );

        prop_assert!(
            !message.contains(input.as_str()),
            "refusal leaked the input: {}",
            message
        );
        prop_assert!(message.contains("title"), "{}", message);
    });
}