@@ -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