
    {hN              
          d dl Z d dlmZ d dl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Zd dlZd dlmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZmZmZ d dlmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.  ede"e-      Z/ ede#e.      Z0 ede!e,      Z1 ede"e-      Z2 ede      Z3 ede!e,      Z4e5e6z  Z7 G d de      Z8 G d dee2e0f         Z9 G d dee/e1e0e2e4f         Z:y)    N)Callable)AsyncExitStack)	timedelta)TracebackType)AnyGenericProtocolTypeVar)MemoryObjectReceiveStreamMemoryObjectSendStream)	BaseModel)Self)McpError)MessageMetadataServerMessageMetadataSessionMessage)CONNECTION_CLOSEDINVALID_PARAMSCancelledNotificationClientNotificationClientRequestClientResult	ErrorDataJSONRPCErrorJSONRPCMessageJSONRPCNotificationJSONRPCRequestJSONRPCResponseProgressNotificationRequestParamsServerNotificationServerRequestServerResultSendRequestTSendResultTSendNotificationTReceiveRequestTReceiveResultT)boundReceiveNotificationTc                   4    e Zd ZdZdededz  dedz  ddfdZy)ProgressFnTz-Protocol for progress notification callbacks.progresstotalNmessagereturnc                    K   y wN )selfr-   r.   r/   s       S/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/mcp/shared/session.py__call__zProgressFnT.__call__1           )__name__
__module____qualname____doc__floatstrr6   r3       r5   r,   r,   .   s&    7dudUT\dCRVJd[_dr?   r,   c                       e Zd ZdZ	 ddedej                  dz  deddded	ge	f   d
e
ddfdZddZdee   dz  dedz  dedz  ddfdZdeez  ddfdZddZedefd       Zedefd       Zy)RequestRespondera  Handles responding to MCP requests and manages request lifecycle.

    This class MUST be used as a context manager to ensure proper cleanup and
    cancellation handling:

    Example:
        with request_responder as resp:
            await resp.respond(result)

    The context manager ensures:
    1. Proper cancellation scope setup and cleanup
    2. Request completion tracking
    3. Cleanup of in-flight requests
    N
request_idrequest_metarequestsessionzBaseSession[
            SendRequestT,
            SendNotificationT,
            SendResultT,
            ReceiveRequestT,
            ReceiveNotificationT
        ]on_complete.RequestResponder[ReceiveRequestT, SendResultT]message_metadatar0   c                     || _         || _        || _        || _        || _        d| _        t        j                         | _        || _	        d| _
        y )NF)rB   rC   rD   rH   _session
_completedanyioCancelScope_cancel_scope_on_complete_entered)r4   rB   rC   rD   rE   rF   rH   s          r5   __init__zRequestResponder.__init__D   sP     %( 0"..0'r?   c                 z    d| _         t        j                         | _        | j                  j	                          | S )zBEnter the context manager, enabling request cancellation tracking.T)rP   rL   rM   rN   	__enter__r4   s    r5   rS   zRequestResponder.__enter__]   s1    "..0$$&r?   exc_typeexc_valexc_tbc                 6   	 | j                   r| j                  |        d| _        | j                  st	        d      | j                  j                  |||       y# d| _        | j                  st	        d      | j                  j                  |||       w xY w)zFExit the context manager, performing cleanup and notifying completion.FNo active cancel scopeN)rK   rO   rP   rN   RuntimeError__exit__r4   rU   rV   rW   s       r5   r[   zRequestResponder.__exit__d   s    	C!!$'!DM%%"#;<<'''6B "DM%%"#;<<'''6Bs   A =Bresponsec                    K   | j                   st        d      | j                  rJ d       | j                  s7d| _        | j                  j                  | j                  |       d{    yy7 w)zSend a response for this request.

        Must be called within a context manager block.
        Raises:
            RuntimeError: If not used within a context manager
            AssertionError: If request was already responded to
        2RequestResponder must be used as a context managerzRequest already responded toTrB   r]   N)rP   rZ   rK   	cancelledrJ   _send_responserB   )r4   r]   s     r5   respondzRequestResponder.respondt   sp      }}STT??B$BB"~~"DO--..??X /    s   A(A3*A1+A3c                 $  K   | j                   st        d      | j                  st        d      | j                  j                          d| _        | j
                  j                  | j                  t        ddd             d{    y7 w)	z-Cancel this request and mark it as completed.r_   rY   Tr   zRequest cancelledNcoder/   datar`   )	rP   rZ   rN   cancelrK   rJ   rb   rB   r   rT   s    r5   rh   zRequestResponder.cancel   s|     }}STT!!788!!#mm**A/BN + 
 	
 	
s   BBB	Bc                 :    | j                    xr | j                   S r2   )rK   ra   rT   s    r5   	in_flightzRequestResponder.in_flight   s    ??"94>>'99r?   c                 .    | j                   j                  S r2   )rN   cancel_calledrT   s    r5   ra   zRequestResponder.cancelled   s    !!///r?   r2   )r0   rG   r0   N)r9   r:   r;   r<   	RequestIdr    Metar'   r   r   r   rQ   rS   typeBaseExceptionr   r[   r%   r   rc   rh   propertyboolrj   ra   r3   r?   r5   rA   rA   4   s   8 -1 $((4/ !	
 OPRUUV * 
2C}%,C %C $	C
 
C kI&= $ &
 :4 : : 04 0 0r?   rA   c                      e Zd ZU dZeeeeez     f   e	d<   e
e	d<   eeeeef   f   e	d<   eeef   e	d<   	 d*deeez     dee   d	ee   d
ee   dedz  ddfdZdefdZdee   dz  dedz  dedz  dedz  fdZ	 	 	 d+dedee   dedz  dededz  defdZ	 d*de dedz  ddfdZ!dedee"z  ddfdZ#d,dZ$d eeef   ddfd!Z%deddfd"Z&	 	 d-d#e'e
z  d$e(d%e(dz  d&e'dz  ddf
d'Z)d(eeef   ez  ez  ddfd)Z*y).BaseSessiona  
    Implements an MCP "session" on top of read/write streams, including features
    like request/response linking, notifications, and progress.

    This class is an async context manager that automatically starts processing
    messages when entered.
    _response_streams_request_id
_in_flight_progress_callbacksNread_streamwrite_streamreceive_request_typereceive_notification_typeread_timeout_secondsr0   c                     || _         || _        i | _        d| _        || _        || _        || _        i | _        i | _        t               | _
        y )Nr   )_read_stream_write_streamrv   rw   _receive_request_type_receive_notification_type_session_read_timeout_secondsrx   ry   r   _exit_stack)r4   rz   r{   r|   r}   r~   s         r5   rQ   zBaseSession.__init__   sV     ()!#%9"*C'-A*#% )+r?   c                    K   t        j                         | _        | j                  j                          d {    | j                  j	                  | j
                         | S 7 +wr2   )rL   create_task_group_task_group
__aenter__
start_soon_receive_looprT   s    r5   r   zBaseSession.__aenter__   sS      224))+++##D$6$67 	,s   7A'A%,A'rU   rV   rW   c                    K   | j                   j                          d {    | j                  j                  j	                          | j                  j                  |||       d {   S 7 M7 wr2   )r   acloser   cancel_scoperh   	__aexit__r\   s       r5   r   zBaseSession.__aexit__   sd      %%''' 	%%,,.%%//'6JJJ 	(
 Ks"   A2A.AA2)A0*A20A2rD   result_typerequest_read_timeout_secondsmetadataprogress_callbackc                   K   | j                   }|dz   | _         t        j                  t        t        z     d      \  }}|| j
                  |<   |j                  ddd      }	|2d|	vri |	d<   d|	d   vri |	d   d<   ||	d   d   d<   || j                  |<   	 t        dd	|d
|	}
| j                  j                  t        t        |
      |             d{    d}||j                         }n&| j                  | j                  j                         }	 t        j                  |      5  |j!                          d{   }ddd       t3        t              rt%        |j4                        |j7                  |j8                        | j
                  j;                  |d       | j                  j;                  |d       |j=                          d{    |j=                          d{    S 7 7 # 1 sw Y   xY w# t"        $ rJ t%        t'        t(        j*                  j,                  d|j.                  j0                   d| d            w xY w7 7 n# | j
                  j;                  |d       | j                  j;                  |d       |j=                          d{  7   |j=                          d{  7   w xY ww)a>  
        Sends a request and wait for a response. Raises an McpError if the
        response contains an error. If a request read timeout is provided, it
        will take precedence over the session read timeout.

        Do not use this method to emit notifications! Use send_notification()
        instead.
           Tjsonby_aliasmodeexclude_noneNparams_metaprogressToken2.0)jsonrpcidr/   r   z(Timed out while waiting for response to z	. Waited z	 seconds.rf   r/   r3   )rw   rL   create_memory_object_streamr   r   rv   
model_dumpry   r   r   sendr   r   total_secondsr   
fail_afterreceiveTimeoutErrorr   r   httpxcodesREQUEST_TIMEOUT	__class__r9   
isinstanceerrormodel_validateresultpopr   )r4   rD   r   r   r   r   rB   response_streamresponse_stream_readerrequest_datajsonrpc_requesttimeoutresponse_or_errors                r5   send_requestzBaseSession.send_request   s      %%
%>272S2STcfrTr2stu2v//-<z* ))4fSW)X(|+)+X&l84424X&w/?IL"7+O<3DD$$Z0(	2,  O $$)).P_A`ks*tuuu G+76DDF33?<<JJL%%g. O.D.L.L.N(N%O +\:06677"112C2J2JK ""&&z48$$((T:!((***(//111C v )OO O 
"[[88F&0099:)&iy2	 	
( +1 ""&&z48$$((T:!((***(//111s   BKA I! G6?I! H -G;G9G;H ?I! AKIK0I1K6I! 9G;;H H AII! KK!AK-J0.KK	KKnotificationrelated_request_idc           	         K   t        d	ddi|j                  ddd      }t        t        |      |rt	        |      nd      }| j
                  j                  |       d{    y7 w)
zk
        Emits a notification, which is a one-way message that does not expect
        a response.
        r   r   Tr   r   )r   Nr   r3   )r   r   r   r   r   r   r   )r4   r   r   jsonrpc_notificationsession_messages        r5   send_notificationzBaseSession.send_notification(  su       3  
 
%%t&t%T 
 )"#78Ug*>PQmq
   %%o666s   A"A,$A*%A,rB   r]   c           	      r  K   t        |t              rGt        d||      }t        t	        |            }| j
                  j                  |       d {    y t        d||j                  ddd            }t        t	        |            }| j
                  j                  |       d {    y 7 ^7 w)Nr   r   r   r   r/   Tr   r   )r   r   r   )	r   r   r   r   r   r   r   r   r   )r4   rB   r]   jsonrpc_errorr   jsonrpc_responses         r5   rb   zBaseSession._send_response=  s     h	*(:XVM,^M5RSO$$))/:::.**DvTX*Y 
 -^DT5UVO$$))/::: ; ;s%   AB7B3AB7-B5.B75B7c                 @   K    j                   4 d {     j                  4 d {    	  j                   2 3 d {   }t        |t              r j	                  |       d {    3t        |j
                  j                  t              r	  j                  j                  |j
                  j                  j                  ddd            }t        |j
                  j                  j                  |j                  j                  r |j                  j                  j                  nd |  fd|j                        }| j                   |j"                  <    j%                  |       d {    |j&                  s j	                  |       d {    et        |j
                  j                  t:              r	  j<                  j                  |j
                  j                  j                  ddd            }t        |j                  t>              rU|j                  j                  j@                  }| j                   v r j                   |   jC                          d {    nt        |j                  tD              r|j                  j                  jF                  }	|	 jH                  v r{ jH                  |	   }
 |
|j                  j                  jJ                  |j                  j                  jL                  |j                  j                  j
                         d {     jO                  |       d {     j	                  |       d {    2 jP                  jS                  |j
                  j                  j                  d       }|r/|j9                  |j
                  j                         d {     j	                  tU        d|              d {    7 7 7 7 7 7 u# t        $ r}t)        j*                  d|        t)        j,                  d|j
                  j                          t/        d|j
                  j                  j                  t1        t2        d	d
            }t5        t7        |            } j                  j9                  |       d {  7   Y d }~d }~ww xY w7 q7 7 7 # t        $ r:}t)        j*                  d| d|j
                  j                          Y d }~d }~ww xY w7 b7 >6 nW# tV        jX                  $ r t)        j,                  d       Y n-t        $ r"}t)        jZ                  d|        Y d }~nd }~ww xY w jP                  j]                         D ]e  \  }}t1        t^        d      }	 |j9                  t/        d||             d {  7   |ja                          d {  7   X# t        $ r Y cw xY w  jP                  jc                          n#  jP                  j]                         D ]e  \  }}t1        t^        d      }	 |j9                  t/        d||             d {  7   |ja                          d {  7   X# t        $ r Y cw xY w  jP                  jc                          w xY wd d d       d {  7   n# 1 d {  7  sw Y   nxY wd d d       d {  7   y # 1 d {  7  sw Y   y xY ww)NTr   r   c                 P    j                   j                  | j                  d       S r2   )rx   r   rB   )rr4   s    r5   <lambda>z+BaseSession._receive_loop.<locals>.<lambda>`  s    doo6I6I!,,X\6] r?   )rB   rC   rD   rE   rF   rH   zFailed to validate request: z Message that failed validation: r   zInvalid request parameters re   r   r   z!Failed to validate notification: z. Message was: z.Received response with an unknown request ID: zRead stream closed by clientz%Unhandled exception in receive loop: zConnection closedr   )2r   r   r   	Exception_handle_incomingr/   rootr   r   r   r   rA   r   r   metar   rx   rB   _received_requestrK   loggingwarningdebugr   r   r   r   r   r   r   r   r   	requestIdrh   r   r   ry   r-   r.   _received_notificationrv   r   rZ   rL   ClosedResourceError	exceptionitemsr   r   clear)r4   r/   validated_request	respondereerror_responser   r   cancelled_idprogress_tokencallbackstreamr   r   s   `             r5   r   zBaseSession._receive_loopK  s    h	/ h	/h	/ h	/d/%)%6%6 M M'!'95"33G<<<#GOO$8$8.I"K040J0J0Y0Y ' 4 4 ? ?TZim ? n1- )9+2??+?+?+B+B#4#9#9#@#@ .?-C-C-J-J-O-O%)(9(,,]181A1A	)I ENDOOI,@,@A"&"8"8"CCC#,#7#7&*&;&;I&F F F$ $GOO$8$8:MN+/+J+J+Y+Y ' 4 4 ? ?TZim ? n,L  *,*;*;=RS/;/@/@/G/G/Q/Q#/4??#B*.//,*G*N*N*P$P$P $.l.?.?AU#V5A5F5F5M5M5[5[N (69Q9Q'Q373K3KN3[.6,8,=,=,D,D,M,M,8,=,=,D,D,J,J,8,=,=,D,D,L,L/* )* )*
 '+&A&A,&O O O&*&;&;L&I I I "&!7!7!;!;GOO<P<P<S<SUY!Z!"(++goo.B.B"CCC"&"7"7 ,/]^e]f-g h#  ah	/ h	/
M<" D !G( K $OO.J1#,NO#MM,LW__MaMaLb*cd-9(-#*??#7#7#:#:&/)7,H)+'".N /=^TbEc.dO"&"4"4"9"9/"JJJK4 %Q)*
 !P I( #OO"CA3oV]VeVeVjVjUk l  DW &7^ ,, > <= O !!$I!"MNNO #'"8"8">">"@ JB%+<FYZE$kk,uSX*YZZZ$mmo--$  &&,,. #'"8"8">">"@ JB%+<FYZE$kk,uSX*YZZZ$mmo--$  &&,,.Qh	/ h	/ h	/ h	/ h	/ h	/ h	/ h	/ h	/ h	/se  \O?\\	P\	["T;T9P
 T9%T;(P
)*T;C P4P
5$PP
P'T;B*S-0S!
1CS-4S$
5S-S'
S-&S*
'S-+A&T;T3
&T;8T6
9T;?\\	T9T;PP	S	B3S	SS	T;S	T;!S-$S-'S-*S--	T0	6/T+	%T;+T0	0T;6T;9T;:X/;(V#X/%V-V
X/
VX/1[" X$W'
%X=X 
>X["	X	["X	["/2[" Z"
ZZ"
ZZ"
!["	Z.+[-Z..[["\	[\	"[4	([+)[4	0\	7\\\	\\\\r   c                    K   yw)z
        Can be overridden by subclasses to handle a request without needing to
        listen on the message stream.

        If the request is responded to within this method, it will not be
        forwarded on to the message stream.
        Nr3   )r4   r   s     r5   r   zBaseSession._received_request  r7   r8   c                    K   yw)z
        Can be overridden by subclasses to handle a notification without needing
        to listen on the message stream.
        Nr3   )r4   r   s     r5   r   z"BaseSession._received_notification  r7   r8   r   r-   r.   r/   c                    K   yw)zh
        Sends a progress notification for a request that is currently being
        processed.
        Nr3   )r4   r   r-   r.   r/   s        r5   send_progress_notificationz&BaseSession.send_progress_notification  r7   r8   reqc                    K   yw)zCA generic handler for incoming messages. Overwritten by subclasses.Nr3   )r4   r   s     r5   r   zBaseSession._handle_incoming  s     
 	r8   r2   )NNNrm   )NN)+r9   r:   r;   r<   dictrn   r   r   r   __annotations__intrA   r'   r%   r,   r   r   r   rp   r*   r   rQ   r   r   rq   r   rs   r   r$   r(   r   r   r&   r   r   rb   r   r   r   r>   r=   r   r   r3   r?   r5   ru   ru      sl    I'=oP\>\']]^^Y 0+1M NNOOi455 26,.~	/IJ, -^<, #?3	,
 $((<#=, ($., 
,($ K}%,K %K $	K
 
K" :>$(04J2J2 .)J2 '0$&6	J2
 "J2 '-J2 
J2^ 047'7 &,7 
	7*;y ;KR[D[ ;`d ;i/V1A/S^B^1_ dh 9M RV  #"
c	
 
 t|	

 t
 

o{:;>RRU^^ 
r?   ru   );r   collections.abcr   
contextlibr   datetimer   typesr   typingr   r   r	   r
   rL   r   anyio.streams.memoryr   r   pydanticr   typing_extensionsr   mcp.shared.exceptionsr   mcp.shared.messager   r   r   	mcp.typesr   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r*   r>   r   rn   r,   rA   ru   r3   r?   r5   <module>r      s    $ %   2 2   R  " * U U    ( ~}mDm\<@/1CEWX +]MJ);57IK]^ #I	e( eh0w;< h0Vw		wr?   