o
    i=                     @  s   d dl 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mZmZmZ d dlmZmZmZmZmZmZmZ d d	lmZ d d
lmZ d dlmZm Z  erbd dl!m"Z"m#Z# dddZ$ej%dddZ&dS )    )annotationsN)	ExitStackcontextmanager)TYPE_CHECKINGAny)ClientSession)Server)BaseSessionReceiveRequestTRequestResponderSendResultT)CallToolRequestClientRequestClientResult	ErrorDataLoggingMessageNotificationServerRequestServerResult)TypeAdapter)handle_internal_errors)attach_contextget_context)	LevelNameLogfirelogfire_instancer   propagate_otel_contextboolc                   s  j ddtjtd# fdd	}|t_tjtd$ fdd}|t_tjtd$fdd}|t_tjtd%fdd}|t_t	j
td&fdd}|t	_
td'fddtd(	fddd)	fd!d" d S )*Nmcp)custom_scope_suffixselfr   requestargskwargsc           
        s   |j }|ddd}d}t|dd  }r-|d| 7 }||d< t|tr-|d|jj 7 }j|fi |#} | | |g|R i |I d H }	|d	|	 |	W  d    S 1 s\w   Y  d S )
Njsonrpcz2.0)r    z
rpc.systemzrpc.jsonrpc.versionzMCP requestmethod: z
rpc.method response)rootgetattr
isinstancer   paramsnamespanset_attribute)
r   r    r!   r"   r(   
attributes	span_namer$   r-   result)_attach_context_to_requestr   original_send_request h/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/logfire/_internal/integrations/mcp.pysend_request!   s$   
$z$instrument_mcp.<locals>.send_requestnotificationc                   s*    |j  | |g|R i |I d H S )N)r(   )r   r7   r!   r"   )r2   original_send_notificationr4   r5   send_notification>   s   
z)instrument_mcp.<locals>.send_notificationc              	     s   t K t|jtrG|jj}|jdv rd}n|j}d}|jr&|d|j 7 } |j j||t|j	dd W d    n1 sBw   Y  W d    n1 sQw   Y  | |g|R i |I d H  d S )N)criticalalert	emergencyfatalzMCP server logz from )data)r/   )
r   r*   r(   r   r+   levelloggerlogdictr>   )r   r7   r!   r"   r+   r?   r0   )_request_contextr   original_received_notificationr4   r5   _received_notificationG   s    
"z.instrument_mcp.<locals>._received_notification	responder-RequestResponder[ServerRequest, ClientResult]returnNonec                   sP   |j j}d} ||| | |I d H  W d    d S 1 s!w   Y  d S )NzMCP client handle request)r    r(   )r   rF   r    r0   )_handle_request_with_contextoriginal_handle_client_requestr4   r5   _received_request_client\   s   "z0instrument_mcp.<locals>._received_request_clientmessage-RequestResponder[ClientRequest, ServerResult]c                   sV   d} ||| | ||g|R i |I d H W  d    S 1 s$w   Y  d S )NzMCP server handle requestr4   )r   rM   r    r!   r"   r0   )rJ   original_handle_server_requestr4   r5   _handle_requestg   s
   $z'instrument_mcp.<locals>._handle_request.RequestResponder[ReceiveRequestT, SendResultT]r0   strc              
   3  s    | Z t | dd  }r|d| 7 }j|| d+t |j d fd
d}||_W d    n1 s8w   Y  d V  W d    n1 sJw   Y  W d    d S W d    d S 1 sbw   Y  d S )Nr$   r%   )r    r'   SendResultT | ErrorDatarespond_argsr   respond_kwargsrH   c                   s"    d|   | g|R i |S )Nr'   )r.   )r'   rT   rU   original_respondr-   r4   r5   _respond_with_logging|   s   zSinstrument_mcp.<locals>._handle_request_with_context.<locals>._respond_with_logging)r'   rS   rT   r   rU   r   rH   r   )r)   r-   r   respond)r    rF   r0   r$   rX   )rC   r   rV   r5   rJ   q   s   
"z4instrument_mcp.<locals>._handle_request_with_contextc              	   3  s    t  9}t#  r#t| dd  }r#t|dd  }r#|t|  W d    n1 s-w   Y  d V  W d    d S 1 s@w   Y  d S )Nr+   meta)r   r   r)   enter_contextr   
model_dump)r    
exit_stackr+   rZ   r   r4   r5   rC      s   "z(instrument_mcp.<locals>._request_contextr(   c                   s    sd S t  }t| dd  }r5t|dd  }r$t|tr|}n| }ni }t|ji |||_d S t	t| 
d|i| _d S )Nr+   rZ   _meta)r   r)   r*   rB   r\   typeMetamodel_validaterZ   _request_params_type_adaptervalidate_pythonr+   )r(   carrierr+   rZ   dumped_metar^   r4   r5   r2      s   

z2instrument_mcp.<locals>._attach_context_to_request)r   r   r    r   r!   r   r"   r   )r   r   r7   r   r!   r   r"   r   )r   r   rF   rG   rH   rI   )r   r   rM   rN   r    r   r!   r   r"   r   rH   r   )r    r   rF   rQ   r0   rR   )r    r   )r(   r   )with_settingsr	   r6   	functoolswrapsr9   r   rE   _received_requestr   rP   r   )r   r   r6   r9   rE   rL   rP   r4   )
r2   rJ   rC   r   rK   rO   rD   r8   r3   r   r5   instrument_mcp   s4   rk   	root_typer   c                 C  s   | j d j}t|S )Nr+   )model_fields
annotationr   )rl   params_typer4   r4   r5   rc      s   rc   )r   r   r   r   )rl   r   )'
__future__r   rh   
contextlibr   r   typingr   r   mcp.client.sessionr   
mcp.serverr   mcp.shared.sessionr	   r
   r   r   	mcp.typesr   r   r   r   r   r   r   pydanticr   logfire._internal.utilsr   logfire.propagater   r   logfirer   r   rk   	lru_cacherc   r4   r4   r4   r5   <module>   s"    $	
 
