o
    h~:                     @   s(  d 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 ddl	m
Z
mZ ddlmZ ddlmZ zddlZW n eyH   ed ed Y nw zdd	lmZ dd
lmZ W n eyi   ed ed Y nw zddlZW n ey{   dZY nw edZejdddddZdd Zdedeeef fddZ		dEdededB de e dB de e fddZ!dedeeedB f fddZ"dFded edB fd!d"Z#e$ dGd#d$Z%e$ ej&d%d&d'dg fdede
edB ej'd(d)d*dddd+f de
e e ej'd,d-d'f ddfd.d/Z(e$ ej&d%d&d'dfded0e
edB ej'd1d2d3d'f ddfd4d5Z)e$ ej&d%d&d'ddg g dfded6e
edB ej'd7d8d9d'f de
edB ej'd(d)d*dddd+f de
e e ej'd,d-d'f d:e
e e ej'd;d<d=d'f d>e
edB ej'd?d@dAdddddBf ddfdCdDZ*dS )HzMCP CLI tools.    N)Path)	AnnotatedAny)FastMCP)Serverz=Error: typer is required. Install with 'pip install mcp[cli]'   )claude)
get_loggerz?Error: mcp.server.fastmcp is not installed or not in PYTHONPATHclimcpzMCP development toolsFT)namehelpadd_completionno_args_is_helpc               	   C   sP   t jdkr&dD ]} ztj| dgdddd | W   S  tjy#   Y qw dS dS )z5Get the correct npx command for the current platform.win32)znpx.cmdznpx.exenpxz	--versionT)checkcapture_outputshellNr   )sysplatform
subprocessrunCalledProcessError)cmd r   L/var/www/html/openai_agents/venv/lib/python3.10/site-packages/mcp/cli/cli.py_get_npx_command*   s   

r   env_varreturnc                 C   sD   d| vrt d|  d td | dd\}}| | fS )z6Parse environment variable string in format KEY=VALUE.=z%Invalid environment variable format: z. Must be KEY=VALUEr   )loggererrorr   exitsplitstrip)r   keyvaluer   r   r   _parse_env_var8   s
   
r(   	file_specwith_editablewith_packagesc                 C   s^   dg}| g d |r| dt|g |r%|D ]}|r$| d|g q| dd| g |S )z@Build the uv run command that runs a MCP server through mcp run.uv)r   --withr   --with-editabler-   r   r   )extendstr)r)   r*   r+   r   pkgr   r   r   _build_uv_commandA   s   r2   c                 C   s   t | dko| d dk}d|r| dd n| v r!| dd\}}n| d}}t|  }| s?td|  t	d |
 sPtd|  t	d ||fS )zParse a file path that may include a server object specification.

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

    Returns:
        Tuple of (file_path, server_object)
    r   :   NzFile not found: zNot a file: )lenrsplitr   
expanduserresolveexistsr!   r"   r   r#   is_file)r)   has_windows_drivefile_strserver_object	file_pathr   r   r   _parse_file_pathX   s   



r?   filer=   c                 C   s  t | j}|tjvrtjd| tjd| }|r|js-t	j
ddt | id td tj|}|j| dtdt fd	d
}|sdD ](}t||rn|t|||  d| sgt	
d|  d| d qFt||  S qFt	j
d|  ddt | id td d|v r|dd\}}zt|}	t|	|d}
W n# ty   t	j
d| ddt | id td Y nw t||d}
|
du rt	j
d| ddt | id td ||
|std |
S )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   r=   object_namec              	   S   sH   t | ts"td| dt|  dt d t | tr td dS dS )zHelper function to check that the server object is supported

        Args:
            server_object: The server object to check.

        Returns:
            True if it's supported.
        zThe server object z is of type z (expecting z).zXNote that only FastMCP server is supported. Low level Server class is not yet supported.FT)
isinstancer   r!   r"   typeLowLevelServerwarning)r=   rD   r   r   r   _check_server_object   s   
	"
z,_import_server.<locals>._check_server_object)r   serverappr3   zIgnoring object 'z#' as it's not a valid server objectzNo server object found in a  . Please either:
1. Use a standard variable name (mcp, server, or app)
2. Specify the object name with file:object syntax3. If the server creates the FastMCP object within main()    or another function, refactor the FastMCP object to be a    global variable named mcp, server, or app.NzCould not import module ''zServer object 'z' not found)r0   parentr   pathinsert	importlibutilspec_from_file_locationloaderr!   r"   r#   module_from_specexec_moduler   hasattrgetattrr$   import_moduleImportError)r@   r=   file_dirspecmodulerI   r   module_namerD   rA   rJ   r   r   r   _import_serverw   sZ   







	







r^   c                  C   sL   zt jd} td|   W dS  t jjy%   td td Y dS w )zShow the MCP version.r   zMCP version z+MCP version unknown (package not installed)r   N)rP   metadataversionprintPackageNotFoundErrorr   r#   )r`   r   r   r   r`      s   r`   .z2Python file to run, optionally with :object suffix)r   r.   z-ez?Directory containing pyproject.toml to install in editable mode)r   r9   	file_okayresolve_pathr-   zAdditional packages to installc              
   C   sL  t | \}}tjdt|||rt|nd|dd zHt||}t|dr.tt||j }t	| ||}t
 }|sCtd td tjdk}tj|d	g| d
|ttj d}	t|	j W dS  tjy }
 ztjdt|t|
|
jdd t|
j W Y d}
~
dS d}
~
w ty   tjddt|id td Y dS w )z(Run a MCP server with the MCP Inspector.zStarting dev serverN)r@   r=   r*   r+   rB   dependencieszbnpx not found. Please ensure Node.js and npm are properly installed and added to your system PATH.r   r   z@modelcontextprotocol/inspectorT)r   r   envzDev server failed)r@   r"   
returncodeznpx not found. Please ensure Node.js and npm are properly installed and added to your system PATH. You may need to restart your terminal after installation.r@   )r?   r!   debugr0   r^   rV   listsetre   r2   r   r"   r   r#   r   r   r   dictosenvironitemsrg   r   FileNotFoundError)r)   r*   r+   r@   r=   rJ   uv_cmdnpx_cmdr   processer   r   r   dev   sZ   






rt   	transportz--transportz-tz(Transport protocol to use (stdio or sse)c                 C   s   t | \}}tjdt|||dd zt||}i }|r!||d< |jd	i | W dS  tyE   tjddt|id t	d Y dS w )
a  Run a MCP server.

    The server can be specified in two ways:

    1. Module approach: server.py - runs the module directly, expecting a server.run() call.

    2. Import approach: server.py:app - imports and runs the specified server object.



    Note: This command runs the server directly. You are responsible for ensuring
    all dependencies are available.

    For dependency management, use `mcp install` or `mcp dev` instead.
    zRunning server)r@   r=   ru   rB   ru   zFailed to run serverr@   r   Nr   )
r?   r!   rh   r0   r^   r   	Exception	exceptionr   r#   )r)   ru   r@   r=   rJ   kwargsr   r   r   r   1  s,   	
r   server_namez--namez-nzMCustom name for the server (defaults to server's name attribute or file name)env_varsz	--env-varz-vz)Environment variables in KEY=VALUE formatenv_filez
--env-filez-fz+Load environment variables from a .env file)r   r9   rc   dir_okayrd   c              
   C   s  t | \}}tjdt||||rt|nd|dd t s)td td |}d}	|s\z
t	||}	|	j
}W n" ttfy[ }
 ztjddt|
id |j}W Y d}
~
nd}
~
ww |	rdt|	d	g ng }|rptt|| }d}|sv|ri }|rtrz|d
d t| D O }W n ttfy   td td Y nw td td |D ]}t|\}}|||< qtj| ||||drtd| d dS td| d td dS )zInstall a MCP server in the Claude desktop app.

    Environment variables are preserved once added and only updated if new values
    are explicitly provided.
    zInstalling serverN)r@   ry   r=   r*   r+   rB   zClaude app not foundr   zFCould not import server (likely missing dependencies), using file namer"   re   c                 S   s   i | ]\}}|d ur||qS Nr   ).0kvr   r   r   
<dictcomp>  s    zinstall.<locals>.<dictcomp>zFailed to load .env filez6python-dotenv is not installed. Cannot load .env file.)r*   r+   rz   zSuccessfully installed z in Claude appzFailed to install )r?   r!   rh   r0   r   get_claude_config_pathr"   r   r#   r^   r   rY   ModuleNotFoundErrorstemrW   ri   rj   dotenvdotenv_valuesrn   OSError
ValueErrorrw   r(   update_claude_configinfo)r)   ry   r*   r+   rz   r{   r@   r=   r   rJ   rs   server_dependenciesenv_dictr   r&   r'   r   r   r   installj  sp   :




 



r   )NNr}   )r   N)+__doc__importlib.metadatarP   importlib.utilrl   r   r   pathlibr   typingr   r   
mcp.serverr   r   rG   typerrY   ra   r#   mcp.clir   $mcp.server.fastmcp.utilities.loggingr	   r   r!   TyperrK   r   r0   tupler(   ri   r2   r?   r^   commandr`   ArgumentOptionrt   r   r   r   r   r   r   <module>   sd   

\
R8'3