
    [ǻi[              
       &   U d dl Z d dlmZ d dlmZmZmZ d dlZd dl	m
Z
m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 d d	lmZ d d
lmZ d dlmZmZmZ d dlm Z   ejB                  dd      Z" e jF                  d      Z$ G d de      Z% G d de      Z& G d de      Z' G d de      Z( G d de      Z)deejT                  ejV                  f   ejX                  z  e-z  ddfdZ.dedef   d ej^                  dej`                  ejb                  z  ejd                  z  fd!Z3dedef   d ejh                  dejj                  ejd                  z  fd"Z6dedef   dejn                  ejd                  z  fd#Z8d ejr                  ddfd$Z: eejV                  ejd                  z        Z;eejV                  ejd                  z     e<d%<    G d& deejz                  ej|                  ejV                  ejT                  ejX                  f         Z?y)'    N)	timedelta)AnyProtocoloverload)MemoryObjectReceiveStreamMemoryObjectSendStream)AnyUrlTypeAdapter)
deprecated)ExperimentalClientFeatures)ExperimentalTaskHandlers)RequestContext)SessionMessage)BaseSessionProgressFnTRequestResponder)SUPPORTED_PROTOCOL_VERSIONSmcpz0.1.0)nameversionclientc                       e Zd Zdedef   dej                  dej                  ej                  z  ej                  z  fdZ
y)SamplingFnTcontextClientSessionparamsreturnc                    K   y wN selfr   r   s      P/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/mcp/client/session.py__call__zSamplingFnT.__call__   s      \_   N)__name__
__module____qualname__r   r   typesCreateMessageRequestParamsCreateMessageResultCreateMessageResultWithTools	ErrorDatar$   r        r#   r   r      sQ    _ 45_ 00_ 
	"	"U%G%G	G%//	Y	_r.   r   c                   l    e Zd Zdedef   dej                  dej                  ej                  z  fdZ	y)ElicitationFnTr   r   r   r   c                    K   y wr   r    r!   s      r#   r$   zElicitationFnT.__call__    s      03r%   N)
r&   r'   r(   r   r   r)   ElicitRequestParamsElicitResultr-   r$   r    r.   r#   r0   r0      sB    3 453 ))3 
		eoo	-	3r.   r0   c                   T    e Zd Zdedef   dej                  ej                  z  fdZy)ListRootsFnTr   r   r   c                    K   y wr   r    )r"   r   s     r#   r$   zListRootsFnT.__call__(   s	     25r%   N)	r&   r'   r(   r   r   r)   ListRootsResultr-   r$   r    r.   r#   r5   r5   '   s0    6%os&:;6				06r.   r5   c                   0    e Zd Zdej                  ddfdZy)
LoggingFnTr   r   Nc                    K   y wr   r    r"   r   s     r#   r$   zLoggingFnT.__call__.         r%   )r&   r'   r(   r)    LoggingMessageNotificationParamsr$   r    r.   r#   r9   r9   -   s    66 
r.   r9   c                   n    e Zd Zdeej
                  ej                  f   ej                  z  ez  ddfdZ	y)MessageHandlerFnTmessager   Nc                    K   y wr   r    )r"   r@   s     r#   r$   zMessageHandlerFnT.__call__5   r<   r%   )
r&   r'   r(   r   r)   ServerRequestClientResultServerNotification	Exceptionr$   r    r.   r#   r?   r?   4   s@    !%"5"5u7I7I"IJUMeMeehqq 
r.   r?   r@   r   c                 \   K   t         j                  j                          d {    y 7 wr   )anyiolowlevel
checkpoint)r@   s    r#   _default_message_handlerrJ   ;   s      ..
#
#
%%%s   ",*,r   r   r   c                 T   K   t        j                  t         j                  d      S w)NzSampling not supportedcoder@   r)   r-   INVALID_REQUESTr   r   s     r#   _default_sampling_callbackrQ   A   s%      ??""(    &(c                 T   K   t        j                  t         j                  d      S w)NzElicitation not supportedrL   rN   rP   s     r#   _default_elicitation_callbackrT   K   s%      ??""+ rR   c                 T   K   t        j                  t         j                  d      S w)NzList roots not supportedrL   rN   )r   s    r#   _default_list_roots_callbackrV   U   s%      ??""* rR   c                    K   y wr   r    r   s    r#   _default_logging_callbackrY   ^   s      	r%   ClientResponsec                   |    e Zd Z	 	 	 	 	 	 	 dJddddeeez     dee   dedz  dedz  de	dz  de
dz  d	edz  d
edz  dej                  dz  dej                  dz  dedz  ddf fdZdej$                  fdZdej(                  dz  fdZedefd       Zdej2                  fdZ	 	 dKdeez  dededz  dedz  ddf
dZdej>                  dej2                  fdZ e! e"d      dedz  dejF                  fd              Z$e!dejJ                  dz  dejF                  fd       Z$e!dejF                  fd        Z$	 dLdd!dedz  dejJ                  dz  dejF                  fd"Z$e! e"d#      dedz  dejL                  fd$              Z'e!dejJ                  dz  dejL                  fd%       Z'e!dejL                  fd&       Z'	 dLdd!dedz  dejJ                  dz  dejL                  fd'Z'd(e(dejR                  fd)Z*d(e(dej2                  fd*Z+d(e(dej2                  fd+Z,	 	 	 dMdd,d-ed.e-ee.f   dz  dedz  d/e/dz  d0e-ee.f   dz  dej`                  fd1Z1d-ed2ej`                  ddfd3Z2e! e"d4      dedz  dejf                  fd5              Z4e!dejJ                  dz  dejf                  fd6       Z4e!dejf                  fd7       Z4	 dLdd!dedz  dejJ                  dz  dejf                  fd8Z4dLd-ed.e-eef   dz  dejj                  fd9Z6	 dLd:ejn                  ejp                  z  d;e-eef   d<e-eef   dz  dejr                  fd=Z:e! e"d>      dedz  dejv                  fd?              Z<e!dejJ                  dz  dejv                  fd@       Z<e!dejv                  fdA       Z<	 dLdd!dedz  dejJ                  dz  dejv                  fdBZ<dNdCZ=dDe>ej~                  ej                  f   ddfdEZAdFe>ej~                  ej                  f   ej                  z  ez  ddfdGZCdHej                  ddfdIZD xZES )Or   N)sampling_capabilitiesexperimental_task_handlersread_streamwrite_streamread_timeout_secondssampling_callbackelicitation_callbacklist_roots_callbacklogging_callbackmessage_handlerclient_infor\   r]   r   c
                x   t         |   ||t        j                  t        j                  |       |	xs t
        | _        |xs t        | _        |
| _	        |xs t        | _        |xs t        | _        |xs t        | _        |xs t         | _        i | _        d | _        d | _        |xs
 t+               | _        y )N)r`   )super__init__r)   rB   rD   DEFAULT_CLIENT_INFO_client_inforQ   _sampling_callback_sampling_capabilitiesrT   _elicitation_callbackrV   _list_roots_callbackrY   _logging_callbackrJ   _message_handler_tool_output_schemas_server_capabilities_experimental_featuresr   _task_handlers)r"   r^   r_   r`   ra   rb   rc   rd   re   rf   r\   r]   	__class__s               r#   ri   zClientSession.__init__p   s     	$$!5 	 	
 (>+>"3"Q7Q&;#%9%Z=Z"$7$W;W!!1!N5N / K3KFH!EI!IM# 9V<T<Vr.   c                   K   | j                   t        ur"| j                  xs t        j                         nd }| j
                  t        ur;t        j                  t        j                         t        j                               nd }| j                  t        urt        j                  d      nd }| j                  t        j                  t        j                  t        j                   t        j"                  t        j$                  ||d || j&                  j)                               | j*                                    t        j,                         d {   }|j.                  t0        vrt3        d|j.                         |j4                  | _        | j9                  t        j:                  t        j<                                      d {    |S 7 7 w)N)formurlT)listChanged)samplingelicitationexperimentalrootstasks)protocolVersioncapabilities
clientInforX   z.Unsupported protocol version from the server: )rl   rQ   rm   r)   SamplingCapabilityrn   rT   ElicitationCapabilityFormElicitationCapabilityUrlElicitationCapabilityro   rV   RootsCapabilitysend_requestClientRequestInitializeRequestInitializeRequestParamsLATEST_PROTOCOL_VERSIONClientCapabilitiesru   build_capabilityrk   InitializeResultr   r   RuntimeErrorr   rs   send_notificationClientNotificationInitializedNotification)r"   r{   r|   r~   results        r#   
initializezClientSession.initialize   s     &&.HH ((FE,D,D,F 	 ))1NN	 ''446224
  	 ((0LL !!d3 	 (('' 88(-(E(E%*%=%=%-(3)-"'"&"5"5"F"F"H& $(#4#4
 ""!
 
& !!)DD!OPVPfPfOghii$*$7$7!$$U%=%=e>[>[>]%^___5
0 	`s%   EGGA8GGGGc                     | j                   S )zReturn the server capabilities received during initialization.

        Returns None if the session has not been initialized yet.
        )rs   r"   s    r#   get_server_capabilitiesz%ClientSession.get_server_capabilities   s    
 (((r.   c                 R    | j                   t        |       | _         | j                   S )a2  Experimental APIs for tasks and other features.

        WARNING: These APIs are experimental and may change without notice.

        Example:
            status = await session.experimental.get_task(task_id)
            result = await session.experimental.get_task_result(task_id, CallToolResult)
        )rt   r   r   s    r#   r}   zClientSession.experimental   s)     &&.*DT*JD'***r.   c                    K   | j                  t        j                  t        j                               t        j                         d{   S 7 w)zSend a ping request.N)r   r)   r   PingRequestEmptyResultr   s    r#   	send_pingzClientSession.send_ping   sC     && 1 1 34
 
 	
 
s   A	AAAprogress_tokenprogresstotalr@   c                    K   | j                  t        j                  t        j                  t        j                  ||||                         d{    y7 w)zSend a progress notification.)progressTokenr   r   r@   rX   N)r   r)   r   ProgressNotificationProgressNotificationParams)r"   r   r   r   r@   s        r#   send_progress_notificationz(ClientSession.send_progress_notification   sV      $$$$** ;;&4!)# '		
 	
 	
s   AAAAlevelc           	         K   | j                  t        j                  t        j                  t        j                  |                  t        j
                         d{   S 7 w)z Send a logging/setLevel request.)r   rX   N)r   r)   r   SetLevelRequestSetLevelRequestParamsr   )r"   r   s     r#   set_logging_levelzClientSession.set_logging_level   sX     &&%% 66UC
 
 
 	
 
   AA(!A&"A(z>Use list_resources(params=PaginatedRequestParams(...)) insteadcursorc                    K   y wr   r    r"   r   s     r#   list_resourceszClientSession.list_resources  s	     UXr%   r   c                   K   y wr   r    r;   s     r#   r   zClientSession.list_resources  s	     qtr%   c                    K   y wr   r    r   s    r#   r   zClientSession.list_resources
  s	     ADr%   rX   c                
  K   ||t        d      ||}n|t        j                  |      }nd}| j                  t        j                  t        j
                  |            t        j                         d{   S 7 w)zSend a resources/list request.

        Args:
            cursor: Simple cursor string for pagination (deprecated, use params instead)
            params: Full pagination parameters including cursor and any future fields
        N%Cannot specify both cursor and paramsr   rX   )
ValueErrorr)   PaginatedRequestParamsr   r   ListResourcesRequestListResourcesResultr"   r   r   request_paramss       r#   r   zClientSession.list_resources  s      &"4DEE#N"99HN!N&& : :. QR%%
 
 	
 
   A:B<B=BzGUse list_resource_templates(params=PaginatedRequestParams(...)) insteadc                    K   y wr   r    r   s     r#   list_resource_templatesz%ClientSession.list_resource_templates(  s	     fir%   c                   K   y wr   r    r;   s     r#   r   z%ClientSession.list_resource_templates,  s      -0r%   c                    K   y wr   r    r   s    r#   r   z%ClientSession.list_resource_templates1  s	     RUr%   c                
  K   ||t        d      ||}n|t        j                  |      }nd}| j                  t        j                  t        j
                  |            t        j                         d{   S 7 w)zSend a resources/templates/list request.

        Args:
            cursor: Simple cursor string for pagination (deprecated, use params instead)
            params: Full pagination parameters including cursor and any future fields
        Nr   r   rX   )r   r)   r   r   r   ListResourceTemplatesRequestListResourceTemplatesResultr   s       r#   r   z%ClientSession.list_resource_templates4  s      &"4DEE#N"99HN!N&& B B. YZ--
 
 	
 
r   uric           	         K   | j                  t        j                  t        j                  t        j                  |                  t        j
                         d{   S 7 w)zSend a resources/read request.r   rX   N)r   r)   r   ReadResourceRequestReadResourceRequestParamsReadResourceResultr"   r   s     r#   read_resourcezClientSession.read_resourceO  sX     &&)) ::sC
 $$
 
 	
 
r   c           	         K   | j                  t        j                  t        j                  t        j                  |                  t        j
                         d{   S 7 w)z#Send a resources/subscribe request.r   rX   N)r   r)   r   SubscribeRequestSubscribeRequestParamsr   r   s     r#   subscribe_resourcez ClientSession.subscribe_resourceZ  sX     &&&& 77C@
 
 
 	
 
r   c           	         K   | j                  t        j                  t        j                  t        j                  |                  t        j
                         d{   S 7 w)z%Send a resources/unsubscribe request.r   rX   N)r   r)   r   UnsubscribeRequestUnsubscribeRequestParamsr   r   s     r#   unsubscribe_resourcez"ClientSession.unsubscribe_resourcee  sX     &&(( 99cB
 
 
 	
 
r   )metar   	argumentsprogress_callbackr   c                x  K   d}|t        j                  j                  di |}| j                  t        j                  t        j
                  t        j                  |||                  t         j                  ||       d{   }|j                  s| j                  ||       d{    |S 7 ,7 w)zBSend a tools/call request with optional progress callback support.N)r   r   _metarX   )request_read_timeout_secondsr   r    )
r)   RequestParamsMetar   r   CallToolRequestCallToolRequestParamsCallToolResultisError_validate_tool_result)r"   r   r   r`   r   r   r   r   s           r#   	call_toolzClientSession.call_toolp  s      26'',,4t4E((%% 66DI]bc
   )=/ ) 	
 	
 ~~,,T6:::	
 ;s$   BB:	B6
%B:/B80B:8B:r   c                   K   || j                   vr| j                          d{    d}|| j                   v r| j                   j                  |      }nt        j	                  d| d       |:ddlm}m}m} |j                  t        d| d      	  ||j                  |       yy7 # |$ r}t        d| d|       d}~w|$ r}t        d	| d|       d}~ww xY ww)
zKValidate the structured content of a tool result against its output schema.NzTool z= not listed by server, cannot validate any structured contentr   )SchemaErrorValidationErrorvalidatez; has an output schema but did not return structured contentz,Invalid structured content returned by tool z: zInvalid schema for tool )rr   
list_toolsgetloggerwarning
jsonschemar   r   r   structuredContentr   )r"   r   r   output_schemar   r   r   es           r#   r   z#ClientSession._validate_tool_result  s	    t000//###4,,, 5599$?MNNU4&(efg$II''/"D6!\] K11=A % $" # _"%QRVQWWYZ[Y\#]^^ K"%=dV2aS#IJJKs@   "C$B+A0C$B- )C$-C!2CC!CC!!C$z<Use list_prompts(params=PaginatedRequestParams(...)) insteadc                    K   y wr   r    r   s     r#   list_promptszClientSession.list_prompts  s	     QTr%   c                   K   y wr   r    r;   s     r#   r   zClientSession.list_prompts  s	     mpr%   c                    K   y wr   r    r   s    r#   r   zClientSession.list_prompts  s	     =@r%   c                
  K   ||t        d      ||}n|t        j                  |      }nd}| j                  t        j                  t        j
                  |            t        j                         d{   S 7 w)zSend a prompts/list request.

        Args:
            cursor: Simple cursor string for pagination (deprecated, use params instead)
            params: Full pagination parameters including cursor and any future fields
        Nr   r   rX   )r   r)   r   r   r   ListPromptsRequestListPromptsResultr   s       r#   r   zClientSession.list_prompts  s      &"4DEE#N"99HN!N&& 8 8 OP##
 
 	
 
r   c           
         K   | j                  t        j                  t        j                  t        j                  ||                  t        j
                         d{   S 7 w)zSend a prompts/get request.)r   r   rX   N)r   r)   r   GetPromptRequestGetPromptRequestParamsGetPromptResult)r"   r   r   s      r#   
get_promptzClientSession.get_prompt  sZ     &&&& 77TYW
 !!
 
 	
 
s   A A)"A'#A)refargumentcontext_argumentsc                 2  K   d}|t        j                  |      }| j                  t        j                  t        j                  t        j
                  |t        j                  di ||                  t         j                         d{   S 7 w)z#Send a completion/complete request.N)r   )r   r   r   rX   r    )r)   CompletionContextr   r   CompleteRequestCompleteRequestParamsCompletionArgumentCompleteResult)r"   r   r   r   r   s        r#   completezClientSession.complete  s      (--8IJG&&%% 66!&!9!9!EH!E '   
 
 	
 
s   BBBBz:Use list_tools(params=PaginatedRequestParams(...)) insteadc                    K   y wr   r    r   s     r#   r   zClientSession.list_tools  s	     MPr%   c                   K   y wr   r    r;   s     r#   r   zClientSession.list_tools  s	     ilr%   c                    K   y wr   r    r   s    r#   r   zClientSession.list_tools  s	     9<r%   c                v  K   ||t        d      ||}n|t        j                  |      }nd}| j                  t        j                  t        j
                  |            t        j                         d{   }|j                  D ]%  }|j                  | j                  |j                  <   ' |S 7 :w)zSend a tools/list request.

        Args:
            cursor: Simple cursor string for pagination (deprecated, use params instead)
            params: Full pagination parameters including cursor and any future fields
        Nr   r   rX   )r   r)   r   r   r   ListToolsRequestListToolsResulttoolsoutputSchemarr   r   )r"   r   r   r   r   tools         r#   r   zClientSession.list_tools  s      &"4DEE#N"99HN!N(( 6 6n MN!!
 
 LL 	ED373D3DD%%dii0	E 
s   A:B9<B7=;B9c                    K   | j                  t        j                  t        j                                      d{    y7 w)z'Send a roots/list_changed notification.N)r   r)   r   RootsListChangedNotificationr   s    r#   send_roots_list_changedz%ClientSession.send_roots_list_changed  s.     $$U%=%=e>`>`>b%cddds   :AAA	responderc                 .  K   t        t        t        f   |j                  |j                  | d       }| j
                  j                  |j                        r0|5  | j
                  j                  ||       d {    d d d        y |j                  j                  xt        j                  d x\  } |5  |j                  0| j
                  j                  |||j                         d {   }n| j                  ||       d {   }t        j!                  |      }|j#                  |       d {    d d d        y  xt        j$                  d x\  } |5  |j                  0| j
                  j'                  |||j                         d {   }n| j)                  ||       d {   }t        j!                  |      }|j#                  |       d {    d d d        y  xt        j*                  d xV\    |5  | j-                  |       d {   }t        j!                  |      }|j#                  |       d {    d d d        y  t        j.                  d xM\   |5  |j#                  t        j0                  t        j2                                      d {   cd d d        S  	 	 y 7 D# 1 sw Y   y xY w7 7 7 # 1 sw Y   y xY w7 K7 37 # 1 sw Y   y xY w7 7 # 1 sw Y   y xY w7 Z# 1 sw Y   y xY ww)N)
request_idr   sessionlifespan_contextrX   r    )root)r   r   r   r  request_metaru   handles_requestrequesthandle_requestr  r)   CreateMessageRequesttaskaugmented_samplingrl   rZ   validate_pythonrespondElicitRequestaugmented_elicitationrn   ListRootsRequestro   r   rC   r   )r"   r  ctxr   responseclient_responses         r#   _received_requestzClientSession._received_request!  s    ]C/0 ++''!	
 ..y/@/@A I))88iHHHI $$:++: ={{.)-)<)<)O)OPSU[]c]h]h)i#i)-)@)@f)M#M&4&D&DX&NO#++O<<<=@ C ; 4$$3 ={{.)-)<)<)R)RSVX^`f`k`k)l#l)-)C)CC)P#P&4&D&DX&NO#++O<<<=, / 4 *'') =%)%>%>s%CCH&4&D&DX&NO#++O<<<=  * ""$ a!*!2!253E3E5K\K\K^3_!``a a % M II $j#M<=@ ' $m#P<=,   D<=  aa s2  AL K:J>;K?8L77K.K/K	K
-K7K8K<$L 7K+K"K+2K%3-K+ K(!K+%#LK;K7-K;K9K;!L1;L	,L-L	0L>KK
LKKKKL"K+%K+(K++K40L7K;9K;;L LL		LLreqc                 B   K   | j                  |       d{    y7 w)z>Handle incoming messages by forwarding to the message handler.N)rq   )r"   r%  s     r#   _handle_incomingzClientSession._handle_incomingT  s     
 ##C(((s   notificationc                    K   |j                   xt        j                  d x\  } | j                  |       d{    y t        j                  d x\  }y 	 y7  w)z%Handle notifications from the server.rX   N)r  r)   LoggingMessageNotificationrp   ElicitCompleteNotification)r"   r(  r   s      r#   _received_notificationz$ClientSession._received_notification[  s]      @11@,,V444 A11@ 	 A
  5s   8AA!A)NNNNNNN)NNr   )NNN)r   N)Fr&   r'   r(   r   r   rE   r   r   r   r0   r5   r9   r?   r)   Implementationr   r   ri   r   r   ServerCapabilitiesr   propertyr   r}   r   r   strintfloatr   LoggingLevelr   r   r   r   r   r   r   r   r	   r   r   r   r   dictr   r   r   r   r   r   r   r   r   ResourceTemplateReferencePromptReferencer  r  r  r   r  r   rB   rC   r$  rD   r'  r,  __classcell__)rv   s   @r#   r   r   g   s    26046:37.24837"W BFFJ"W.~	/IJ"W -^<"W ($.	"W
 '-"W -t3"W *D0"W %t+"W +T1"W ))D0"W  %77$>"W %=t$C"W 
"WH1%"8"8 1f))A)AD)H ) +8 + +
!2!2 
 #"
c	
 
 t|	

 t
 

*	
U-?-? 	
EDUDU 	
 PQX3:X%:S:SX R Xte.J.JT.QtV[VoVot tDe&?&?D D "
 7;	
d

 ,,t3	

 
	"	"
6 YZiC$Ji5CdCdi [ i055<0		*	*0 0 Uu/P/PU U "
 7;	
d

 ,,t3	

 
	*	*
6	
v 	
%2J2J 	
	
F 	
u7H7H 	
	
f 	
9J9J 	
 ,01504 '+ S>D( ($.	
 '- 38nt# 
		>K KU=Q=Q KVZ K4 NOTtT8O8OT P TpE,H,H4,OpTYTkTkp p@E$;$;@ @ "
 7;	
d

 ,,t3	

 
	 	 
6	
S 	
T#s(^d5J 	
V[VkVk 	
 48	
,,u/D/DD
 sCx.
  S>D0	

 
		
0 LMPsTzPe6K6KP N Pl%*F*F*MlRWRgRgl l<%"7"7< < "  7;	 d
  ,,t3	 
 
		 De11A%BUBUW\WiWiBi1j 1os 1f)e1153E3EEFIaIaadmm) 
)9Q9Q VZ r.   )@loggingdatetimer   typingr   r   r   anyio.lowlevelrG   anyio.streams.memoryr   r   pydanticr	   r
   typing_extensionsr   	mcp.typesr)   mcp.client.experimentalr   %mcp.client.experimental.task_handlersr   mcp.shared.contextr   mcp.shared.messager   mcp.shared.sessionr   r   r   mcp.shared.versionr   r-  rj   	getLoggerr   r   r0   r5   r9   r?   rB   rC   rD   rE   rJ   r*   r+   r,   r-   rQ   r2   r3   rT   r7   rV   r=   rY   rZ   __annotations__r   r   r   r    r.   r#   <module>rH     s&     * *  R ( (  > J - - I I :*e**wG 			8	$_( _3X 368 6  &e1153E3EEFIaIaadmm&	&OS01,, !C!CCeooUOS01%% %//)OS01
U__,	22			 EPPUPbPbejetetPtDuE..@A u@    		"@r.   