fix(git): accept JSON-encoded string for git_add files argument#4276
Open
adityasingh2400 wants to merge 1 commit into
Open
fix(git): accept JSON-encoded string for git_add files argument#4276adityasingh2400 wants to merge 1 commit into
adityasingh2400 wants to merge 1 commit into
Conversation
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 modelcontextprotocol#4242
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #4242. The git server's
git_addtool rejects a perfectly reasonable call when a client sends thefilesargument as a JSON-encoded array string rather than a real array.Server Details
git_addinput schema and handler)Motivation and Context
In #4242 a client called
git_addwithfilesset to the string'["inc/admin/functions.php", "inc/admin/menu.php", ...]'and got back:There are two layers to this:
GitAdd.fileswas typedlist[str], soGitAdd.model_json_schema()published{"type": "array"}. The low-level MCP SDK validates raw arguments against the tool'sinputSchemabefore the handler runs, so a string value is rejected with the exact "is not of type 'array'" message above.git_addpassed the raw string straight torepo.git.add("--", *files), which iterates the string character by character. That producesgit add -- [ " f i l e 1 ..., which fails withfatal: pathspec '[' did not match any files.LLM clients fairly often serialize array arguments as JSON strings, so this is a recurring friction point worth handling leniently.
What changed
GitAdd.filestolist[str] | strwith a description, so the published schema becomesanyOf: [array of strings, string]and the SDK no longer pre-rejects a string value.normalize_file_listhelper used bygit_add: whenfilesis a string it parses a JSON array of strings into a list, and otherwise treats the value as a single path. Real lists pass through unchanged, so the happy path and["."]behavior are untouched.How Has This Been Tested?
Ran the git server suite from
src/gitwithuv run pytest: 44 passed, including four new cases:test_git_add_json_encoded_string: stages two files passed as'["file1.txt", "file2.txt"]'.test_git_add_single_path_string: a bare path string stages exactly that one file, not its characters.test_normalize_file_list: lists pass through, JSON arrays parse, non-JSON and non-string-list JSON fall back to a single entry.test_git_add_all_filesandtest_git_add_specific_filesstill pass.I confirmed the new behavioral tests fail on the unmodified code: the original
git_add('["file1.txt", "file2.txt"]')raisesGitCommandError(git add -- [ " f i l e 1 ...). I also validated end to end thatGitAdd.model_json_schema()now accepts the array, the JSON-string, and the single-path forms throughjsonschema.validate, matching how the SDK gates calls.uv run pyrightreports 0 errors anduv run ruff check .passes.Breaking Changes
None. Existing array inputs behave exactly as before. This only adds acceptance of additional input shapes.
Types of changes
Checklist
Additional context
The leniency is intentionally narrow: only a JSON array whose elements are all strings is expanded into multiple paths. Any other string, including malformed JSON or a JSON object, is treated as a single literal path, which keeps the existing strict pathspec semantics for genuine single-file calls.