tix-tracker 0.3.0

A terminal issue tracker: markdown + YAML frontmatter issues with a kanban TUI and CLI
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
//! The MCP tool surface (WP04): the static 13-tool registry, strict argument
//! extraction, the FR-014 structured result shape, and the handlers wired to
//! the WP02/WP03 command cores (plus `Issue::add_comment` for
//! `comment_on_issue`, which has no CLI counterpart — C-003).
//!
//! Two layers stay strictly separate (`contracts/mcp-tools.md` §4, T021):
//! - *protocol* errors (`-32601` unknown tool, `-32602` type-invalid
//!   arguments) are returned as JSON-RPC errors by [`call`];
//! - *domain* failures (bad selector, unknown column, missing external CLI…)
//!   are tool-level results with `isError: true` and the CLI's exact error
//!   string verbatim (`C-006`) — never a JSON-RPC error (`NFR-002`), so a
//!   failing tool never kills the server.

use std::sync::OnceLock;

use serde_json::{Value, json};

use crate::commands::{
    create, delete, edit, export, import, lint, list, mv, parse_priority, parse_selector, search,
    status, view,
};
use crate::config::Config;
use crate::error::Result as TixResult;
use crate::model::{Issue, Status};
use crate::search::SearchHit;
use crate::storage::Store;

use super::protocol;

// ---------------------------------------------------------------------------
// T019 – registry + argument helpers
// ---------------------------------------------------------------------------

/// A tool's static metadata. `input_schema` mirrors `contracts/mcp-tools.md`
/// §3 exactly (property names, types, `required`, `additionalProperties`).
struct ToolDescriptor {
    name: &'static str,
    description: &'static str,
    input_schema: Value,
}

/// A handler's argument type errors are protocol failures (`-32602 Invalid params`);
/// handlers use [`ToolOutcome`] so a failed core call becomes an `isError` result
/// rather than propagating as a protocol error.
type ToolOutcome = std::result::Result<Value, ()>;

fn registry() -> &'static [ToolDescriptor] {
    static REGISTRY: OnceLock<Vec<ToolDescriptor>> = OnceLock::new();
    REGISTRY.get_or_init(|| vec![
        ToolDescriptor {
            name: "create_issue",
            description: "Create a new issue. Returns the created issue (its number, filename, and fields) exactly as `tix create` would write it.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "title": { "type": "string", "description": "Non-empty title (required; no TTY prompt fallback)." },
                    "status": { "type": "string", "description": "Column; default the first configured column." },
                    "priority": { "type": "string", "enum": ["low", "medium", "high"], "description": "Default medium." },
                    "assignee": { "type": "string", "description": "Handle (`alice`/`@alice`/`@me`) or full identity; default git author." },
                    "labels": { "type": "string", "description": "Comma-separated labels." },
                    "links": { "type": "string", "description": "Comma-separated issue numbers to link (back-references added)." },
                    "files": { "type": "string", "description": "Comma-separated spec file paths." },
                    "estimate": { "type": "string" },
                    "body": { "type": "string", "description": "Markdown body." }
                },
                "required": ["title"]
            }),
        },
        ToolDescriptor {
            name: "list_issues",
            description: "List issues as the board table. Filter by column.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "status": { "type": "string", "description": "Column to filter by." }
                }
            }),
        },
        ToolDescriptor {
            name: "get_issue",
            description: "Return the full issue (frontmatter + body) for a selector.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "issue": { "type": ["string", "integer"], "description": "Number, or a filename/title fragment." }
                },
                "required": ["issue"]
            }),
        },
        ToolDescriptor {
            name: "board_status",
            description: "Column counts plus the total (`tix status`).",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {}
            }),
        },
        ToolDescriptor {
            name: "update_issue",
            description: "Merge fields into an issue (CLI `tix edit` flag path only; `$EDITOR` is never opened). At least one optional field must be provided.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "issue": { "type": ["string", "integer"], "description": "Number, or a filename/title fragment." },
                    "title": { "type": "string" },
                    "body": { "type": "string" },
                    "status": { "type": "string" },
                    "priority": { "type": "string", "enum": ["low", "medium", "high"] },
                    "assignee": { "type": "string" },
                    "labels": { "type": "string", "description": "Comma-separated." },
                    "links": { "type": "string", "description": "Comma-separated issue numbers replacing the link list (back-references reconciled)." },
                    "files": { "type": "string", "description": "Comma-separated spec file paths." },
                    "estimate": { "type": "string" },
                    "commit": { "type": "string", "description": "Record a commit on the issue; `\"clear\"` removes it." }
                },
                "required": ["issue"]
            }),
        },
        ToolDescriptor {
            name: "move_issue",
            description: "Move an issue to a configured column (CLI `tix move`). A commit is recorded only when passed; never prompts.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "issue": { "type": ["string", "integer"], "description": "Number, or a filename/title fragment." },
                    "column": { "type": "string", "description": "A configured column." },
                    "commit": { "type": "string", "description": "Optional commit to record when moving to the done column." }
                },
                "required": ["issue", "column"]
            }),
        },
        ToolDescriptor {
            name: "delete_issue",
            description: "Delete an issue file. Irreversible; requires confirm: true.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "issue": { "type": ["string", "integer"], "description": "Number, or a filename/title fragment." },
                    "confirm": { "type": "boolean", "description": "Must be true to delete." }
                },
                "required": ["issue", "confirm"]
            }),
        },
        ToolDescriptor {
            name: "search_issues",
            description: "Case-insensitive substring search over the full issue markdown (frontmatter + body + comments), same corpus and matcher as `tix search`.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "query": { "type": "string" }
                },
                "required": ["query"]
            }),
        },
        ToolDescriptor {
            name: "comment_on_issue",
            description: "Append a comment to an issue's `## Comments` section via the model writer. Whitespace-only text is a no-op.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "issue": { "type": ["string", "integer"], "description": "Number, or a filename/title fragment." },
                    "author": { "type": "string" },
                    "text": { "type": "string" }
                },
                "required": ["issue", "author", "text"]
            }),
        },
        ToolDescriptor {
            name: "export_issues",
            description: "Mirror local issues to GitLab or Linear via the CLI tool (`glab`/`linear-cli`); idempotent.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "target": { "type": "string", "enum": ["gitlab", "linear"] },
                    "project": { "type": "string", "description": "Optional `--project` override." },
                    "dry_run": { "type": "boolean", "description": "Do not invoke the external CLI." }
                },
                "required": ["target"]
            }),
        },
        ToolDescriptor {
            name: "import_issues",
            description: "Pull open remote issues from GitLab or Linear via the CLI tool; idempotent by remote id.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "target": { "type": "string", "enum": ["gitlab", "linear"] }
                },
                "required": ["target"]
            }),
        },
        ToolDescriptor {
            name: "link_issue",
            description: "Symmetrically link two issues by selector (`tix link`). Both issues must exist; linking an issue to itself errors. Returns both updated issues.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {
                    "a": { "type": ["string", "integer"], "description": "Number, or a filename/title fragment." },
                    "b": { "type": ["string", "integer"], "description": "Number, or a filename/title fragment." }
                },
                "required": ["a", "b"]
            }),
        },
        ToolDescriptor {
            name: "lint_issues",
            description: "Validate frontmatter values in every issue file (same checks as `tix lint`); never crashes on malformed files.",
            input_schema: json!({
                "type": "object",
                "additionalProperties": false,
                "properties": {}
            }),
        },
    ])
}

/// The 13 descriptors serialized for the `tools/list` result (`FR-002`).
pub fn list() -> Vec<Value> {
    registry()
        .iter()
        .map(|t| {
            json!({
                "name": t.name,
                "description": t.description,
                "inputSchema": t.input_schema,
            })
        })
        .collect()
}

/// Required string argument. Absent + `null` → protocol error.
fn req_string(args: &Value, key: &str) -> std::result::Result<String, ()> {
    let value = args.get(key).ok_or(())?;
    value.as_str().map(str::to_string).ok_or(())
}

/// Optional string argument. `null` counts as absent (lenient), any other
/// non-string type is a strict `-32602` protocol error.
fn opt_string(args: &Value, key: &str) -> std::result::Result<Option<String>, ()> {
    let Some(value) = args.get(key) else {
        return Ok(None);
    };
    if value.is_null() {
        return Ok(None);
    }
    value.as_str().map(|s| Some(s.to_string())).ok_or(())
}

/// Optional boolean argument with the same strictness as [`opt_string`].
fn opt_bool(args: &Value, key: &str) -> std::result::Result<Option<bool>, ()> {
    let Some(value) = args.get(key) else {
        return Ok(None);
    };
    if value.is_null() {
        return Ok(None);
    }
    value.as_bool().map(Some).ok_or(())
}

/// Issue selector: accepts an integer (`#123`) or a string fragment, matching
/// `parse_selector` precedence (`contracts/mcp-tools.md` §2).
fn selector(args: &Value, key: &str) -> std::result::Result<String, ()> {
    let value = args.get(key).ok_or(())?;
    match value {
        Value::Null => Err(()),
        Value::String(s) => Ok(s.clone()),
        Value::Number(n) => n.as_i64().map(|i| i.to_string()).ok_or(()),
        _ => Err(()),
    }
}

// ---------------------------------------------------------------------------
// T021 – structured results + domain-error mapping
// ---------------------------------------------------------------------------

/// FR-014 success shape: `content` mirror + `structuredContent`, `isError: false`.
fn success_result(text: String, structured: Value) -> Value {
    json!({
        "content": [{ "type": "text", "text": text }],
        "structuredContent": structured,
        "isError": false,
    })
}

/// FR-014 failure shape: `isError: true`, the CLI's exact error string, and
/// **no** `structuredContent` (`contracts/mcp-tools.md` §4).
fn error_result(text: String) -> Value {
    json!({
        "content": [{ "type": "text", "text": text }],
        "isError": true,
    })
}

/// The issue object used by `create_issue`/`get_issue`/`update_issue`/
/// `move_issue`/`comment_on_issue`: the frontmatter fields plus `filename` and
/// `body` (which the derived serializer skips).
fn issue_value(issue: &Issue) -> Value {
    let mut obj = serde_json::to_value(issue).expect("Issue serialization is infallible");
    obj["filename"] = Value::String(issue.filename.clone());
    obj["body"] = Value::String(issue.body.clone());
    obj
}

// ---------------------------------------------------------------------------
// T022/T023 – handlers
// ---------------------------------------------------------------------------

fn create_issue(config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let title = req_string(args, "title")?;
    let body = opt_string(args, "body")?;
    let status = match opt_string(args, "status")? {
        None => None,
        Some(s) if s.trim().is_empty() => {
            return Ok(error_result(
                "invalid input: --status must not be empty".into(),
            ));
        }
        Some(s) => Some(Status::new(s)),
    };
    let priority = match opt_string(args, "priority")? {
        None => None,
        Some(p) => match parse_priority(&p) {
            Ok(priority) => Some(priority),
            Err(e) => return Ok(error_result(e.to_string())),
        },
    };
    let assignee = opt_string(args, "assignee")?;
    let labels = opt_string(args, "labels")?;
    let links = opt_string(args, "links")?;
    let files = opt_string(args, "files")?;
    let estimate = opt_string(args, "estimate")?;
    match create::create(
        config,
        store,
        &title,
        body.as_deref(),
        status,
        priority,
        assignee.as_deref(),
        labels.as_deref(),
        links.as_deref(),
        files.as_deref(),
        estimate.as_deref(),
    ) {
        Ok(issue) => Ok(success_result(
            format!("Created {}", issue.filename),
            issue_value(&issue),
        )),
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn list_issues(store: &Store, args: &Value) -> ToolOutcome {
    let status = opt_string(args, "status")?;
    match list::query(store, status.as_deref()) {
        Ok(issues) => {
            let values: Vec<Value> = issues.iter().map(issue_value).collect();
            Ok(success_result(
                list_text(&issues),
                json!({ "issues": values }),
            ))
        }
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn get_issue(store: &Store, args: &Value) -> ToolOutcome {
    let raw = selector(args, "issue")?;
    match view::get(store, &raw) {
        Ok(issue) => Ok(success_result(
            issue.to_file_contents(),
            issue_value(&issue),
        )),
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn board_status(config: &Config, store: &Store, _args: &Value) -> ToolOutcome {
    match status::board(config, store) {
        Ok(summary) => {
            let columns: Vec<Value> = summary
                .columns
                .iter()
                .map(|(column, count)| json!({ "column": column, "count": count }))
                .collect();
            Ok(success_result(
                status_text(config, &summary),
                json!({ "columns": columns, "total": summary.total }),
            ))
        }
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn update_issue(config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let raw = selector(args, "issue")?;
    let title = opt_string(args, "title")?;
    let body = opt_string(args, "body")?;
    let status = opt_string(args, "status")?;
    let priority = opt_string(args, "priority")?;
    let assignee = opt_string(args, "assignee")?;
    let labels = opt_string(args, "labels")?;
    let links = opt_string(args, "links")?;
    let files = opt_string(args, "files")?;
    let estimate = opt_string(args, "estimate")?;
    let commit = opt_string(args, "commit")?;
    if title.is_none()
        && body.is_none()
        && status.is_none()
        && priority.is_none()
        && assignee.is_none()
        && labels.is_none()
        && links.is_none()
        && files.is_none()
        && estimate.is_none()
        && commit.is_none()
    {
        return Ok(error_result(
            "invalid input: no fields to update — pass at least one of title, body, status, priority, assignee, labels, links, files, estimate, commit"
                .into(),
        ));
    }
    match edit::apply(
        config,
        store,
        &raw,
        title.as_deref(),
        body.as_deref(),
        status.as_deref(),
        priority.as_deref(),
        assignee.as_deref(),
        labels.as_deref(),
        links.as_deref(),
        files.as_deref(),
        estimate.as_deref(),
        commit.as_deref(),
    ) {
        Ok(issue) => Ok(success_result(
            format!("Updated {}", issue.filename),
            issue_value(&issue),
        )),
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn move_issue(config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let raw = selector(args, "issue")?;
    let column = req_string(args, "column")?;
    let commit = opt_string(args, "commit")?;
    match mv::apply(config, store, &raw, &column, commit.as_deref()) {
        Ok(issue) => Ok(success_result(
            format!("Moved {} to {column}", issue.filename),
            issue_value(&issue),
        )),
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn link_issue(store: &Store, args: &Value) -> ToolOutcome {
    let a = selector(args, "a")?;
    let b = selector(args, "b")?;
    match store.link(&a, &b) {
        Ok((issue_a, issue_b)) => Ok(success_result(
            format!("Linked #{} ↔ #{}", issue_a.number, issue_b.number),
            json!({ "a": issue_value(&issue_a), "b": issue_value(&issue_b) }),
        )),
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn delete_issue(store: &Store, args: &Value) -> ToolOutcome {
    let raw = selector(args, "issue")?;
    if args.get("confirm").and_then(Value::as_bool) != Some(true) {
        return Ok(error_result(
            "delete requires confirm=true — the issue file is untouched".into(),
        ));
    }
    let filename = match resolve_filename(store, &raw) {
        Ok(f) => f,
        Err(e) => return Ok(error_result(e.to_string())),
    };
    match delete::remove(store, &raw, true) {
        Ok(()) => Ok(success_result(
            format!("Deleted {filename}"),
            json!({ "deleted": filename }),
        )),
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn search_issues(store: &Store, args: &Value) -> ToolOutcome {
    let query = req_string(args, "query")?;
    match search::find(store, &query) {
        Ok(hits) => {
            let values: Vec<Value> = hits
                .iter()
                .map(|hit| json!({ "issue": issue_value(&hit.issue), "matched_lines": hit.matched_lines }))
                .collect();
            Ok(success_result(
                search_text(&hits),
                json!({ "hits": values }),
            ))
        }
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn comment_on_issue(store: &Store, args: &Value) -> ToolOutcome {
    let raw = selector(args, "issue")?;
    let author = req_string(args, "author")?;
    let text = req_string(args, "text")?;
    let outcome = (|| -> TixResult<Issue> {
        let selector = parse_selector(store, &raw)?;
        let mut issue = store.get(selector)?;
        issue.add_comment(&author, &text);
        store.update(&issue.filename, &issue)?;
        Ok(issue)
    })();
    match outcome {
        Ok(issue) => Ok(success_result(
            format!("Commented on {}", issue.filename),
            issue_value(&issue),
        )),
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn export_issues(config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let target = req_string(args, "target")?;
    let dry_run = opt_bool(args, "dry_run")?.unwrap_or(false);
    let project = opt_string(args, "project")?;
    match export::run_core(config, store, Some(&target), dry_run, project.as_deref()) {
        Ok(report) => Ok(success_result(report.clone(), json!({ "report": report }))),
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn import_issues(config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let target = req_string(args, "target")?;
    match import::run_core(config, store, Some(&target)) {
        Ok(report) => Ok(success_result(report.clone(), json!({ "report": report }))),
        Err(e) => Ok(error_result(e.to_string())),
    }
}

fn lint_issues(config: &Config, store: &Store, _args: &Value) -> ToolOutcome {
    let findings = match lint::collect(config, store) {
        Ok(f) => f,
        Err(e) => return Ok(error_result(e.to_string())),
    };
    let count = match store.list() {
        Ok(issues) => issues.len(),
        Err(e) => return Ok(error_result(e.to_string())),
    };
    let values: Vec<Value> = findings
        .iter()
        .map(|f| {
            json!({
                "file": f.file,
                "line": f.line,
                "column": f.column,
                "kind": f.kind,
                "value": f.value,
                "message": f.message,
            })
        })
        .collect();
    Ok(success_result(
        lint_text(count, &findings),
        json!({ "findings": values }),
    ))
}

// ---------------------------------------------------------------------------
// text mirrors (byte-identical to the CLI's stdout rendering, NFR-005)
// ---------------------------------------------------------------------------

/// Truncate a cell to `width` characters (same as `list::run`).
fn clip(value: &str, width: usize) -> String {
    value.chars().take(width).collect()
}

/// `tix list` table text.
fn list_text(issues: &[Issue]) -> String {
    let num_width = 6usize;
    let title_width = issues
        .iter()
        .map(|i| i.title.chars().count())
        .chain([5])
        .max()
        .unwrap()
        .min(40);
    let status_width = issues
        .iter()
        .map(|i| i.status.display().chars().count())
        .chain([6])
        .max()
        .unwrap()
        .min(16);
    let priority_width = issues
        .iter()
        .map(|i| i.priority.to_string().chars().count())
        .chain([8])
        .max()
        .unwrap()
        .min(12);

    let mut out = String::new();
    out.push_str(&format!(
        "{:<num_width$}  {:<title_width$}  {:<status_width$}  {:<priority_width$}\n",
        "NUM", "TITLE", "STATUS", "PRIORITY"
    ));
    for issue in issues {
        out.push_str(&format!(
            "{:<num_width$}  {:<title_width$}  {:<status_width$}  {:<priority_width$}\n",
            issue.number,
            clip(&issue.title, title_width),
            clip(issue.status.display(), status_width),
            clip(&issue.priority.to_string(), priority_width),
        ));
    }
    out
}

/// `tix status` table text.
fn status_text(config: &Config, summary: &status::BoardSummary) -> String {
    let column_width = config
        .default_config()
        .columns
        .iter()
        .map(|c| c.chars().count())
        .chain([6, 5])
        .max()
        .unwrap()
        .min(30);
    let count_width = summary.total.to_string().len().max(5).max(1);

    let mut out = String::new();
    out.push_str(&format!(
        "{:<column_width$}  {:<count_width$}\n",
        "COLUMN", "COUNT"
    ));
    for (column, count) in &summary.columns {
        out.push_str(&format!(
            "{:<column_width$}  {:<count_width$}\n",
            column,
            count.to_string()
        ));
    }
    out.push_str(&format!(
        "{:<column_width$}  {:<count_width$}\n",
        "Total",
        summary.total.to_string()
    ));
    out
}

const MAX_LINE_WIDTH: usize = 200;

/// Truncate a matched context line (same as `search::run`).
fn clipped(line: &str) -> String {
    if line.chars().count() <= MAX_LINE_WIDTH {
        return line.to_string();
    }
    let head: String = line.chars().take(MAX_LINE_WIDTH).collect();
    format!("{head}…")
}

/// `tix search` result text (empty query yields `No issues found.`).
fn search_text(hits: &[SearchHit]) -> String {
    if hits.is_empty() {
        return "No issues found.\n".to_string();
    }
    let mut out = String::new();
    for hit in hits {
        out.push_str(&format!(
            "#{} {} — {} ({})\n",
            hit.issue.number, hit.issue.title, hit.issue.status, hit.issue.filename
        ));
        for line in &hit.matched_lines {
            out.push_str(&format!("  {}\n", clipped(line)));
        }
    }
    out
}

/// `tix lint` text: the CLI's clean line, or a count line plus one
/// `file:line:col kind value message` line per finding
/// (`contracts/mcp-tools.md` §3.12).
fn lint_text(n_issues: usize, findings: &[lint::TixIssueFinding]) -> String {
    if findings.is_empty() {
        return format!("Lint OK: {n_issues} issues checked.\n");
    }
    let mut out = format!(
        "Checked {n_issues} issues; {} finding(s).\n",
        findings.len()
    );
    for f in findings {
        out.push_str(&format!(
            "{}:{}:{} {} {} {}\n",
            f.file, f.line, f.column, f.kind, f.value, f.message
        ));
    }
    out
}

/// Resolve an issue's filename for a selector (used to report the deleted
/// path before it is gone).
fn resolve_filename(store: &Store, raw: &str) -> TixResult<String> {
    let selector = parse_selector(store, raw)?;
    let issue = store.get(selector)?;
    Ok(issue.filename)
}

// ---------------------------------------------------------------------------
// dispatch
// ---------------------------------------------------------------------------

/// Handle a `tools/call` for one of the 12 registered tools.
///
/// - unknown tool name → `-32601 Method not found`;
/// - strict argument type mismatch → `-32602 Invalid params`;
/// - everything else returns the *tool result* (success or `isError`), never
///   a JSON-RPC error.
pub fn call(
    config: &Config,
    store: &Store,
    id: protocol::Id,
    name: &str,
    args: &Value,
) -> std::result::Result<Value, protocol::ErrorResponse> {
    if !registry().iter().any(|t| t.name == name) {
        return Err(protocol::method_not_found(id));
    }
    let arguments = if args.is_null() { &Value::Null } else { args };
    let outcome: ToolOutcome = match name {
        "create_issue" => create_issue(config, store, arguments),
        "list_issues" => list_issues(store, arguments),
        "get_issue" => get_issue(store, arguments),
        "board_status" => board_status(config, store, arguments),
        "update_issue" => update_issue(config, store, arguments),
        "move_issue" => move_issue(config, store, arguments),
        "link_issue" => link_issue(store, arguments),
        "delete_issue" => delete_issue(store, arguments),
        "search_issues" => search_issues(store, arguments),
        "comment_on_issue" => comment_on_issue(store, arguments),
        "export_issues" => export_issues(config, store, arguments),
        "import_issues" => import_issues(config, store, arguments),
        "lint_issues" => lint_issues(config, store, arguments),
        _ => unreachable!("name validated against the registry above"),
    };
    outcome.map_err(|()| protocol::invalid_params(id))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;
    use std::fs;
    use tempfile::TempDir;

    fn make_repo() -> (TempDir, Config, Store) {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path().join("issues");
        fs::create_dir_all(&dir).unwrap();
        let config_path = tmp.path().join("tix.toml");
        let config = Config {
            issues_dir: dir.clone(),
            ..Config::default()
        };
        config.save(&config_path).unwrap();
        let config = Config::load_with_warning(&config_path, |_| {}).unwrap();
        let store = Store {
            issues_dir: dir,
            config: config.clone(),
        };
        (tmp, config, store)
    }

    fn write_issue(store: &Store, number: u32, title: &str, status: &str) {
        let contents = format!(
            "---\nnumber: {number}\ntitle: \"{title}\"\nstatus: \"{status}\"\nlabels: []\npriority: \"medium\"\ncreated: \"2026-01-01\"\nupdated: \"2026-01-01\"\n---\n\nbody"
        );
        let slug = crate::slug::slugify(title);
        fs::create_dir_all(store.issues_dir.join("alice")).unwrap();
        fs::write(
            store.issues_dir.join(format!("alice/2026-01-01-{slug}.md")),
            contents,
        )
        .unwrap();
    }

    fn invoke(config: &Config, store: &Store, name: &str, args: Value) -> Value {
        call(config, store, protocol::Id::Number(1), name, &args)
            .expect("protocol-level calls in unit tests must not fail")
    }

    #[test]
    fn registry_has_13_tools_with_valid_schemas() {
        let tools = list();
        assert_eq!(tools.len(), 13);
        for tool in &tools {
            let schema = &tool["inputSchema"];
            assert_eq!(schema["type"], "object");
            assert_eq!(schema["additionalProperties"], false);
            assert!(tool["description"].as_str().is_some_and(|d| !d.is_empty()));
            assert!(tool["name"].as_str().is_some());
        }
    }

    #[test]
    fn registry_names_match_the_contract_parity_table() {
        let tools = list();
        let names: Vec<&str> = tools.iter().filter_map(|t| t["name"].as_str()).collect();
        assert_eq!(names.len(), 13);
        for expected in [
            "create_issue",
            "list_issues",
            "get_issue",
            "board_status",
            "update_issue",
            "move_issue",
            "link_issue",
            "delete_issue",
            "search_issues",
            "comment_on_issue",
            "export_issues",
            "import_issues",
            "lint_issues",
        ] {
            assert!(names.contains(&expected), "missing tool {expected}");
        }
    }

    #[test]
    fn arg_helpers_reject_wrong_types() {
        let args = json!({ "title": 42, "dry_run": "yes", "issue": true });
        assert!(req_string(&args, "title").is_err());
        assert!(opt_string(&args, "title").is_err());
        assert!(opt_bool(&args, "dry_run").is_err());
        assert!(selector(&args, "issue").is_err());
        assert_eq!(selector(&json!({ "issue": 123 }), "issue").unwrap(), "123");
        assert_eq!(
            selector(&json!({ "issue": "n 1" }), "issue").unwrap(),
            "n 1"
        );
    }

    #[test]
    fn null_optionals_are_treated_as_absent() {
        assert_eq!(opt_string(&json!({ "x": null }), "x").unwrap(), None);
        assert_eq!(opt_bool(&json!({ "x": null }), "x").unwrap(), None);
        assert!(req_string(&json!({ "x": null }), "x").is_err());
    }

    #[test]
    fn call_type_mismatch_is_invalid_params() {
        let (_tmp, config, store) = make_repo();
        let err = call(
            &config,
            &store,
            protocol::Id::Number(1),
            "create_issue",
            &json!({ "title": 5 }),
        )
        .unwrap_err();
        assert_eq!(err.error.code, protocol::INVALID_PARAMS);
        assert_eq!(err.id, protocol::Id::Number(1));
    }

    #[test]
    fn call_unknown_tool_is_method_not_found() {
        let (_tmp, config, store) = make_repo();
        let err = call(
            &config,
            &store,
            protocol::Id::Number(2),
            "bogus",
            &json!({}),
        )
        .unwrap_err();
        assert_eq!(err.error.code, protocol::METHOD_NOT_FOUND);
    }

    #[test]
    fn create_issue_writes_file_and_returns_structured_result() {
        let (_tmp, config, store) = make_repo();
        let result = invoke(
            &config,
            &store,
            "create_issue",
            json!({ "title": "Build the tool registry", "labels": "mcp, core", "priority": "high" }),
        );
        assert_eq!(result["isError"], false);
        let structured = &result["structuredContent"];
        assert_eq!(structured["title"], "Build the tool registry");
        assert_eq!(structured["labels"], json!(["mcp", "core"]));
        assert_eq!(structured["priority"], "high");
        let filename = structured["filename"].as_str().unwrap();
        assert!(store.issues_dir.join(filename).exists());
        assert!(
            result["content"][0]["text"]
                .as_str()
                .unwrap()
                .starts_with("Created ")
        );
    }

    #[test]
    fn delete_issue_requires_confirm_and_files_survive_refusal() {
        let (_tmp, config, store) = make_repo();
        write_issue(&store, 1, "Alpha", "todo");
        let refused = invoke(&config, &store, "delete_issue", json!({ "issue": "1" }));
        assert_eq!(refused["isError"], true);
        assert!(store.issues_dir.join("alice/2026-01-01-alpha.md").exists());

        let done = invoke(
            &config,
            &store,
            "delete_issue",
            json!({ "issue": 1, "confirm": true }),
        );
        assert_eq!(done["isError"], false);
        assert_eq!(
            done["structuredContent"]["deleted"],
            "alice/2026-01-01-alpha.md"
        );
        assert!(!store.issues_dir.join("alice/2026-01-01-alpha.md").exists());
    }

    #[test]
    fn update_issue_with_no_fields_is_a_tool_error() {
        let (_tmp, config, store) = make_repo();
        write_issue(&store, 1, "Alpha", "todo");
        let result = invoke(&config, &store, "update_issue", json!({ "issue": "1" }));
        assert_eq!(result["isError"], true);
        assert!(
            result["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("no fields to update")
        );
    }

    #[test]
    fn create_issue_accepts_links_and_files_params() {
        let (_tmp, config, store) = make_repo();
        let a = invoke(
            &config,
            &store,
            "create_issue",
            json!({ "title": "A", "assignee": "" }),
        );
        let b = invoke(
            &config,
            &store,
            "create_issue",
            json!({ "title": "B", "assignee": "" }),
        );
        let num_a = a["structuredContent"]["number"].as_u64().unwrap();
        let num_b = b["structuredContent"]["number"].as_u64().unwrap();
        let linked = invoke(
            &config,
            &store,
            "create_issue",
            json!({
                "title": "C",
                "assignee": "",
                "links": format!("{},{}", num_a, num_b),
                "files": "openspec/changes/x/proposal.md,openspec/changes/x/specs/y/spec.md",
            }),
        );
        assert_eq!(linked["isError"], false);
        let structured = &linked["structuredContent"];
        assert_eq!(
            structured["links"],
            json!([num_a.to_string(), num_b.to_string()])
        );
        assert_eq!(
            structured["files"],
            json!([
                "openspec/changes/x/proposal.md",
                "openspec/changes/x/specs/y/spec.md"
            ])
        );
        let reread_a = invoke(&config, &store, "get_issue", json!({ "issue": num_a }));
        assert_eq!(
            reread_a["structuredContent"]["links"],
            json!([structured["number"].as_u64().unwrap().to_string()])
        );
    }

    #[test]
    fn update_issue_sets_links_and_files_params() {
        let (_tmp, config, store) = make_repo();
        let a = invoke(
            &config,
            &store,
            "create_issue",
            json!({ "title": "A", "assignee": "" }),
        );
        let b = invoke(
            &config,
            &store,
            "create_issue",
            json!({ "title": "B", "assignee": "" }),
        );
        let num_a = a["structuredContent"]["number"].as_u64().unwrap();
        let num_b = b["structuredContent"]["number"].as_u64().unwrap();
        let updated = invoke(
            &config,
            &store,
            "update_issue",
            json!({
                "issue": num_a,
                "links": num_b.to_string(),
                "files": "specs/m/spec.md",
            }),
        );
        assert_eq!(updated["isError"], false);
        let structured = &updated["structuredContent"];
        assert_eq!(structured["links"], json!([num_b.to_string()]));
        assert_eq!(structured["files"], json!(["specs/m/spec.md"]));
    }

    #[test]
    fn link_issue_links_two_issues_symmetrically() {
        let (_tmp, config, store) = make_repo();
        let a = invoke(
            &config,
            &store,
            "create_issue",
            json!({ "title": "A", "assignee": "" }),
        );
        let b = invoke(
            &config,
            &store,
            "create_issue",
            json!({ "title": "B", "assignee": "" }),
        );
        let num_a = a["structuredContent"]["number"].as_u64().unwrap();
        let num_b = b["structuredContent"]["number"].as_u64().unwrap();
        let result = invoke(
            &config,
            &store,
            "link_issue",
            json!({ "a": num_a, "b": num_b }),
        );
        assert_eq!(result["isError"], false);
        assert_eq!(
            result["structuredContent"]["a"]["links"],
            json!([num_b.to_string()])
        );
        assert_eq!(
            result["structuredContent"]["b"]["links"],
            json!([num_a.to_string()])
        );
    }

    #[test]
    fn move_issue_unknown_column_mirrors_cli_error_text() {
        let (_tmp, config, store) = make_repo();
        write_issue(&store, 1, "Alpha", "todo");
        let result = invoke(
            &config,
            &store,
            "move_issue",
            json!({ "issue": "1", "column": "nope" }),
        );
        assert_eq!(result["isError"], true);
        assert_eq!(
            result["content"][0]["text"],
            "invalid input: unknown column 'nope' — valid columns: todo, in progress, in review, done"
        );
    }

    #[test]
    fn lint_clean_store_reports_ok() {
        let (_tmp, config, store) = make_repo();
        write_issue(&store, 1, "Alpha", "todo");
        let result = invoke(&config, &store, "lint_issues", json!({}));
        assert_eq!(result["isError"], false);
        assert!(
            result["content"][0]["text"]
                .as_str()
                .unwrap()
                .starts_with("Lint OK: 1 issues checked.")
        );
    }

    #[test]
    fn export_without_gitlab_config_is_a_structured_error() {
        let (_tmp, config, store) = make_repo();
        write_issue(&store, 1, "Alpha", "todo");
        let result = invoke(
            &config,
            &store,
            "export_issues",
            json!({ "target": "gitlab", "dry_run": true }),
        );
        assert_eq!(result["isError"], true);
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("no `[export.gitlab]` section"), "got: {text}");
    }
}