From f29b403935f52d12c6156b22a9a6c374ce40c302 Mon Sep 17 00:00:00 2001 From: STiFLeR7 Date: Thu, 21 May 2026 12:43:36 +0530 Subject: [PATCH] fix(git): resolve server crash on validation errors --- src/git/src/mcp_server_git/server.py | 2 +- src/git/tests/test_server.py | 36 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/git/src/mcp_server_git/server.py b/src/git/src/mcp_server_git/server.py index 5ce953e545..a8585d23b7 100644 --- a/src/git/src/mcp_server_git/server.py +++ b/src/git/src/mcp_server_git/server.py @@ -584,4 +584,4 @@ async def call_tool(name: str, arguments: dict) -> list[TextContent]: options = server.create_initialization_options() async with stdio_server() as (read_stream, write_stream): - await server.run(read_stream, write_stream, options, raise_exceptions=True) + await server.run(read_stream, write_stream, options, raise_exceptions=False) diff --git a/src/git/tests/test_server.py b/src/git/tests/test_server.py index a5492adc85..6dd960f7ab 100644 --- a/src/git/tests/test_server.py +++ b/src/git/tests/test_server.py @@ -482,3 +482,39 @@ def test_git_branch_rejects_contains_flag_injection(test_repository): with pytest.raises(BadName): git_branch(test_repository, "local", not_contains="--exec=evil") + + +def test_server_handles_invalid_json_rpc_gracefully(tmp_path): + """The server should handle invalid or deeply nested JSON-RPC messages without crashing.""" + import json + import subprocess + import sys + + # Initialize a dummy git repository to satisfy the server start requirements + repo_path = tmp_path / "test_repo" + repo = git.Repo.init(repo_path) + repo.close() + + inner = {"leaf": True} + for _ in range(200): + inner = {"child": inner} + + msgs = [ + {"jsonrpc": "2.0", "id": 1, "method": "initialize", + "params": {"protocolVersion": "2025-03-26", "capabilities": {}, + "clientInfo": {"name": "test", "version": "0.1"}}}, + {"jsonrpc": "2.0", "method": "notifications/initialized"}, + {"jsonrpc": "2.0", "id": 1001, "method": "tools/call", + "params": {"name": "git_add", "arguments": inner}}, + ] + payload = ("\n".join(json.dumps(m) for m in msgs) + "\n").encode() + + proc = subprocess.Popen( + [sys.executable, "-m", "mcp_server_git", "-r", str(repo_path)], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + ) + out, err = proc.communicate(payload, timeout=10) + + assert proc.returncode == 0 + assert b"protocolVersion" in out + assert b"ValidationError" in err or b"recursion limit" in err