
    ~hM                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
 d dlmZ d dlmZmZmZmZmZmZ d dlZd dlZd dlZd dlmZmZ d dlmZmZmZmZ d dlm Z! d dl"m#Z# d d	l$m%Z% d d
l&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m Z  d dl/m0Z0 d dl1m2Z2m3Z3  e0e4      Z5 edd      Z6g dZ7 G d ded      Z8 G d de jr                        Z: G d de:      Z; G d de:      Z< G d de:      Z= G d d e:      Z> G d! d"e>      Z? G d# d$e>      Z@ G d% d&e>      ZA G d' d(e>      ZB G d) d*e>      ZC G d+ d,e:      ZD G d- d.e:      ZEed/e6d0e6fd1       ZFed/e d0eDfd2       ZFed/e!d0eDfd3       ZFed/e2d0eEfd4       ZFed/eGeHef   d0eEfd5       ZFed/e%d0e<e=z  fd6       ZFed/eHd0e?eAz  e<z  e=z  fd7       ZFed/ed0e?eAz  fd8       ZFd/e:e z  e!z  e%z  ez  e2z  eGeHef   z  eHz  d0e:fd9ZFy):    N)AsyncIteratorCallable)Path)AnyLiteral	TypedDictTypeVarcastoverload)ClientSessionStdioServerParameters)ListRootsFnT
LoggingFnTMessageHandlerFnTSamplingFnT)FastMCP)#create_client_server_memory_streams)AnyUrl)Unpack)
BearerAuth)OAuth)get_http_headers)
get_logger)	MCPConfiginfer_transport_type_from_urlClientTransportTClientTransport)bound)r   SSETransportStreamableHttpTransportStdioTransportPythonStdioTransportFastMCPStdioTransportNodeStdioTransportUvxStdioTransportNpxStdioTransportFastMCPTransportinfer_transportc                       e Zd ZU dZej
                  dz  ed<   edz  ed<   edz  ed<   e	dz  ed<   e
dz  ed<   ej                  j                  dz  ed<   y)	SessionKwargsz8Keyword arguments for the MCP ClientSession constructor.Nread_timeout_secondssampling_callbacklist_roots_callbacklogging_callbackmessage_handlerclient_info)__name__
__module____qualname____doc__datetime	timedelta__annotations__r   r   r   r   mcptypesImplementation     U/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/fastmcp/client/transports.pyr*   r*   2   sW    B",,t33"T))%,, 4''&--))D00r<   r*   F)totalc                       e Zd ZdZej
                  ej                  dee	   de
e   fd              ZdefdZd Zdej"                  ed   z  ez  d	z  fd
Zy	)r   z
    Abstract base class for different MCP client transport mechanisms.

    A Transport is responsible for establishing and managing connections
    to an MCP server, and providing a ClientSession within an async context.

    session_kwargsreturnc                   K   t         w)a6  
        Establishes a connection and yields an active ClientSession.

        The ClientSession is *not* expected to be initialized in this context manager.

        The session is guaranteed to be valid only within the scope of the
        async context manager. Connection setup and teardown are handled
        within this context.

        Args:
            **session_kwargs: Keyword arguments to pass to the ClientSession
                              constructor (e.g., callbacks, timeouts).

        Yields:
            A mcp.ClientSession instance.
        )NotImplementedErrorselfr@   s     r=   connect_sessionzClientTransport.connect_sessionF   s     * "!s   	c                 6    d| j                   j                   dS )N<>)	__class__r1   rE   s    r=   __repr__zClientTransport.__repr__^   s    4>>**+1--r<   c                    K   yw)zClose the transport.Nr;   rK   s    r=   closezClientTransport.closeb   s	     s   authoauthNc                     |t        d      y )Nz$This transport does not support auth)
ValueErrorrE   rO   s     r=   	_set_authzClientTransport._set_authf   s    CDD r<   )r1   r2   r3   r4   abcabstractmethod
contextlibasynccontextmanagerr   r*   r   r   rF   strrL   rN   httpxAuthr   rT   r;   r<   r=   r   r   =   s     	## &} 5	}	% $ ,.# .Eejj77+;;cADH Er<   c                   h    e Zd ZdZdeez  fdZej                  de	e
   dee   fd       ZdefdZy)	WSTransportzGTransport implementation that connects to an MCP server via WebSockets.urlc                     t        j                  dt        d       t        |t              rt        |      }t        |t
              r|j                  d      st        d      || _        y )NzwWSTransport is a deprecated MCP transport and will be removed in a future version. Use StreamableHttpTransport instead.   )
stacklevelwszInvalid WebSocket URL provided.)	warningswarnDeprecationWarning
isinstancer   rY   
startswithrR   r^   )rE   r^   s     r=   __init__zWSTransport.__init__n   sW     F	

 c6"c(C#s#3>>$+?>??r<   r@   rA   c                d  K   	 ddl m}  || j                        4 d {   }|\  }}t	        ||fi |4 d {   }| d d d       d {    d d d       d {    y # t        $ r t        d      w xY w7 ]7 D7 1# 1 d {  7  sw Y   AxY w7 8# 1 d {  7  sw Y   y xY ww)Nr   )websocket_clientzxThe websocket transport is not available. Please install fastmcp[websockets] or install the websockets package manually.)mcp.client.websocketrj   ImportErrorr^   r   )rE   r@   rj   	transportread_streamwrite_streamsessions          r=   rF   zWSTransport.connect_session{   s     	= $DHH- 	 	(1%K$\-;   	 	 	  	 K 	
	    	 	 	 	s   B0A& B0A>B0BB BBBBBB0 B!B0&A;;B0 BBB	
BB	BB0B-!B$"B-)B0c                 "    d| j                    dS )Nz<WebSocketTransport(url='')>r^   rK   s    r=   rL   zWSTransport.__repr__   s    *488*C88r<   N)r1   r2   r3   r4   rY   r   rh   rW   rX   r   r*   r   r   rF   rL   r;   r<   r=   r]   r]   k   sU    QC&L  ## &} 5	}	% $"9# 9r<   r]   c                   @   e Zd ZdZ	 	 	 	 ddeez  deeef   dz  dej                  e	d   z  ez  dz  de
j                  ez  ez  dz  deg ej                  f   dz  f
d	Zdej                  e	d   z  ez  dz  fd
Zej&                  dee   dee   fd       ZdefdZy)r   zOTransport implementation that connects to an MCP server via Server-Sent Events.Nr^   headersrO   rP   sse_read_timeouthttpx_client_factoryc                 N   t        |t              rt        |      }t        |t              r|j                  d      st	        d      || _        |xs i | _        | j                  |       || _        t        |t        t        z        rt        j                  |      }|| _        y )Nhttpz$Invalid HTTP/S URL provided for SSE.secondsrf   r   rY   rg   rR   r^   ru   rT   rw   intfloatr5   r6   rv   rE   r^   ru   rO   rv   rw   s         r=   rh   zSSETransport.__init__   s     c6"c(C#s#3>>&+ACDD}"t$8!&e4'11:JK 0r<   c                     |dk(  rt        | j                        }|| _        y t        |t              rt	        |      }|| _        y NrP   r   r^   rf   rY   r   rO   rS   s     r=   rT   zSSETransport._set_auth   <    7??D 	 c"d#D	r<   r@   rA   c                  K   ddl m} i }t               | j                  z  |d<   | j                  | j                  j                         |d<   |j                  dd       <t        t        j                  |j                  d            }|j                         |d<   | j                  | j                  |d<    || j                  fd| j                  i|4 d {   }|\  }}t        ||fi |4 d {   }| d d d       d {    d d d       d {    y 7 E7 ,7 # 1 d {  7  sw Y   )xY w7  # 1 d {  7  sw Y   y xY ww)	Nr   )
sse_clientru   rv   r+   timeoutrw   rO   )mcp.client.sser   r   ru   rv   total_secondsgetr
   r5   r6   rw   r^   rO   r   )	rE   r@   r   client_kwargsr+   rm   rn   ro   rp   s	            r=   rF   zSSETransport.connect_session   sZ     	.(*
 $4#5#Di    ,040E0E0S0S0UM,-4d;G#'""N$6$67M$N$  (<'I'I'KM)$$$0484M4MM01dhhHTYYH-H 	 	I(1%K$\-;   	 	 	    	 	 	 	s   CED ED=5D"6D=9D&?D=
D$D=ED;E"D=$D=&D8	,D/-D8	4D=;E=EEEEc                 "    d| j                    dS )Nz<SSETransport(url='rr   rs   rK   s    r=   rL   zSSETransport.__repr__   s    $TXXJc22r<   NNNNr1   r2   r3   r4   rY   r   dictrZ   r[   r   r5   r6   r~   r}   r   AsyncClientrh   rT   rW   rX   r   r*   r   r   rF   rL   r;   r<   r=   r   r      s    Y
 *.;?DHGK16\1 c3h$&1 jj77++c1D8	1
 #,,u4s:TA1 'r5+<+<'<=D1*ejj77+;;cADH  ## &} 5	}	% $@3# 3r<   r   c                   @   e Zd ZdZ	 	 	 	 ddeez  deeef   dz  dej                  e	d   z  ez  dz  de
j                  ez  ez  dz  deg ej                  f   dz  f
d	Zdej                  e	d   z  ez  dz  fd
Zej&                  dee   dee   fd       ZdefdZy)r    zUTransport implementation that connects to an MCP server via Streamable HTTP Requests.Nr^   ru   rO   rP   rv   rw   c                 N   t        |t              rt        |      }t        |t              r|j                  d      st	        d      || _        |xs i | _        | j                  |       || _        t        |t        t        z        rt        j                  |      }|| _        y )Nry   z0Invalid HTTP/S URL provided for Streamable HTTP.rz   r|   r   s         r=   rh   z StreamableHttpTransport.__init__   s     c6"c(C#s#3>>&+AOPP}"t$8!&e4'11:JK 0r<   c                     |dk(  rt        | j                        }|| _        y t        |t              rt	        |      }|| _        y r   r   rS   s     r=   rT   z!StreamableHttpTransport._set_auth   r   r<   r@   rA   c                >  K   ddl m} i }t               | j                  z  |d<   | j                  | j                  |d<   |j                  dd       |j                  d      |d<   | j                  | j                  |d<    || j                  fd| j                  i|4 d {   }|\  }}}t        ||fi |4 d {   }| d d d       d {    d d d       d {    y 7 F7 ,7 # 1 d {  7  sw Y   )xY w7  # 1 d {  7  sw Y   y xY ww)	Nr   )streamablehttp_clientru   rv   r+   r   rw   rO   )
mcp.client.streamable_httpr   r   ru   rv   r   rw   r^   rO   r   )	rE   r@   r   r   rm   rn   ro   _rp   s	            r=   rF   z'StreamableHttpTransport.connect_session   sA     	E(*
 $4#5#Di    ,040E0EM,-4d;G'5'9'9:P'QM)$$$0484M4MM01(HH

 
 		 		 +4(Kq$\-;   		 		 		    		 		 		 		s   B"D$C+%D(D C-DC1
DC/DD%D&D-D/D1D	7C:8D	?DDDDDDc                 "    d| j                    dS )Nz<StreamableHttpTransport(url='rr   rs   rK   s    r=   rL   z StreamableHttpTransport.__repr__  s    /z==r<   r   r   r;   r<   r=   r    r       s    _
 *.;?DHGK16\1 c3h$&1 jj77++c1D8	1
 #,,u4s:TA1 'r5+<+<'<=D1*ejj77+;;cADH  ## &} 5	}	% $B># >r<   r    c                       e Zd ZdZ	 	 	 ddedee   deeef   dz  dedz  dedz  f
dZe	j                  d	ee   d
ee   fd       Zd	ee   d
edz  fdZd Zd Zd
efdZy)r!   z
    Base transport for connecting to an MCP server via subprocess with stdio.

    This is a base class that can be subclassed for specific command-based
    transports like Python, Node, Uvx, etc.
    Ncommandargsenvcwd
keep_alivec                     || _         || _        || _        || _        |d}|| _        d| _        d| _        t        j                         | _	        t        j                         | _
        y)a\  
        Initialize a Stdio transport.

        Args:
            command: The command to run (e.g., "python", "node", "uvx")
            args: The arguments to pass to the command
            env: Environment variables to set for the subprocess
            cwd: Current working directory for the subprocess
            keep_alive: Whether to keep the subprocess alive between connections.
                       Defaults to True. When True, the subprocess remains active
                       after the connection context exits, allowing reuse in
                       subsequent connections.
        NT)r   r   r   r   r   _session_connect_taskanyioEvent_ready_event_stop_event)rE   r   r   r   r   r   s         r=   rh   zStdioTransport.__init__"  s]    * 	J$.226!KKM ;;=r<   r@   rA   c                x  K   	  | j                   di | d {    | j                  J | j                   | j                  s| j                          d {    y t        j                  d       y 7 \7 # | j                  s| j                          d {  7   w t        j                  d       w xY ww)Nz6Stdio transport has keep_alive=True, not disconnectingr;   )connectr   r   
disconnectloggerdebugrD   s     r=   rF   zStdioTransport.connect_sessionD  s     	W$,,0000==,,,--??oo'''UV 1
 ( ??oo'''UVsI   B:A: A6!A: B:A8B:6A: 8B:: B7BB77B:c                     K    j                   y  fd}t        j                   |              _          j                  j	                          d {    y 7 w)Nc                    K   ddl m}  t        j                         4 d {   }	 t	        j
                  j                  j                  j                        }|j                   | |             d {   }|\  }}|j                  t        ||fi        d {   _        t        j                  d       j                  j                          j                   j#                          d {    d _        t        j                  d       d d d       d {    y 7 7 7 7 8# d _        t        j                  d       w xY w7 /# 1 d {  7  sw Y   y xY ww)Nr   )stdio_client)r   r   r   r   zStdio transport connectedzStdio transport disconnected)mcp.client.stdior   rW   AsyncExitStackr   r   r   r   r   enter_async_contextr   r   r   r   r   setr   wait)r   stackserver_paramsrm   rn   ro   rE   r@   s         r=   r   z-StdioTransport.connect.<locals>._connect_taskX  sG    5!002 A AeA$9 $499$((PTPXPX%M ',&?&?$]3' !I 1:-K*/*C*C%k<R>R+ %DM LL!<=%%))+ **//111 %)DMLL!?@+A A A
!% 2 %)DMLL!?@+A A A As   E+D*E+EAD39D-:(D3"D/#AD38D19D3=EE+$E%E+-D3/D31D33EEE+E(EE($E+)r   asynciocreate_taskr   r   )rE   r@   r   s   `` r=   r   zStdioTransport.connectR  sM      )	A6 %00A$$&&&s   AAAAc                    K   | j                   y | j                  j                          | j                    d {    d | _         t        j                         | _        t        j                         | _        y 7 >wN)r   r   r   r   r   r   rK   s    r=   r   zStdioTransport.disconnectw  se     % 	      " ;;=!KKM 	!s   7A:A8?A:c                 @   K   | j                          d {    y 7 wr   )r   rK   s    r=   rN   zStdioTransport.close  s     oos   c                 j    d| j                   j                   d| j                   d| j                   dS )NrH   z
(command='z', args=z)>)rJ   r1   r   r   rK   s    r=   rL   zStdioTransport.__repr__  s3    ''(
4<<.SUV	
r<   )NNN)r1   r2   r3   r4   rY   listr   boolrh   rW   rX   r   r*   r   r   rF   r   r   rN   rL   r;   r<   r=   r!   r!     s     &*"& ) ) 3i ) #s(^d"	 )
 4Z ) 4K )D ##W &} 5W	}	%W $W#' &} 5#'		#'J* 
# 
r<   r!   c                        e Zd ZdZdddej
                  dfdeez  dee   dz  de	eef   dz  dedz  dede
dz  f fd	Z xZS )
r"   z%Transport for running Python scripts.Nscript_pathr   r   r   
python_cmdr   c                 8   t        |      j                         }|j                         st        d|       t	        |      j                  d      st        d|       t	        |      g}|r|j                  |       t        | %  |||||       || _
        y)a  
        Initialize a Python transport.

        Args:
            script_path: Path to the Python script to run
            args: Additional arguments to pass to the script
            env: Environment variables to set for the subprocess
            cwd: Current working directory for the subprocess
            python_cmd: Python command to use (default: "python")
            keep_alive: Whether to keep the subprocess alive between connections.
                       Defaults to True. When True, the subprocess remains active
                       after the connection context exits, allowing reuse in
                       subsequent connections.
        Script not found: .pyNot a Python script: r   r   r   r   r   Nr   resolveis_fileFileNotFoundErrorrY   endswithrR   extendsuperrh   r   )	rE   r   r   r   r   r   r   	full_argsrJ   s	           r=   rh   zPythonStdioTransport.__init__  s    . ;'//1""$#&8$FGG;((/4[MBCC%&	T"! 	 	
 'r<   )r1   r2   r3   r4   sys
executablerY   r   r   r   r   rh   __classcell__rJ   s   @r=   r"   r"     s    /
 "&%).."&('4Z(' 3i$(' #s(^d"	('
 4Z(' (' 4K(' ('r<   r"   c                   p     e Zd ZdZ	 	 	 	 d	deez  dee   dz  deeef   dz  dedz  dedz  f
 fdZ	 xZ
S )
r#   z<Transport for running FastMCP servers using the FastMCP CLI.Nr   r   r   r   r   c                    t        |      j                         }|j                         st        d|       t	        |      j                  d      st        d|       t        | !  ddt	        |      g|||       || _	        y )Nr   r   r   fastmcprunr   )
r   r   r   r   rY   r   rR   r   rh   r   )rE   r   r   r   r   r   rJ   s         r=   rh   zFastMCPStdioTransport.__init__  s     ;'//1""$#&8$FGG;((/4[MBCC[)*! 	 	
 'r<   r   r1   r2   r3   r4   rY   r   r   r   r   rh   r   r   s   @r=   r#   r#     sp    F
 "&%)"&'4Z' 3i$' #s(^d"	'
 4Z' 4K' 'r<   r#   c                   v     e Zd ZdZ	 	 	 	 	 d
deez  dee   dz  deeef   dz  dedz  dededz  f fd	Z	 xZ
S )r$   z&Transport for running Node.js scripts.Nr   r   r   r   node_cmdr   c                 8   t        |      j                         }|j                         st        d|       t	        |      j                  d      st        d|       t	        |      g}|r|j                  |       t        | %  |||||       || _
        y)a  
        Initialize a Node transport.

        Args:
            script_path: Path to the Node.js script to run
            args: Additional arguments to pass to the script
            env: Environment variables to set for the subprocess
            cwd: Current working directory for the subprocess
            node_cmd: Node.js command to use (default: "node")
            keep_alive: Whether to keep the subprocess alive between connections.
                       Defaults to True. When True, the subprocess remains active
                       after the connection context exits, allowing reuse in
                       subsequent connections.
        r   .jszNot a JavaScript script: r   Nr   )	rE   r   r   r   r   r   r   r   rJ   s	           r=   rh   zNodeStdioTransport.__init__  s    . ;'//1""$#&8$FGG;((/8FGG%&	T"9#3: 	 	
 'r<   )NNNnodeNr   r   s   @r=   r$   r$     s}    0
 "&%)"&$'4Z$' 3i$$' #s(^d"	$'
 4Z$' $' 4K$' $'r<   r$   c                        e Zd ZdZ	 	 	 	 	 	 	 ddedee   dz  dedz  dedz  dee   dz  dedz  d	eeef   dz  d
edz  f fdZ xZ	S )r%   z0Transport for running commands via the uvx tool.N	tool_name	tool_argsproject_directorypython_versionwith_packagesfrom_packageenv_varsr   c	                    |r't        |      j                         st        d|       g }	|r|	j                  d|g       |r|	j                  d|g       |xs g D ]  }
|	j                  d|
g        |	j	                  |       |r|	j                  |       d}|r/t
        j                  j                         }|j                  |       t        | )  d|	|||       || _        y)a  
        Initialize a Uvx transport.

        Args:
            tool_name: Name of the tool to run via uvx
            tool_args: Arguments to pass to the tool
            project_directory: Project directory (for package resolution)
            python_version: Python version to use
            with_packages: Additional packages to include
            from_package: Package to install the tool from
            env_vars: Additional environment variables
            keep_alive: Whether to keep the subprocess alive between connections.
                       Defaults to True. When True, the subprocess remains active
                       after the connection context exits, allowing reuse in
                       subsequent connections.
        Project directory not found: z--pythonz--fromz--withNuvxr   )r   existsNotADirectoryErrorr   appendosenvironcopyupdater   rh   r   )rE   r   r   r   r   r   r   r   r   uvx_argspkgr   rJ   s               r=   rh   zUvxStdioTransport.__init__  s    8 T*;%<%C%C%E$/0A/BC 
 OOZ89OOX|45 &B 	-COOXsO,	- 		"OOI& **//#CJJx !! 	 	
 #r<   )NNNNNNN
r1   r2   r3   r4   rY   r   r   r   rh   r   r   s   @r=   r%   r%     s    :
 '+(,%)*.#'*."&<#<# 9t#<# :	<#
 d
<# Cy4'<# Dj<# sCx.4'<# 4K<# <#r<   r%   c                   p     e Zd ZdZ	 	 	 	 	 d
dedee   dz  dedz  deeef   dz  dededz  f fd	Z xZ	S )r&   z0Transport for running commands via the npx tool.Npackager   r   r   use_package_lockr   c                    t        j                  d      t        d      |r't        |      j	                         st        d|       g }|r|j                  d       |j                  |       |r|j                  |       d}|r/t        j                  j                         }|j                  |       t        	| 5  d||||       || _        y)a  
        Initialize an Npx transport.

        Args:
            package: Name of the npm package to run
            args: Arguments to pass to the package command
            project_directory: Project directory with package.json
            env_vars: Additional environment variables
            use_package_lock: Whether to use package-lock.json (--prefer-offline)
            keep_alive: Whether to keep the subprocess alive between connections.
                       Defaults to True. When True, the subprocess remains active
                       after the connection context exits, allowing reuse in
                       subsequent connections.
        npxNzCommand 'npx' not foundr   z--prefer-offliner   )shutilwhichrR   r   r   r   r   r   r   r   r   r   r   rh   r   )
rE   r   r   r   r   r   r   npx_argsr   rJ   s
            r=   rh   zNpxStdioTransport.__init__G  s    0 <<&677 T*;%<%C%C%E$/0A/BC 
 OO./ 	 OOD! **//#CJJx !! 	 	
 r<   )NNNTNr   r   s   @r=   r&   r&   D  sy    :
 "&(,*.!%"&88 3i$8 :	8
 sCx.4'8 8 4K8 8r<   r&   c                   n    e Zd ZdZd
deez  defdZej                  de
e   dee   fd       ZdefdZy	)r'   a\  In-memory transport for FastMCP servers.

    This transport connects directly to a FastMCP server instance in the same
    Python process. It works with both FastMCP 2.x servers and FastMCP 1.0
    servers from the low-level MCP SDK. This is particularly useful for unit
    tests or scenarios where client and server run in the same runtime.
    r8   raise_exceptionsc                      || _         || _        y)z=Initialize a FastMCPTransport from a FastMCP server instance.N)serverr   )rE   r8   r   s      r=   rh   zFastMCPTransport.__init__  s      0r<   r@   rA   c           	     Z   	K   t               4 d {   \  }}|\  }}|\  	t        j                         4 d {   }|j                   	fd       	 t	        d||d|4 d {   }| d d d       d {    |j
                  j                          d d d       d {    d d d       d {    y 7 7 7 X7 E# 1 d {  7  sw Y   UxY w# |j
                  j                          w xY w7 Q# 1 d {  7  sw Y   axY w7 X# 1 d {  7  sw Y   y xY ww)Nc                       j                   j                  j                   j                   j                  j                          j                        S )N)r   )r   _mcp_serverr   create_initialization_optionsr   )rE   server_readserver_writes   r=   <lambda>z2FastMCPTransport.connect_session.<locals>.<lambda>  sF    DKK3377#$//MMO)-)>)>	 8  r<   )rn   ro   r;   )r   r   create_task_group
start_soonr   cancel_scopecancel)
rE   r@   client_streamsserver_streamsclient_readclient_writetgclient_sessionr   r   s
   `       @@r=   rF   z FastMCPTransport.connect_session  s0     78 	- 	- =
(6%K(6%K ..0 - -B-,  $/%1  )  - - (,,- - OO**,%- -	- 	- 	--- - - - - OO**,%- - - -	- 	- 	- 	-s   D+CD+&DCDC?C,C
-C0C	6CCCC? D+C=,D0D+;D<D+DCC	CCCCC::C?=D?D	DD	DD+D(DD($D+c                 6    d| j                   j                   dS )Nz<FastMCPTransport(server='rr   )r   namerK   s    r=   rL   zFastMCPTransport.__repr__  s    +DKK,<,<+=SAAr<   N)F)r1   r2   r3   r4   r   FastMCP1Serverr   rh   rW   rX   r   r*   r   r   rF   rY   rL   r;   r<   r=   r'   r'     sa    1Gn4 1 1 ##- &} 5-	}	%- $->B# Br<   r'   c                   h    e Zd ZdZdeez  fdZej                  de	e
   dee   fd       ZdefdZy)	MCPConfigTransporta  Transport for connecting to one or more MCP servers defined in an MCPConfig.

    This transport provides a unified interface to multiple MCP servers defined in an MCPConfig
    object or dictionary matching the MCPConfig schema. It supports two key scenarios:

    1. If the MCPConfig contains exactly one server, it creates a direct transport to that server.
    2. If the MCPConfig contains multiple servers, it creates a composite client by mounting
       all servers on a single FastMCP instance, with each server's name used as its mounting prefix.

    In the multi-server case, tools are accessible with the prefix pattern `{server_name}_{tool_name}`
    and resources with the pattern `protocol://{server_name}/path/to/resource`.

    This is particularly useful for creating clients that need to interact with multiple specialized
    MCP servers through a single interface, simplifying client code.

    Examples:
        ```python
        from fastmcp import Client
        from fastmcp.utilities.mcp_config import MCPConfig

        # Create a config with multiple servers
        config = {
            "mcpServers": {
                "weather": {
                    "url": "https://weather-api.example.com/mcp",
                    "transport": "streamable-http"
                },
                "calendar": {
                    "url": "https://calendar-api.example.com/mcp",
                    "transport": "streamable-http"
                }
            }
        }

        # Create a client with the config
        client = Client(config)

        async with client:
            # Access tools with prefixes
            weather = await client.call_tool("weather_get_forecast", {"city": "London"})
            events = await client.call_tool("calendar_list_events", {"date": "2023-06-01"})

            # Access resources with prefixed URIs
            icons = await client.read_resource("weather://weather/icons/sunny")
        ```
    configc                    ddl m} t        |t              rt	        j
                  |      }|| _        t        | j                  j                        dk(  rt        d      t        | j                  j                        dk(  rDt        | j                  j                  j                               d   j                         | _        y t               }| j                  j                  j                         D ]B  \  }} ||j                               }|j!                  |t        j"                  |             D t%        |      | _        y )Nr   )Clientz$No MCP servers defined in the config   rm   )prefixr   r8   )fastmcp.client.clientr  rf   r   r   	from_dictr  len
mcpServersrR   r   valuesto_transportrm   r   itemsmountas_proxyr'   )rE   r  r  composite_serverr  r   server_clients          r=   rh   zMCPConfigTransport.__init__  s   0fd#((0F t{{%%&!+CDD ''(A-!$++"8"8"?"?"AB1ERRTDN  'y $ 6 6 < < > f &1D1D1F G &&(8(8(G '  .2BCDNr<   r@   rA   c                   K    | j                   j                  di |4 d {   }| d d d       d {    y 7 7 # 1 d {  7  sw Y   y xY ww)Nr;   )rm   rF   )rE   r@   rp   s      r=   rF   z"MCPConfigTransport.connect_session  sU      24>>11CNC 	 	wM	 	 	 	 	 	 	s@   !A>AAAA A AAA	AAc                 "    d| j                    dS )Nz<MCPConfigTransport(config='rr   r  rK   s    r=   rL   zMCPConfigTransport.__repr__
  s    -dkk]#>>r<   N)r1   r2   r3   r4   r   r   rh   rW   rX   r   r*   r   r   rF   rY   rL   r;   r<   r=   r  r    s[    -^Dy4/ D6 ## &} 5	}	% $?# ?r<   r  rm   rA   c                      y r   r;   r  s    r=   r(   r(         FIr<   c                      y r   r;   r  s    r=   r(   r(     s    =@r<   c                      y r   r;   r  s    r=   r(   r(     s    DGr<   c                      y r   r;   r  s    r=   r(   r(     s    ADr<   c                      y r   r;   r  s    r=   r(   r(     r'  r<   c                      y r   r;   r  s    r=   r(   r(   "  s     .1r<   c                      y r   r;   r  s    r=   r(   r(   (  s    
 r<   c                      y r   r;   r  s    r=   r(   r(   0  s    SVr<   c                    t        | t              r| S t        | t        t        z        rt	        |       }nt        | t
        t        z        rut        |       j                         r\t        |       j                  d      rt        |       }nt        |       j                  d      rt        |       }nt        d|        t        | t        t        z        rDt        |       j                  d      r*t        |       }|dk(  rt        |       }n?t!        |       }n2t        | t"        t$        z        rt'        | 	      }nt        d
|        t(        j+                  d|        |S )a:  
    Infer the appropriate transport type from the given transport argument.

    This function attempts to infer the correct transport type from the provided
    argument, handling various input types and converting them to the appropriate
    ClientTransport subclass.

    The function supports these input types:
    - ClientTransport: Used directly without modification
    - FastMCP or FastMCP1Server: Creates an in-memory FastMCPTransport
    - Path or str (file path): Creates PythonStdioTransport (.py) or NodeStdioTransport (.js)
    - AnyUrl or str (URL): Creates StreamableHttpTransport (default) or SSETransport (for /sse endpoints)
    - MCPConfig or dict: Creates MCPConfigTransport, potentially connecting to multiple servers

    For HTTP URLs, they are assumed to be Streamable HTTP URLs unless they end in `/sse`.

    For MCPConfig with multiple servers, a composite client is created where each server
    is mounted with its name as prefix. This allows accessing tools and resources from multiple
    servers through a single unified client interface, using naming patterns like
    `servername_toolname` for tools and `protocol://servername/path` for resources.
    If the MCPConfig contains only one server, a direct connection is established without prefixing.

    Examples:
        ```python
        # Connect to a local Python script
        transport = infer_transport("my_script.py")

        # Connect to a remote server via HTTP
        transport = infer_transport("http://example.com/mcp")

        # Connect to multiple servers using MCPConfig
        config = {
            "mcpServers": {
                "weather": {"url": "http://weather.example.com/mcp"},
                "calendar": {"url": "http://calendar.example.com/mcp"}
            }
        }
        transport = infer_transport(config)
        ```
    r  r   )r   r   zUnsupported script type: ry   ssers   r%  z(Could not infer a valid transport from: zInferred transport: )rf   r   r   r  r'   r   rY   r   r   r"   r$   rR   r   rg   r   r   r    r   r   r  r   r   )rm   inferred_transportinferred_transport_types      r=   r(   r(   4  s4   h )_- 
Iw7	8-)< 
Itcz	*tI/E/E/Gy>""5)!5)!L^$$U+!3	!J8DEE 
Iv|	,Y1J1J61R"?	"J"e+!-)!<!8Y!G 
Iti/	0/yA CI;OPP
LL'(:';<=r<   )IrU   r   rW   r5   r   r   r   rc   collections.abcr   r   pathlibr   typingr   r   r   r	   r
   r   r   rZ   	mcp.typesr8   r   r   mcp.client.sessionr   r   r   r   mcp.server.fastmcpr   r  mcp.shared.memoryr   pydanticr   typing_extensionsr   fastmcp.client.auth.bearerr   fastmcp.client.auth.oauthr   fastmcp.server.dependenciesr   fastmcp.server.serverfastmcp.utilities.loggingr   fastmcp.utilities.mcp_configr   r   r1   r   r   __all__r*   ABCr   r]   r   r    r!   r"   r#   r$   r%   r&   r'   r  r(   r   rY   r;   r<   r=   <module>rD     s   
    	  
  3  C C    4 W W 8 A  $ 1 + 8 ) 0 Q	H	 -5FG 1IU 1+Ecgg +E\#9/ #9LA3? A3HB>o B>Jr
_ r
j+'> +'\'N '6'' ''T?# ?#D; ;|3B 3BlS? S?l 
 I/ I4D I 
 I 
 @w @+; @ 
 @ 
 G~ G2B G 
 G 
 Dy D-? D 
 D 
 ItCH~ I2D I 
 I 
11++1 
1
 
 --<?VV 
 
 Vt V(<?Q(Q V 
 VU  	
  38n 

U Ur<   