gpui-form 0.7.0

The gpui-form crate
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
use super::*;

#[test]
fn async_editor_submit_does_not_close_session_if_revision_changes_before_success() {
    let (started_tx, started_rx) = mpsc::channel();
    let (release_tx, release_rx) = mpsc::channel();
    let release_rx = Arc::new(Mutex::new(release_rx));

    let mut server = test_server();
    form::<PlainRequest>(&mut server)
        .model_async_with_editor({
            let release_rx = Arc::clone(&release_rx);
            move |request| {
                let started_tx = started_tx.clone();
                let release_rx = Arc::clone(&release_rx);
                async move {
                    started_tx.send(()).expect("submit should signal start");
                    release_rx
                        .lock()
                        .expect("release receiver should lock")
                        .recv()
                        .expect("submit should be released");
                    Ok::<ObjectResponse, String>(object_response(json!({
                        "title": request.title,
                        "mode": "async-submit"
                    })))
                }
            }
        })
        .expect("async submit and editor tools should register");

    let tools = editor_tool_names(PlainRequest::descriptor());
    let open = server.call_tool(
        &tools.open,
        Some(json!({ "values": { "title": "Initial title" } })),
    );
    assert_eq!(open.is_error, Some(false));
    let opened = open.structured_content.expect("open should return state");
    assert_eq!(opened["revision"], json!(0));
    let session_id = opened["session_id"]
        .as_str()
        .expect("session id should be a string")
        .to_string();

    let submit_server = server.clone();
    let submit_tool = tools.submit.clone();
    let submit_session_id = session_id.clone();
    let submit_handle = std::thread::spawn(move || {
        submit_server.call_tool(
            &submit_tool,
            Some(json!({
                "session_id": submit_session_id,
                "expected_revision": 0
            })),
        )
    });

    started_rx.recv().expect("submit should start");

    let patched = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "expected_revision": 0,
            "values": {
                "title": "Updated while submit runs"
            }
        })),
    );
    assert_eq!(patched.is_error, Some(false));
    let patched = patched
        .structured_content
        .expect("patch should return state");
    assert_eq!(patched["revision"], json!(1));
    assert_eq!(
        patched["submit_arguments"],
        json!({ "title": "Updated while submit runs" })
    );

    release_tx.send(()).expect("submit should release");
    let submit = submit_handle.join().expect("submit thread should join");
    assert_eq!(submit.is_error, Some(false));
    assert_eq!(
        submit.structured_content,
        Some(json!({
            "title": "Initial title",
            "mode": "async-submit"
        }))
    );

    let read = server.call_tool(
        &tools.read,
        Some(json!({
            "session_id": session_id
        })),
    );
    assert_eq!(read.is_error, Some(false));
    let read = read
        .structured_content
        .expect("newer revision should remain open");
    assert_eq!(read["revision"], json!(1));
    assert_eq!(
        read["submit_arguments"],
        json!({ "title": "Updated while submit runs" })
    );
}

#[test]
fn generated_mcp_editor_decodes_single_field_updates() {
    let mut holder = PlainRequestFormValueHolder::default();

    PlainRequest::decode_field(&mut holder, "title", McpAny::from(json!("Draft")))
        .expect("title should patch");
    PlainRequest::decode_field(&mut holder, "retries", McpAny::from(json!(3)))
        .expect("optional value should patch");
    PlainRequest::decode_field(&mut holder, "enabled", McpAny::from(json!(false)))
        .expect("defaulted value should patch");

    assert_eq!(holder.title, "Draft");
    assert_eq!(holder.retries, Some(3));
    assert!(!holder.enabled);

    let error = PlainRequest::decode_field(&mut holder, "unknown", McpAny::from(json!(true)))
        .expect_err("unknown fields should fail");
    assert_eq!(
        error,
        McpToolError::UnknownField {
            field: "unknown".to_string()
        }
    );
}

#[test]
fn editor_tools_open_patch_read_validate_and_close_sessions() {
    let mut server = test_server();
    form::<PlainRequest>(&mut server)
        .editor()
        .expect("editor tools should register");
    let tools = editor_tool_names(PlainRequest::descriptor());
    assert!(!server.contains_tool(&tools.submit));
    let default_idle_timeout_ms = DEFAULT_EDITOR_SESSION_IDLE_TIMEOUT.as_millis() as u64;

    let listed = server.call_tool(&tools.list, Some(json!({})));
    assert_eq!(listed.is_error, Some(false));
    assert_eq!(
        listed
            .structured_content
            .as_ref()
            .expect("list should return sessions")["session_count"],
        json!(0)
    );
    assert_eq!(
        listed
            .structured_content
            .as_ref()
            .expect("list should return sessions")["session_limit"],
        json!(DEFAULT_EDITOR_SESSION_LIMIT)
    );
    assert_eq!(
        listed
            .structured_content
            .as_ref()
            .expect("list should return sessions")["session_idle_timeout_ms"],
        json!(default_idle_timeout_ms)
    );
    assert_eq!(
        listed
            .structured_content
            .as_ref()
            .expect("list should return sessions")["cleanup"],
        json!({
            "expired_count": 0,
            "expired_session_ids": [],
            "evicted_count": 0,
            "evicted_session_ids": []
        })
    );

    let open = server.call_tool(&tools.open, Some(json!({})));
    assert_eq!(open.is_error, Some(false));
    let opened = open
        .structured_content
        .expect("open should return a session");
    let session_id = opened["session_id"]
        .as_str()
        .expect("session id should be a string");
    assert_eq!(opened["revision"], json!(0));
    assert_eq!(
        opened["cleanup"],
        json!({
            "expired_count": 0,
            "expired_session_ids": [],
            "evicted_count": 0,
            "evicted_session_ids": []
        })
    );
    assert_eq!(opened["missing_required"], json!(["title"]));
    assert_eq!(opened["valid"], false);
    assert_eq!(
        opened["errors"],
        json!([{
            "scope": "field",
            "message": "missing required field `title`",
            "field": "title",
            "validator": "required"
        }])
    );
    assert_eq!(opened["fields"][0]["name"], "title");
    assert_eq!(opened["fields"][0]["required"], true);
    assert_eq!(opened["fields"][0]["present"], false);
    assert_eq!(opened["fields"][0]["has_value"], false);
    assert_eq!(opened["fields"][0]["value"], Value::Null);
    assert_eq!(opened["fields"][0]["missing"], true);
    assert_eq!(
        opened["fields"][0]["errors"],
        json!([{
            "scope": "field",
            "message": "missing required field `title`",
            "field": "title",
            "validator": "required"
        }])
    );
    assert_eq!(opened["fields"][0]["schema"]["type"], "string");
    assert_eq!(opened["fields"][2]["name"], "enabled");
    assert_eq!(opened["fields"][2]["required"], false);
    assert_eq!(opened["fields"][2]["has_default"], true);
    assert_eq!(opened["fields"][2]["errors"], json!([]));
    assert_eq!(opened["submit_arguments"], json!({}));

    let listed = server.call_tool(&tools.list, Some(json!({})));
    assert_eq!(listed.is_error, Some(false));
    let listed = listed
        .structured_content
        .expect("list should return active session");
    assert_eq!(listed["session_count"], json!(1));
    assert_eq!(listed["sessions"][0]["session_id"], session_id);
    assert_eq!(listed["sessions"][0]["revision"], json!(0));
    assert_eq!(listed["sessions"][0]["valid"], false);
    assert_eq!(listed["sessions"][0]["fields"][0]["missing"], true);

    let patched = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "expected_revision": 0,
            "values": {
                "title": "Edited title"
            }
        })),
    );
    assert_eq!(patched.is_error, Some(false));
    let patched = patched
        .structured_content
        .expect("patch should return state");
    assert_eq!(patched["revision"], json!(1));
    assert_eq!(patched["missing_required"], json!([]));
    assert_eq!(patched["valid"], true);
    assert_eq!(patched["errors"], json!([]));
    assert_eq!(patched["fields"][0]["present"], true);
    assert_eq!(patched["fields"][0]["has_value"], true);
    assert_eq!(patched["fields"][0]["value"], "Edited title");
    assert_eq!(patched["fields"][0]["missing"], false);
    assert_eq!(patched["fields"][0]["errors"], json!([]));
    assert_eq!(patched["values"]["title"], "Edited title");
    assert_eq!(
        patched["submit_arguments"],
        json!({ "title": "Edited title" })
    );

    let listed = server.call_tool(&tools.list, Some(json!({})));
    assert_eq!(listed.is_error, Some(false));
    let listed = listed
        .structured_content
        .expect("list should return patched session");
    assert_eq!(
        listed["sessions"][0]["submit_arguments"],
        json!({ "title": "Edited title" })
    );
    assert_eq!(listed["sessions"][0]["revision"], json!(1));

    let stale_patch = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "expected_revision": 0,
            "values": {
                "title": "Stale title"
            }
        })),
    );
    assert_eq!(stale_patch.is_error, Some(true));
    assert_eq!(
        stale_patch.structured_content.expect("structured error")["error"],
        json!({
            "kind": "validation",
            "message": "validation failed: edit session revision mismatch: expected 0, current 1",
            "detail": "edit session revision mismatch: expected 0, current 1"
        })
    );

    let patched = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "expected_revision": 1,
            "values": {
                "retries": 5
            }
        })),
    );
    assert_eq!(patched.is_error, Some(false));
    let patched = patched
        .structured_content
        .expect("patch should return state");
    assert_eq!(patched["revision"], json!(2));
    assert_eq!(patched["values"]["retries"], 5);
    assert_eq!(
        patched["submit_arguments"],
        json!({ "title": "Edited title", "retries": 5 })
    );

    let patched = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "clear": ["retries"]
        })),
    );
    assert_eq!(patched.is_error, Some(false));
    let patched = patched
        .structured_content
        .expect("clear should return state");
    assert_eq!(patched["valid"], true);
    assert_eq!(patched["values"].get("retries"), None);
    assert_eq!(
        patched["submit_arguments"],
        json!({ "title": "Edited title" })
    );

    let patched = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "values": {
                "retries": null
            }
        })),
    );
    assert_eq!(patched.is_error, Some(false));
    let patched = patched
        .structured_content
        .expect("patch should return state");
    assert_eq!(patched["values"]["retries"], Value::Null);
    assert_eq!(patched["fields"][1]["name"], "retries");
    assert_eq!(patched["fields"][1]["has_value"], true);
    assert_eq!(patched["fields"][1]["value"], Value::Null);
    assert_eq!(
        patched["submit_arguments"],
        json!({ "title": "Edited title", "retries": null })
    );

    let patched = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "values": {
                "retries": 5
            }
        })),
    );
    assert_eq!(patched.is_error, Some(false));

    let validation = server.call_tool(
        &tools.validate,
        Some(json!({
            "session_id": session_id
        })),
    );
    assert_eq!(validation.is_error, Some(false));
    let validation = validation
        .structured_content
        .expect("validation should return state");
    assert_eq!(validation["valid"], true);
    assert_eq!(validation["errors"], json!([]));
    assert_eq!(
        validation["submit_arguments"],
        json!({ "title": "Edited title", "retries": 5 })
    );

    let read = server.call_tool(
        &tools.read,
        Some(json!({
            "session_id": session_id
        })),
    );
    assert_eq!(read.is_error, Some(false));
    assert_eq!(
        read.structured_content.expect("read")["submit_arguments"],
        json!({ "title": "Edited title", "retries": 5 })
    );

    let replaced = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "replace": true,
            "values": {
                "title": "Replacement title"
            }
        })),
    );
    assert_eq!(replaced.is_error, Some(false));
    let replaced = replaced
        .structured_content
        .expect("replace patch should return state");
    assert_eq!(replaced["valid"], true);
    assert_eq!(
        replaced["submit_arguments"],
        json!({ "title": "Replacement title" })
    );

    let patched = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "values": {
                "title": "Edited title",
                "retries": 5
            }
        })),
    );
    assert_eq!(patched.is_error, Some(false));

    let patched = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "clear": ["title"]
        })),
    );
    assert_eq!(patched.is_error, Some(false));
    let patched = patched
        .structured_content
        .expect("required clear should return state");
    assert_eq!(patched["valid"], false);
    assert_eq!(patched["missing_required"], json!(["title"]));
    assert_eq!(
        patched["fields"][0]["errors"],
        json!([{
            "scope": "field",
            "message": "missing required field `title`",
            "field": "title",
            "validator": "required"
        }])
    );
    assert_eq!(patched["submit_arguments"], json!({ "retries": 5 }));
    let close_revision = patched["revision"]
        .as_u64()
        .expect("patched revision should be an integer");

    let stale_close = server.call_tool(
        &tools.close,
        Some(json!({
            "session_id": session_id,
            "expected_revision": 0
        })),
    );
    assert_eq!(stale_close.is_error, Some(true));
    assert_eq!(
        stale_close.structured_content.expect("structured error")["error"],
        json!({
            "kind": "validation",
            "message": format!(
                "validation failed: edit session revision mismatch: expected 0, current {close_revision}"
            ),
            "detail": format!(
                "edit session revision mismatch: expected 0, current {close_revision}"
            )
        })
    );

    let close = server.call_tool(
        &tools.close,
        Some(json!({
            "session_id": session_id,
            "expected_revision": close_revision
        })),
    );
    assert_eq!(close.is_error, Some(false));

    let listed = server.call_tool(&tools.list, Some(json!({})));
    assert_eq!(listed.is_error, Some(false));
    assert_eq!(
        listed
            .structured_content
            .as_ref()
            .expect("list after close should return no sessions")["session_count"],
        json!(0)
    );

    let read_after_close = server.call_tool(
        &tools.read,
        Some(json!({
            "session_id": session_id
        })),
    );
    assert_eq!(read_after_close.is_error, Some(true));
    assert_eq!(
        read_after_close
            .structured_content
            .expect("structured error")["error"],
        json!({
            "kind": "invalid_field_value",
            "message": format!("unknown value `{session_id}` for field `session_id`"),
            "field": "session_id",
            "value": session_id
        })
    );

    let first = server.call_tool(&tools.open, Some(json!({})));
    assert_eq!(first.is_error, Some(false));
    let first_id = first.structured_content.unwrap()["session_id"]
        .as_str()
        .expect("first reopened session id")
        .to_string();
    let second = server.call_tool(
        &tools.open,
        Some(json!({
            "values": {
                "title": "Second session"
            }
        })),
    );
    assert_eq!(second.is_error, Some(false));
    let second_id = second.structured_content.unwrap()["session_id"]
        .as_str()
        .expect("second reopened session id")
        .to_string();

    let close_all = server.call_tool(&tools.close_all, Some(json!({})));
    assert_eq!(close_all.is_error, Some(false));
    let close_all = close_all
        .structured_content
        .expect("close_all should return closed session ids");
    assert_eq!(
        close_all["form"],
        json!(PlainRequest::descriptor().tool_name())
    );
    assert_eq!(close_all["form_name"], "PlainRequest");
    assert_eq!(close_all["closed_count"], json!(2));
    assert_eq!(close_all["session_ids"], json!([first_id, second_id]));

    let listed = server.call_tool(&tools.list, Some(json!({})));
    assert_eq!(listed.is_error, Some(false));
    assert_eq!(
        listed
            .structured_content
            .as_ref()
            .expect("list after close_all should return no sessions")["session_count"],
        json!(0)
    );

    let empty_close_all = server.call_tool(&tools.close_all, Some(json!({})));
    assert_eq!(empty_close_all.is_error, Some(false));
    let empty_close_all = empty_close_all
        .structured_content
        .expect("empty close_all should still succeed");
    assert_eq!(empty_close_all["closed_count"], json!(0));
    assert_eq!(empty_close_all["session_ids"], json!([]));
}

#[test]
fn editor_options_evict_oldest_sessions_past_limit() {
    let mut server = test_server();
    form::<PlainRequest>(&mut server)
        .editor_with_options(McpFormEditorOptions::default().with_session_limit(2))
        .expect("editor tools should register with options");
    let tools = editor_tool_names(PlainRequest::descriptor());

    let mut session_ids = Vec::new();
    let mut third_opened = None;
    for title in ["First", "Second", "Third"] {
        let opened = server.call_tool(
            &tools.open,
            Some(json!({
                "values": {
                    "title": title
                }
            })),
        );
        assert_eq!(opened.is_error, Some(false));
        let opened = opened
            .structured_content
            .expect("open should return a session");
        session_ids.push(
            opened["session_id"]
                .as_str()
                .expect("session id should be a string")
                .to_string(),
        );
        if title == "Third" {
            third_opened = Some(opened);
        }
    }
    let third_opened = third_opened.expect("third open should be captured");
    assert_eq!(
        third_opened["cleanup"],
        json!({
            "expired_count": 0,
            "expired_session_ids": [],
            "evicted_count": 1,
            "evicted_session_ids": [session_ids[0]]
        })
    );

    let listed = server.call_tool(&tools.list, Some(json!({})));
    assert_eq!(listed.is_error, Some(false));
    let listed = listed
        .structured_content
        .expect("list should return bounded sessions");
    assert_eq!(listed["session_limit"], json!(2));
    assert_eq!(
        listed["cleanup"],
        json!({
            "expired_count": 0,
            "expired_session_ids": [],
            "evicted_count": 0,
            "evicted_session_ids": []
        })
    );
    assert_eq!(listed["session_count"], json!(2));
    assert_eq!(listed["sessions"][0]["session_id"], session_ids[1]);
    assert_eq!(
        listed["sessions"][0]["submit_arguments"],
        json!({ "title": "Second" })
    );
    assert_eq!(listed["sessions"][1]["session_id"], session_ids[2]);
    assert_eq!(
        listed["sessions"][1]["submit_arguments"],
        json!({ "title": "Third" })
    );

    let evicted_read = server.call_tool(
        &tools.read,
        Some(json!({
            "session_id": session_ids[0]
        })),
    );
    assert_eq!(evicted_read.is_error, Some(true));
    assert_eq!(
        evicted_read
            .structured_content
            .expect("read should return a structured error")["error"]["kind"],
        json!("invalid_field_value")
    );
}

#[test]
fn editor_options_expire_idle_sessions_before_next_operation() {
    let mut server = test_server();
    form::<PlainRequest>(&mut server)
        .editor_with_options(
            McpFormEditorOptions::default().with_session_idle_timeout(Duration::ZERO),
        )
        .expect("editor tools should register with idle timeout");
    let tools = editor_tool_names(PlainRequest::descriptor());

    let opened = server.call_tool(
        &tools.open,
        Some(json!({
            "values": {
                "title": "Expires immediately after open"
            }
        })),
    );
    assert_eq!(opened.is_error, Some(false));
    let session_id = opened
        .structured_content
        .expect("open should return a session")["session_id"]
        .as_str()
        .expect("session id should be a string")
        .to_string();

    let listed = server.call_tool(&tools.list, Some(json!({})));
    assert_eq!(listed.is_error, Some(false));
    let listed = listed
        .structured_content
        .expect("list should expire idle sessions");
    assert_eq!(listed["session_idle_timeout_ms"], json!(0));
    assert_eq!(
        listed["cleanup"],
        json!({
            "expired_count": 1,
            "expired_session_ids": [session_id],
            "evicted_count": 0,
            "evicted_session_ids": []
        })
    );
    assert_eq!(listed["session_count"], json!(0));

    let expired_read = server.call_tool(
        &tools.read,
        Some(json!({
            "session_id": session_id
        })),
    );
    assert_eq!(expired_read.is_error, Some(true));
    assert_eq!(
        expired_read
            .structured_content
            .expect("read should return a structured error")["error"]["kind"],
        json!("invalid_field_value")
    );
}

#[test]
fn editor_bulk_patch_rejects_invalid_values_without_partial_mutation() {
    let mut server = test_server();
    form::<PlainRequest>(&mut server)
        .editor()
        .expect("editor tools should register");
    let tools = editor_tool_names(PlainRequest::descriptor());

    let open = server.call_tool(&tools.open, Some(json!({})));
    assert_eq!(open.is_error, Some(false));
    let opened = open
        .structured_content
        .expect("open should return a session");
    let session_id = opened["session_id"]
        .as_str()
        .expect("session id should be a string");

    let failed = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "values": {
                "title": "Should not commit",
                "unknown": true
            }
        })),
    );
    assert_eq!(failed.is_error, Some(true));
    assert_eq!(
        failed.structured_content.expect("structured error")["error"],
        json!({
            "kind": "unknown_field",
            "message": "unknown field `unknown`",
            "field": "unknown"
        })
    );

    let read = server.call_tool(
        &tools.read,
        Some(json!({
            "session_id": session_id
        })),
    );
    assert_eq!(read.is_error, Some(false));
    let read = read.structured_content.expect("read should return state");
    assert_eq!(read["submit_arguments"], json!({}));
    assert_eq!(read["missing_required"], json!(["title"]));
    assert_eq!(read["valid"], false);

    let failed = server.call_tool(
        &tools.patch,
        Some(json!({
            "session_id": session_id,
            "clear": ["unknown"]
        })),
    );
    assert_eq!(failed.is_error, Some(true));
    assert_eq!(
        failed.structured_content.expect("structured error")["error"],
        json!({
            "kind": "unknown_field",
            "message": "unknown field `unknown`",
            "field": "unknown"
        })
    );
}