Skip to content

Commit 3cb9bfd

Browse files
committed
fix(git): resolve server crash on validation errors
1 parent 7b1170d commit 3cb9bfd

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,4 +599,4 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]:
599599

600600
options = server.create_initialization_options()
601601
async with stdio_server() as (read_stream, write_stream):
602-
await server.run(read_stream, write_stream, options, raise_exceptions=True)
602+
await server.run(read_stream, write_stream, options, raise_exceptions=False)

‎src/git/tests/test_server.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,39 @@ def test_git_branch_rejects_contains_flag_injection(test_repository):
508508

509509
with pytest.raises(BadName):
510510
git_branch(test_repository, "local", not_contains="--exec=evil")
511+
512+
513+
def test_server_handles_invalid_json_rpc_gracefully(tmp_path):
514+
"""The server should handle invalid or deeply nested JSON-RPC messages without crashing."""
515+
import json
516+
import subprocess
517+
import sys
518+
519+
# Initialize a dummy git repository to satisfy the server start requirements
520+
repo_path = tmp_path / "test_repo"
521+
repo = git.Repo.init(repo_path)
522+
repo.close()
523+
524+
inner = {"leaf": True}
525+
for _ in range(200):
526+
inner = {"child": inner}
527+
528+
msgs = [
529+
{"jsonrpc": "2.0", "id": 1, "method": "initialize",
530+
"params": {"protocolVersion": "2025-03-26", "capabilities": {},
531+
"clientInfo": {"name": "test", "version": "0.1"}}},
532+
{"jsonrpc": "2.0", "method": "notifications/initialized"},
533+
{"jsonrpc": "2.0", "id": 1001, "method": "tools/call",
534+
"params": {"name": "git_add", "arguments": inner}},
535+
]
536+
payload = ("\n".join(json.dumps(m) for m in msgs) + "\n").encode()
537+
538+
proc = subprocess.Popen(
539+
[sys.executable, "-m", "mcp_server_git", "-r", str(repo_path)],
540+
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
541+
)
542+
out, err = proc.communicate(payload, timeout=10)
543+
544+
assert proc.returncode == 0
545+
assert b"protocolVersion" in out
546+
assert b"ValidationError" in err or b"recursion limit" in err

0 commit comments

Comments
 (0)