
    Ch#                        S SK r S SKrS SKrS SKJr  S SKJr  S SKJrJ	r	  S SK
r
S SKr
S SKJr  S SKJrJr  S SKJr  S SKJrJr  S SKJr  S S	KJr  S S
KJrJrJrJr  S SKJ r   \ RB                  " \"5      r#\RH                  S:X  a  / SQO/ SQr%Sr&S\'\(\(4   4S jr) " S S\5      r*\\RV                  4S\*S\	4S jj5       r,S\(S\(4S jr-S\RV                  S4S\(S\.\(   S\'\(\(4   S-  S\	S\\(-  S-  4
S jjr/S S\\-  S\0SS4S jjr1g)!    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PATHEXTPROCESSOR_ARCHITECTURESYSTEMDRIVE
SYSTEMROOTTEMPUSERNAMEUSERPROFILE)HOMELOGNAMEr   SHELLTERMUSER       @returnc                      0 n [          HC  n[        R                  R                  U5      nUc  M'  UR	                  S5      (       a  M?  X U'   ME     U $ )zc
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      S/home/james-whalen/.local/lib/python3.13/site-packages/mcp/client/stdio/__init__.pyget_default_environmentr1   3   sO    
 C)

s#=D!!C * J    c                       \ rS rSr% \\S'    \" \S9r\\   \S'    Sr	\
\\4   S-  \S'    Sr\\-  S-  \S'    Sr\\S	'    S
r\S   \S'   Srg)StdioServerParametersH   command)default_factoryargsNr-   cwdzutf-8encodingstrict)r;   ignorereplaceencoding_error_handler )__name__
__module____qualname____firstlineno__str__annotations__r   listr8   r-   dictr9   r   r:   r>   r   __static_attributes__r?   r2   r0   r4   r4   H   s}    L4D1D$s)1;!%Cc3h$	% "Ctd	!AHc FNG$ABMr2   r4   servererrlogc           
       ^ ^^	^
#    [         R                  " S5      u  m	n[         R                  " S5      u  nm
 [        T R                  5      n[	        UT R
                  T R                  b  0 [        5       ET R                  EO	[        5       UT R                  S9I Sh  vN mUU	U 4S jnUU U
4S jn[         R                  " 5        ISh  vN nT ISh  vN   UR                  U5        UR                  U5         X#47v   TR                  (       a#   TR                  R                  5       I Sh  vN    [         R                  " [         5         TR#                  5       I Sh  vN   SSS5        UR                  5       I Sh  vN   UR                  5       I Sh  vN   T	R                  5       I Sh  vN   T
R                  5       I Sh  vN   SSS5      ISh  vN   SSS5      ISh  vN   g GNX! [         af    UR                  5       I Sh  vN    UR                  5       I Sh  vN    T	R                  5       I Sh  vN    T
R                  5       I Sh  vN    e f = f GN GN GN>! [         a     GNIf = f GN! , (       d  f       GN#= f! [$         a    ['        T5      I Sh  vN     GNG[(         a     GNSf = f GND GN/ GN GN! TR                  (       a5   TR                  R                  5       I Sh  vN    O! [         a     Of = f [         R                  " [         5         TR#                  5       I Sh  vN    SSS5        O@! , (       d  f       O2= f! [$         a    ['        T5      I Sh  vN     O[(         a     Of = fUR                  5       I Sh  vN    UR                  5       I Sh  vN    T	R                  5       I Sh  vN    T
R                  5       I Sh  vN    f = f GN%! , ISh  vN  (       d  f       GN;= f GN3! , ISh  vN  (       d  f       g= f7f)z~
Client transport for stdio: this will connect to a server by spawning a
process and communicating with it over stdin/stdout.
r   N)r6   r8   r-   rJ   r9   c                    >#    TR                   (       d   S5       e T IS h  vN   Sn [        TR                   T	R                  T	R                  S9  S h  vN nX-   R	                  S5      nUR                  5       n U HG  n [        R                  R                  U5      n[        U5      nTR                  U5      I S h  vN   MI     M{   N Nx! [         a:  n[        R                  S5        TR                  U5      I S h  vN     S nAM  S nAff = f NU
 S S S 5      IS h  vN    g ! , IS h  vN  (       d  f       g = f! [        R                   a*    [        R                   R#                  5       I S h  vN     g f = f7f)Nz Opened process is missing stdout r:   errors
z+Failed to parse JSONRPC message from server)stdoutr
   r:   r>   splitpoptypesJSONRPCMessagemodel_validate_json	Exceptionlogger	exceptionsendr   anyioClosedResourceErrorlowlevel
checkpoint)
bufferchunklineslinemessageexcsession_messageprocessread_stream_writerrI   s
          r0   stdout_reader#stdio_client.<locals>.stdout_reader   sA    ~~AAA~	.))#4NN#__!88$ G%
 $^2248E"YY[F %%&+&:&:&N&Nt&TG +9*A055oFFF !& *G  ) %",,-Z["4"9"9#">>>$% G#$ *)))))( (( 	...++---	.s   F	E CE ,D.DCD)D.C$D.D

D.E D
D	)D	DD	D.D	D.D.E 'D*(E -F	.E4D75EE F	E 5F=F >FF	FF	c            	        >#    TR                   (       d   S5       e T IS h  vN   T  S h  vN n U R                  R                  SSS9nTR                   R                  US-   R	                  TR
                  TR                  S95      I S h  vN   Mm   Ns Nj N

 S S S 5      IS h  vN    g ! , IS h  vN  (       d  f       g = f! [        R                   a*    [        R                  R                  5       I S h  vN     g f = f7f)NzOpened process is missing stdinT)by_aliasexclude_nonerP   rN   )stdinrc   model_dump_jsonrZ   encoder:   r>   r[   r\   r]   r^   )re   jsonrf   rI   write_stream_readers     r0   stdin_writer"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
 
5D?D DDDD)r[   create_memory_object_stream_get_executable_commandr6   #_create_platform_compatible_processr8   r-   r1   r9   OSErroraclosecreate_task_group
start_soonrm   rW   
fail_afterPROCESS_TERMINATION_TIMEOUTwaitTimeoutError_terminate_process_treeProcessLookupError)rI   rJ   read_streamwrite_streamr6   rh   rr   tgrf   rg   rq   s   `       @@@r0   stdio_clientr   i   sA     ',&G&G&J#(-(I(I!(L%L%)&..9 <@F

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


 
.6." 	!!R
m$
l#	/++ }}!--..000

%%&AB!,,.(( C $$&&&%%'''$++---%,,...A 	 	"!!w
    """!!### '')))!((***h 	" 1   ) CB 7 .g666%  ''-.+ }}!--..000  
%%&AB!,,.(( CBB 7 .g666%  $$&&&%%'''$++---%,,...A 	 	"!!!s  3QA&G;  G8!G; %)QI.QP(I1P(#P
KP
I76I47I7<JJ*J
+J/J7P

KP
"K#P
:K;P
KP
P("P#P('Q2P%3Q8G; ;I+HI+1H42I+
II+#I&$I++Q1P(4I77
JP
JP
J
JJP
JK5J8
6K;P
>	KP

KP
P
P
P
P-L
LLP
LPLP"M0<MMMM0P
M-)M0,P-M00NN	NP	NPNP2N53POP$O'%P=P >PP
P(
P"	PP"	P(%Q(P?.P1/P?;Qr6   c                 D    [         R                  S:X  a  [        U 5      $ U $ )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   )r6   s    r0   ru   ru      s      ||w-g66r2   r8   r-   r9   c                    #    [         R                  S:X  a  [        XX#U5      I Sh  vN nU$ [        R                  " U /UQUUUSS9I Sh  vN nU$  N+ N7f)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-   stderrr9   start_new_session)r   r   r   r[   open_process)r6   r8   r-   rJ   r9   rf   s         r0   rv   rv      sk      ||w.wc3OO N **t"
 
 N P
s!   %AA$AAAArf   timeout_secondsc                    #    [         R                  S:X  a  [        X5      I Sh  vN   g[        U [        5      (       d   e[        X5      I Sh  vN   g N0 N7f)aJ  
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   )rf   r   s     r0   r   r     sP      ||w,WFFF '7++++*7DDD	 	G 	Es!   #AA*AAAA)r%   )2loggingr)   r   
contextlibr   pathlibr   typingr   r   r[   anyio.lowlevel	anyio.abcr   anyio.streams.memoryr   r	   anyio.streams.textr
   pydanticr   r   	mcp.typesrT   mcp.os.posix.utilitiesr   mcp.os.win32.utilitiesr   r   r   r   mcp.shared.messager   	getLoggerr@   rX   r   r(   r|   rG   rD   r1   r4   r   r   ru   rF   rv   floatr   r?   r2   r0   <module>r      sW    	 
 *  "    R 0 %  ?  .			8	$$ ||w 
>! ( " c3h *I B GJzz n/4 n/f n/ n/bS S & "&ZZ!
s) 
c3h$	 	
 
d	6E7_+D EW\ Egk Er2   