Coverage for /Users/les/Projects/crackerjack/crackerjack/mcp/client_runner.py: 0%

53 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-03 15:39 -0700

1import asyncio 

2import socket 

3import subprocess 

4import sys 

5from pathlib import Path 

6 

7from rich.console import Console 

8 

9from mcp import stdio_client 

10 

11from .progress_monitor import ( 

12 run_crackerjack_with_enhanced_progress as run_crackerjack_with_progress, 

13) 

14 

15 

16def is_mcp_server_running(host: str = "localhost", port: int = 5173) -> bool: 

17 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

18 try: 

19 result = sock.connect_ex((host, port)) 

20 return result == 0 

21 finally: 

22 sock.close() 

23 

24 

25async def ensure_mcp_server_running() -> subprocess.Popen | None: 

26 console = Console() 

27 

28 if is_mcp_server_running(): 

29 console.print("[green]✅ MCP server already running[/ green]") 

30 return None 

31 

32 console.print("[yellow]🚀 Starting MCP server...[/ yellow]") 

33 server_process = subprocess.Popen( 

34 [sys.executable, "- m", "crackerjack", "- - start - mcp-server"], 

35 stdout=subprocess.PIPE, 

36 stderr=subprocess.PIPE, 

37 start_new_session=True, 

38 ) 

39 

40 for _i in range(20): 

41 if is_mcp_server_running(): 

42 console.print("[green]✅ MCP server started successfully[/ green]") 

43 return server_process 

44 await asyncio.sleep(0.5) 

45 

46 console.print("[red]❌ Failed to start MCP server[/ red]") 

47 server_process.terminate() 

48 msg = "Failed to start MCP server within timeout period" 

49 raise RuntimeError(msg) 

50 

51 

52async def run_with_mcp_server(command: str = "/ crackerjack: run") -> None: 

53 console = Console() 

54 

55 server_process = await ensure_mcp_server_running() 

56 

57 try: 

58 server_script = Path(__file__).parent.parent / "__main__.py" 

59 async with ( 

60 stdio_client( 

61 sys.executable, 

62 str(server_script), 

63 "- - start - mcp-server", 

64 ) as (read_stream, write_stream), 

65 read_stream.session( 

66 read_stream=read_stream, 

67 write_stream=write_stream, 

68 ) as session, 

69 ): 

70 try: 

71 await run_crackerjack_with_progress(session, command) 

72 except Exception as e: 

73 console.print(f"[bold red]Error: {e}[/ bold red]") 

74 sys.exit(1) 

75 finally: 

76 if server_process: 

77 console.print( 

78 "[yellow]Note: MCP server continues running in background[/ yellow]", 

79 ) 

80 

81 

82def main() -> None: 

83 import argparse 

84 

85 parser = argparse.ArgumentParser( 

86 description="Run Crackerjack commands through MCP with progress monitoring", 

87 ) 

88 parser.add_argument( 

89 "command", 

90 nargs="?", 

91 default="/ crackerjack: run", 

92 help="Command to execute (default: / crackerjack: run)", 

93 ) 

94 args = parser.parse_args() 

95 

96 try: 

97 asyncio.run(run_with_mcp_server(args.command)) 

98 except KeyboardInterrupt: 

99 Console().print("\n[yellow]Operation cancelled by user[/ yellow]") 

100 sys.exit(1) 

101 

102 

103if __name__ == "__main__": 

104 main()