
    ~h                     @   d Z ddl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  ed      Ze	d   Zded	efd
Zded	eeedz  f   fdZddededz  d	efdZded	efdZ	 ddededz  dee   dz  d	efdZ	 	 	 	 	 ddededz  dedz  dedz  dedz  dee   dz  d	dfdZy)z#FastMCP run command implementation.    N)Path)AnyLiteral)
get_loggerzcli.run)stdiozstreamable-httpssepathreturnc                 `    t        j                  d      }t        |j                  |             S )zCheck if a string is a URL.z
^https?://)recompileboolmatch)r	   url_patterns     K/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/fastmcp/cli/run.pyis_urlr      s&    **]+K!!$'((    server_specc                    t        |       dkD  xr | d   dk(  }d|r| dd n| v r| j                  dd      \  }}n| d}}t        |      j                         j	                         }|j                         s-t        j                  d|        t        j                  d       |j                         s-t        j                  d|        t        j                  d       ||fS )zParse a file path that may include a server object specification.

    Args:
        server_spec: Path to file, optionally with :object suffix

    Returns:
        Tuple of (file_path, server_object)
       :   NzFile not found: zNot a file: )lenrsplitr   
expanduserresolveexistsloggererrorsysexitis_file)r   has_windows_drivefile_strserver_object	file_paths        r   parse_file_pathr'      s     K(1,FQ31F "3{12E"-"4"4S!"<-"-t- X))+335I'	{34|I;/0m##r   filer%   c                 \   t        | j                        }|t        j                  vr t        j                  j	                  d|       t
        j                  j                  d|       }|r|j                  s7t        j                  ddt        |       i       t        j                  d       |J |j                  J t
        j                  j                  |      }|j                  j                  |       |s\dD ]  }t        ||      st        ||      c S  t        j                  d	|  d
dt        |       i       t        j                  d       |J d|v r9|j!                  dd      \  }}	 t        j"                  |      }t        ||d      }	nt        ||d      }		;t        j                  d| ddt        |       i       t        j                  d       |	S # t$        $ r> t        j                  d| ddt        |       i       t        j                  d       Y w xY w)zImport a MCP server from a file.

    Args:
        file: Path to the file
        server_object: Optional object name in format "module:object" or just "object"

    Returns:
        The server object
    r   server_modulezCould not load moduler(   )extrar   N)mcpserverappzNo server object found in zy. Please either:
1. Use a standard variable name (mcp, server, or app)
2. Specify the object name with file:object syntaxr   zCould not import module ''zServer object 'z' not found)strparentr    r	   insert	importlibutilspec_from_file_locationloaderr   r   r!   module_from_specexec_modulehasattrgetattrsplitimport_moduleImportError)
r(   r%   file_dirspecmodulenamemodule_nameobject_namer*   r-   s
             r   import_serverrD   5   s    4;;Hsxx8$ >>11/4HDt{{,VSY4GH;;"""^^,,T2FKKF# , 	-Dvt$vt,,	- 	( /A A 3t9%	 	 	
 	$$$ m#0#6#6sA#> [	%33K@M]K>F 5~m_K83t9% 	 	
 	M#  	LL+K=:s4y)   HHQK	s   5"G$ $AH+*H+urlc                     	 ddl }|j                  |       }|j                  j                  |      }|S # t        $ r:}t
        j                  d|  d|        t        j                  d       Y d}~yd}~ww xY w)zCreate a FastMCP server from a client URL.

    Args:
        url: The URL to connect to

    Returns:
        A FastMCP server instance
    r   Nz Failed to create client for URL z: r   )	fastmcpClientFastMCPfrom_client	Exceptionr   r   r    r!   )rE   rG   clientr-   es        r   create_client_serverrN   {   sh    $,,V4 7uBqcBCs   14 	A70A22A7server_argsc                     |rCt         j                  dd }	 t        |       g|z   t         _        t        | |      |t         _        S t        | |      S # |t         _        w xY w)a  Import a server with optional command line arguments.

    Args:
        file: Path to the server file
        server_object: Optional server object name
        server_args: Optional command line arguments to inject

    Returns:
        The imported server object
    N)r    argvr0   rD   )r(   r%   rO   original_argvs       r   import_server_with_argsrS      sT     	%D	{[0CH }5$CHT=11 %CHs   #A A	transporthostport	log_levelc                    t        |       r$t        |       }t        j                  d|         n@t	        |       \  }}t        |||      }t        j                  d|j                   d|        i }	|r||	d<   |r||	d<   |r||	d<   |r||	d<   	  |j                  di |	 y
# t        $ r7}
t        j                  d|
        t        j                  d	       Y d
}
~
y
d
}
~
ww xY w)a  Run a MCP server or connect to a remote one.

    Args:
        server_spec: Python file, object specification (file:obj), or URL
        transport: Transport protocol to use
        host: Host to bind to when using http transport
        port: Port to bind to when using http transport
        log_level: Log level
        server_args: Additional arguments to pass to the server
    z Created client proxy server for zFound server "z" in rT   rU   rV   rW   zFailed to run server: r   N )r   rN   r   debugr'   rS   rA   runrK   r   r    r!   )r   rT   rU   rV   rW   rO   r-   r(   r%   kwargsrM   s              r   run_commandr]      s    $ k%k27}EF .k:m(}kJ~fkk]%v>? F'{vv'{

V -aS12s   B" "	C"+-CC")N)NN)NNNNN)__doc__importlib.utilr3   r   r    pathlibr   typingr   r   fastmcp.utilities.loggingr   r   TransportTyper0   r   r   tupler'   rD   rN   listrS   intr]   rY   r   r   <module>rg      sC   )  	 
   0	I	9:) ) )$ $tS4Z/?)@ $>C CS4Z C3 CLc c * SW2
2"Tz2?CCy4?O224 ! $(++Tz+ *+ *	+
 Tz+ cT!+ 
+r   