o
    i                     @   s   d dl mZmZ d dlZd dl Z d dlmZ d dl mZmZ d dlm	Z	 d dl
mZmZmZmZ d dlmZ d dlmZmZmZmZmZ eeZG d	d
 d
e jZG dd deZG dd deZdS )    )OptionalUnionN)AsyncExitStack)ProtocolAny)ClientSession)ListPromptsResultEmbeddedResourceImageContentTextContent)MCPException)FunctionToolFunctionSystemMessageTypedDictAssistantMessageTypedDictTextChunkTypedDictc                   @   s.   e Zd ZU ee ed< eeee	f  ed< dS )MCPSystemPromptdescriptionmessagesN)
__name__
__module____qualname__r   str__annotations__listr   r   r    r   r   ^/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/mistralai/extra/mcp/base.pyr      s   
 r   c                   @   s   e Zd ZU dZeed< dee ddfddZddd	Z	de
e fd
dZdedede
e fddZdedeeef defddZdefddZdS )MCPClientProtocolz9MCP client that converts MCP artifacts to Mistral format._name
exit_stackreturnNc                       d S Nr   selfr   r   r   r   
initialize       zMCPClientProtocol.initializec                    r!   r"   r   r$   r   r   r   aclose#   r&   zMCPClientProtocol.aclosec                    r!   r"   r   r'   r   r   r   	get_tools&   r&   zMCPClientProtocol.get_toolsname	argumentsc                    r!   r"   r   r$   r*   r+   r   r   r   execute_tool)      zMCPClientProtocol.execute_toolc                    r!   r"   r   r,   r   r   r   get_system_prompt.   r.   z#MCPClientProtocol.get_system_promptc                    r!   r"   r   r'   r   r   r   list_system_prompts3   r&   z%MCPClientProtocol.list_system_prompts)r    N)r   r   r   __doc__r   r   r   r   r%   r(   r   r   r)   dictr   r-   r   r   r/   r   r0   r   r   r   r   r      s*   
 



r   c                   @   s  e Zd ZU dZeed< d"dee fddZde	e
eef defd	d
Zdee	e
eef  dee fddZdee fddZdedeeef dee fddZdedeeef defddZdefddZd"dee ddfddZdd Zdd Zdd Zdefd d!ZdS )#MCPClientBasezHBase class to implement functionalities from an initialized MCP session._sessionNr*   c                 C   s   |p| j j| _d | _d| _d S )NF)	__class__r   r   _exit_stack_is_initialized)r$   r*   r   r   r   __init__<   s   
zMCPClientBase.__init__mcp_contentr    c                 C   s   |j dks	tdd|jdS )Ntextz,Only supporting text tool responses for now.)typer:   )r;   r   r:   )r$   r9   r   r   r   _convert_contentA   s   
zMCPClientBase._convert_contentmcp_contentsc                 C   s"   g }|D ]
}| | | q|S r"   )appendr<   )r$   r=   content_chunksr9   r   r   r   _convert_content_listH   s   z#MCPClientBase._convert_content_listc                    sJ   | j  I d H }g }|jD ]}|tdt|j|j|jddd q|S )NfunctionT)r*   r   
parametersstrict)r;   rA   )	r4   
list_toolstoolsr>   r   r   r*   r   inputSchema)r$   	mcp_toolsrE   mcp_toolr   r   r   r)   P   s    
zMCPClientBase.get_toolsr+   c                    s$   | j j||dI d H }| |jS )Nr*   r+   )r4   	call_toolr@   content)r$   r*   r+   contentsr   r   r   r-   a   s   zMCPClientBase.execute_toolc                    s4    j j||dI d H }|j fdd|jD dS )NrI   c              	      s2   g | ]}t tttf |j j|jd dqS ))r9   )rolerK   )typingcastr   r   r   rM   r<   rK   ).0messager'   r   r   
<listcomp>m   s    
z3MCPClientBase.get_system_prompt.<locals>.<listcomp>)r   r   )r4   
get_promptr   r   )r$   r*   r+   prompt_resultr   r'   r   r/   g   s   
zMCPClientBase.get_system_promptc                    s   | j  I d H S r"   )r4   list_promptsr'   r   r   r   r0   y   s   z!MCPClientBase.list_system_promptsr   c                    sr   | j rdS |du rt | _| j}| j|dI dH }|t|d |d dI dH }| I dH  || _d| _ dS )zInitialize the MCP session.N)r   r      )read_streamwrite_streamT)r7   r   r6   _get_transportenter_async_contextr   r%   r4   )r$   r   stdio_transportmcp_sessionr   r   r   r%   |   s    

zMCPClientBase.initializec                    s    | j r| j  I dH  dS dS )zClose the MCP session.N)r6   r(   r'   r   r   r   r(      s   zMCPClientBase.aclosec                 C   s$   d| j j d| jdt| ddS )N<z name=z id=0xx>)r5   r   r   idr'   r   r   r   __repr__   s   $zMCPClientBase.__repr__c                 C   s   | j j d| j dS )Nz(name=))r5   r   r   r'   r   r   r   __str__   s   zMCPClientBase.__str__c                    s   t r"   )NotImplementedErrorr#   r   r   r   rY      r&   zMCPClientBase._get_transportr"   ) r   r   r   r1   r   r   r   r   r8   r   r   r
   r	   r   r<   r   r@   r   r)   r2   r   r-   r   r/   r   r0   r   r%   r(   ra   rc   rY   r   r   r   r   r3   7   sF   
 





r3   )rN   r   r   logging
contextlibr   r   r   mcpr   	mcp.typesr   r	   r
   r   mistralai.extra.exceptionsr   mistralai.modelsr   r   r   r   r   	getLoggerr   logger	TypedDictr   r   r3   r   r   r   r   <module>   s    
