mcp-git 0.2.0

MCP server that lets LLMs explore and search Git repositories
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
use std::path::PathBuf;
use std::sync::Arc;

use rmcp::handler::server::router::tool::ToolRouter;
use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::*;
use rmcp::{schemars, tool, tool_handler, tool_router, ServerHandler};
use serde::Deserialize;

use crate::error::McpGitError;
use std::process::Command;

#[derive(Clone)]
pub struct RepoEntry {
    pub name: String,
    pub path: PathBuf,
}

#[derive(Clone)]
pub struct McpGitServer {
    repos: Arc<Vec<RepoEntry>>,
    max_diff_lines: u32,
    max_log_entries: u32,
    tool_router: ToolRouter<Self>,
}

// -- Tool parameter types --

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct RepoParam {
    #[schemars(description = "Repository name (optional if only one repo is connected)")]
    #[serde(default)]
    pub repo: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct LogParams {
    #[schemars(description = "Repository name (optional if only one repo is connected)")]
    #[serde(default)]
    pub repo: Option<String>,

    #[schemars(description = "Maximum number of commits to return")]
    #[serde(default)]
    pub max_count: Option<u32>,

    #[schemars(description = "Branch or ref to show log for (default: HEAD)")]
    #[serde(default)]
    pub branch: Option<String>,

    #[schemars(description = "Filter commits by author name or email")]
    #[serde(default)]
    pub author: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct DiffParams {
    #[schemars(description = "Repository name (optional if only one repo is connected)")]
    #[serde(default)]
    pub repo: Option<String>,

    #[schemars(description = "Starting ref (commit SHA, branch, or tag)")]
    pub from_ref: String,

    #[schemars(description = "Ending ref (commit SHA, branch, or tag). Default: HEAD")]
    #[serde(default)]
    pub to_ref: Option<String>,

    #[schemars(description = "Filter diff to a specific file path")]
    #[serde(default)]
    pub path: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct CommitParams {
    #[schemars(description = "Repository name (optional if only one repo is connected)")]
    #[serde(default)]
    pub repo: Option<String>,

    #[schemars(description = "Commit SHA or ref to show")]
    pub commit: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct SearchParams {
    #[schemars(description = "Repository name (optional if only one repo is connected)")]
    #[serde(default)]
    pub repo: Option<String>,

    #[schemars(description = "Search query to match against commit messages")]
    pub query: String,

    #[schemars(description = "Maximum number of results to return")]
    #[serde(default)]
    pub max_count: Option<u32>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FileAtRefParams {
    #[schemars(description = "Repository name (optional if only one repo is connected)")]
    #[serde(default)]
    pub repo: Option<String>,

    #[schemars(description = "Path to the file within the repository")]
    pub path: String,

    #[schemars(description = "Git ref (commit SHA, branch, or tag). Default: HEAD")]
    #[serde(default, rename = "ref")]
    pub rev: Option<String>,
}

impl McpGitServer {
    pub fn new(repos: Vec<RepoEntry>, max_diff_lines: u32, max_log_entries: u32) -> Self {
        Self {
            repos: Arc::new(repos),
            max_diff_lines,
            max_log_entries,
            tool_router: Self::tool_router(),
        }
    }

    fn resolve(&self, name: Option<&str>) -> Result<&RepoEntry, McpGitError> {
        match name {
            Some(n) => self
                .repos
                .iter()
                .find(|r| r.name == n)
                .ok_or_else(|| McpGitError::RepoNotFound(n.to_string())),
            None if self.repos.len() == 1 => Ok(&self.repos[0]),
            None => Err(McpGitError::AmbiguousRepo),
        }
    }

    fn open_repo(&self, entry: &RepoEntry) -> Result<gix::Repository, McpGitError> {
        gix::discover(&entry.path)
            .map_err(|e| McpGitError::Git(format!("Cannot open repository '{}': {}", entry.name, e)))
    }

    fn err(&self, e: McpGitError) -> ErrorData {
        e.to_mcp_error()
    }
}

// -- Public methods for testability --

impl McpGitServer {
    pub fn do_list_repos(&self) -> Result<CallToolResult, ErrorData> {
        let mut results = Vec::new();
        for entry in self.repos.iter() {
            let branch = match self.open_repo(entry) {
                Ok(repo) => repo
                    .head_name()
                    .ok()
                    .flatten()
                    .map(|r| r.shorten().to_string())
                    .unwrap_or_else(|| "detached".to_string()),
                Err(_) => "unknown".to_string(),
            };

            results.push(serde_json::json!({
                "name": entry.name,
                "path": entry.path.display().to_string(),
                "branch": branch,
            }));
        }

        let text =
            serde_json::to_string_pretty(&results).unwrap_or_else(|_| "[]".to_string());
        Ok(CallToolResult::success(vec![Content::text(text)]))
    }

    pub fn do_log(&self, params: LogParams) -> Result<CallToolResult, ErrorData> {
        let entry = self.resolve(params.repo.as_deref()).map_err(|e| self.err(e))?;
        let repo = self.open_repo(entry).map_err(|e| self.err(e))?;

        let max = params.max_count.unwrap_or(self.max_log_entries);
        let rev_spec = params.branch.as_deref().unwrap_or("HEAD");

        let commit_id = repo
            .rev_parse_single(gix::bstr::BStr::new(rev_spec.as_bytes()))
            .map_err(|e| self.err(McpGitError::InvalidRef(format!("{}: {}", rev_spec, e))))?
            .detach();

        let mut commits = Vec::new();
        let walk = repo
            .rev_walk([commit_id])
            .all()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        for info in walk {
            if commits.len() >= max as usize {
                break;
            }
            let info = info.map_err(|e| self.err(McpGitError::Git(e.to_string())))?;
            let commit = info
                .object()
                .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

            let author = commit.author().map_err(|e| self.err(McpGitError::Git(e.to_string())))?;
            let author_name = author.name.to_string();
            let author_email = author.email.to_string();
            let message = commit.message_raw_sloppy().to_string();
            let time = author.time.seconds;

            // Apply author filter if specified
            if let Some(ref filter) = params.author {
                let filter_lower = filter.to_lowercase();
                if !author_name.to_lowercase().contains(&filter_lower)
                    && !author_email.to_lowercase().contains(&filter_lower)
                {
                    continue;
                }
            }

            commits.push(serde_json::json!({
                "sha": commit.id().to_string(),
                "author": format!("{} <{}>", author_name, author_email),
                "timestamp": time,
                "message": message.trim(),
            }));
        }

        let text = serde_json::to_string_pretty(&serde_json::json!({
            "commits": commits,
            "count": commits.len(),
        }))
        .unwrap_or_else(|_| "{}".to_string());
        Ok(CallToolResult::success(vec![Content::text(text)]))
    }

    pub fn do_diff(&self, params: DiffParams) -> Result<CallToolResult, ErrorData> {
        let entry = self.resolve(params.repo.as_deref()).map_err(|e| self.err(e))?;
        let repo = self.open_repo(entry).map_err(|e| self.err(e))?;

        let from = repo
            .rev_parse_single(gix::bstr::BStr::new(params.from_ref.as_bytes()))
            .map_err(|e| self.err(McpGitError::InvalidRef(format!("{}: {}", params.from_ref, e))))?;
        let to_ref = params.to_ref.as_deref().unwrap_or("HEAD");
        let to = repo
            .rev_parse_single(gix::bstr::BStr::new(to_ref.as_bytes()))
            .map_err(|e| self.err(McpGitError::InvalidRef(format!("{}: {}", to_ref, e))))?;

        let from_commit = repo
            .find_object(from)
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?
            .try_into_commit()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;
        let to_commit = repo
            .find_object(to)
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?
            .try_into_commit()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let from_tree = from_commit
            .tree()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;
        let to_tree = to_commit
            .tree()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        // Compute tree diff to find changed files
        use gix::object::tree::diff::{Action as DiffAction, Change as DiffChange};
        let mut changes = Vec::new();
        let max_files = self.max_diff_lines as usize;

        from_tree
            .changes()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?
            .for_each_to_obtain_tree(&to_tree, |change: DiffChange<'_, '_, '_>| {
                let path = change.location().to_string();

                // Apply path filter if specified
                if let Some(ref filter_path) = params.path {
                    if !path.starts_with(filter_path.as_str()) {
                        return Ok::<_, std::convert::Infallible>(DiffAction::Continue);
                    }
                }

                let change_type = match &change {
                    DiffChange::Addition { .. } => "added",
                    DiffChange::Deletion { .. } => "deleted",
                    DiffChange::Modification { .. } => "modified",
                    DiffChange::Rewrite { copy: true, .. } => "copied",
                    DiffChange::Rewrite { .. } => "renamed",
                };

                if changes.len() < max_files {
                    changes.push(serde_json::json!({
                        "path": path,
                        "change": change_type,
                    }));
                }

                Ok(DiffAction::Continue)
            })
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let text = serde_json::to_string_pretty(&serde_json::json!({
            "from": params.from_ref,
            "to": to_ref,
            "from_sha": from_commit.id().to_string(),
            "to_sha": to_commit.id().to_string(),
            "files": changes,
            "file_count": changes.len(),
        }))
        .unwrap_or_else(|_| "{}".to_string());
        Ok(CallToolResult::success(vec![Content::text(text)]))
    }

    pub fn do_show_commit(&self, params: CommitParams) -> Result<CallToolResult, ErrorData> {
        let entry = self.resolve(params.repo.as_deref()).map_err(|e| self.err(e))?;
        let repo = self.open_repo(entry).map_err(|e| self.err(e))?;

        let id = repo
            .rev_parse_single(gix::bstr::BStr::new(params.commit.as_bytes()))
            .map_err(|e| self.err(McpGitError::InvalidRef(format!("{}: {}", params.commit, e))))?;

        let commit = repo
            .find_object(id)
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?
            .try_into_commit()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let author = commit.author().map_err(|e| self.err(McpGitError::Git(e.to_string())))?;
        let committer = commit.committer().map_err(|e| self.err(McpGitError::Git(e.to_string())))?;
        let message = commit.message_raw_sloppy().to_string();
        let time = author.time.seconds;

        let parent_ids: Vec<String> = commit
            .parent_ids()
            .map(|id| id.to_string())
            .collect();

        let text = serde_json::to_string_pretty(&serde_json::json!({
            "sha": commit.id().to_string(),
            "author": format!("{} <{}>", author.name, author.email),
            "committer": format!("{} <{}>", committer.name, committer.email),
            "timestamp": time,
            "message": message.trim(),
            "parents": parent_ids,
        }))
        .unwrap_or_else(|_| "{}".to_string());
        Ok(CallToolResult::success(vec![Content::text(text)]))
    }

    pub fn do_list_branches(&self, params: RepoParam) -> Result<CallToolResult, ErrorData> {
        let entry = self.resolve(params.repo.as_deref()).map_err(|e| self.err(e))?;
        let repo = self.open_repo(entry).map_err(|e| self.err(e))?;

        let head_name = repo
            .head_name()
            .ok()
            .flatten()
            .map(|r| r.shorten().to_string());

        let platform = repo
            .references()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let local = platform
            .local_branches()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let mut branches = Vec::new();
        for reference in local.flatten() {
            let name = reference.name().shorten().to_string();
            let is_current = head_name.as_deref() == Some(name.as_str());
            branches.push(serde_json::json!({
                "name": name,
                "current": is_current,
            }));
        }

        // Also list remote branches
        let remote = platform
            .remote_branches()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let mut remote_branches = Vec::new();
        for reference in remote.flatten() {
            let name = reference.name().shorten().to_string();
            remote_branches.push(serde_json::json!({
                "name": name,
            }));
        }

        let text = serde_json::to_string_pretty(&serde_json::json!({
            "local": branches,
            "remote": remote_branches,
        }))
        .unwrap_or_else(|_| "{}".to_string());
        Ok(CallToolResult::success(vec![Content::text(text)]))
    }

    pub fn do_search_commits(&self, params: SearchParams) -> Result<CallToolResult, ErrorData> {
        let entry = self.resolve(params.repo.as_deref()).map_err(|e| self.err(e))?;
        let repo = self.open_repo(entry).map_err(|e| self.err(e))?;
        let max = params.max_count.unwrap_or(self.max_log_entries);

        let head = repo
            .head_id()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let walk = repo
            .rev_walk([head.detach()])
            .all()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let query_lower = params.query.to_lowercase();
        let mut matches = Vec::new();

        for info in walk.flatten() {
            let commit = match info.object() {
                Ok(c) => c,
                Err(_) => continue,
            };

            let message = commit.message_raw_sloppy().to_string();
            if message.to_lowercase().contains(&query_lower) {
                let author_str = match commit.author() {
                    Ok(a) => format!("{} <{}>", a.name, a.email),
                    Err(_) => "unknown".to_string(),
                };
                matches.push(serde_json::json!({
                    "sha": commit.id().to_string(),
                    "author": author_str,
                    "message": message.trim(),
                }));
            }

            if matches.len() >= max as usize {
                break;
            }
        }

        let text = serde_json::to_string_pretty(&serde_json::json!({
            "query": params.query,
            "matches": matches,
            "count": matches.len(),
        }))
        .unwrap_or_else(|_| "{}".to_string());
        Ok(CallToolResult::success(vec![Content::text(text)]))
    }

    pub fn do_status(&self, params: RepoParam) -> Result<CallToolResult, ErrorData> {
        let entry = self.resolve(params.repo.as_deref()).map_err(|e| self.err(e))?;

        let output = Command::new("git")
            .args(["status", "--porcelain=v1"])
            .current_dir(&entry.path)
            .output()
            .map_err(|e| {
                self.err(McpGitError::Git(format!(
                    "Failed to run git status: {}",
                    e
                )))
            })?;

        if !output.status.success() {
            return Err(self.err(McpGitError::Git(format!(
                "git status failed: {}",
                String::from_utf8_lossy(&output.stderr)
            ))));
        }

        let stdout = String::from_utf8_lossy(&output.stdout);
        let mut staged = Vec::new();
        let mut unstaged = Vec::new();
        let mut untracked = Vec::new();

        for line in stdout.lines() {
            if line.len() < 3 {
                continue;
            }
            let bytes = line.as_bytes();
            let index_status = bytes[0] as char;
            let worktree_status = bytes[1] as char;
            let path = &line[3..];

            if index_status == '?' {
                untracked.push(path.to_string());
            } else {
                if index_status != ' ' {
                    staged.push(serde_json::json!({
                        "path": path,
                        "status": match index_status {
                            'A' => "added",
                            'M' => "modified",
                            'D' => "deleted",
                            'R' => "renamed",
                            'C' => "copied",
                            _ => "unknown",
                        },
                    }));
                }
                if worktree_status != ' ' {
                    unstaged.push(serde_json::json!({
                        "path": path,
                        "status": match worktree_status {
                            'M' => "modified",
                            'D' => "deleted",
                            _ => "unknown",
                        },
                    }));
                }
            }
        }

        let text = serde_json::to_string_pretty(&serde_json::json!({
            "staged": staged,
            "unstaged": unstaged,
            "untracked": untracked,
            "is_clean": staged.is_empty() && unstaged.is_empty() && untracked.is_empty(),
        }))
        .unwrap_or_else(|_| "{}".to_string());
        Ok(CallToolResult::success(vec![Content::text(text)]))
    }

    pub fn do_get_file_contents(
        &self,
        params: FileAtRefParams,
    ) -> Result<CallToolResult, ErrorData> {
        let entry = self.resolve(params.repo.as_deref()).map_err(|e| self.err(e))?;
        let repo = self.open_repo(entry).map_err(|e| self.err(e))?;

        let rev = params.rev.as_deref().unwrap_or("HEAD");
        let spec = format!("{}:{}", rev, params.path);

        let id = repo
            .rev_parse_single(gix::bstr::BStr::new(spec.as_bytes()))
            .map_err(|e| self.err(McpGitError::InvalidRef(format!("{}: {}", spec, e))))?;

        let object = repo
            .find_object(id)
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let data = &object.data;
        let is_binary = data.iter().take(8192).any(|&b| b == 0);

        if is_binary {
            let text = serde_json::to_string_pretty(&serde_json::json!({
                "path": params.path,
                "ref": rev,
                "binary": true,
                "size": data.len(),
            }))
            .unwrap_or_else(|_| "{}".to_string());
            Ok(CallToolResult::success(vec![Content::text(text)]))
        } else {
            let content = String::from_utf8_lossy(data);
            let text = serde_json::to_string_pretty(&serde_json::json!({
                "path": params.path,
                "ref": rev,
                "content": content,
                "size": data.len(),
            }))
            .unwrap_or_else(|_| "{}".to_string());
            Ok(CallToolResult::success(vec![Content::text(text)]))
        }
    }

    pub fn do_list_tags(&self, params: RepoParam) -> Result<CallToolResult, ErrorData> {
        let entry = self.resolve(params.repo.as_deref()).map_err(|e| self.err(e))?;
        let repo = self.open_repo(entry).map_err(|e| self.err(e))?;

        let platform = repo
            .references()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let tag_refs = platform
            .tags()
            .map_err(|e| self.err(McpGitError::Git(e.to_string())))?;

        let mut tags = Vec::new();
        for mut reference in tag_refs.flatten() {
            let name = reference.name().shorten().to_string();
            let sha = reference
                .peel_to_id_in_place()
                .map(|id| id.to_string())
                .unwrap_or_else(|_| "unknown".to_string());

            tags.push(serde_json::json!({
                "name": name,
                "sha": sha,
            }));
        }

        let text = serde_json::to_string_pretty(&serde_json::json!({
            "tags": tags,
            "count": tags.len(),
        }))
        .unwrap_or_else(|_| "{}".to_string());
        Ok(CallToolResult::success(vec![Content::text(text)]))
    }

    pub fn do_get_remote_info(&self, params: RepoParam) -> Result<CallToolResult, ErrorData> {
        let entry = self.resolve(params.repo.as_deref()).map_err(|e| self.err(e))?;
        let repo = self.open_repo(entry).map_err(|e| self.err(e))?;

        let names = repo.remote_names();
        let mut remotes = Vec::new();

        for name in &names {
            match repo.find_remote(name.as_ref()) {
                Ok(remote) => {
                    let fetch_url = remote
                        .url(gix::remote::Direction::Fetch)
                        .map(|u| u.to_bstring().to_string())
                        .unwrap_or_default();
                    let push_url = remote
                        .url(gix::remote::Direction::Push)
                        .map(|u| u.to_bstring().to_string())
                        .unwrap_or_default();

                    remotes.push(serde_json::json!({
                        "name": name.to_string(),
                        "fetch_url": fetch_url,
                        "push_url": push_url,
                    }));
                }
                Err(_) => continue,
            }
        }

        let text = serde_json::to_string_pretty(&serde_json::json!({
            "remotes": remotes,
            "count": remotes.len(),
        }))
        .unwrap_or_else(|_| "{}".to_string());
        Ok(CallToolResult::success(vec![Content::text(text)]))
    }

    pub fn do_blame(&self, params: FileAtRefParams) -> Result<CallToolResult, ErrorData> {
        let entry = self.resolve(params.repo.as_deref()).map_err(|e| self.err(e))?;
        let rev = params.rev.as_deref().unwrap_or("HEAD");

        let output = Command::new("git")
            .args(["blame", "--line-porcelain", rev, "--", &params.path])
            .current_dir(&entry.path)
            .output()
            .map_err(|e| {
                self.err(McpGitError::Git(format!(
                    "Failed to run git blame: {}",
                    e
                )))
            })?;

        if !output.status.success() {
            return Err(self.err(McpGitError::Git(format!(
                "git blame failed: {}",
                String::from_utf8_lossy(&output.stderr)
            ))));
        }

        let stdout = String::from_utf8_lossy(&output.stdout);

        struct BlameLine {
            sha: String,
            author: String,
            line_no: u32,
        }

        let mut lines: Vec<BlameLine> = Vec::new();
        let mut sha = String::new();
        let mut author = String::new();
        let mut line_no = 0u32;

        for raw in stdout.lines() {
            if raw.starts_with('\t') {
                lines.push(BlameLine {
                    sha: sha.clone(),
                    author: author.clone(),
                    line_no,
                });
                continue;
            }

            if raw.len() > 40 && raw.as_bytes()[40] == b' ' {
                let maybe_sha = &raw[..40];
                if maybe_sha.chars().all(|c| c.is_ascii_hexdigit()) {
                    sha = maybe_sha.to_string();
                    let rest: Vec<&str> = raw[41..].splitn(3, ' ').collect();
                    if rest.len() >= 2 {
                        line_no = rest[1].parse().unwrap_or(0);
                    }
                    continue;
                }
            }

            if let Some(a) = raw.strip_prefix("author ") {
                author = a.to_string();
            }
        }

        // Group consecutive lines by same commit
        let mut groups = Vec::new();
        let mut i = 0;
        while i < lines.len() {
            let start = lines[i].line_no;
            let group_sha = lines[i].sha.clone();
            let group_author = lines[i].author.clone();
            let mut end = start;
            i += 1;

            while i < lines.len() && lines[i].sha == group_sha {
                end = lines[i].line_no;
                i += 1;
            }

            let line_range = if start == end {
                format!("{}", start)
            } else {
                format!("{}-{}", start, end)
            };

            groups.push(serde_json::json!({
                "commit": &group_sha[..std::cmp::min(8, group_sha.len())],
                "author": group_author,
                "lines": line_range,
            }));
        }

        let text = serde_json::to_string_pretty(&serde_json::json!({
            "path": params.path,
            "ref": rev,
            "blame": groups,
            "total_lines": lines.len(),
        }))
        .unwrap_or_else(|_| "{}".to_string());
        Ok(CallToolResult::success(vec![Content::text(text)]))
    }
}

// -- MCP tool handlers (thin wrappers) --

#[tool_router]
impl McpGitServer {
    #[tool(
        name = "list_repos",
        description = "List all connected Git repositories with their paths and current branch"
    )]
    async fn list_repos(&self) -> Result<CallToolResult, ErrorData> {
        self.do_list_repos()
    }

    #[tool(
        name = "log",
        description = "Show commit history for a repository. Returns commit SHA, author, date, and message."
    )]
    async fn log(
        &self,
        Parameters(params): Parameters<LogParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.do_log(params)
    }

    #[tool(
        name = "diff",
        description = "Show the diff between two refs (commits, branches, or tags)"
    )]
    async fn diff(
        &self,
        Parameters(params): Parameters<DiffParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.do_diff(params)
    }

    #[tool(
        name = "show_commit",
        description = "Show details of a specific commit including message, author, date, and files changed"
    )]
    async fn show_commit(
        &self,
        Parameters(params): Parameters<CommitParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.do_show_commit(params)
    }

    #[tool(
        name = "list_branches",
        description = "List all branches in the repository with current branch marked"
    )]
    async fn list_branches(
        &self,
        Parameters(params): Parameters<RepoParam>,
    ) -> Result<CallToolResult, ErrorData> {
        self.do_list_branches(params)
    }

    #[tool(
        name = "search_commits",
        description = "Search commit messages for a given query string"
    )]
    async fn search_commits(
        &self,
        Parameters(params): Parameters<SearchParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.do_search_commits(params)
    }

    #[tool(
        name = "status",
        description = "Show working tree status including staged, unstaged, and untracked files"
    )]
    async fn status(
        &self,
        Parameters(params): Parameters<RepoParam>,
    ) -> Result<CallToolResult, ErrorData> {
        self.do_status(params)
    }

    #[tool(
        name = "get_file_contents",
        description = "Get the content of a file at a specific Git revision"
    )]
    async fn get_file_contents(
        &self,
        Parameters(params): Parameters<FileAtRefParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.do_get_file_contents(params)
    }

    #[tool(
        name = "list_tags",
        description = "List all tags in the repository with their commit SHAs"
    )]
    async fn list_tags(
        &self,
        Parameters(params): Parameters<RepoParam>,
    ) -> Result<CallToolResult, ErrorData> {
        self.do_list_tags(params)
    }

    #[tool(
        name = "get_remote_info",
        description = "List configured Git remotes with their fetch and push URLs"
    )]
    async fn get_remote_info(
        &self,
        Parameters(params): Parameters<RepoParam>,
    ) -> Result<CallToolResult, ErrorData> {
        self.do_get_remote_info(params)
    }

    #[tool(
        name = "blame",
        description = "Show line-by-line authorship for a file, grouped by commit"
    )]
    async fn blame(
        &self,
        Parameters(params): Parameters<FileAtRefParams>,
    ) -> Result<CallToolResult, ErrorData> {
        self.do_blame(params)
    }
}

#[tool_handler]
impl ServerHandler for McpGitServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo {
            protocol_version: ProtocolVersion::V_2024_11_05,
            capabilities: ServerCapabilities::builder().enable_tools().build(),
            server_info: Implementation {
                name: "mcp-git".to_string(),
                version: env!("CARGO_PKG_VERSION").to_string(),
                ..Default::default()
            },
            instructions: Some(
                "Git repository server. Tools: list_repos (connected repos), log (commit history), \
                 diff (compare refs), show_commit (commit details), list_branches (branches), \
                 search_commits (search messages), status (working tree status), \
                 get_file_contents (file at revision), list_tags (tags), \
                 get_remote_info (remotes), blame (line authorship)."
                    .to_string(),
            ),
        }
    }
}