kagi-mcp 1.0.2

MCP stdio server for kagi-sdk
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
use std::time::Duration;

use kagi_sdk::ClientConfig;
use mockito::{Matcher, Server};
use rmcp::{
    model::{CallToolRequestParams, ErrorCode},
    ServiceError, ServiceExt,
};
use serde_json::{json, Value};
use url::Url;

use crate::{
    backend::{BackendRuntime, EnvConfig, ENV_API_KEY, ENV_BACKEND_MODE, ENV_SESSION_TOKEN},
    KagiMcpServer,
};

fn build_config(server: &Server) -> ClientConfig {
    ClientConfig::default()
        .with_base_url(Url::parse(&server.url()).expect("mock server URL is always valid"))
}

fn build_official_backend(server: &Server) -> BackendRuntime {
    BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("official".to_string()),
            api_key: Some("official_token".to_string()),
            session_token: Some("session_token".to_string()),
        },
        build_config(server),
    )
    .expect("official backend should build")
}

fn build_session_backend(server: &Server) -> BackendRuntime {
    BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("session".to_string()),
            api_key: Some("official_token".to_string()),
            session_token: Some("session_token".to_string()),
        },
        build_config(server),
    )
    .expect("session backend should build")
}

async fn start_server(
    backend: BackendRuntime,
) -> (
    rmcp::service::RunningService<rmcp::RoleClient, ()>,
    tokio::task::JoinHandle<()>,
) {
    let (server_transport, client_transport) = tokio::io::duplex(64 * 1024);
    let server = KagiMcpServer::from_backend(backend).expect("server should construct");

    let handle = tokio::spawn(async move {
        let running = server
            .serve(server_transport)
            .await
            .expect("server should start");
        let _ = running.waiting().await.expect("server should stop cleanly");
    });

    let client = ().serve(client_transport).await.expect("client should connect");
    (client, handle)
}

fn json_object(value: Value) -> serde_json::Map<String, Value> {
    value
        .as_object()
        .cloned()
        .expect("arguments should be a JSON object")
}

fn assert_invalid_params(error: ServiceError) {
    match error {
        ServiceError::McpError(data) => {
            assert_eq!(data.code, ErrorCode::INVALID_PARAMS);
        }
        unexpected => panic!("expected invalid params mcp error, got {unexpected:?}"),
    }
}

fn schema_property<'a>(schema: &'a Value, field: &str, context: &str) -> &'a Value {
    schema
        .get("properties")
        .and_then(Value::as_object)
        .and_then(|properties| properties.get(field))
        .unwrap_or_else(|| panic!("{context} should expose `{field}` property"))
}

fn schema_property_names(schema: &Value, context: &str) -> Vec<String> {
    let mut names = schema
        .get("properties")
        .and_then(Value::as_object)
        .unwrap_or_else(|| panic!("{context} should expose properties"))
        .keys()
        .cloned()
        .collect::<Vec<_>>();
    names.sort();
    names
}

fn schema_required_fields(schema: &Value, context: &str) -> Vec<String> {
    let mut required = schema
        .get("required")
        .and_then(Value::as_array)
        .unwrap_or_else(|| panic!("{context} should expose required fields"))
        .iter()
        .map(|field| {
            field
                .as_str()
                .unwrap_or_else(|| panic!("{context} required field should be string"))
                .to_string()
        })
        .collect::<Vec<_>>();
    required.sort();
    required
}

fn assert_additional_properties_false(schema: &Value, context: &str) {
    assert_eq!(
        schema.get("additionalProperties"),
        Some(&Value::Bool(false)),
        "{context} must disallow unknown fields"
    );
}

fn resolve_local_schema_ref<'a>(
    root: &'a Value,
    mut schema: &'a Value,
    context: &str,
) -> &'a Value {
    loop {
        let Some(reference) = schema.get("$ref").and_then(Value::as_str) else {
            return schema;
        };

        let pointer = reference
            .strip_prefix('#')
            .unwrap_or_else(|| panic!("{context} must use a local JSON pointer reference"));
        schema = root
            .pointer(pointer)
            .unwrap_or_else(|| panic!("{context} points to missing schema ref `{reference}`"));
    }
}

fn search_result_html(count: usize) -> String {
    let mut html = String::from("<html><body>");
    for index in 0..count {
        html.push_str(&format!(
            r#"<div class="search-result"><a class="__sri_title_link" href="https://example.com/{index}">Result {index}</a><div class="__sri-desc">Snippet {index}</div></div>"#
        ));
    }
    html.push_str("</body></html>");
    html
}

#[tokio::test]
async fn tool_listing_has_exactly_two_tools_with_read_only_idempotent_metadata() {
    let server = Server::new();
    let backend = build_official_backend(&server);
    let (client, handle) = start_server(backend).await;

    let tools = client
        .peer()
        .list_all_tools()
        .await
        .expect("tools list should succeed");

    let mut names = tools
        .iter()
        .map(|tool| tool.name.to_string())
        .collect::<Vec<_>>();
    names.sort();
    assert_eq!(names, vec!["kagi_search", "kagi_summarize"]);

    for tool in tools {
        let annotations = tool.annotations.expect("tool annotations are required");
        assert_eq!(annotations.read_only_hint, Some(true));
        assert_eq!(annotations.idempotent_hint, Some(true));
    }

    let prompts = client
        .peer()
        .list_all_prompts()
        .await
        .expect("prompt list should succeed");
    assert!(prompts.is_empty());

    let resources = client
        .peer()
        .list_all_resources()
        .await
        .expect("resource list should succeed");
    assert!(resources.is_empty());

    client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    handle.await.expect("server join should succeed");
}

#[tokio::test]
async fn tool_schemas_publish_strict_v1_contract() {
    let server = Server::new();
    let backend = build_official_backend(&server);
    let (client, handle) = start_server(backend).await;

    let tools = client
        .peer()
        .list_all_tools()
        .await
        .expect("tools list should succeed");

    let search_tool = tools
        .iter()
        .find(|tool| tool.name == "kagi_search")
        .expect("kagi_search tool should exist");
    let search_input_schema = Value::Object(search_tool.input_schema.as_ref().clone());
    assert_eq!(search_input_schema.get("type"), Some(&json!("object")));
    assert_additional_properties_false(&search_input_schema, "search input schema");
    assert_eq!(
        schema_property_names(&search_input_schema, "search input schema"),
        vec!["limit", "query"]
    );
    assert_eq!(
        schema_required_fields(&search_input_schema, "search input schema"),
        vec!["query"]
    );

    let search_query_schema = schema_property(&search_input_schema, "query", "search input schema");
    assert_eq!(search_query_schema.get("type"), Some(&json!("string")));
    assert_eq!(search_query_schema.get("minLength"), Some(&json!(1)));
    assert_eq!(search_query_schema.get("pattern"), Some(&json!(".*\\S.*")));

    let search_limit_schema = schema_property(&search_input_schema, "limit", "search input schema");
    assert_eq!(search_limit_schema.get("type"), Some(&json!("integer")));
    assert_eq!(search_limit_schema.get("minimum"), Some(&json!(1)));
    assert_eq!(search_limit_schema.get("maximum"), Some(&json!(10)));
    assert_eq!(search_limit_schema.get("default"), Some(&json!(5)));

    let search_output_root = Value::Object(
        search_tool
            .output_schema
            .as_ref()
            .expect("search output schema should exist")
            .as_ref()
            .clone(),
    );
    let search_output_schema = resolve_local_schema_ref(
        &search_output_root,
        &search_output_root,
        "search output root schema",
    );
    assert_eq!(search_output_schema.get("type"), Some(&json!("object")));
    assert_additional_properties_false(search_output_schema, "search output schema");
    assert_eq!(
        schema_property_names(search_output_schema, "search output schema"),
        vec!["results", "total_returned"]
    );
    assert_eq!(
        schema_required_fields(search_output_schema, "search output schema"),
        vec!["results", "total_returned"]
    );

    let total_returned_schema = schema_property(
        search_output_schema,
        "total_returned",
        "search output schema",
    );
    assert_eq!(
        total_returned_schema,
        &json!({
            "type": "integer",
            "minimum": 0
        })
    );
    assert_eq!(
        total_returned_schema.get("format"),
        None,
        "total_returned schema must not publish unsupported integer formats"
    );

    let search_results_schema =
        schema_property(search_output_schema, "results", "search output schema");
    assert_eq!(search_results_schema.get("type"), Some(&json!("array")));
    let search_result_card_schema = resolve_local_schema_ref(
        &search_output_root,
        search_results_schema
            .get("items")
            .expect("search results should include item schema"),
        "search result card schema",
    );
    assert_eq!(
        search_result_card_schema.get("type"),
        Some(&json!("object"))
    );
    assert_additional_properties_false(search_result_card_schema, "search result card schema");
    assert_eq!(
        schema_property_names(search_result_card_schema, "search result card schema"),
        vec!["snippet", "title", "url"]
    );
    assert_eq!(
        schema_required_fields(search_result_card_schema, "search result card schema"),
        vec!["title", "url"]
    );

    let summarize_tool = tools
        .iter()
        .find(|tool| tool.name == "kagi_summarize")
        .expect("kagi_summarize tool should exist");
    let summarize_input_schema = Value::Object(summarize_tool.input_schema.as_ref().clone());
    assert_eq!(summarize_input_schema.get("type"), Some(&json!("object")));
    assert_additional_properties_false(&summarize_input_schema, "summarize input schema");
    assert_eq!(
        schema_property_names(&summarize_input_schema, "summarize input schema"),
        vec!["text", "url"]
    );
    assert_eq!(
        summarize_input_schema.get("required"),
        None,
        "summarize input schema should not require top-level fields"
    );

    for forbidden_keyword in [
        "oneOf",
        "anyOf",
        "allOf",
        "not",
        "minProperties",
        "maxProperties",
    ] {
        assert_eq!(
            summarize_input_schema.get(forbidden_keyword),
            None,
            "summarize input schema must not publish `{forbidden_keyword}`"
        );
    }

    let summarize_url_property =
        schema_property(&summarize_input_schema, "url", "summarize input schema");
    assert_eq!(summarize_url_property.get("type"), Some(&json!("string")));
    for forbidden_keyword in ["format", "pattern", "minLength", "maxLength"] {
        assert_eq!(
            summarize_url_property.get(forbidden_keyword),
            None,
            "summarize url property must not publish `{forbidden_keyword}`"
        );
    }

    let summarize_text_property =
        schema_property(&summarize_input_schema, "text", "summarize input schema");
    assert_eq!(summarize_text_property.get("type"), Some(&json!("string")));
    for forbidden_keyword in ["format", "pattern", "minLength", "maxLength"] {
        assert_eq!(
            summarize_text_property.get(forbidden_keyword),
            None,
            "summarize text property must not publish `{forbidden_keyword}`"
        );
    }

    let summarize_output_root = Value::Object(
        summarize_tool
            .output_schema
            .as_ref()
            .expect("summarize output schema should exist")
            .as_ref()
            .clone(),
    );
    let summarize_output_schema = resolve_local_schema_ref(
        &summarize_output_root,
        &summarize_output_root,
        "summarize output root schema",
    );
    assert_eq!(summarize_output_schema.get("type"), Some(&json!("object")));
    assert_additional_properties_false(summarize_output_schema, "summarize output schema");
    assert_eq!(
        schema_property_names(summarize_output_schema, "summarize output schema"),
        vec!["markdown", "source_url", "text"]
    );
    assert_eq!(
        schema_required_fields(summarize_output_schema, "summarize output schema"),
        vec!["markdown"]
    );

    client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    handle.await.expect("server join should succeed");
}

#[tokio::test]
async fn official_backend_parity_matrix_search_and_summarize_modes() {
    let mut server = Server::new();
    let _search_mock = server
        .mock("GET", "/api/v0/search")
        .match_query(Matcher::UrlEncoded("q".into(), "rust sdk".into()))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{"data":{"results":[{"title":"One","url":"https://one.test","snippet":"alpha","unused":"x"},{"title":"Two","url":"https://two.test"}]}}"#,
        )
        .create();

    let _summarize_get_mock = server
        .mock("GET", "/api/v0/summarize")
        .match_query(Matcher::UrlEncoded(
            "url".into(),
            "https://example.com/post".into(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "data": {
                    "markdown": "# Official URL",
                    "text": "url summary",
                    "source_url": "https://example.com/post",
                    "metadata": { "ignored": true }
                }
            })
            .to_string(),
        )
        .create();

    let _summarize_post_mock = server
        .mock("POST", "/api/v0/summarize")
        .match_body(Matcher::JsonString(
            json!({ "text": "  keep exact spacing  " }).to_string(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "data": {
                    "markdown": "# Official Text",
                    "text": "  keep exact spacing  "
                }
            })
            .to_string(),
        )
        .create();

    let backend = build_official_backend(&server);
    let (client, handle) = start_server(backend).await;

    let search_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "  rust sdk  ",
                "limit": 2
            }))),
        )
        .await
        .expect("official search should succeed");
    assert_eq!(search_result.is_error, Some(false));
    let search_output: crate::SearchToolOutput = search_result
        .into_typed()
        .expect("search output should deserialize");
    assert_eq!(search_output.total_returned, 2);
    assert_eq!(search_output.results[0].title, "One");
    assert_eq!(search_output.results[1].url, "https://two.test");

    let summarize_url_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "url": "https://example.com/post"
            }))),
        )
        .await
        .expect("official summarize(url) should succeed");
    let summarize_url_output: crate::SummarizeToolOutput = summarize_url_result
        .into_typed()
        .expect("summarize(url) output should deserialize");
    assert_eq!(summarize_url_output.markdown, "# Official URL");
    assert_eq!(
        summarize_url_output.source_url.as_deref(),
        Some("https://example.com/post")
    );

    let summarize_text_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "text": "  keep exact spacing  "
            }))),
        )
        .await
        .expect("official summarize(text) should succeed");
    let summarize_text_output: crate::SummarizeToolOutput = summarize_text_result
        .into_typed()
        .expect("summarize(text) output should deserialize");
    assert_eq!(summarize_text_output.markdown, "# Official Text");
    assert_eq!(
        summarize_text_output.text.as_deref(),
        Some("  keep exact spacing  ")
    );

    client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    handle.await.expect("server join should succeed");
}

#[tokio::test]
async fn session_backend_parity_matrix_search_and_summarize_modes() {
    let mut server = Server::new();
    let _search_mock = server
        .mock("GET", "/html/search")
        .match_query(Matcher::UrlEncoded("q".into(), "rust sdk".into()))
        .with_status(200)
        .with_header("content-type", "text/html")
        .with_body(search_result_html(3))
        .create();

    let _summarize_url_mock = server
        .mock("GET", "/mother/summary_labs")
        .match_query(Matcher::UrlEncoded(
            "url".into(),
            "https://example.com/post".into(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "markdown": "# Session URL",
                "text": "session url",
                "metadata": {
                    "source_url": "https://example.com/post",
                    "tokens": 42
                }
            })
            .to_string(),
        )
        .create();

    let _summarize_text_mock = server
        .mock("POST", "/mother/summary_labs/")
        .match_body(Matcher::UrlEncoded(
            "text".to_string(),
            "  keep exact spacing  ".to_string(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "markdown": "# Session Text",
                "text": "  keep exact spacing  "
            })
            .to_string(),
        )
        .create();

    let backend = build_session_backend(&server);
    let (client, handle) = start_server(backend).await;

    let search_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "  rust sdk  ",
                "limit": 2
            }))),
        )
        .await
        .expect("session search should succeed");
    let search_output: crate::SearchToolOutput = search_result
        .into_typed()
        .expect("search output should deserialize");
    assert_eq!(search_output.total_returned, 2);
    assert_eq!(search_output.results[0].title, "Result 0");

    let summarize_url_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "url": "https://example.com/post"
            }))),
        )
        .await
        .expect("session summarize(url) should succeed");
    let summarize_url_output: crate::SummarizeToolOutput = summarize_url_result
        .into_typed()
        .expect("summarize(url) output should deserialize");
    assert_eq!(summarize_url_output.markdown, "# Session URL");
    assert_eq!(
        summarize_url_output.source_url.as_deref(),
        Some("https://example.com/post")
    );

    let summarize_text_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "text": "  keep exact spacing  "
            }))),
        )
        .await
        .expect("session summarize(text) should succeed");
    let summarize_text_output: crate::SummarizeToolOutput = summarize_text_result
        .into_typed()
        .expect("summarize(text) output should deserialize");
    assert_eq!(summarize_text_output.markdown, "# Session Text");
    assert_eq!(
        summarize_text_output.text.as_deref(),
        Some("  keep exact spacing  ")
    );

    client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    handle.await.expect("server join should succeed");
}

#[tokio::test]
async fn search_validation_rules_and_limit_behavior_are_enforced() {
    let mut server = Server::new();
    let _search_mock = server
        .mock("GET", "/api/v0/search")
        .match_query(Matcher::UrlEncoded("q".into(), "rust".into()))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{"data":{"results":[{"title":"1","url":"https://1.test"},{"title":"2","url":"https://2.test"},{"title":"3","url":"https://3.test"},{"title":"4","url":"https://4.test"},{"title":"5","url":"https://5.test"},{"title":"6","url":"https://6.test"}]}}"#,
        )
        .create();

    let backend = build_official_backend(&server);
    let (client, handle) = start_server(backend).await;

    let missing_query = client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "limit": 3
            }))),
        )
        .await
        .expect_err("missing query must fail");
    assert_invalid_params(missing_query);

    let blank_query = client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "    "
            }))),
        )
        .await
        .expect_err("blank query must fail");
    assert_invalid_params(blank_query);

    let invalid_limit_low = client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "rust",
                "limit": 0
            }))),
        )
        .await
        .expect_err("limit below min must fail");
    assert_invalid_params(invalid_limit_low);

    let invalid_limit_high = client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "rust",
                "limit": 11
            }))),
        )
        .await
        .expect_err("limit above max must fail");
    assert_invalid_params(invalid_limit_high);

    let unknown_field = client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "rust",
                "extra": true
            }))),
        )
        .await
        .expect_err("unknown field must fail");
    assert_invalid_params(unknown_field);

    let default_limit_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "  rust  "
            }))),
        )
        .await
        .expect("search with default limit should succeed");
    let default_limit_output: crate::SearchToolOutput = default_limit_result
        .into_typed()
        .expect("default limit output should deserialize");
    assert_eq!(default_limit_output.total_returned, 5);

    let explicit_limit_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "rust",
                "limit": 1
            }))),
        )
        .await
        .expect("search with explicit limit should succeed");
    let explicit_limit_output: crate::SearchToolOutput = explicit_limit_result
        .into_typed()
        .expect("explicit limit output should deserialize");
    assert_eq!(explicit_limit_output.total_returned, 1);

    client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    handle.await.expect("server join should succeed");
}

#[tokio::test]
async fn summarize_validation_rules_and_byte_limits_are_enforced() {
    let mut server = Server::new();
    let _summarize_url_mock = server
        .mock("GET", "/api/v0/summarize")
        .match_query(Matcher::UrlEncoded(
            "url".into(),
            "https://example.com/post".into(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(json!({ "data": { "markdown": "# URL" } }).to_string())
        .create();

    let _summarize_text_mock = server
        .mock("POST", "/api/v0/summarize")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(json!({ "data": { "markdown": "# TEXT" } }).to_string())
        .create();

    let backend = build_official_backend(&server);
    let (client, handle) = start_server(backend).await;

    let missing_both = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({}))),
        )
        .await
        .expect_err("missing both url/text must fail");
    assert_invalid_params(missing_both);

    let url_with_empty_text = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "url": "https://example.com/post",
                "text": ""
            }))),
        )
        .await
        .expect("url should succeed when text is exact empty string");
    let url_with_empty_text_output: crate::SummarizeToolOutput = url_with_empty_text
        .into_typed()
        .expect("url+empty text output should deserialize");
    assert_eq!(url_with_empty_text_output.markdown, "# URL");

    let text_with_empty_url = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "text": "real text",
                "url": ""
            }))),
        )
        .await
        .expect("text should succeed when url is exact empty string");
    let text_with_empty_url_output: crate::SummarizeToolOutput = text_with_empty_url
        .into_typed()
        .expect("text+empty url output should deserialize");
    assert_eq!(text_with_empty_url_output.markdown, "# TEXT");

    let both_fields = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "url": "https://example.com/post",
                "text": "hello"
            }))),
        )
        .await
        .expect_err("url+text together must fail");
    assert_invalid_params(both_fields);

    let both_empty_fields = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "url": "",
                "text": ""
            }))),
        )
        .await
        .expect_err("url+text exact empty strings must fail");
    assert_invalid_params(both_empty_fields);

    let url_with_whitespace_only_text = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "url": "https://example.com/post",
                "text": "   "
            }))),
        )
        .await
        .expect_err("url + whitespace-only text must fail");
    assert_invalid_params(url_with_whitespace_only_text);

    let text_with_whitespace_padded_url = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "text": "real text",
                "url": " https://example.com/post"
            }))),
        )
        .await
        .expect_err("text + whitespace-padded url must fail");
    assert_invalid_params(text_with_whitespace_padded_url);

    let text_with_whitespace_only_url = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "text": "real text",
                "url": "   "
            }))),
        )
        .await
        .expect_err("text + whitespace-only url must fail");
    assert_invalid_params(text_with_whitespace_only_url);

    let invalid_url = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "url": "ftp://example.com/post"
            }))),
        )
        .await
        .expect_err("non-http url must fail");
    assert_invalid_params(invalid_url);

    let padded_url = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "url": " https://example.com/post "
            }))),
        )
        .await
        .expect_err("whitespace-padded url must fail");
    assert_invalid_params(padded_url);

    let blank_text = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "text": "    "
            }))),
        )
        .await
        .expect_err("blank text must fail");
    assert_invalid_params(blank_text);

    let unknown_field = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "url": "https://example.com/post",
                "backend": "official"
            }))),
        )
        .await
        .expect_err("unknown summarize field must fail");
    assert_invalid_params(unknown_field);

    let accepted_text = "a".repeat(50_000);
    client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "text": accepted_text
            }))),
        )
        .await
        .expect("50_000 byte summarize text should be accepted");

    let rejected_text = "a".repeat(50_001);
    let rejected_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "text": rejected_text
            }))),
        )
        .await
        .expect_err("50_001 byte summarize text should be rejected");
    assert_invalid_params(rejected_result);

    let accepted_multibyte_text = "é".repeat(25_000);
    client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "text": accepted_multibyte_text
            }))),
        )
        .await
        .expect("50_000-byte multibyte summarize text should be accepted");

    let rejected_multibyte_text = "é".repeat(25_001);
    let rejected_multibyte_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "text": rejected_multibyte_text
            }))),
        )
        .await
        .expect_err("50_002-byte multibyte summarize text should be rejected");
    assert_invalid_params(rejected_multibyte_result);

    client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    handle.await.expect("server join should succeed");
}

#[tokio::test]
async fn outputs_never_leak_provider_envelopes_or_metadata_and_do_not_truncate() {
    let mut server = Server::new();
    let long_markdown = "x".repeat(70_000);

    let _search_mock = server
        .mock("GET", "/api/v0/search")
        .match_query(Matcher::UrlEncoded("q".into(), "rust".into()))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            r#"{"data":{"results":[{"title":"One","url":"https://one.test","snippet":"alpha","provider_meta":{"rank":1}}],"extra":{"ignored":true}}}"#,
        )
        .create();

    let _summarize_mock = server
        .mock("GET", "/api/v0/summarize")
        .match_query(Matcher::UrlEncoded(
            "url".into(),
            "https://example.com/post".into(),
        ))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "data": {
                    "markdown": long_markdown,
                    "text": "plain",
                    "metadata": {"tokens": 999},
                    "status": "ok"
                }
            })
            .to_string(),
        )
        .create();

    let backend = build_official_backend(&server);
    let (client, handle) = start_server(backend).await;

    let search_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "rust",
                "limit": 5
            }))),
        )
        .await
        .expect("search should succeed");
    let search_output: crate::SearchToolOutput = search_result
        .into_typed()
        .expect("search output should deserialize");
    let serialized_search =
        serde_json::to_value(search_output).expect("search output should serialize");
    let mut search_keys = serialized_search
        .as_object()
        .expect("search output should be object")
        .keys()
        .cloned()
        .collect::<Vec<_>>();
    search_keys.sort();
    assert_eq!(search_keys, vec!["results", "total_returned"]);

    let summarize_result = client
        .call_tool(
            CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                "url": "https://example.com/post"
            }))),
        )
        .await
        .expect("summarize should succeed");
    let summarize_output: crate::SummarizeToolOutput = summarize_result
        .into_typed()
        .expect("summarize output should deserialize");
    assert_eq!(summarize_output.markdown.len(), 70_000);
    let serialized_summarize =
        serde_json::to_value(summarize_output).expect("summarize output should serialize");
    let mut summarize_keys = serialized_summarize
        .as_object()
        .expect("summarize output should be object")
        .keys()
        .cloned()
        .collect::<Vec<_>>();
    summarize_keys.sort();
    assert_eq!(summarize_keys, vec!["markdown", "source_url", "text"]);

    client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    handle.await.expect("server join should succeed");
}

#[tokio::test]
async fn tool_errors_are_mapped_for_auth_upstream_parse_and_transport_failures() {
    let mut auth_server = Server::new();
    let _auth_mock = auth_server
        .mock("GET", "/api/v0/search")
        .match_query(Matcher::UrlEncoded("q".into(), "rust".into()))
        .with_status(401)
        .with_body("bad token secret_auth_token")
        .create();

    let auth_backend = build_official_backend(&auth_server);
    let (auth_client, auth_handle) = start_server(auth_backend).await;
    let auth_result = auth_client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "rust"
            }))),
        )
        .await
        .expect("auth failure should still return tool result");
    assert_eq!(auth_result.is_error, Some(true));
    let auth_text = auth_result
        .content
        .first()
        .and_then(|content| content.as_text())
        .map(|content| content.text.clone())
        .expect("auth failure should return text error");
    assert!(auth_text.contains("Authentication failed"));
    assert!(auth_text.contains(ENV_API_KEY));
    assert!(auth_text.contains(ENV_SESSION_TOKEN));
    assert!(!auth_text.contains("secret_auth_token"));
    auth_client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    auth_handle.await.expect("server join should succeed");

    let mut upstream_server = Server::new();
    let _upstream_mock = upstream_server
        .mock("GET", "/api/v0/search")
        .match_query(Matcher::UrlEncoded("q".into(), "rust".into()))
        .with_status(429)
        .with_header("content-type", "application/json")
        .with_body(r#"{"error":"rate limited"}"#)
        .create();

    let upstream_backend = build_official_backend(&upstream_server);
    let (upstream_client, upstream_handle) = start_server(upstream_backend).await;
    let upstream_result = upstream_client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "rust"
            }))),
        )
        .await
        .expect("upstream failure should still return tool result");
    let upstream_text = upstream_result
        .content
        .first()
        .and_then(|content| content.as_text())
        .map(|content| content.text.clone())
        .expect("upstream failure should return text error");
    assert!(upstream_text.contains("HTTP 429"));
    upstream_client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    upstream_handle.await.expect("server join should succeed");

    let mut parse_server = Server::new();
    let _parse_mock = parse_server
        .mock("GET", "/api/v0/search")
        .match_query(Matcher::UrlEncoded("q".into(), "rust".into()))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(r#"{"data":{"unexpected":[]}}"#)
        .create();

    let parse_backend = build_official_backend(&parse_server);
    let (parse_client, parse_handle) = start_server(parse_backend).await;
    let parse_result = parse_client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "rust"
            }))),
        )
        .await
        .expect("parse drift should still return tool result");
    let parse_text = parse_result
        .content
        .first()
        .and_then(|content| content.as_text())
        .map(|content| content.text.clone())
        .expect("parse drift should return text error");
    assert!(parse_text.contains("unexpected response shape"));
    parse_client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    parse_handle.await.expect("server join should succeed");

    let transport_backend = BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("official".to_string()),
            api_key: Some("official_token".to_string()),
            session_token: None,
        },
        ClientConfig::default()
            .with_base_url(Url::parse("http://127.0.0.1:9").expect("url should parse"))
            .with_timeout(Duration::from_millis(50)),
    )
    .expect("transport backend should build");

    let (transport_client, transport_handle) = start_server(transport_backend).await;
    let transport_result = transport_client
        .call_tool(
            CallToolRequestParams::new("kagi_search").with_arguments(json_object(json!({
                "query": "rust"
            }))),
        )
        .await
        .expect("transport failure should still return tool result");
    let transport_text = transport_result
        .content
        .first()
        .and_then(|content| content.as_text())
        .map(|content| content.text.clone())
        .expect("transport failure should return text error");
    assert!(transport_text.contains("transport") || transport_text.contains("timed out"));

    transport_client
        .cancel()
        .await
        .expect("client shutdown should succeed");
    transport_handle.await.expect("server join should succeed");
}

#[tokio::test]
async fn session_summarize_http_200_auth_like_failures_map_to_auth_message() {
    for unauthorized_payload in [
        r#"{"error":"Unauthorized"}"#,
        r#"{"success":false,"message":"Unauthorized: Unauthorized"}"#,
    ] {
        let mut server = Server::new();
        let _summarize_mock = server
            .mock("GET", "/mother/summary_labs")
            .match_query(Matcher::UrlEncoded(
                "url".into(),
                "https://example.com/protected".into(),
            ))
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(unauthorized_payload)
            .create();

        let backend = build_session_backend(&server);
        let (client, handle) = start_server(backend).await;
        let summarize_result = client
            .call_tool(
                CallToolRequestParams::new("kagi_summarize").with_arguments(json_object(json!({
                    "url": "https://example.com/protected"
                }))),
            )
            .await
            .expect("auth-like summarize failure should still return tool result");

        assert_eq!(summarize_result.is_error, Some(true));
        let summarize_error_text = summarize_result
            .content
            .first()
            .and_then(|content| content.as_text())
            .map(|content| content.text.clone())
            .expect("auth-like summarize failure should return text error");

        assert!(summarize_error_text.contains("Authentication failed with Kagi"));
        assert!(summarize_error_text.contains(ENV_SESSION_TOKEN));
        assert!(summarize_error_text.contains(ENV_API_KEY));
        assert!(summarize_error_text.contains("may belong"));
        assert!(!summarize_error_text.contains("application-level failure"));

        client
            .cancel()
            .await
            .expect("client shutdown should succeed");
        handle.await.expect("server join should succeed");
    }
}

#[test]
fn startup_backend_selection_and_failures_match_contract() {
    let default_config = ClientConfig::default();

    let auto_prefers_official = BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("auto".to_string()),
            api_key: Some("official_token".to_string()),
            session_token: Some("session_token".to_string()),
        },
        default_config.clone(),
    )
    .expect("auto mode should prefer api key");
    assert!(matches!(auto_prefers_official, BackendRuntime::Official(_)));

    let auto_falls_back_to_session = BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("auto".to_string()),
            api_key: None,
            session_token: Some("session_token".to_string()),
        },
        default_config.clone(),
    )
    .expect("auto mode should use session when api key missing");
    assert!(matches!(
        auto_falls_back_to_session,
        BackendRuntime::Session(_)
    ));

    let missing = BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("auto".to_string()),
            api_key: None,
            session_token: None,
        },
        default_config.clone(),
    )
    .expect_err("auto mode without any credential must fail");
    assert!(missing.to_string().contains(ENV_API_KEY));

    let missing_official_with_session_present = BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("official".to_string()),
            api_key: None,
            session_token: Some("session_token".to_string()),
        },
        default_config.clone(),
    )
    .expect_err("official mode without api key must fail");
    let missing_official_message = missing_official_with_session_present.to_string();
    assert!(missing_official_message.contains(ENV_API_KEY));
    assert!(missing_official_message.contains(ENV_SESSION_TOKEN));
    assert!(missing_official_message.contains("may belong"));
    assert!(missing_official_message.contains("session"));

    let missing_session_with_api_present = BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("session".to_string()),
            api_key: Some("official_token".to_string()),
            session_token: None,
        },
        default_config.clone(),
    )
    .expect_err("session mode without session token must fail");
    let missing_session_message = missing_session_with_api_present.to_string();
    assert!(missing_session_message.contains(ENV_SESSION_TOKEN));
    assert!(missing_session_message.contains(ENV_API_KEY));
    assert!(missing_session_message.contains("may belong"));
    assert!(missing_session_message.contains("official"));

    let invalid_mode = BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("invalid".to_string()),
            api_key: Some("official_token".to_string()),
            session_token: None,
        },
        default_config.clone(),
    )
    .expect_err("invalid backend mode must fail");
    assert!(invalid_mode.to_string().contains(ENV_BACKEND_MODE));

    let invalid_official_credential = BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("official".to_string()),
            api_key: Some("secret official token".to_string()),
            session_token: None,
        },
        default_config.clone(),
    )
    .expect_err("invalid official credential must fail");
    let invalid_official_message = invalid_official_credential.to_string();
    assert!(invalid_official_message.contains(ENV_API_KEY));
    assert!(!invalid_official_message.contains("secret official token"));

    let invalid_session_credential = BackendRuntime::from_env_config(
        EnvConfig {
            backend_mode: Some("session".to_string()),
            api_key: None,
            session_token: Some("secret session token".to_string()),
        },
        default_config,
    )
    .expect_err("invalid session credential must fail");
    let invalid_session_message = invalid_session_credential.to_string();
    assert!(invalid_session_message.contains(ENV_SESSION_TOKEN));
    assert!(!invalid_session_message.contains("secret session token"));
}