
    Oj                        U d Z ddlmZ ddlZddlZddlZddlZddlmZ  e	ej                  j                  dd            Zej                  j                  dd      Zd	e d
Zdaded<   ddZddZdddZddZy)u  Engine-spawned macOS desktop-control MCP (macos-mcp), over local HTTP.

macOS-only. On Windows this module is a no-op stub: enabled() returns False so
start()/wait_ready() do nothing, because Windows desktop control is provided
IN-PROCESS by voice/desktop_control.py (no HTTP server, no macos-mcp).

Why not a normal stdio MCP server: the Claude CLI spawns stdio MCP servers, and
macOS attributes their TCC permissions to the CLI helper — NOT Jarvis.app. So a
desktop-control server spawned that way is DENIED Accessibility and macos-mcp
exits on startup ("Missing permissions: Accessibility"). Exactly the
responsible-process wall that broke screenshots (see voice/screen.py).

Fix: the ENGINE launches macos-mcp here as its own child, so it inherits
Jarvis.app's Accessibility + Screen Recording grants and runs. Jarvis reaches it
over loopback HTTP (llm.py wires the "desktop" server as an http MCP to
DESKTOP_URL with the bearer key). Localhost-only; the key just satisfies the
server's auth. Set MCP_DESKTOP=0 to disable.
    )annotationsN)PathDESKTOP_MCP_PORT8765DESKTOP_MCP_KEYzjarvis-desktop-local-keyzhttp://127.0.0.1:z/mcpsubprocess.Popen | None_procc                 r    t         j                  dk7  xr# t         j                  j                  dd      dk7  S )NntMCP_DESKTOP10)osnameenvironget     B/Users/ahmed/devFolder/Ultron/claude-voice/voice/desktop_server.pyenabledr   #   s+     77d?Hrzz~~mSASHHr   c                    t               syt        j                  g dt        j                  t        j                         t	        t        t              j                         j                  j                  dz  dz  dz        } 	 t        dd      }t        j                  | d	d
ddddddt	        t              dt        g||      at        S # t        $ r}t        d| d       daY d}~yd}~ww xY w)zLaunch macos-mcp (HTTP) as a child of the engine. Idempotent-ish: kills
    any stale server on the port first (a prior engine that was SIGKILLed can
    orphan it and hold the port).N)pkillz-fzmacos_mcp serve)stdoutstderrz.venvbinpythonz/tmp/desktop_mcp.logaz-m	macos_mcpservez--transportzstreamable-httpz--host	127.0.0.1z--portz
--auth-keyz   desktop MCP failed to launch ())r   
subprocessrunDEVNULLstrr   __file__resolveparentopenPopenDESKTOP_PORTDESKTOP_KEYr	   	Exceptionprint)venv_pyloges      r   startr2   )   s    
 9NN5$,,Z5G5GI$x.((*11887BUJXUVG)3/  dK-{Hc,.?;( s
  0156s   AC 	C1C,,C1c                \   t               syt        j                         | z   }t        j                         |k  r)	 t        j                  dt        fd      5  	 ddd       yy# 1 sw Y   nxY wn$# t
        $ r t        j                  d       Y nw xY wt        j                         |k  rsK)zBlock until the HTTP server is listening, so the brain doesn't connect to
    the desktop MCP before it's up (that would leave Jarvis with no app tools).Fr    g      ?timeoutNTg333333?)r   timesocketcreate_connectionr+   OSErrorsleep)r5   ends     r   
wait_readyr<   D   s     9
))+
C
))+
	));*EsS TS 	 TSS 	JJsO		 ))+
s)   A1 A$A1 $A-)A1 1BBc                     t         rBt         j                         .t         j                          	 t         j                  d       d a y d a y # t        $ r t         j                          Y d a y w xY w)N   r4   )r	   poll	terminatewaitr-   killr   r   r   stoprC   S   sW    %	JJqJ! EDE  	JJLE	s   A A.-A.)returnbool)rD   r   )g       @)r5   floatrD   rE   )rD   None)__doc__
__future__r   r   r7   r"   r6   pathlibr   intr   r   r+   r,   DESKTOP_URLr	   __annotations__r   r2   r<   rC   r   r   r   <module>rN      s|   & # 	    2::>>"4f=>jjnn.0JK!,t4!% %I6r   