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
mod common;

use mcp_git::server::{
    CommitParams, DiffParams, FileAtRefParams, LogParams, McpGitServer, RepoEntry, RepoParam,
    SearchParams,
};

fn make_server(repo: &common::TestRepo) -> McpGitServer {
    let entry = RepoEntry {
        name: repo.name(),
        path: repo.path(),
    };
    McpGitServer::new(vec![entry], 500, 50)
}

fn make_multi_server(repos: &[&common::TestRepo]) -> McpGitServer {
    let entries: Vec<RepoEntry> = repos
        .iter()
        .map(|r| RepoEntry {
            name: r.name(),
            path: r.path(),
        })
        .collect();
    McpGitServer::new(entries, 500, 50)
}

fn extract_text(result: rmcp::model::CallToolResult) -> serde_json::Value {
    let text = result
        .content
        .first()
        .and_then(|c| c.as_text())
        .map(|t| t.text.clone())
        .unwrap_or_default();
    serde_json::from_str(&text).unwrap_or(serde_json::Value::Null)
}

#[test]
fn test_list_repos() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let result = server.do_list_repos().expect("list_repos failed");
    let json = extract_text(result);
    let arr = json.as_array().expect("should be array");

    assert_eq!(arr.len(), 1);
    assert_eq!(arr[0]["name"], repo.name());
    assert_eq!(arr[0]["branch"], "main");
}

#[test]
fn test_log_basic() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let params = LogParams {
        repo: None,
        max_count: None,
        branch: None,
        author: None,
    };
    let result = server.do_log(params).expect("log failed");
    let json = extract_text(result);

    assert_eq!(json["count"], 3);
    let commits = json["commits"].as_array().unwrap();
    // Most recent first
    assert!(commits[0]["message"].as_str().unwrap().contains("lib.rs"));
    assert!(commits[2]["message"].as_str().unwrap().contains("README"));
}

#[test]
fn test_log_with_max_count() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let params = LogParams {
        repo: None,
        max_count: Some(2),
        branch: None,
        author: None,
    };
    let result = server.do_log(params).expect("log failed");
    let json = extract_text(result);

    assert_eq!(json["count"], 2);
}

#[test]
fn test_log_with_author_filter() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    // On main branch, all commits are by Alice — filter for Bob should return 0
    let params = LogParams {
        repo: None,
        max_count: Some(50),
        branch: None,
        author: Some("Bob".to_string()),
    };
    let result = server.do_log(params).expect("log failed");
    let json = extract_text(result);
    assert_eq!(json["count"], 0);

    // On feature branch, there's one commit by Bob
    let params = LogParams {
        repo: None,
        max_count: Some(50),
        branch: Some("feature".to_string()),
        author: Some("Bob".to_string()),
    };
    let result = server.do_log(params).expect("log failed");
    let json = extract_text(result);
    assert_eq!(json["count"], 1);
    assert!(json["commits"][0]["author"]
        .as_str()
        .unwrap()
        .contains("Bob"));
}

#[test]
fn test_log_author_filter_does_not_reduce_max() {
    // Regression test: author filter should not count filtered-out commits toward max
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    // feature branch has 4 commits: 3 by Alice + 1 by Bob
    // With max=50 and author=Alice, we should get all 3 Alice commits
    let params = LogParams {
        repo: None,
        max_count: Some(50),
        branch: Some("feature".to_string()),
        author: Some("Alice".to_string()),
    };
    let result = server.do_log(params).expect("log failed");
    let json = extract_text(result);
    assert_eq!(
        json["count"], 3,
        "Should find all 3 Alice commits on feature branch"
    );
}

#[test]
fn test_show_commit() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    // First get a commit SHA from the log
    let log_params = LogParams {
        repo: None,
        max_count: Some(1),
        branch: None,
        author: None,
    };
    let log_result = server.do_log(log_params).expect("log failed");
    let log_json = extract_text(log_result);
    let sha = log_json["commits"][0]["sha"].as_str().unwrap().to_string();

    let params = CommitParams {
        repo: None,
        commit: sha.clone(),
    };
    let result = server.do_show_commit(params).expect("show_commit failed");
    let json = extract_text(result);

    assert_eq!(json["sha"], sha);
    assert!(json["author"].as_str().unwrap().contains("Alice"));
    assert!(json["message"].as_str().is_some());
    assert!(json["parents"].as_array().is_some());
}

#[test]
fn test_list_branches() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let params = RepoParam { repo: None };
    let result = server
        .do_list_branches(params)
        .expect("list_branches failed");
    let json = extract_text(result);

    let local = json["local"].as_array().unwrap();
    let names: Vec<&str> = local.iter().map(|b| b["name"].as_str().unwrap()).collect();
    assert!(names.contains(&"main"), "should contain main");
    assert!(names.contains(&"feature"), "should contain feature");

    // main should be marked as current
    let main_branch = local.iter().find(|b| b["name"] == "main").unwrap();
    assert_eq!(main_branch["current"], true);
}

#[test]
fn test_search_commits() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let params = SearchParams {
        repo: None,
        query: "README".to_string(),
        max_count: None,
    };
    let result = server
        .do_search_commits(params)
        .expect("search_commits failed");
    let json = extract_text(result);

    assert_eq!(json["count"], 1);
    assert!(json["matches"][0]["message"]
        .as_str()
        .unwrap()
        .contains("README"));
}

#[test]
fn test_diff() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    // Get first and last commit SHAs on main
    let log_params = LogParams {
        repo: None,
        max_count: Some(10),
        branch: None,
        author: None,
    };
    let log_result = server.do_log(log_params).expect("log failed");
    let log_json = extract_text(log_result);
    let commits = log_json["commits"].as_array().unwrap();

    let newest = commits[0]["sha"].as_str().unwrap().to_string();
    let oldest = commits.last().unwrap()["sha"].as_str().unwrap().to_string();

    let params = DiffParams {
        repo: None,
        from_ref: oldest,
        to_ref: Some(newest),
        path: None,
    };
    let result = server.do_diff(params).expect("diff failed");
    let json = extract_text(result);

    assert!(
        json["file_count"].as_u64().unwrap() > 0,
        "should have changed files"
    );
    let files = json["files"].as_array().unwrap();
    let paths: Vec<&str> = files.iter().map(|f| f["path"].as_str().unwrap()).collect();
    assert!(paths.contains(&"src/main.rs"), "should include src/main.rs");
    assert!(paths.contains(&"src/lib.rs"), "should include src/lib.rs");
}

#[test]
fn test_diff_with_path_filter() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let log_params = LogParams {
        repo: None,
        max_count: Some(10),
        branch: None,
        author: None,
    };
    let log_result = server.do_log(log_params).expect("log failed");
    let log_json = extract_text(log_result);
    let commits = log_json["commits"].as_array().unwrap();
    let newest = commits[0]["sha"].as_str().unwrap().to_string();
    let oldest = commits.last().unwrap()["sha"].as_str().unwrap().to_string();

    let params = DiffParams {
        repo: None,
        from_ref: oldest,
        to_ref: Some(newest),
        path: Some("src/main".to_string()),
    };
    let result = server.do_diff(params).expect("diff failed");
    let json = extract_text(result);

    let files = json["files"].as_array().unwrap();
    assert_eq!(files.len(), 1);
    assert_eq!(files[0]["path"], "src/main.rs");
}

#[test]
fn test_resolve_single_repo() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    // With single repo, None should work
    let params = RepoParam { repo: None };
    let result = server.do_list_branches(params);
    assert!(
        result.is_ok(),
        "resolve(None) should succeed with single repo"
    );
}

#[test]
fn test_resolve_ambiguous() {
    let repo1 = common::TestRepo::new();
    let repo2 = common::TestRepo::new();
    let server = make_multi_server(&[&repo1, &repo2]);

    // With multiple repos, None should fail
    let params = RepoParam { repo: None };
    let result = server.do_list_branches(params);
    assert!(
        result.is_err(),
        "resolve(None) should fail with multiple repos"
    );
}

#[test]
fn test_resolve_not_found() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let params = RepoParam {
        repo: Some("nonexistent".to_string()),
    };
    let result = server.do_list_branches(params);
    assert!(result.is_err(), "resolve with wrong name should fail");
}

// -- New tool tests --

#[test]
fn test_status_clean() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let params = RepoParam { repo: None };
    let result = server.do_status(params).expect("status failed");
    let json = extract_text(result);

    assert_eq!(json["is_clean"], true);
    assert_eq!(json["staged"].as_array().unwrap().len(), 0);
    assert_eq!(json["unstaged"].as_array().unwrap().len(), 0);
    assert_eq!(json["untracked"].as_array().unwrap().len(), 0);
}

#[test]
fn test_status_modified() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    // Modify a tracked file
    std::fs::write(repo.path().join("README.md"), "# Modified\n").unwrap();

    let params = RepoParam { repo: None };
    let result = server.do_status(params).expect("status failed");
    let json = extract_text(result);

    assert_eq!(json["is_clean"], false);
    let unstaged = json["unstaged"].as_array().unwrap();
    assert!(
        unstaged.iter().any(|e| e["path"] == "README.md"),
        "README.md should show as unstaged modified"
    );
}

#[test]
fn test_get_file_contents() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let params = FileAtRefParams {
        repo: None,
        path: "README.md".to_string(),
        rev: None,
    };
    let result = server
        .do_get_file_contents(params)
        .expect("get_file_contents failed");
    let json = extract_text(result);

    assert_eq!(json["path"], "README.md");
    assert_eq!(json["content"], "# Test\n");
    assert_eq!(json["size"], 7);
}

#[test]
fn test_get_file_contents_at_ref() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    // Get the oldest commit SHA (which only has README.md)
    let log_params = LogParams {
        repo: None,
        max_count: Some(10),
        branch: None,
        author: None,
    };
    let log_result = server.do_log(log_params).expect("log failed");
    let log_json = extract_text(log_result);
    let commits = log_json["commits"].as_array().unwrap();
    let oldest_sha = commits.last().unwrap()["sha"].as_str().unwrap().to_string();

    // src/main.rs should NOT exist at the oldest commit
    let params = FileAtRefParams {
        repo: None,
        path: "src/main.rs".to_string(),
        rev: Some(oldest_sha),
    };
    let result = server.do_get_file_contents(params);
    assert!(
        result.is_err(),
        "src/main.rs should not exist at the oldest commit"
    );
}

#[test]
fn test_get_file_contents_not_found() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let params = FileAtRefParams {
        repo: None,
        path: "nonexistent.txt".to_string(),
        rev: None,
    };
    let result = server.do_get_file_contents(params);
    assert!(result.is_err(), "nonexistent file should return error");
}

#[test]
fn test_list_tags() {
    let repo = common::TestRepo::new();

    // Create tags using git CLI
    let path = repo.path();
    let git = |args: &[&str]| {
        std::process::Command::new("git")
            .args(args)
            .current_dir(&path)
            .env("GIT_COMMITTER_NAME", "Alice")
            .env("GIT_COMMITTER_EMAIL", "alice@test.com")
            .output()
            .expect("git command failed")
    };
    git(&["tag", "v1.0"]);
    git(&["tag", "-a", "v2.0", "-m", "Release 2.0"]);

    let server = make_server(&repo);
    let params = RepoParam { repo: None };
    let result = server.do_list_tags(params).expect("list_tags failed");
    let json = extract_text(result);

    assert_eq!(json["count"], 2);
    let tags = json["tags"].as_array().unwrap();
    let tag_names: Vec<&str> = tags.iter().map(|t| t["name"].as_str().unwrap()).collect();
    assert!(tag_names.contains(&"v1.0"), "should contain v1.0");
    assert!(tag_names.contains(&"v2.0"), "should contain v2.0");
}

#[test]
fn test_get_remote_info() {
    let repo = common::TestRepo::new();

    // Add a remote
    let path = repo.path();
    std::process::Command::new("git")
        .args([
            "remote",
            "add",
            "origin",
            "https://example.com/test.git",
        ])
        .current_dir(&path)
        .output()
        .expect("git remote add failed");

    let server = make_server(&repo);
    let params = RepoParam { repo: None };
    let result = server
        .do_get_remote_info(params)
        .expect("get_remote_info failed");
    let json = extract_text(result);

    assert_eq!(json["count"], 1);
    let remotes = json["remotes"].as_array().unwrap();
    assert_eq!(remotes[0]["name"], "origin");
    assert_eq!(remotes[0]["fetch_url"], "https://example.com/test.git");
}

#[test]
fn test_blame() {
    let repo = common::TestRepo::new();
    let server = make_server(&repo);

    let params = FileAtRefParams {
        repo: None,
        path: "README.md".to_string(),
        rev: None,
    };
    let result = server.do_blame(params).expect("blame failed");
    let json = extract_text(result);

    assert_eq!(json["path"], "README.md");
    assert_eq!(json["total_lines"], 1);
    let blame = json["blame"].as_array().unwrap();
    assert!(!blame.is_empty(), "blame should have entries");
    assert_eq!(blame[0]["author"], "Alice");
}