Coverage for rdgai/gui/serve.py: 0.00%

47 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-24 23:53 +0000

1""" 

2Running the GUI on its own, or in the same process (and event loop) as the MCP server, sharing its store. 

3""" 

4import socket 

5import sys 

6 

7import anyio 

8import uvicorn 

9 

10 

11def bind(host:str, port:int, quiet:bool=False, retry_seconds:float=0) -> socket.socket | None: 

12 """ Binds a listening socket, or returns None (with a message on stderr unless quiet) if the port is unavailable. """ 

13 sock = socket.socket(socket.AF_INET6 if ":" in host else socket.AF_INET) 

14 try: 

15 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 

16 sock.bind((host, port)) 

17 sock.listen(128) 

18 sock.setblocking(False) 

19 return sock 

20 except OSError as error: 

21 sock.close() 

22 if not quiet: 

23 retry = f" MCP is still running; will try the port again every {retry_seconds:g} s." if retry_seconds > 0 else " MCP is still running." 

24 print(f"rdgai: GUI not started: port {port} on {host} is unavailable ({error.strerror}).{retry}", file=sys.stderr, flush=True) 

25 return None 

26 

27 

28def gui_server(app, host:str, port:int) -> uvicorn.Server: 

29 # No access log and no logging configuration: uvicorn's access log writes to stdout, which is the MCP stdio channel. 

30 config = uvicorn.Config(app, host=host, port=port, log_config=None, access_log=False, lifespan="off") 

31 return uvicorn.Server(config) 

32 

33 

34async def serve_mcp_with_gui( 

35 mcp_server, gui_app, gui_host:str, gui_port:int, transport:str="stdio", host:str="127.0.0.1", port:int=8000, 

36 retry_seconds:float=15.0, 

37): 

38 """ 

39 Runs the MCP server and, if its port is free, the GUI in the same event loop. The MCP server always runs. 

40 If the GUI port is taken (e.g. by a second process for the same file), it is tried again every retry_seconds 

41 (0: never), so that the GUI survives whichever process exits first. 

42 """ 

43 

44 async def run_mcp(): 

45 if transport == "stdio": 

46 await mcp_server.run_stdio_async() 

47 else: 

48 await mcp_server.run_streamable_http_async(host=host, port=port) 

49 

50 gui = gui_server(gui_app, gui_host, gui_port) 

51 url = f"http://{gui_host}:{gui_port}/" 

52 

53 async def take_over_when_free(): 

54 while True: 

55 await anyio.sleep(retry_seconds) 

56 sock = bind(gui_host, gui_port, quiet=True) 

57 if sock is not None: 

58 print(f"rdgai: took over the GUI port: annotation GUI at {url}", file=sys.stderr, flush=True) 

59 await gui.serve([sock]) 

60 return 

61 

62 sock = bind(gui_host, gui_port, retry_seconds=retry_seconds) 

63 async with anyio.create_task_group() as group: 

64 if sock is not None: 

65 print(f"rdgai: annotation GUI at {url}", file=sys.stderr, flush=True) 

66 group.start_soon(gui.serve, [sock]) 

67 elif retry_seconds > 0: 

68 group.start_soon(take_over_when_free) 

69 try: 

70 await run_mcp() 

71 finally: 

72 gui.should_exit = True 

73 group.cancel_scope.cancel()