
    Ri                        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ZdedefdZdede
d   fdZde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dz  dee   dz  dedededdfdZ	 	 	 d"dededz  dedz  dedz  ddf
d Zy)#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                 `    t        j                  d      }t        |j                  |             S )zCheck if a string is a URL.z
^https?://)recompileboolmatch)r   url_patterns     u/home/jay/workspace/.worktrees/task-2117-dev1/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/cli/run.pyis_urlr      s&    **]+K!!$'((    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:    )	fastmcpClientr   as_proxy	Exceptionloggererrorsysexit)r   r"   clientserveres        r   create_client_serverr-      sh    $))&1 7uBqcBCs   14 	A70A22A7mcp_config_pathc                     ddl m} | j                         5 }t        j                  |      }ddd        |j
                        }|S # 1 sw Y   xY w)z)Create a FastMCP server from a MCPConfig.r   r   N)r"   r   openjsonloadr$   )r.   r   src
mcp_configr+   s        r   create_mcp_config_serverr5   3   sN    				 $3YYs^
$ Wj)FM	$ $s   A		Aconfig_pathc                 f    t        j                  |       }|j                  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)r6   configs     r   load_mcp_server_configr<   >   s/     &&{3F ,,[9Mr   server_spec	transporthostport	log_levelserver_argsshow_banneruse_direct_importskip_sourcec
                   K   t        |       r%t        |       }
t        j                  d|         n7| j	                  d      rxt        |       }t        |      5 }t        j                  |      }ddd       dv rt        |      }
nt        |      }|xs |j                  j                  }|xs |j                  j                  }|xs |j                  j                  }|xs |j                  j                  }|xs |j                  j                   }||n|j                  j"                  }|	s|j%                          d{   nd ddlm} ddlm} |r ||      n |       }|5  |j.                  j1                          d{   }
ddd       t        j                  d
j2                   d	|        nt5        | 
      }t7        |      }|	s|j%                          d{   nd ddlm} ddlm} |r ||      n |       }|5  |j.                  j1                          d{   }
ddd       t        j                  d
j2                   d|j                          t9        |
t:              rt=        |
|||       d{    yi }|r||d<   |r||d<   |r||d<   |r||d<   |r||d<   |sd|d<   	  |
j>                  di | d{    y# 1 sw Y   kxY w7 7 `# 1 sw Y   `xY w7 7 # 1 sw Y   xY w7 ~7 8# t@        $ r7}t        jC                  d|        tE        jF                  d       Y d}~yd}~ww xY 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@   r>   r>   r?   r@   r   rA   FrC   zFailed to run server: r!    )$r   r-   r&   debugendswithr   r0   r1   r2   r5   r<   r9   r>   r?   r@   r   rA   argsprepare_source
contextlibrH   fastmcp.cli.clirI   rJ   load_servernamer
   r	   
isinstance	FastMCP1xrun_v1_server_async	run_asyncr%   r'   r(   r)   )r=   r>   r?   r@   r   rA   rB   rC   rD   rE   r+   r6   fdatar;   rH   rI   argv_contextrJ   kwargsr,   s                        r   run_commandr\   O   s    6 k%k27}EF			g	&;'+ 	 !99Q<D	  4-k:F ,K8F "@V%6%6%@%@I16,,11D16,,11D16,,11D!@V%6%6%@%@I*6F<M<M<R<R 
 2=&'')))$ /1 6A9[1kmL ;%}}88::; LL>&++n[MRS "{3 / .9f##%%%d 	+- 2=y-+- 	7!==4466F	7 	~fkk]%}EF
 &)$!&t$)TTTF'{vvv'{ %}f((((a	  	 , * ;; ; 	& 7	7 	7 	U& 	) -aS12s   AMK1CMK'%M,K-
K*K-AM*K:+%MK?.K=/K?3AML1M?L LL MK$	M*K--K72	M=K??LML 	M-M
M
MMr+   c                 8  K   |r|| j                   _        |r|| j                   _        |xdk(  r | j                          d{    yxxdk(  rnxdk(  rnxn n  | j	                          d{    ydk(  r| j                          d{    yy7 P7 %7 
w)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@   r>   s       r   rV   rV      s      ##
((***.V'$.22444&&(((  +4(s6   ABB,B0B1BBBBB)	NNNNNNTFF)NNN) __doc__r1   r   r(   pathlibr   typingr   r   mcp.server.fastmcpr   rU   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-   r5   r<   intlistr\   rV   rK   r   r   <module>ro      s   B  	 
   3 ) 0 W	I	 ABFG) ) )c c (d wt}   & '+%)$(#vvt#v *v *	v
 *v d"v cT!v v v v 
vv &*	))
*) *) t#	)
 
)r   