
    i                     
   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 d dl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mZ d dlmZmZ d dlmZ d dlmZ er	  ee       Z!ededefd       Z"ededefd       Z"ede	defd       Z"ededefd       Z"ede#e$ef   defd       Z"ededeez  fd       Z"ede$deez  ez  ez  fd       Z"ededeez  fd       Z"deez  e	z  ez  ez  ez  e#e$ef   z  e$z  defdZ"y)    )Path)TYPE_CHECKINGAnycastoverload)FastMCP)AnyUrl)ClientTransportClientTransportT)MCPConfigTransport)StreamableHttpTransport)FastMCPTransport)SSETransport)NodeStdioTransportPythonStdioTransport)	MCPConfiginfer_transport_type_from_url)
get_logger	transportreturnc                      y N r   s    s/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/client/transports/inference.pyinfer_transportr          FI    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   1   s    
 r   c                      y r   r   r   s    r   r   r   9   s    SVr   c                    t        | t              r| S t        | t        t        z        r*t	        t        t        t           t        z  |             }nt        | t        t        z        rt        |       j                         ryt        |       j                  d      rt        t        t        |             }n$t        |       j                  d      rt        t        t        |             }nt        d|        t        | t        t        z        rt        |       j                  d      rit!        t        t        t        z  |             }|dk(  r"t#        t        t        t        z  |             }nit%        t        t        t        z  |             }nGt        | t&        t(        z        r"t+        t        t&        t(        z  |       	      }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)
        ```
    )mcpz.py)script_pathz.jszUnsupported script type: httpsse)url)configz(Could not infer a valid transport from: zInferred transport: )
isinstancer
   r   FastMCP1Serverr   r   r   r   strexistsendswithr   r   
ValueErrorr	   
startswithr   r   r   dictr   r   loggerdebug)r   inferred_transportinferred_transport_types      r   r   r   =   s   h )_- 
Iw7	8-WS\N2I>

 
Itcz	*tI/E/E/Gy>""5)!5$tYBW!X^$$U+!3T9@U!V8DEE 
Iv|	,Y1J1J61R"?#y)#
 #e+!-$v|Y2O!P!8#y1"
 
Iti/	0/y()4
 CI;OPP
LL'(:';<=r   N)%pathlibr   typingr   r   r   r   mcp.server.fastmcpr   r.   pydanticr	   fastmcp.client.transports.baser
   r    fastmcp.client.transports.configr   fastmcp.client.transports.httpr    fastmcp.client.transports.memoryr   fastmcp.client.transports.sser   fastmcp.client.transports.stdior   r   fastmcp.mcp_configr   r   fastmcp.server.serverfastmcp.utilities.loggingr   __name__r5   r   r4   r/   r   r   r   <module>rG      s    5 5 8  L ? B = 6 T G ) 0	H	 
 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]  	
  38n 

] ]r   