trusty-analyze 0.1.6

Sidecar code-analysis daemon for trusty-search: complexity, smells, quality, facts
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
//! MCP (Model Context Protocol) server for trusty-analyzer.
//!
//! Why: full parity with the HTTP surface so an MCP client gets the same
//! capabilities as a curl user. The dispatcher is a pure translator — JSON-RPC
//! in, HTTP out — and owns no state beyond a `reqwest::Client` and the
//! analyzer daemon's base URL.
//!
//! Tools (mirrors `trusty-analyzer-service`):
//!
//! | MCP tool              | Daemon endpoint                              |
//! |-----------------------|----------------------------------------------|
//! | `complexity_hotspots` | `GET /indexes/:id/complexity_hotspots`       |
//! | `find_smells`         | `GET /indexes/:id/smells`                    |
//! | `analyze_quality`     | `GET /indexes/:id/quality`                   |
//! | `list_facts`          | `GET /facts`                                 |
//! | `upsert_fact`         | `POST /facts`                                |
//! | `delete_fact`         | `DELETE /facts/:id`                          |
//! | `cluster_concepts`    | `GET /indexes/:id/clusters`                  |
//! | `ingest_scip`         | `POST /indexes/:id/scip`                     |
//! | `analyzer_health`     | `GET /health`                                |

pub mod sse;
pub mod stdio;

use serde::{Deserialize, Serialize};
use serde_json::Value;

pub mod error_codes {
    pub const INVALID_REQUEST: i32 = -32600;
    pub const METHOD_NOT_FOUND: i32 = -32601;
    pub const INVALID_PARAMS: i32 = -32602;
    pub const INTERNAL_ERROR: i32 = -32603;
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Request {
    pub jsonrpc: String,
    pub id: Option<Value>,
    pub method: String,
    #[serde(default)]
    pub params: Value,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Response {
    pub jsonrpc: String,
    pub id: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<JsonRpcError>,
    #[serde(skip)]
    pub suppress: bool,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct JsonRpcError {
    pub code: i32,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Value>,
}

impl Response {
    pub fn ok(id: Value, result: Value) -> Self {
        Self {
            jsonrpc: "2.0".into(),
            id,
            result: Some(result),
            error: None,
            suppress: false,
        }
    }

    pub fn err(id: Value, code: i32, message: impl Into<String>) -> Self {
        Self {
            jsonrpc: "2.0".into(),
            id,
            result: None,
            error: Some(JsonRpcError {
                code,
                message: message.into(),
                data: None,
            }),
            suppress: false,
        }
    }

    pub fn suppressed() -> Self {
        Self {
            jsonrpc: "2.0".into(),
            id: Value::Null,
            result: None,
            error: None,
            suppress: true,
        }
    }
}

/// MCP dispatcher backed by an HTTP client targeting the analyzer daemon.
#[derive(Clone)]
pub struct AnalyzerMcpServer {
    base_url: String,
    http: reqwest::Client,
}

impl AnalyzerMcpServer {
    pub fn new(base_url: impl Into<String>) -> Self {
        Self {
            base_url: base_url.into(),
            http: reqwest::Client::new(),
        }
    }

    pub fn base_url(&self) -> &str {
        &self.base_url
    }

    /// Translate one JSON-RPC request into a daemon HTTP call. Always returns
    /// a `Response`; transport / daemon failures are reported in-band.
    pub async fn dispatch(&self, req: Request) -> Response {
        let is_notification = req.id.is_none();
        let id = req.id.clone().unwrap_or(Value::Null);

        if req.jsonrpc != "2.0" {
            if is_notification {
                return Response::suppressed();
            }
            return Response::err(id, error_codes::INVALID_REQUEST, "jsonrpc must be \"2.0\"");
        }

        match req.method.as_str() {
            "initialize" => {
                return Response::ok(
                    id,
                    serde_json::json!({
                        "protocolVersion": "2024-11-05",
                        "capabilities": { "tools": {}, "resources": {} },
                        "serverInfo": {
                            "name": "trusty-analyzer",
                            "version": env!("CARGO_PKG_VERSION"),
                        }
                    }),
                );
            }
            "notifications/initialized" | "initialized" => {
                return Response::suppressed();
            }
            "resources/list" => {
                return self.list_resources(id).await;
            }
            _ => {}
        }

        let (tool, arguments, via_tools_call) = match req.method.as_str() {
            "tools/call" => {
                let name = req
                    .params
                    .get("name")
                    .and_then(Value::as_str)
                    .map(str::to_owned);
                let args = req
                    .params
                    .get("arguments")
                    .cloned()
                    .unwrap_or(Value::Object(Default::default()));
                match name {
                    Some(n) => (n, args, true),
                    None => {
                        return Response::err(
                            id,
                            error_codes::INVALID_PARAMS,
                            "tools/call requires a 'name' field",
                        )
                    }
                }
            }
            "tools/list" => {
                return Response::ok(id, serde_json::json!({ "tools": tool_descriptors() }));
            }
            other => (other.to_string(), req.params.clone(), false),
        };

        let outcome = self.call_tool(&tool, &arguments).await;

        if via_tools_call {
            match outcome {
                Ok(value) => Response::ok(id, wrap_tool_result(&value)),
                Err(DispatchError::UnknownTool) => Response::err(
                    id,
                    error_codes::METHOD_NOT_FOUND,
                    format!("unknown tool: {tool}"),
                ),
                Err(DispatchError::InvalidParams(msg)) => Response::ok(id, wrap_tool_error(&msg)),
                Err(DispatchError::Transport(msg)) => Response::ok(id, wrap_tool_error(&msg)),
            }
        } else {
            match outcome {
                Ok(value) => Response::ok(id, wrap_text_content(&value)),
                Err(DispatchError::UnknownTool) => Response::err(
                    id,
                    error_codes::METHOD_NOT_FOUND,
                    format!("unknown tool: {tool}"),
                ),
                Err(DispatchError::InvalidParams(msg)) => {
                    Response::err(id, error_codes::INVALID_PARAMS, msg)
                }
                Err(DispatchError::Transport(msg)) => {
                    Response::err(id, error_codes::INTERNAL_ERROR, msg)
                }
            }
        }
    }

    /// Handle the JSON-RPC `resources/list` method.
    ///
    /// Why: MCP clients enumerate resources to discover what context a server
    /// can expose. The analyzer exposes each trusty-search index as a resource
    /// so clients can see, at a glance, what is available for analysis.
    /// What: calls `GET /indexes` on the daemon, maps each index ID to an MCP
    /// resource descriptor (`trusty-analyzer://indexes/{id}`), and returns the
    /// `{ resources: [...] }` envelope. A daemon failure surfaces as an empty
    /// list rather than an error so the client still initializes cleanly.
    /// Test: `resources_list_returns_envelope` checks the shape when the daemon
    /// is unreachable (empty list).
    async fn list_resources(&self, id: Value) -> Response {
        let resources = match self.get("/indexes").await {
            Ok(value) => {
                // GET /indexes returns `[{ "id": "..." }, ...]`.
                let ids: Vec<String> = value
                    .as_array()
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.get("id").and_then(Value::as_str).map(str::to_owned))
                            .collect()
                    })
                    .unwrap_or_default();
                ids.into_iter()
                    .map(|index_id| {
                        serde_json::json!({
                            "uri": format!("trusty-analyzer://indexes/{index_id}"),
                            "name": format!("Index: {index_id}"),
                            "description": "trusty-search index available for analysis",
                            "mimeType": "application/json",
                        })
                    })
                    .collect::<Vec<_>>()
            }
            Err(e) => {
                tracing::warn!("resources/list: GET /indexes failed: {e:?}");
                Vec::new()
            }
        };
        Response::ok(id, serde_json::json!({ "resources": resources }))
    }

    /// Top-level tool dispatch. Each tool delegates to a `handle_<tool>`
    /// function that owns parameter parsing and HTTP call construction.
    ///
    /// Why: A 130-line match block hid the per-tool logic. Per-handler
    /// functions cap dispatch cyclo at the number of tools and let each
    /// handler be tested without going through the JSON-RPC envelope.
    /// What: Looks up the tool name and forwards `(args, self)` to the
    /// handler.
    /// Test: `unknown_tool_returns_method_not_found` covers the fall-through
    /// arm; `handle_analyzer_health_calls_health_endpoint` exercises one
    /// handler directly.
    async fn call_tool(&self, tool: &str, args: &Value) -> Result<Value, DispatchError> {
        match tool {
            "complexity_hotspots" => self.handle_complexity_hotspots(args).await,
            "find_smells" => self.handle_find_smells(args).await,
            "analyze_quality" => self.handle_analyze_quality(args).await,
            "run_diagnostics" => self.handle_run_diagnostics(args).await,
            "list_facts" => self.handle_list_facts(args).await,
            "upsert_fact" => self.handle_upsert_fact(args).await,
            "delete_fact" => self.handle_delete_fact(args).await,
            "extract_graph" => self.handle_extract_graph(args).await,
            "list_entities" => self.handle_list_entities(args).await,
            "cluster_concepts" => self.handle_cluster_concepts(args).await,
            "analyzer_health" => self.handle_analyzer_health(args).await,
            "ingest_scip" => self.handle_ingest_scip(args).await,
            "extract_ner" => self.handle_extract_ner(args).await,
            "suggest_refactors" => self.handle_suggest_refactors(args).await,
            "review_diff" => self.handle_review_diff(args).await,
            "review_github_pr" => self.handle_review_github_pr(args).await,
            "deep_analysis" => self.handle_deep_analysis(args).await,
            _ => Err(DispatchError::UnknownTool),
        }
    }

    async fn handle_complexity_hotspots(&self, args: &Value) -> Result<Value, DispatchError> {
        let index_id = index_id_or_default(args);
        let top_n = args.get("top_n").and_then(Value::as_u64).unwrap_or(20);
        self.get(&format!(
            "/indexes/{index_id}/complexity_hotspots?top_n={top_n}"
        ))
        .await
    }

    async fn handle_find_smells(&self, args: &Value) -> Result<Value, DispatchError> {
        let index_id = index_id_or_default(args);
        self.get(&format!("/indexes/{index_id}/smells")).await
    }

    async fn handle_analyze_quality(&self, args: &Value) -> Result<Value, DispatchError> {
        let index_id = index_id_or_default(args);
        self.get(&format!("/indexes/{index_id}/quality")).await
    }

    /// Handle the `run_diagnostics` tool: forward to
    /// `GET /indexes/{id}/diagnostics`, which runs the discovered external
    /// static-analysis tools (clippy, ruff, biome, ...) on demand.
    async fn handle_run_diagnostics(&self, args: &Value) -> Result<Value, DispatchError> {
        let index_id = index_id_or_default(args);
        let q = build_query(args, &["language", "tools"]);
        self.get(&format!("/indexes/{index_id}/diagnostics{q}"))
            .await
    }

    async fn handle_list_facts(&self, args: &Value) -> Result<Value, DispatchError> {
        let q = build_query(args, &["subject", "predicate", "object"]);
        self.get(&format!("/facts{q}")).await
    }

    async fn handle_upsert_fact(&self, args: &Value) -> Result<Value, DispatchError> {
        let subject = require_str(args, "subject")?;
        let predicate = require_str(args, "predicate")?;
        let object = require_str(args, "object")?;
        let index_id = require_str(args, "index_id")?;
        let confidence = args
            .get("confidence")
            .and_then(Value::as_f64)
            .unwrap_or(1.0);
        let provenance = args
            .get("provenance")
            .cloned()
            .unwrap_or_else(|| Value::Array(vec![]));
        let body = serde_json::json!({
            "subject": subject,
            "predicate": predicate,
            "object": object,
            "index_id": index_id,
            "confidence": confidence,
            "provenance": provenance,
        });
        self.post("/facts", &body).await
    }

    async fn handle_delete_fact(&self, args: &Value) -> Result<Value, DispatchError> {
        let id = args
            .get("id")
            .and_then(Value::as_u64)
            .ok_or_else(|| DispatchError::InvalidParams("missing 'id' (u64)".into()))?;
        self.delete(&format!("/facts/{id}")).await
    }

    async fn handle_extract_graph(&self, args: &Value) -> Result<Value, DispatchError> {
        let index_id = index_id_or_default(args);
        let mut path = format!("/indexes/{index_id}/graph");
        if let Some(lang) = args.get("language").and_then(Value::as_str) {
            path.push_str(&format!("?language={}", urlencode(lang)));
        }
        self.get(&path).await
    }

    async fn handle_list_entities(&self, args: &Value) -> Result<Value, DispatchError> {
        let index_id = index_id_or_default(args);
        let q = build_query(args, &["kind", "language"]);
        self.get(&format!("/indexes/{index_id}/entities{q}")).await
    }

    async fn handle_cluster_concepts(&self, args: &Value) -> Result<Value, DispatchError> {
        let index_id = index_id_or_default(args);
        let k = args.get("k").and_then(Value::as_u64).unwrap_or(8);
        let path = match args.get("method").and_then(Value::as_str) {
            Some(m) => format!("/indexes/{index_id}/clusters?k={k}&method={m}"),
            None => format!("/indexes/{index_id}/clusters?k={k}"),
        };
        self.get(&path).await
    }

    async fn handle_analyzer_health(&self, _args: &Value) -> Result<Value, DispatchError> {
        self.get("/health").await
    }

    async fn handle_suggest_refactors(&self, args: &Value) -> Result<Value, DispatchError> {
        let index_id = index_id_or_default(args);
        let top_k = args.get("top_k").and_then(Value::as_u64).unwrap_or(20);
        let mut path = format!("/indexes/{index_id}/refactor-suggestions?top_k={top_k}");
        if let Some(file) = args.get("file").and_then(Value::as_str) {
            path.push_str(&format!("&file={}", urlencode(file)));
        }
        if let Some(sev) = args.get("min_severity").and_then(Value::as_str) {
            path.push_str(&format!("&min_severity={}", urlencode(sev)));
        }
        self.get(&path).await
    }

    async fn handle_extract_ner(&self, args: &Value) -> Result<Value, DispatchError> {
        let index_id = index_id_or_default(args);
        let top_k = args.get("top_k").and_then(Value::as_u64).unwrap_or(50);
        self.get(&format!("/indexes/{index_id}/ner?top_k={top_k}"))
            .await
    }

    async fn handle_ingest_scip(&self, args: &Value) -> Result<Value, DispatchError> {
        use base64::Engine;
        let index_id = index_id_or_default(args);
        let b64 = require_str(args, "scip_base64")?;
        let bytes = base64::engine::general_purpose::STANDARD
            .decode(b64)
            .map_err(|e| {
                DispatchError::InvalidParams(format!("scip_base64 is not valid base64: {e}"))
            })?;
        self.post_bytes(&format!("/indexes/{index_id}/scip"), bytes)
            .await
    }

    /// Handle the `review_diff` tool: forward a unified diff to `POST /review`.
    ///
    /// Why: parity with the `POST /review` endpoint so MCP clients (Claude
    /// Code) can ask for a PR review without shelling out. Like every other
    /// analyzer tool, review is backed by trusty-search: the daemon fetches the
    /// named index's chunk corpus to cross-reference the diff.
    /// What: requires a `diff` string param and an `index_id` string param,
    /// and POSTs the diff as `text/x-patch` to `/review?index_id=...`.
    /// Test: `review_diff_requires_diff_param` and
    /// `review_diff_requires_index_id` check the missing-param paths.
    async fn handle_review_diff(&self, args: &Value) -> Result<Value, DispatchError> {
        let diff = require_str(args, "diff")?;
        let index_id = require_str(args, "index_id")?;
        let path = format!("/review?index_id={}", urlencode(index_id));
        self.post_text(&path, diff).await
    }

    /// Handle the `deep_analysis` MCP tool: forward to `POST /analyze/deep`.
    ///
    /// Why: pairs with the [`POST /analyze/deep`] HTTP endpoint so MCP clients
    /// can opt into the LLM-augmented analysis without going through the
    /// deterministic `review_diff` path. Keeps the two surfaces separate so
    /// `review_diff` remains cheap and deterministic.
    /// What: requires `index_id`; optional `model` overrides the daemon
    /// default. POSTs a JSON body shaped like [`DeepAnalyzeRequest`] and
    /// returns the [`DeepAnalysisReport`] JSON.
    /// Test: `deep_analysis_requires_index_id` and
    /// `deep_analysis_posts_to_endpoint` cover param + URL construction.
    async fn handle_deep_analysis(&self, args: &Value) -> Result<Value, DispatchError> {
        let index_id = require_str(args, "index_id")?;
        let mut body = serde_json::json!({ "index_id": index_id });
        if let Some(model) = args.get("model").and_then(Value::as_str) {
            body["model"] = Value::from(model);
        }
        // The HTTP endpoint accepts an optional pre-computed `report`; the MCP
        // tool surface deliberately keeps the schema minimal (index_id +
        // model) — re-running the synthesis on the daemon is the simpler
        // ergonomics for AI clients.
        self.post("/analyze/deep", &body).await
    }

    /// Handle the `review_github_pr` tool: forward to `POST /review/github-pr`.
    ///
    /// Why: parity with the HTTP endpoint so MCP clients can review a GitHub PR
    /// by number. The daemon owns the GitHub token and the fetch/analyze/comment
    /// pipeline; the MCP server is a pure translator.
    /// What: requires `owner`, `repo`, `pr`, and `index_id`; `post_comment` is
    /// optional (default false). POSTs a `GithubPrRequest`-shaped JSON body.
    /// Test: `review_github_pr_requires_owner` checks the missing-param path.
    async fn handle_review_github_pr(&self, args: &Value) -> Result<Value, DispatchError> {
        let owner = require_str(args, "owner")?;
        let repo = require_str(args, "repo")?;
        let pr = args
            .get("pr")
            .and_then(Value::as_u64)
            .ok_or_else(|| DispatchError::InvalidParams("missing or non-integer 'pr'".into()))?;
        let index_id = require_str(args, "index_id")?;
        let post_comment = args
            .get("post_comment")
            .and_then(Value::as_bool)
            .unwrap_or(false);
        let body = serde_json::json!({
            "owner": owner,
            "repo": repo,
            "pr": pr,
            "index_id": index_id,
            "post_comment": post_comment,
        });
        self.post("/review/github-pr", &body).await
    }

    async fn post_text(&self, path: &str, body: &str) -> Result<Value, DispatchError> {
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .post(&url)
            .header("content-type", "text/x-patch")
            .body(body.to_string())
            .send()
            .await
            .map_err(|e| DispatchError::Transport(format!("POST {url}: {e}")))?;
        let status = resp.status();
        let body: Value = resp
            .json()
            .await
            .map_err(|e| DispatchError::Transport(format!("decode {url}: {e}")))?;
        if !status.is_success() {
            return Err(DispatchError::Transport(format!(
                "POST {url} returned {status}: {body}"
            )));
        }
        Ok(body)
    }

    async fn post_bytes(&self, path: &str, body: Vec<u8>) -> Result<Value, DispatchError> {
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .post(&url)
            .header("content-type", "application/octet-stream")
            .body(body)
            .send()
            .await
            .map_err(|e| DispatchError::Transport(format!("POST {url}: {e}")))?;
        let status = resp.status();
        let body: Value = resp
            .json()
            .await
            .map_err(|e| DispatchError::Transport(format!("decode {url}: {e}")))?;
        if !status.is_success() {
            return Err(DispatchError::Transport(format!(
                "POST {url} returned {status}: {body}"
            )));
        }
        Ok(body)
    }

    async fn get(&self, path: &str) -> Result<Value, DispatchError> {
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .get(&url)
            .send()
            .await
            .map_err(|e| DispatchError::Transport(format!("GET {url}: {e}")))?;
        let status = resp.status();
        let body: Value = resp
            .json()
            .await
            .map_err(|e| DispatchError::Transport(format!("decode {url}: {e}")))?;
        if !status.is_success() {
            return Err(DispatchError::Transport(format!(
                "GET {url} returned {status}: {body}"
            )));
        }
        Ok(body)
    }

    async fn post(&self, path: &str, body: &Value) -> Result<Value, DispatchError> {
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .post(&url)
            .json(body)
            .send()
            .await
            .map_err(|e| DispatchError::Transport(format!("POST {url}: {e}")))?;
        let status = resp.status();
        let body: Value = resp
            .json()
            .await
            .map_err(|e| DispatchError::Transport(format!("decode {url}: {e}")))?;
        if !status.is_success() {
            return Err(DispatchError::Transport(format!(
                "POST {url} returned {status}: {body}"
            )));
        }
        Ok(body)
    }

    async fn delete(&self, path: &str) -> Result<Value, DispatchError> {
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .http
            .delete(&url)
            .send()
            .await
            .map_err(|e| DispatchError::Transport(format!("DELETE {url}: {e}")))?;
        let status = resp.status();
        let body: Value = resp
            .json()
            .await
            .map_err(|e| DispatchError::Transport(format!("decode {url}: {e}")))?;
        if !status.is_success() {
            return Err(DispatchError::Transport(format!(
                "DELETE {url} returned {status}: {body}"
            )));
        }
        Ok(body)
    }
}

#[derive(Debug)]
enum DispatchError {
    UnknownTool,
    InvalidParams(String),
    Transport(String),
}

fn require_str<'a>(args: &'a Value, key: &str) -> Result<&'a str, DispatchError> {
    args.get(key)
        .and_then(Value::as_str)
        .ok_or_else(|| DispatchError::InvalidParams(format!("missing or non-string '{key}'")))
}

/// Read `index` (preferred) or `index_id` (legacy alias) from `args`,
/// falling back to `"default"`.
///
/// Why: Multiple tools accept either parameter name and need the same
/// fallback behaviour; centralising removes 9 copies of the same chain.
/// What: Tries `index`, then `index_id`, then `"default"`.
/// Test: Covered indirectly by every per-tool handler test.
fn index_id_or_default(args: &Value) -> &str {
    args.get("index")
        .or_else(|| args.get("index_id"))
        .and_then(Value::as_str)
        .unwrap_or("default")
}

/// Build a `?key=val&...` query string from whichever of `keys` is present
/// in `args` (skipping missing or non-string values). Returns an empty
/// string if no keys were found.
fn build_query(args: &Value, keys: &[&str]) -> String {
    let mut q = String::new();
    for key in keys {
        if let Some(v) = args.get(*key).and_then(Value::as_str) {
            let sep = if q.is_empty() { '?' } else { '&' };
            q.push(sep);
            q.push_str(key);
            q.push('=');
            q.push_str(&urlencode(v));
        }
    }
    q
}

/// Minimal URL encoding for the bits we pass through to `/facts?subject=...`.
/// Avoids pulling a full url crate into the MCP server.
fn urlencode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(b as char);
            }
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

fn wrap_text_content(value: &Value) -> Value {
    serde_json::json!({
        "content": [{
            "type": "text",
            "text": serde_json::to_string_pretty(value).unwrap_or_else(|_| value.to_string()),
        }]
    })
}

fn wrap_tool_result(value: &Value) -> Value {
    serde_json::json!({
        "content": [{
            "type": "text",
            "text": serde_json::to_string_pretty(value).unwrap_or_else(|_| value.to_string()),
        }],
        "isError": false,
    })
}

fn wrap_tool_error(msg: &str) -> Value {
    serde_json::json!({
        "content": [{ "type": "text", "text": format!("Error: {msg}") }],
        "isError": true,
    })
}

pub fn tool_descriptors() -> Value {
    serde_json::json!([
        {
            "name": "complexity_hotspots",
            "description": "Top-N chunks ranked by cyclomatic complexity",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "index": { "type": "string" },
                    "index_id": { "type": "string" },
                    "top_n": { "type": "number" }
                }
            }
        },
        {
            "name": "find_smells",
            "description": "Chunks with at least one detected code smell",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "index": { "type": "string" },
                    "index_id": { "type": "string" }
                }
            }
        },
        {
            "name": "analyze_quality",
            "description": "Aggregate quality stats: avg cyclomatic, %A, smell count",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "index": { "type": "string" },
                    "index_id": { "type": "string" }
                }
            }
        },
        {
            "name": "run_diagnostics",
            "description": "Run available external static-analysis tools (clippy, ruff, biome, staticcheck, pmd, rubocop, phpstan, swiftlint, detekt, clang-tidy) across the index corpus on demand. Tools are auto-discovered: only installed binaries run. Returns normalized diagnostics with file, line, severity, rule code, and message.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "index":    { "type": "string" },
                    "index_id": { "type": "string" },
                    "language": { "type": "string", "description": "Optional: restrict to one language tag (rust, python, typescript, go, java, ruby, php, swift, kotlin, cpp)" },
                    "tools":    { "type": "string", "description": "Optional: comma-separated list of tool names to run; defaults to all available" }
                }
            }
        },
        {
            "name": "list_facts",
            "description": "List canonical facts, optionally filtered by subject/predicate/object",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "subject":   { "type": "string" },
                    "predicate": { "type": "string" },
                    "object":    { "type": "string" }
                }
            }
        },
        {
            "name": "upsert_fact",
            "description": "Insert or update a canonical fact triple",
            "inputSchema": {
                "type": "object",
                "required": ["subject", "predicate", "object", "index_id"],
                "properties": {
                    "subject":    { "type": "string" },
                    "predicate":  { "type": "string" },
                    "object":     { "type": "string" },
                    "index_id":   { "type": "string" },
                    "confidence": { "type": "number" },
                    "provenance": { "type": "array", "items": { "type": "string" } }
                }
            }
        },
        {
            "name": "delete_fact",
            "description": "Delete a fact by its u64 id",
            "inputSchema": {
                "type": "object",
                "required": ["id"],
                "properties": { "id": { "type": "number" } }
            }
        },
        {
            "name": "analyzer_health",
            "description": "Probe analyzer daemon liveness and version",
            "inputSchema": { "type": "object", "properties": {} }
        },
        {
            "name": "extract_graph",
            "description": "Build the multi-language knowledge graph (nodes + edges) for an index",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "index":    { "type": "string" },
                    "index_id": { "type": "string" },
                    "language": { "type": "string" }
                }
            }
        },
        {
            "name": "cluster_concepts",
            "description": "Group chunks into concept clusters using k-means over embeddings (BOW or neural)",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "index":    { "type": "string" },
                    "index_id": { "type": "string" },
                    "k":        { "type": "number" },
                    "method":   { "type": "string", "description": "Embedding method: 'bow' (default, fast) or 'neural' (semantic, requires fastembed model)" }
                }
            }
        },
        {
            "name": "ingest_scip",
            "description": "Ingest a SCIP (Scalable and Precise Index for Code) protobuf index for a given index_id, enriching the knowledge graph with fully-resolved symbols and cross-file relationships. The SCIP bytes must be base64-encoded.",
            "inputSchema": {
                "type": "object",
                "required": ["scip_base64"],
                "properties": {
                    "index":        { "type": "string" },
                    "index_id":     { "type": "string" },
                    "scip_base64":  { "type": "string", "description": "Base64-encoded SCIP Index protobuf payload" }
                }
            }
        },
        {
            "name": "extract_ner",
            "description": "Extract named entities from doc comments for a code index using NER",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "index":    { "type": "string" },
                    "index_id": { "type": "string", "description": "Index ID" },
                    "top_k":    { "type": "integer", "description": "Max entities to return", "default": 50 }
                }
            }
        },
        {
            "name": "suggest_refactors",
            "description": "Suggest concrete refactoring actions (extract method, reduce nesting, ...) ranked by severity, derived from complexity metrics and code smells",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "index":        { "type": "string" },
                    "index_id":     { "type": "string" },
                    "file":         { "type": "string", "description": "Optional path filter — restrict suggestions to one file" },
                    "min_severity": { "type": "string", "description": "Minimum severity: 'low' (default), 'medium', 'high', 'critical'" },
                    "top_k":        { "type": "number", "description": "Cap on suggestions returned (default 20)" }
                }
            }
        },
        {
            "name": "review_diff",
            "description": "Review a unified git diff and return a structured quality report (per-file complexity, code smells, grade A-F, recommendations). Cross-references the diff against the trusty-search index corpus, so trusty-search must be running. Deterministic and LLM-free — use the deep_analysis tool for LLM-augmented narrative.",
            "inputSchema": {
                "type": "object",
                "required": ["diff", "index_id"],
                "properties": {
                    "diff":     { "type": "string", "description": "Unified git diff text to review" },
                    "index_id": { "type": "string", "description": "Index ID to cross-reference the diff against in trusty-search" }
                }
            }
        },
        {
            "name": "deep_analysis",
            "description": "Run an LLM-augmented deep analysis pass over an index: synthesises a deterministic review report from the indexed corpus, looks up detected frameworks, and asks an OpenRouter model for a prose narrative plus framework-aware recommendations. Requires OPENROUTER_API_KEY configured on the daemon.",
            "inputSchema": {
                "type": "object",
                "required": ["index_id"],
                "properties": {
                    "index_id": { "type": "string", "description": "trusty-search index ID to analyse" },
                    "model":    { "type": "string", "description": "Optional OpenRouter model id (e.g. 'openai/gpt-4o-mini'); falls back to TRUSTY_LLM_MODEL on the daemon" }
                }
            }
        },
        {
            "name": "review_github_pr",
            "description": "Fetch a GitHub pull request's unified diff and run a structured quality review against a trusty-search index. Requires GITHUB_TOKEN set on the daemon. Optionally posts the review back as a PR comment.",
            "inputSchema": {
                "type": "object",
                "required": ["owner", "repo", "pr", "index_id"],
                "properties": {
                    "owner":        { "type": "string", "description": "Repository owner (user or org)" },
                    "repo":         { "type": "string", "description": "Repository name" },
                    "pr":           { "type": "integer", "description": "Pull request number" },
                    "index_id":     { "type": "string", "description": "trusty-search index ID to cross-reference" },
                    "post_comment": { "type": "boolean", "description": "Post the review back as a PR comment (default false)", "default": false }
                }
            }
        },
        {
            "name": "list_entities",
            "description": "List symbol-level entities (functions, classes, ...) for an index",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "index":    { "type": "string" },
                    "index_id": { "type": "string" },
                    "kind":     { "type": "string" },
                    "language": { "type": "string" }
                }
            }
        }
    ])
}

#[cfg(test)]
mod tests {
    use super::*;

    fn req(method: &str, params: Value) -> Request {
        Request {
            jsonrpc: "2.0".into(),
            id: Some(Value::from(1u64)),
            method: method.into(),
            params,
        }
    }

    #[tokio::test]
    async fn tools_list_contains_full_surface() {
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let resp = server.dispatch(req("tools/list", Value::Null)).await;
        let result = resp.result.expect("expected result");
        let tools = result
            .get("tools")
            .and_then(Value::as_array)
            .expect("array");
        let names: Vec<&str> = tools
            .iter()
            .filter_map(|t| t.get("name").and_then(Value::as_str))
            .collect();
        for required in [
            "complexity_hotspots",
            "find_smells",
            "analyze_quality",
            "run_diagnostics",
            "list_facts",
            "upsert_fact",
            "delete_fact",
            "analyzer_health",
            "ingest_scip",
        ] {
            assert!(
                names.contains(&required),
                "missing tool '{required}' (got {names:?})"
            );
        }
    }

    #[tokio::test]
    async fn tools_list_includes_review_diff() {
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let resp = server.dispatch(req("tools/list", Value::Null)).await;
        let result = resp.result.expect("expected result");
        let tools = result
            .get("tools")
            .and_then(Value::as_array)
            .expect("array");
        let names: Vec<&str> = tools
            .iter()
            .filter_map(|t| t.get("name").and_then(Value::as_str))
            .collect();
        assert!(names.contains(&"review_diff"), "got {names:?}");
    }

    #[tokio::test]
    async fn review_diff_requires_diff_param() {
        // Missing 'diff' → InvalidParams before any HTTP call is attempted.
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let err = server
            .handle_review_diff(&serde_json::json!({ "index_id": "x" }))
            .await
            .expect_err("missing diff param should fail");
        assert!(matches!(err, DispatchError::InvalidParams(_)));
    }

    #[tokio::test]
    async fn review_diff_requires_index_id() {
        // Missing 'index_id' → InvalidParams: review is backed by trusty-search
        // and needs an index to cross-reference against.
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let err = server
            .handle_review_diff(&serde_json::json!({ "diff": "+++ b/x.rs\n" }))
            .await
            .expect_err("missing index_id param should fail");
        assert!(matches!(err, DispatchError::InvalidParams(_)));
    }

    #[tokio::test]
    async fn review_diff_with_args_attempts_post_to_review() {
        // Daemon unreachable — a Transport error mentioning /review proves the
        // handler built the right URL (with index_id) and method.
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let err = server
            .handle_review_diff(&serde_json::json!({
                "diff": "+++ b/x.rs\n",
                "index_id": "my-idx",
            }))
            .await
            .expect_err("daemon unreachable");
        match err {
            DispatchError::Transport(msg) => {
                assert!(msg.contains("/review"), "got {msg}");
                assert!(msg.contains("index_id=my-idx"), "got {msg}");
            }
            other => panic!("expected Transport, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn tools_list_includes_review_github_pr() {
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let resp = server.dispatch(req("tools/list", Value::Null)).await;
        let result = resp.result.expect("expected result");
        let tools = result
            .get("tools")
            .and_then(Value::as_array)
            .expect("array");
        let names: Vec<&str> = tools
            .iter()
            .filter_map(|t| t.get("name").and_then(Value::as_str))
            .collect();
        assert!(names.contains(&"review_github_pr"), "got {names:?}");
    }

    #[tokio::test]
    async fn review_github_pr_requires_owner() {
        // Missing 'owner' → InvalidParams before any HTTP call.
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let err = server
            .handle_review_github_pr(&serde_json::json!({
                "repo": "r", "pr": 1, "index_id": "i"
            }))
            .await
            .expect_err("missing owner should fail");
        assert!(matches!(err, DispatchError::InvalidParams(_)));
    }

    #[tokio::test]
    async fn review_github_pr_requires_pr_number() {
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let err = server
            .handle_review_github_pr(&serde_json::json!({
                "owner": "o", "repo": "r", "index_id": "i"
            }))
            .await
            .expect_err("missing pr should fail");
        assert!(matches!(err, DispatchError::InvalidParams(_)));
    }

    #[tokio::test]
    async fn review_github_pr_posts_to_endpoint() {
        // Daemon unreachable — a Transport error referencing /review/github-pr
        // proves the handler built the right URL after parsing all params.
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let err = server
            .handle_review_github_pr(&serde_json::json!({
                "owner": "o", "repo": "r", "pr": 7, "index_id": "i"
            }))
            .await
            .expect_err("daemon unreachable");
        match err {
            DispatchError::Transport(msg) => {
                assert!(msg.contains("/review/github-pr"), "got {msg}");
            }
            other => panic!("expected Transport, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn tools_list_includes_deep_analysis() {
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let resp = server.dispatch(req("tools/list", Value::Null)).await;
        let result = resp.result.expect("expected result");
        let tools = result
            .get("tools")
            .and_then(Value::as_array)
            .expect("array");
        let names: Vec<&str> = tools
            .iter()
            .filter_map(|t| t.get("name").and_then(Value::as_str))
            .collect();
        assert!(names.contains(&"deep_analysis"), "got {names:?}");
    }

    #[tokio::test]
    async fn deep_analysis_requires_index_id() {
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let err = server
            .handle_deep_analysis(&serde_json::json!({}))
            .await
            .expect_err("missing index_id should fail");
        assert!(matches!(err, DispatchError::InvalidParams(_)));
    }

    #[tokio::test]
    async fn deep_analysis_posts_to_endpoint() {
        // Daemon unreachable — a Transport error referencing /analyze/deep
        // proves the handler built the right URL after parsing index_id.
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let err = server
            .handle_deep_analysis(&serde_json::json!({ "index_id": "i", "model": "m" }))
            .await
            .expect_err("daemon unreachable");
        match err {
            DispatchError::Transport(msg) => {
                assert!(msg.contains("/analyze/deep"), "got {msg}");
            }
            other => panic!("expected Transport, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn resources_list_returns_envelope() {
        // Daemon unreachable → GET /indexes fails → empty resource list, but
        // the response is still a well-formed `{ resources: [] }` result.
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let resp = server.dispatch(req("resources/list", Value::Null)).await;
        let result = resp.result.expect("expected result");
        let resources = result
            .get("resources")
            .and_then(Value::as_array)
            .expect("resources array");
        assert!(resources.is_empty(), "expected empty list when daemon down");
    }

    #[tokio::test]
    async fn initialize_advertises_resources_capability() {
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let resp = server.dispatch(req("initialize", Value::Null)).await;
        let result = resp.result.expect("expected result");
        assert!(result["capabilities"]["resources"].is_object());
    }

    #[tokio::test]
    async fn unknown_tool_returns_method_not_found() {
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let resp = server
            .dispatch(req(
                "tools/call",
                serde_json::json!({ "name": "no_such_tool", "arguments": {} }),
            ))
            .await;
        let err = resp.error.expect("expected error");
        assert_eq!(err.code, error_codes::METHOD_NOT_FOUND);
    }

    #[tokio::test]
    async fn handle_analyzer_health_calls_health_endpoint() {
        // Direct handler invocation, bypassing dispatch. Daemon is unreachable,
        // so we expect a Transport error referencing /health, which proves the
        // handler constructed the right URL without us going through tools/call.
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let err = server
            .handle_analyzer_health(&Value::Null)
            .await
            .expect_err("daemon unreachable, expected transport error");
        match err {
            DispatchError::Transport(msg) => {
                assert!(
                    msg.contains("/health"),
                    "expected transport error to mention /health, got: {msg}"
                );
            }
            other => panic!("expected DispatchError::Transport, got {other:?}"),
        }
    }

    #[test]
    fn index_id_or_default_prefers_index_then_alias_then_default() {
        let with_index = serde_json::json!({ "index": "primary" });
        assert_eq!(index_id_or_default(&with_index), "primary");

        let with_alias = serde_json::json!({ "index_id": "alias" });
        assert_eq!(index_id_or_default(&with_alias), "alias");

        let empty = serde_json::json!({});
        assert_eq!(index_id_or_default(&empty), "default");
    }

    #[test]
    fn build_query_skips_missing_keys() {
        let args = serde_json::json!({ "subject": "fn auth", "object": "JWT" });
        let q = build_query(&args, &["subject", "predicate", "object"]);
        // urlencoded space → %20
        assert!(q.starts_with('?'), "expected leading '?', got {q}");
        assert!(q.contains("subject=fn%20auth"), "got {q}");
        assert!(q.contains("object=JWT"), "got {q}");
        assert!(!q.contains("predicate"), "got {q}");
    }

    #[tokio::test]
    async fn rejects_wrong_jsonrpc_version() {
        let server = AnalyzerMcpServer::new("http://127.0.0.1:1");
        let r = Request {
            jsonrpc: "1.0".into(),
            id: Some(Value::from(7u64)),
            method: "tools/list".into(),
            params: Value::Null,
        };
        let resp = server.dispatch(r).await;
        let err = resp.error.expect("expected error");
        assert_eq!(err.code, error_codes::INVALID_REQUEST);
    }
}