o
    i                     @   st  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 ddlm
Z
 ddlmZ ddlmZ ddlmZ ed	Zed
 Zed ZdedefddZdedefddZdede
d fddZdedefddZ									d)dededB dedB dedB dedB dedB d ee dB d!ed"ed#eddfd$d%Z			d*d&ededB dedB dedB ddf
d'd(ZdS )+z<FastMCP run command implementation with enhanced type hints.    N)Path)AnyLiteralFastMCP)
get_logger)MCPServerConfig)FileSystemSourcezcli.run)stdiohttpssestreamable-http)DEBUGINFOWARNINGERRORCRITICALpathreturnc                 C   s   t d}t|| S )zCheck if a string is a URL.z
^https?://)recompileboolmatch)r   url_pattern r   U/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/cli/run.pyis_url   s   
r   urlc              
   C   sl   zddl }|| }|j|}|W S  ty5 } ztd|  d|  td W Y d}~dS d}~w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:    )	fastmcpClientr   as_proxy	Exceptionloggererrorsysexit)r   r   clientserverer   r   r   create_client_server   s   	
r*   mcp_config_pathc                 C   sL   ddl m} |  }t|}W d   n1 sw   Y  ||}|S )z)Create a FastMCP server from a MCPConfig.r   r   N)r   r   openjsonloadr!   )r+   r   src
mcp_configr(   r   r   r   create_mcp_config_server3   s   

r1   config_pathc                 C   s   t | }|j|  |S )zLoad a FastMCP configuration from a fastmcp.json file.

    Args:
        config_path: Path to fastmcp.json file

    Returns:
        MCPServerConfig object
    )r   	from_file
deploymentapply_runtime_settings)r2   configr   r   r   load_mcp_server_config>   s   
	r7   TFserver_spec	transporthostport	log_levelserver_argsshow_banneruse_direct_importskip_sourcec
              
      s  t | rt| }
td|   n| drt| }t|}t|}W d   n1 s/w   Y  d|v r=t	|}
nt
|}|pF|jj}|pL|jj}|pR|jj}|pX|jj}|p^|jj}|dure|n|jj}|	sr| I dH nd ddlm} ddlm} |r||n| }| |j I dH }
W d   n1 sw   Y  td|
j d	|  nQt| d
}t|d}|	s| I dH nd ddlm} ddlm} |r||n| }| |j I dH }
W d   n1 sw   Y  td|
j d|j  t|
trt|
|||dI dH  dS i }|r||d< |r$||d< |r+||d< |r2||d< |r9||d< |s@d|d< z|
jdi |I dH  W dS  t yo } zt!d|  t"#d W Y d}~dS d}~ww )a  Run a MCP server or connect to a remote one.

    Args:
        server_spec: Python file, object specification (file:obj), config file, 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
        path: Path to bind to when using http transport
        log_level: Log level
        server_args: Additional arguments to pass to the server
        show_banner: Whether to show the server banner
        use_direct_import: Whether to use direct import instead of subprocess
        skip_source: Whether to skip source preparation step
    z Created client proxy server for z.jsonN
mcpServersr   )nullcontext)	with_argvzFound server "z" from config )r   )sourcez" in )r:   r;   r9   r9   r:   r;   r   r<   Fr>   zFailed to run server: r   r   )$r   r*   r#   debugendswithr   r,   r-   r.   r1   r7   r4   r9   r:   r;   r   r<   argsprepare_source
contextlibrB   fastmcp.cli.clirC   rD   load_servernamer	   r   
isinstance	FastMCP1xrun_v1_server_async	run_asyncr"   r$   r%   r&   )r8   r9   r:   r;   r   r<   r=   r>   r?   r@   r(   r2   fdatar6   rB   rC   argv_contextrD   kwargsr)   r   r   r   run_commandO   s|   




rU   r(   c                    s   |r|| j _|r|| j _| dkr |  I dH  dS   dkr"n dkr'n du r,n n  |  I dH  dS dkrE|  I dH  dS dS )zRun a FastMCP 1.x server using async methods.

    Args:
        server: FastMCP 1.x server instance
        host: Host to bind to
        port: Port to bind to
        transport: Transport protocol to use
    r
   Nr   r   r   )settingsr:   r;   run_stdio_asyncrun_streamable_http_asyncrun_sse_async)r(   r:   r;   r9   r   r   r   rO      s   
(rO   )	NNNNNNTFF)NNN) __doc__r-   r   r%   pathlibr   typingr   r   mcp.server.fastmcpr   rN   fastmcp.server.serverfastmcp.utilities.loggingr   #fastmcp.utilities.mcp_server_configr   9fastmcp.utilities.mcp_server_config.v1.sources.filesystemr	   r#   TransportTypeLogLevelTypestrr   r   r*   r1   r7   intlistrU   rO   r   r   r   r   <module>   s    
	

{