
    {h#                        d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
Z
d dlZ
d dlmZ d dlmZmZ d dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZmZmZmZ d dlm Z   e jB                  e"      Z#ejH                  dk(  rg dng dZ%dZ&de'e(e(f   fdZ) G d de      Z*eejV                  fde*de	fd       Z,de(de(fdZ-dejV                  dfde(de.e(   de'e(e(f   dz  de	dee(z  dz  f
dZ/d deez  de0ddfdZ1y)!    N)asynccontextmanager)Path)LiteralTextIO)Process)MemoryObjectReceiveStreamMemoryObjectSendStream)TextReceiveStream)	BaseModelField)terminate_posix_process_tree)FallbackProcesscreate_windows_processget_windows_executable_commandterminate_windows_process_tree)SessionMessagewin32)APPDATA	HOMEDRIVEHOMEPATHLOCALAPPDATAPATHPROCESSOR_ARCHITECTURESYSTEMDRIVE
SYSTEMROOTTEMPUSERNAMEUSERPROFILE)HOMELOGNAMEr   SHELLTERMUSER       @returnc                      i } t         D ];  }t        j                  j                  |      }|%|j	                  d      r7|| |<   = | S )zo
    Returns a default environment object including only environment variables deemed
    safe to inherit.
    z())DEFAULT_INHERITED_ENV_VARSosenvironget
startswith)envkeyvalues      Z/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/mcp/client/stdio/__init__.pyget_default_environmentr0   2   sS    
 C) 	

s#=D!C	 J    c                       e Zd ZU eed<   	  ee      Zee   ed<   	 dZe	eef   dz  ed<   	 dZ
eez  dz  ed<   	 dZeed<   	 d	Zed
   ed<   y)StdioServerParameterscommand)default_factoryargsNr,   cwdzutf-8encodingstrict)r9   ignorereplaceencoding_error_handler)__name__
__module____qualname__str__annotations__r   listr6   r,   dictr7   r   r8   r<   r    r1   r/   r3   r3   G   s}    L4D1D$s)1;!%Cc3h$	% "Ctd	!AHc FNG$ABMr1   r3   servererrlogc           
     6   	
K   t        j                  d      \  	}t        j                  d      \  }
	 t         j                        }t	        | j
                   j                  i t                j                  n	t               | j                         d{   	 fd} 
fd}t        j                         4 d{   }4 d{    |j                  |       |j                  |       	 ||f j                  r#	 j                  j                          d{    	 t        j                  t               5  j#                          d{    ddd       |j                          d{    |j                          d{    	j                          d{    
j                          d{    ddd      d{    ddd      d{    y7 P# t        $ rf |j                          d{  7   |j                          d{  7   	j                          d{  7   
j                          d{  7    w xY w7 7 7 <# t        $ r Y Fw xY w7 # 1 sw Y   xY w# t$        $ r t'               d{  7   Y ?t(        $ r Y Jw xY w7 ;7 &7 7 # j                  r4	 j                  j                          d{  7   n# t        $ r Y nw xY w	 t        j                  t               5  j#                          d{  7   ddd       n# 1 sw Y   nxY wn-# t$        $ r t'               d{  7   Y nt(        $ r Y nw xY w|j                          d{  7   |j                          d{  7   	j                          d{  7   
j                          d{  7   w xY w7 # 1 d{  7  sw Y   xY w7 # 1 d{  7  sw Y   yxY ww)z
    Client transport for stdio: this will connect to a server by spawning a
    process and communicating with it over stdin/stdout.
    r   N)r4   r6   r,   rF   r7   c                    K   j                   sJ d       	 4 d {    d} t        j                   	j                  	j                        2 3 d {   }| |z   j	                  d      }|j                         } |D ]F  }	 t        j                  j                  |      }t        |      }j                  |       d {    H y7 7 v# t        $ r$}j                  |       d {  7   Y d }~vd }~ww xY w7 ;6 d d d       d {  7   y # 1 d {  7  sw Y   y xY w# t        j                  $ r* t        j                  j                          d {  7   Y y w xY ww)Nz Opened process is missing stdout r8   errors
)stdoutr
   r8   r<   splitpoptypesJSONRPCMessagemodel_validate_json	Exceptionsendr   anyioClosedResourceErrorlowlevel
checkpoint)
bufferchunklineslinemessageexcsession_messageprocessread_stream_writerrE   s
          r/   stdout_readerz#stdio_client.<locals>.stdout_reader   sX    ~~AAA~	.) G G#4NN#__!88$ G G%
 $e^2248E"YY[F % G%&+&:&:&N&Nt&TG
 +9*A055oFFFGGG  ) %"4"9"9#">>>$%
 G!$G G G G G& (( 	...++---	.s   E(D( C
D( /DD CD *DC#DC>
D
D( D 	C;	C6	+C.,C6	1D6C;	;D DD( DD( E(D%DD%!D( $E(%D( (4E%EE%"E($E%%E(c                    K   j                   sJ d       	 4 d {    2 3 d {   } | j                  j                  dd      }j                   j                  |dz   j	                  j
                  j                               d {    r7 x7 o7 	6 d d d       d {  7   y # 1 d {  7  sw Y   y xY w# t        j                  $ r* t        j                  j                          d {  7   Y y w xY ww)NzOpened process is missing stdinT)by_aliasexclude_nonerL   rJ   )stdinr]   model_dump_jsonrT   encoder8   r<   rU   rV   rW   rX   )r_   jsonr`   rE   write_stream_readers     r/   stdin_writerz"stdio_client.<locals>.stdin_writer   s     }}???}	.*  -@  /*22BBD_cBdD!--,,,,%+__#)#@#@ -    .A     (( 	...++---	.s   DC BC B0BBBA$B0BB0C BB0B0C )B,*C /D0C6B97C>C DC 4D9C<:D?DDD)rU   create_memory_object_stream_get_executable_commandr4   #_create_platform_compatible_processr6   r,   r0   r7   OSErroraclosecreate_task_group
start_soonrf   rS   
fail_afterPROCESS_TERMINATION_TIMEOUTwaitTimeoutError_terminate_process_treeProcessLookupError)rE   rF   read_streamwrite_streamr4   rb   rk   tgr`   ra   rj   s   `       @@@r/   stdio_clientr|   h   s     ',&G&G&J#(-(I(I!(L%L%)&..9 <@F

@V<+-<<\s\u


 
.4." 	!"/ "/%'"/ "/ 	m$
l#	/|++ }}!--..000

%%&AB )!,,.(() $$&&&%%'''$++---%,,...E"/ "/ "/ "/ "/s
    """!!### '')))!((***d"/ "/ 1   )) ) 7 .g666%  ''-.+ }}!--..000  
%%&AB )!,,.(() ) ) 7 .g666%  $$&&&%%'''$++---%,,...E"/ "/ "/ "/ "/ "/ "/ "/s  1PA(G3  G0!G3 %&PI%PPI(P#O+>KO+I./I+0I.5JJ"I>
#J'J/O+J=O+K O+2K3O+
KO+PO(PP*P+P0G3 3I"HI"(H+)I"II"II""P(P+I..	I;7O+:I;;O+>JJJJ:%J(
&J:+O+.J:6O+9J::O+ O+O+O+O%K:3K64K:9O%:	LO%LO%
M#M7L:8M=	MMMO%M=*M-+M=0O%2M=:O%<M==O%NO%,N/-O%OO%O!O%%O+(P+O>	1O42O>	9PPP
PPPr4   c                 B    t         j                  dk(  rt        |       S | S )z
    Get the correct executable command normalized for the current platform.

    Args:
        command: Base command (e.g., 'uvx', 'npx')

    Returns:
        str: Platform-appropriate command
    r   )sysplatformr   )r4   s    r/   rm   rm      s      ||w-g66r1   r6   r,   r7   c                    K   t         j                  dk(  rt        | ||||       d{   }|S t        j                  | g||||d       d{   }|S 7 -7 w)z
    Creates a subprocess in a platform-compatible way.

    Unix: Creates process in a new session/process group for killpg support
    Windows: Creates process in a Job Object for reliable child termination
    r   NT)r,   stderrr7   start_new_session)r~   r   r   rU   open_process)r4   r6   r,   rF   r7   r`   s         r/   rn   rn      sq      ||w.wc63OO N **t"
 
 N P
s!   &AA&AAAAr`   timeout_secondsc                    K   t         j                  dk(  rt        | |       d{    yt        | t              sJ t        | |       d{    y7 ,7 w)af  
    Terminate a process and all its children using platform-specific methods.

    Unix: Uses os.killpg() for atomic process group termination
    Windows: Uses Job Objects via pywin32 for reliable child process cleanup

    Args:
        process: The process to terminate
        timeout_seconds: Timeout in seconds before force killing (default: 2.0)
    r   N)r~   r   r   
isinstancer   r   )r`   r   s     r/   rw   rw     sS      ||w,WoFFF '7+++*7ODDD	 	G 	Es!   #AA&AAAA)r$   )2loggingr(   r~   
contextlibr   pathlibr   typingr   r   rU   anyio.lowlevel	anyio.abcr   anyio.streams.memoryr   r	   anyio.streams.textr
   pydanticr   r   	mcp.typesrP   mcp.os.posix.utilitiesr   mcp.os.win32.utilitiesr   r   r   r   mcp.shared.messager   	getLoggerr=   loggerr   r'   rt   rC   r@   r0   r3   r   r|   rm   rB   rn   floatrw   rD   r1   r/   <module>r      sY    	 
 *  "    R 0 %  ?  .			8	$" ||w 
> & " c3h *I B GJzz m/4 m/f m/ m/`S S & "&ZZ!
s) 
c3h$	 	
 
d	6E7_+D EW\ Egk Er1   