
    ~h                        d Z ddlZddlZddlZddlmZmZmZ ddlZddl	Z	ddl
Z
ddlmZ  ej                  e      Zd Z G d de      Zedk(  rddlZdd	l	mZ dd
lmZ  e edddgddiej*                         e             5 Z ee        eed   j0                          eed   j2                          eed   j4                          eed   j6                         ddd       yy# 1 sw Y   yxY w)a,  This module implements the SmolAgents adapter.

SmolAgents do not support async tools, so this adapter will only work with the sync
context manager.

Example Usage:
>>> with MCPAdapt(StdioServerParameters(command="uv", args=["run", "src/echo.py"]), SmolAgentAdapter()) as tools:
>>>     print(tools)
    N)AnyCallable	Coroutine)ToolAdapterc                     | j                  dd      } t        j                  dd|       } | d   j                         rd|  } t	        j
                  |       r|  d} | S )z
    A function to sanitize function names to be used as a tool name.
    Prevent the use of dashes or other python keywords as function names by tool.
    -_z[^\w_] r   )replaceresubisdigitkeyword	iskeyword)names    W/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/mcpadapt/smolagents_adapter.py_sanitize_function_namer      sc     <<S!D 66)R&D Aw4&z qzK    c                   0   e Zd ZdZdeedz  gej                  j                  f   dej                  j                  de
j                  fdZdeedz  geeeej                  j                  f   f   dej                  j                  de
j                  fdZy)	SmolAgentsAdapteraQ  Adapter for the `smolagents` framework.

    Note that the `smolagents` framework do not support async tools at this time so we
    write only the adapt method.

    Warning: if the mcp tool name is a python keyword, starts with digits or contains
    dashes, the tool name will be sanitized to become a valid python function name.

    funcNmcp_toolreturnc                     G fddt         j                        }t        j                  |j                        j                         D ci c]  \  }}|dk7  r|| }}}|d   j                         D ]$  \  }}d|vrd|d   |   d<   d|vsd|d   |   d<   &  ||j                  |j                  xs d	|d   d
      }|S c c}}w )zAdapt a MCP tool to a SmolAgents tool.

        Args:
            func: The function to adapt.
            mcp_tool: The MCP tool to adapt.

        Returns:
            A SmolAgents tool.
        c            
       J    e Zd Zdededeeeeef   f   defdZdef fdZy)	-SmolAgentsAdapter.adapt.<locals>.MCPAdaptToolr   descriptioninputsoutput_typec                 j    t        |      | _        || _        || _        || _        d| _        d| _        y )NT)r   r   r   r   r   is_initialized!skip_forward_signature_validation)selfr   r   r   r   s        r   __init__z6SmolAgentsAdapter.adapt.<locals>.MCPAdaptTool.__init__J   s7     4D9	#. $#. &*#9=6r   r   c           	         t        |      dkD  rHt        |      dk(  r!t        |d   t              r|s |d         }n!t        d| j                   d       |      }t        |j
                        dk(  rt        d| j                   d      t        |j
                        dkD  r#t        j                  d| j                   d       t        |j
                  d   t        j                  j                        s2t        d| j                   dt        |j
                  d          d      |j
                  d   j                  S )	Nr      ztool z\ does not support multiple positional arguments or combined positional and keyword argumentsz returned an empty contentz/ returned multiple content, using the first onez returned a non-text content: ``)len
isinstancedict
ValueErrorr   contentloggerwarningmcptypesTextContenttypetext)r#   argskwargs
mcp_outputr   s       r   forwardz5SmolAgentsAdapter.adapt.<locals>.MCPAdaptTool.forwardX   s1   t9q=4yA~*T!Wd*CF%)$q']
(#DII;  /K  L  "&fJz))*a/$uTYYK7Q%RSSz))*Q.NN		{*YZ "*"4"4Q"79N9NO$		{*I$zOaOabcOdJeIffgh  "))!,111r   N)__name__
__module____qualname__strr*   r$   r7   )r   s   r   MCPAdaptToolr   I   sL    >> !> S$sCx.01	>
 !>2# 2r   r<   z$defs
propertiesr   zsee tool descriptionr2   stringr
   )r   r   r   r   )
smolagentsTooljsonrefreplace_refsinputSchemaitemsr   r   )r#   r   r   r<   kvinput_schematools    `      r   adaptzSmolAgentsAdapter.adapt:   s    '	2:?? '	2X  ,,X-A-ABHHJ
1G| qD
 
 !.446 	ADAqA%?U\*1-m<Q8@\*1-f5		A  ,,2- 	
 )
s   Cafuncc                     K   t        d      w)Nz3async is not supported by the SmolAgents framework.)NotImplementedError)r#   rJ   r   s      r   async_adaptzSmolAgentsAdapter.async_adapt   s     
 ""WXXs   )r8   r9   r:   __doc__r   r*   r/   r0   CallToolResultr@   r?   rI   r   r   rM    r   r   r   r   /   s    Mt}cii&>&>>?M ))..M 
	M^Yyc399;S;S1S'TTUY ))..Y 
	Yr   r   __main__)StdioServerParameters)MCPAdaptuvxz--quietzpubmedmcp@0.1.3	UV_PYTHONz3.12)commandr4   env)rN   loggingr   r   typingr   r   r   rA   r/   r?   mcpadapt.corer   	getLoggerr8   r-   r   r   osrR   rS   environtoolsprintr   r   r   r   rP   r   r   <module>r`      s     	 + +  
  %			8	$,_Y _YD z)&	./f3

3	

 	
 $ 
eeAhmmeAh""#eAhooeAh""#$ $ $ $s   AC..C7