o
    h>                  	   @   s  U d dl Z d dlmZ d dlmZmZ d dlZd dlm	Z	m
Z
 d dlmZ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mZmZ d d
lmZ ejdddZe  dZ!G dd deZ"G dd deZ#G dd deZ$G dd deZ%G dd deZ&deej'ej(f ej)B e*B ddfddZ+dedef dej,dej-ej.B fd d!Z/dedef dej0dej1ej.B fd"d#Z2dedef dej3ej.B fd$d%Z4dej5ddfd&d'Z6eej(ej.B Z7eej(ej.B  e8d(< G d)d deej9ej:ej(ej'ej)f Z;dS )*    N)	timedelta)AnyProtocol)MemoryObjectReceiveStreamMemoryObjectSendStream)SchemaErrorValidationErrorvalidate)AnyUrlTypeAdapter)RequestContext)SessionMessage)BaseSessionProgressFnTRequestResponder)SUPPORTED_PROTOCOL_VERSIONSmcpz0.1.0)nameversionclientc                   @   4   e Zd Zdedef dejdejejB fddZ	dS )SamplingFnTcontextClientSessionparamsreturnc                       d S N selfr   r   r   r   S/var/www/html/openai_agents/venv/lib/python3.10/site-packages/mcp/client/session.py__call__      zSamplingFnT.__call__N)
__name__
__module____qualname__r   r   typesCreateMessageRequestParamsCreateMessageResult	ErrorDatar"   r   r   r   r!   r          

r   c                   @   r   )ElicitationFnTr   r   r   r   c                    r   r   r   r   r   r   r!   r"      r#   zElicitationFnT.__call__N)
r$   r%   r&   r   r   r'   ElicitRequestParamsElicitResultr*   r"   r   r   r   r!   r,      r+   r,   c                   @   s.   e Zd Zdedef dejejB fddZdS )ListRootsFnTr   r   r   c                    r   r   r   )r    r   r   r   r!   r"   &   s   zListRootsFnT.__call__N)	r$   r%   r&   r   r   r'   ListRootsResultr*   r"   r   r   r   r!   r/   %   s    

r/   c                   @   s    e Zd ZdejddfddZdS )
LoggingFnTr   r   Nc                    r   r   r   )r    r   r   r   r!   r"   ,      zLoggingFnT.__call__)r$   r%   r&   r'    LoggingMessageNotificationParamsr"   r   r   r   r!   r1   +   s    r1   c                   @   s4   e Zd Zdeejejf ejB eB ddfddZ	dS )MessageHandlerFnTmessager   Nc                    r   r   r   )r    r5   r   r   r!   r"   3   r2   zMessageHandlerFnT.__call__)
r$   r%   r&   r   r'   ServerRequestClientResultServerNotification	Exceptionr"   r   r   r   r!   r4   2   s    r4   r5   r   c                    s   t j I d H  d S r   )anyiolowlevel
checkpoint)r5   r   r   r!   _default_message_handler9   s   r=   r   r   r   c                       t jt jddS )NzSampling not supportedcoder5   r'   r*   INVALID_REQUESTr   r   r   r   r!   _default_sampling_callback?   
   rD   c                    r>   )NzElicitation not supportedr?   rA   rC   r   r   r!   _default_elicitation_callbackI   rE   rF   c                    r>   )NzList roots not supportedr?   rA   )r   r   r   r!   _default_list_roots_callbackS   s
   rG   c                    r   r   r   r   r   r   r!   _default_logging_callback\   r2   rI   ClientResponsec                       s  e Zd Z							dEdeeeB  dee dedB dedB de	dB de
dB dedB d	edB d
ejdB ddf fddZdejfddZdejfddZ		dFdeeB dededB dedB ddf
ddZdejdejfddZdGdedB dejfddZdGdedB dejfddZd edejfd!d"Z d edejfd#d$Z!d edejfd%d&Z"			dHd'ed(e#ee$f dB dedB d)e%dB dej&f
d*d+Z'd'ed,ej&ddfd-d.Z(dGdedB dej)fd/d0Z*dGd'ed(e#eef dB dej+fd1d2Z,	dGd3ej-ej.B d4e#eef d5e#eef dB dej/fd6d7Z0dGdedB dej1fd8d9Z2dId:d;Z3d<e4ej5ej6f ddfd=d>Z7d?e4ej5ej6f ej8B eB ddfd@dAZ9dBej8ddfdCdDZ:  Z;S )Jr   Nread_streamwrite_streamread_timeout_secondssampling_callbackelicitation_callbacklist_roots_callbacklogging_callbackmessage_handlerclient_infor   c
           
         s`   t  j||tjtj|d |	pt| _|pt| _|pt	| _
|pt| _|p$t| _|p)t| _i | _d S )N)rM   )super__init__r'   r6   r8   DEFAULT_CLIENT_INFO_client_inforD   _sampling_callbackrF   _elicitation_callbackrG   _list_roots_callbackrI   _logging_callbackr=   _message_handler_tool_output_schemas)
r    rK   rL   rM   rN   rO   rP   rQ   rR   rS   	__class__r   r!   rU   n   s   






zClientSession.__init__c                    s   | j tur
t nd }| jturt nd }| jtur"tj	ddnd }| 
ttjtjtjtj||d |d| jddtjI d H }|jtvrQtd|j | tt I d H  |S )NT)listChanged)samplingelicitationexperimentalroots)protocolVersioncapabilities
clientInforH   z.Unsupported protocol version from the server: )rX   rD   r'   SamplingCapabilityrY   rF   ElicitationCapabilityrZ   rG   RootsCapabilitysend_requestClientRequestInitializeRequestInitializeRequestParamsLATEST_PROTOCOL_VERSIONClientCapabilitiesrW   InitializeResultre   r   RuntimeErrorsend_notificationClientNotificationInitializedNotification)r    ra   rb   rd   resultr   r   r!   
initialize   s:   
	

zClientSession.initializec                    s    |  tt tjI dH S )zSend a ping request.N)rk   r'   rl   PingRequestEmptyResultr    r   r   r!   	send_ping   s
   
zClientSession.send_pingprogress_tokenprogresstotalr5   c                    s2   |  ttjtj||||ddI dH  dS )zSend a progress notification.)progressTokenr}   r~   r5   rH   N)rs   r'   rt   ProgressNotificationProgressNotificationParams)r    r|   r}   r~   r5   r   r   r!   send_progress_notification   s   z(ClientSession.send_progress_notificationlevelc                    ,   |  ttjtj|ddtjI dH S )z Send a logging/setLevel request.)r   rH   N)rk   r'   rl   SetLevelRequestSetLevelRequestParamsry   )r    r   r   r   r!   set_logging_level      

zClientSession.set_logging_levelcursorc                    8   |  ttj|durtj|dnddtjI dH S )zSend a resources/list request.Nr   rH   )rk   r'   rl   ListResourcesRequestPaginatedRequestParamsListResourcesResultr    r   r   r   r!   list_resources      
zClientSession.list_resourcesc                    r   )z(Send a resources/templates/list request.Nr   rH   )rk   r'   rl   ListResourceTemplatesRequestr   ListResourceTemplatesResultr   r   r   r!   list_resource_templates   r   z%ClientSession.list_resource_templatesuric                    r   )zSend a resources/read request.r   rH   N)rk   r'   rl   ReadResourceRequestReadResourceRequestParamsReadResourceResultr    r   r   r   r!   read_resource   r   zClientSession.read_resourcec                    r   )z#Send a resources/subscribe request.r   rH   N)rk   r'   rl   SubscribeRequestSubscribeRequestParamsry   r   r   r   r!   subscribe_resource   r   z ClientSession.subscribe_resourcec                    r   )z%Send a resources/unsubscribe request.r   rH   N)rk   r'   rl   UnsubscribeRequestUnsubscribeRequestParamsry   r   r   r   r!   unsubscribe_resource  r   z"ClientSession.unsubscribe_resourcer   	argumentsprogress_callbackc                    sP   | j ttjtj||ddtj||dI dH }|js&| ||I dH  |S )zBSend a tools/call request with optional progress callback support.r   r   rH   )request_read_timeout_secondsr   N)rk   r'   rl   CallToolRequestCallToolRequestParamsCallToolResultisError_validate_tool_result)r    r   r   rM   r   rv   r   r   r!   	call_tool  s"   	zClientSession.call_toolrv   c              
      s   || j vr|  I dH  d}|| j v r| j |}n	td| d |durj|jdu r5td| dz	t|j| W dS  tyT } z
td| d| d}~w t	yi } z
td| d| d}~ww dS )zKValidate the structured content of a tool result against its output schema.NzTool z= not listed by server, cannot validate any structured contentz; has an output schema but did not return structured contentz,Invalid structured content returned by tool z: zInvalid schema for tool )
r]   
list_toolsgetloggerwarningstructuredContentrr   r	   r   r   )r    r   rv   output_schemaer   r   r!   r   *  s(   


z#ClientSession._validate_tool_resultc                    r   )zSend a prompts/list request.Nr   rH   )rk   r'   rl   ListPromptsRequestr   ListPromptsResultr   r   r   r!   list_prompts@  r   zClientSession.list_promptsc              	      s.   |  ttjtj||ddtjI dH S )zSend a prompts/get request.r   rH   N)rk   r'   rl   GetPromptRequestGetPromptRequestParamsGetPromptResult)r    r   r   r   r   r!   
get_promptK  s   
zClientSession.get_promptrefargumentcontext_argumentsc                    sT   d}|durt j|d}| t t jt j|t jdi ||ddt jI dH S )z#Send a completion/complete request.N)r   )r   r   r   rH   r   )r'   CompletionContextrk   rl   CompleteRequestCompleteRequestParamsCompletionArgumentCompleteResult)r    r   r   r   r   r   r   r!   completeV  s    	
zClientSession.completec                    sV   |  ttj|durtj|dnddtjI dH }|jD ]	}|j| j|j	< q|S )zSend a tools/list request.Nr   rH   )
rk   r'   rl   ListToolsRequestr   ListToolsResulttoolsoutputSchemar]   r   )r    r   rv   toolr   r   r!   r   n  s   

zClientSession.list_toolsc                    s    |  tt I dH  dS )z'Send a roots/list_changed notification.N)rs   r'   rt   RootsListChangedNotificationrz   r   r   r!   send_roots_list_changed  s   z%ClientSession.send_roots_list_changed	responderc                    s  t ttf |j|j| d d}|jj tjd rJ d  } | | 	||I d H }t
|}||I d H  W d    d S 1 sCw   Y  d S   tjd r d  } | | ||I d H }t
|}||I d H  W d    d S 1 s|w   Y  d S   tjd r  | | |I d H }t
|}||I d H  W d    d S 1 sw   Y  d S  tjd r | |tjt dI d H W  d    S 1 sw   Y  d S  d S )N)
request_idmetasessionlifespan_contextrH   r   r   )root)r   r   r   r   request_metarequestr   r'   CreateMessageRequestrX   rJ   validate_pythonrespondElicitRequestrY   ListRootsRequestrZ   rx   r7   ry   )r    r   ctxr   responseclient_responser   r   r!   _received_request  sD   

"
"
"$zClientSession._received_requestreqc                    s   |  |I dH  dS )z>Handle incoming messages by forwarding to the message handler.N)r\   )r    r   r   r   r!   _handle_incoming  s   zClientSession._handle_incomingnotificationc                    s:   |j tjd r d  }| |I dH  dS  	 	 dS )z%Handle notifications from the server.rH   r   N)r   r'   LoggingMessageNotificationr[   )r    r   r   r   r   r!   _received_notification  s   z$ClientSession._received_notification)NNNNNNN)NNr   )NNN)r   N)<r$   r%   r&   r   r   r9   r   r   r   r,   r/   r1   r4   r'   ImplementationrU   rq   rw   ry   r{   strintfloatr   LoggingLevelr   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   r6   r7   r   r8   r   r   __classcell__r   r   r^   r!   r   e   s    
	
'

&




)<loggingdatetimer   typingr   r   anyio.lowlevelr:   anyio.streams.memoryr   r   
jsonschemar   r   r	   pydanticr
   r   	mcp.typesr'   mcp.shared.contextr   mcp.shared.messager   mcp.shared.sessionr   r   r   mcp.shared.versionr   r   rV   	getLoggerr   r   r,   r/   r1   r4   r6   r7   r8   r9   r=   r(   r)   r*   rD   r-   r.   rF   r0   rG   r3   rI   rJ   __annotations__rl   rt   r   r   r   r   r!   <module>   sv   
 












	
$
