Skip to content

Commit 4180d90

Browse files
fix(git): accept JSON-encoded string for git_add files argument
Some clients send the files argument to git_add as a JSON-encoded array string, for example '["a.py", "b.py"]', rather than a real array. The published input schema declared files as an array only, so the SDK rejected the call at the validation layer with "is not of type array". Even past that layer, git_add passed the raw string through to git, which split it into individual characters. Widen the schema to accept a list of strings or a string, then normalize a string into a list inside git_add: parse a JSON array of strings when present, otherwise treat the value as a single path. Real lists are unchanged. Fixes #4242
1 parent 64b1cb0 commit 4180d90

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

‎src/git/src/mcp_server_git/server.py‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
from pathlib import Path
34
from typing import Sequence, Optional
@@ -42,7 +43,10 @@ class GitCommit(BaseModel):
4243

4344
class GitAdd(BaseModel):
4445
repo_path: str
45-
files: list[str]
46+
files: list[str] | str = Field(
47+
...,
48+
description="The files to stage. Normally a list of paths. A JSON-encoded array string such as '[\"a.py\", \"b.py\"]' or a single path string are also accepted for leniency.",
49+
)
4650

4751
class GitReset(BaseModel):
4852
repo_path: str
@@ -129,7 +133,23 @@ def git_commit(repo: git.Repo, message: str) -> str:
129133
commit = repo.index.commit(message)
130134
return f"Changes committed successfully with hash {commit.hexsha}"
131135

132-
def git_add(repo: git.Repo, files: list[str]) -> str:
136+
def normalize_file_list(files: list[str] | str) -> list[str]:
137+
# Some clients send the files argument as a JSON-encoded array string,
138+
# e.g. '["a.py", "b.py"]', instead of a real array. Accept that form, and
139+
# also accept a single bare path string, so a stray string does not fail
140+
# the call outright.
141+
if isinstance(files, str):
142+
try:
143+
parsed = json.loads(files)
144+
except json.JSONDecodeError:
145+
return [files]
146+
if isinstance(parsed, list) and all(isinstance(item, str) for item in parsed):
147+
return parsed
148+
return [files]
149+
return files
150+
151+
def git_add(repo: git.Repo, files: list[str] | str) -> str:
152+
files = normalize_file_list(files)
133153
if files == ["."]:
134154
repo.git.add(".")
135155
else:

‎src/git/tests/test_server.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
git_checkout,
77
git_branch,
88
git_add,
9+
normalize_file_list,
910
git_status,
1011
git_diff_unstaged,
1112
git_diff_staged,
@@ -109,6 +110,45 @@ def test_git_add_specific_files(test_repository):
109110
assert "file2.txt" not in staged_files
110111
assert result == "Files staged successfully"
111112

113+
def test_git_add_json_encoded_string(test_repository):
114+
file1 = Path(test_repository.working_dir) / "file1.txt"
115+
file2 = Path(test_repository.working_dir) / "file2.txt"
116+
file1.write_text("file 1 content")
117+
file2.write_text("file 2 content")
118+
119+
# Some clients send the files argument as a JSON-encoded array string.
120+
result = git_add(test_repository, '["file1.txt", "file2.txt"]')
121+
122+
staged_files = [item.a_path for item in test_repository.index.diff("HEAD")]
123+
assert "file1.txt" in staged_files
124+
assert "file2.txt" in staged_files
125+
assert result == "Files staged successfully"
126+
127+
def test_git_add_single_path_string(test_repository):
128+
file1 = Path(test_repository.working_dir) / "file1.txt"
129+
file2 = Path(test_repository.working_dir) / "file2.txt"
130+
file1.write_text("file 1 content")
131+
file2.write_text("file 2 content")
132+
133+
# A single bare path string is treated as one file, not split apart.
134+
result = git_add(test_repository, "file1.txt")
135+
136+
staged_files = [item.a_path for item in test_repository.index.diff("HEAD")]
137+
assert "file1.txt" in staged_files
138+
assert "file2.txt" not in staged_files
139+
assert result == "Files staged successfully"
140+
141+
def test_normalize_file_list():
142+
# Real lists pass through unchanged.
143+
assert normalize_file_list(["a.py", "b.py"]) == ["a.py", "b.py"]
144+
# JSON-encoded array strings are parsed into a list.
145+
assert normalize_file_list('["a.py", "b.py"]') == ["a.py", "b.py"]
146+
# A bare path that is not valid JSON is kept as a single entry.
147+
assert normalize_file_list("a.py") == ["a.py"]
148+
# A JSON value that is not a list of strings is treated as a single path.
149+
assert normalize_file_list('{"a": 1}') == ['{"a": 1}']
150+
assert normalize_file_list("[1, 2]") == ["[1, 2]"]
151+
112152
def test_git_status(test_repository):
113153
result = git_status(test_repository)
114154

0 commit comments

Comments
 (0)