
    ~h                    j    d dl mZ d dlZd dlmZ d dlmZmZ d dlm	Z	 dgZ
erd dlmZ  G d d      Zy)	    )annotationsN)TracebackType)TYPE_CHECKINGAny)Tool	MCPClient)StdioServerParametersc                  f    e Zd ZdZ	 d		 	 	 d
dZd Z	 	 	 d	 	 	 	 	 ddZddZddZ	 	 	 	 	 	 ddZ	y)r   a_  Manages the connection to an MCP server and make its tools available to SmolAgents.

    Note: tools can only be accessed after the connection has been started with the
        `connect()` method, done during the init. If you don't use the context manager
        we strongly encourage to use "try ... finally" to ensure the connection is cleaned up.

    Args:
        server_parameters (StdioServerParameters | dict[str, Any] | list[StdioServerParameters | dict[str, Any]]):
            Configuration parameters to connect to the MCP server. Can be a list if you want to connect multiple MCPs at once.

            - An instance of `mcp.StdioServerParameters` for connecting a Stdio MCP server via standard input/output using a subprocess.

            - A `dict` with at least:
              - "url": URL of the server.
              - "transport": Transport protocol to use, one of:
                - "streamable-http": (recommended) Streamable HTTP transport.
                - "sse": Legacy HTTP+SSE transport (deprecated).
              If "transport" is omitted, the legacy "sse" transport is assumed (a deprecation warning will be issued).

            <Deprecated version="1.17.0">
            The HTTP+SSE transport is deprecated and future behavior will default to the Streamable HTTP transport.
            Please pass explicitly the "transport" key.
            </Deprecated>

        adapter_kwargs (dict[str, Any], optional):
            Additional keyword arguments to be passed directly to `MCPAdapt`.

    Example:
        ```python
        # fully managed context manager + stdio
        with MCPClient(...) as tools:
            # tools are now available

        # context manager + Streamable HTTP transport:
        with MCPClient({"url": "http://localhost:8000/mcp", "transport": "streamable-http"}) as tools:
            # tools are now available

        # manually manage the connection via the mcp_client object:
        try:
            mcp_client = MCPClient(...)
            tools = mcp_client.get_tools()

            # use your tools here.
        finally:
            mcp_client.disconnect()
        ```
    Nc                ^   	 ddl m} ddlm} t        |t              rG|j                  d      }|!t        j                  dt               d}||d<   |dvrt        d	| d
      |xs i } || |       fi || _        d | _        | j                          y # t        $ r t	        d      w xY w)Nr   )MCPAdapt)SmolAgentsAdapterzLPlease install 'mcp' extra to use MCPClient: `pip install 'smolagents[mcp]'`	transporta  Passing a dict as server_parameters without specifying the 'transport' key is deprecated. For now, it defaults to the legacy 'sse' (HTTP+SSE) transport, but this default will change to 'streamable-http' in version 1.20. Please add the 'transport' key explicitly. sse>   streamable-httpr   zUnsupported transport: z7. Supported transports are 'streamable-http' and 'sse'.)mcpadapt.corer   mcpadapt.smolagents_adapterr   ModuleNotFoundError
isinstancedictgetwarningswarnFutureWarning
ValueError_adapter_toolsconnect)selfserver_parametersadapter_kwargsr   r   r   s         Q/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/smolagents/mcp_client.py__init__zMCPClient.__init__R   s    
	v.E '.)--k:I h "	 "	1:!+. :: -i[8op  (-2 !24E4GZ>Z)-) # 	v%&tuu	vs   B B,c                B    | j                   j                         | _        y)z3Connect to the MCP server and initialize the tools.N)r   	__enter__r   r   s    r!   r   zMCPClient.connectp   s    "&--"9"9";    c                >    | j                   j                  |||       y)zDisconnect from the MCP serverN)r   __exit__r   exc_type	exc_valueexc_tracebacks       r!   
disconnectzMCPClient.disconnectt   s     	xMBr&   c                H    | j                   t        d      | j                   S )a  The SmolAgents tools available from the MCP server.

        Note: for now, this always returns the tools available at the creation of the session,
        but it will in a future release return also new tools available from the MCP server if
        any at call time.

        Raises:
            ValueError: If the MCP server tools is None (usually assuming the server is not started).

        Returns:
            list[Tool]: The SmolAgents tools available from the MCP server.
        zbCouldn't retrieve tools from MCP server, run `mcp_client.connect()` first before accessing `tools`)r   r   r%   s    r!   	get_toolszMCPClient.get_tools}   s*     ;;t  {{r&   c                    | j                   S )zConnect to the MCP server and return the tools directly.

        Note that because of the `.connect` in the init, the mcp_client
        is already connected at this point.
        )r   r%   s    r!   r$   zMCPClient.__enter__   s     {{r&   c                *    | j                  |||       y)zDisconnect from the MCP server.N)r-   r)   s       r!   r(   zMCPClient.__exit__   s     	)];r&   )N)r   zY'StdioServerParameters' | dict[str, Any] | list['StdioServerParameters' | dict[str, Any]]r    zdict[str, Any] | None)NNN)r*   ztype[BaseException] | Noner+   zBaseException | Noner,   zTracebackType | None)returnz
list[Tool])
__name__
__module____qualname____doc__r"   r   r-   r/   r$   r(    r&   r!   r   r   !   s    .f 15t .<< 04*..2	C,C (C ,	C&<,< (< ,	<r&   )
__future__r   r   typesr   typingr   r   smolagents.toolsr   __all__r   r	   r   r7   r&   r!   <module>r=      s1   $ #   % ! -3~< ~<r&   