
     Wi"                         d dl Z d dlmZmZ d dlZ e j        e j        d            e j        e          Z	 G d d          Z
dS )    N)DictAnyz4%(asctime)s - %(name)s - %(levelname)s - %(message)s)levelformatc                   h    e Zd ZdefdZd Zd Zd ZdefdZ	d Z
d	 Zd
edeeef         defdZdS )AgrotaMcpClient
server_urlc                 ~    |                     dd          | _        t          j        d          | _        d| _        d S )Nz/ssez/mcpg      ^@)timeoutF)replacer	   httpxAsyncClientclient_is_connected)selfr	   s     yc:\Users\Dell Inspiron 16\Desktop\tws\github tws\Agrota\back-agrota-powerbi\mcp-client-agrota\app\services\mcp_service.py__init__zAgrotaMcpClient.__init__   s<    $,,VV<<'666"    c                 >   K   |                                   d {V  | S N)connectr   s    r   
__aenter__zAgrotaMcpClient.__aenter__   s+      llnnr   c                 >   K   |                                   d {V  d S r   )
disconnect)r   exc_typeexc_valexc_tbs       r   	__aexit__zAgrotaMcpClient.__aexit__   s.      oor   c                   K   | j         ri	 | j                                          d{V  n4# t          $ r'}t                              d|            Y d}~nd}~ww xY wd| _        n# d| _        w xY wt                              d           dS )zCierra el cliente HTTP.NzError al cerrar cliente HTTP: FzDesconectado.)r   aclose	Exceptionloggererrorr   info)r   es     r   r   zAgrotaMcpClient.disconnect   s      ; 	++k((********** C C CAaAABBBBBBBBC &+""U"****O$$$$$s+   + A' 
AAA' AA' '	A0returnc                     | j         S )u$   Verifica si el cliente está activo.)r   r   s    r   is_connectedzAgrotaMcpClient.is_connected$   s    !!r   c                 n   K   | j         rdS t                              d| j                    d| _         dS )uF   Marca el cliente como conectado (sin verificación real de conexión).NzCliente HTTP listo para T)r   r#   r%   r	   r   s    r   r   zAgrotaMcpClient.connect(   sA       	F@t@@AAA!r   c                 x   K   ddl m}  |ddg          } |dddg          } | |dd	
          g          S )zBLista las herramientas disponibles (simulado para compatibilidad).r   )
namedtuple	ToolsListtoolsToolnamedescriptionconsultarAgrotazTTransforma una solicitud en lenguaje natural a DAX y consulta el Power BI de Agrota.)r0   r1   )r.   )collectionsr,   )r   r,   r-   r/   s       r   
list_toolszAgrotaMcpClient.list_tools0   s{       	+*****J{WI66	z&6="9::y* v  
 
 
 	
r   	tool_name	argumentsc                 @  K   |dk    rt          d|           	 t                              d| j                    | j                            | j        d|                    dd          iddi           d	{V }|j        d
k    r*|                                }|                    dd          S t          	                    d|j         d|j
                    d|j         S # t          $ r,}t          	                    d|            d| cY d	}~S d	}~ww xY w)z$Llama al endpoint HTTP directamente.r2   zHerramienta desconocida: u$   🌐 Enviando solicitud HTTP POST a prompt zContent-Typezapplication/json)jsonheadersN   resultzSin resultadou   ❌ Error HTTP z: zError del servidor: u%   ❌ Error llamando al endpoint HTTP: z"Error al ejecutar la herramienta: )RuntimeErrorr#   r%   r	   r   postgetstatus_coder:   r$   textr"   )r   r5   r6   responsedatar&   s         r   	call_toolzAgrotaMcpClient.call_tool@   sj     )))F9FFGGG	<KKPtPPQQQ![--	h ; ;<');< .        H #s**}}xx/:::Vx/CVVx}VVWWWDh.BDDD 	< 	< 	<LLDDDEEE;;;;;;;;;	<s$   BC' 33C' '
D1!DDDN)__name__
__module____qualname__strr   r   r   r   boolr)   r   r4   r   r   rE    r   r   r   r      s        #3 # # # #       	% 	% 	%"d " " " "" " "
 
 
 < <c3h <C < < < < < <r   r   )loggingtypingr   r   r   basicConfigDEBUG	getLoggerrF   r#   r   rK   r   r   <module>rQ      s               
- V    
	8	$	$J< J< J< J< J< J< J< J< J< J<r   