
    .h                        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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 ej        dk    rg dng dZdeeef         fdZ G d de          Z eej!        fde defd            Z"dedefdZ#dej!        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S )    N)asynccontextmanager)Path)LiteralTextIO)MemoryObjectReceiveStreamMemoryObjectSendStream)TextReceiveStream)	BaseModelField)SessionMessage   )create_windows_processget_windows_executable_commandterminate_windows_processwin32)APPDATA	HOMEDRIVEHOMEPATHLOCALAPPDATAPATHPROCESSOR_ARCHITECTURESYSTEMDRIVE
SYSTEMROOTTEMPUSERNAMEUSERPROFILE)HOMELOGNAMEr   SHELLTERMUSERreturnc                      i } t           D ]?}t          j                            |          }|$|                    d          r:|| |<   @| S )zo
    Returns a default environment object including only environment variables deemed
    safe to inherit.
    Nz())DEFAULT_INHERITED_ENV_VARSosenvironget
startswith)envkeyvalues      V/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/mcp/client/stdio/__init__.pyget_default_environmentr-   *   s\    
 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<   dS )StdioServerParameterscommand)default_factoryargsNr)   cwdzutf-8encodingstrict)r6   ignorereplaceencoding_error_handler)__name__
__module____qualname__str__annotations__r   listr3   r)   dictr4   r   r5   r9   r    r.   r,   r0   r0   ?   s         LLL4eD111D$s)111;!%Cc3h$	%%% "Ctd	!!!AHc FNG$ABMMM r.   r0   servererrlogc                |   	
K   t          j        d          \  	}t          j        d          \  }
t           j                  }t	          | j         j        i t                       j        nt                      | j                   d{V 	 fd} 
fd}t          j	                    4 d{V 	 }4 d{V  |
                    |           |
                    |           	 ||fW V  t          j        dk    rt                     d{V  nT                                 n?# t          j        dk    rt                     d{V  w                                  w xY wddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  dS # 1 d{V swxY w Y   dS )z
    Client transport for stdio: this will connect to a server by spawning a
    process and communicating with it over stdin/stdout.
    r   N)r1   r3   r)   rC   r4   c                    K   j         s
J d            	 4 d {V  d} t          j         	j        	j                  2 3 d {V }| |z                       d          }|                                } |D ]}	 t          j                            |          }n2# t          $ r%}
                    |           d {V  Y d }~Md }~ww xY wt          |          }
                    |           d {V  6 	 d d d           d {V  d S # 1 d {V swxY w Y   d S # t          j        $ r( t          j                                         d {V  Y d S w xY w)Nz Opened process is missing stdout r5   errors
)stdoutr	   r5   r9   splitpoptypesJSONRPCMessagemodel_validate_json	Exceptionsendr   anyioClosedResourceErrorlowlevel
checkpoint)
bufferchunklineslinemessageexcsession_messageprocessread_stream_writerrB   s
          r,   stdout_readerz#stdio_client.<locals>.stdout_reader~   s     ~AAAAAA	.) G G G G G G G G#4N#_!8$ $ $ G G G G G G G%
 $e^22488E"YY[[F % G G%&+&:&N&Nt&T&TGG( % % %"4"9"9#">">>>>>>>>$HHHH% +9*A*A055oFFFFFFFFFFG$ $G G G G G G G G G G G G G G G G G G G G G G G G G G G G G G& ( 	. 	. 	..++------------	.sj   D  $DC81D:BD
C		$C	?DC			0D:D  
DD  DD   3EEc                    K   j         s
J d            	 4 d {V  2 3 d {V } | j                            dd          }j                             |dz                       j        j                             d {V  f6 	 d d d           d {V  d S # 1 d {V swxY w Y   d S # t          j        $ r( t          j	        
                                 d {V  Y d S w xY w)NzOpened process is missing stdinT)by_aliasexclude_nonerI   rG   )stdinrZ   model_dump_jsonrQ   encoder5   r9   rR   rS   rT   rU   )r\   jsonr]   rB   write_stream_readers     r,   stdin_writerz"stdio_client.<locals>.stdin_writer   s     }??????	.* 
 
 
 
 
 
 
 
-@ 	 	 	 	 	 	 	/*2BB!%D C  D "-,,,,%+_#)#@ -           	 .A-@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 ( 	. 	. 	..++------------	.s?   B/ BBA!B	B/ 
B&&B/ )B&*B/ /3C&%C&r   )rR   create_memory_object_stream_get_executable_commandr1   #_create_platform_compatible_processr3   r)   r-   r4   create_task_group
start_soonsysplatformr   	terminate)rB   rC   read_streamwrite_streamr1   r_   rh   tgr]   r^   rg   s   `       @@@r,   stdio_clientrt   `   sW      ',&G&J&J#(-(I!(L(L%L%%fn55G 8[ z% 8&((7FJ77(**J
 
 
 
 
 
 
 
 
G. . . . . . .4. . . . . . .& 	!!$ $ $ $ $ $ $ $%'$ $ $ $ $ $ $ $ 	m$$$
l###	$|+++++ |w&&/8888888888!!#### |w&&/8888888888!!####$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $sN   ?	F++F4D6;;F6<E22F5F+
F	F+F	F++
F58F5r1   c                 D    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   )rn   ro   r   )r1   s    r,   rj   rj      s$     |w-g666r.   r3   r)   r4   c                    K   t           j        dk    rt          | ||||           d{V }n!t          j        | g||||           d{V }|S )zZ
    Creates a subprocess in a platform-compatible way.
    Returns a process handle.
    r   N)r)   stderrr4   )rn   ro   r   rR   open_process)r1   r3   r)   rC   r4   r]   s         r,   rk   rk      s       |w.wc63OOOOOOOO*t#f#
 
 
 
 
 
 
 
 
 Nr.   )&r%   rn   
contextlibr   pathlibr   typingr   r   rR   anyio.lowlevelanyio.streams.memoryr   r   anyio.streams.textr	   pydanticr
   r   	mcp.typesrM   mcp.shared.messager   r   r   r   r   ro   r$   r@   r=   r-   r0   rw   rt   rj   r?   rk   rA   r.   r,   <module>r      s]   				 



 * * * * * *       " " " " " " " "      R R R R R R R R 0 0 0 0 0 0 % % % % % % % %       - - - - - -         * |w    
>	=	= &c3h    *    I   B GJz V$ V$4 V$f V$ V$ V$ V$rS S    & "&Z! 
s) 
c3h$	 	
 
d	     r.   