
    )`iK;                        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 	 ddlZn&# e$ r  ed            ej        d           Y nw xY w	 dd	lmZ dd
lmZ n&# e$ r  ed            ej        d           Y nw xY w	 ddlZn# e$ r dZY nw xY w ed          Z ej        dddd          Zd Zdedeeef         fdZ	 	 d<dededz  de e         dz  de e         fdZ!dedeeedz  f         fdZ"d=dededz  fdZ#e$                                d>d            Z%e$                                 ej&        dd !          dg fdede
edz   ej'        d"d#d$ddd%          f         de
e e          ej'        d&d'!          f         ddfd(            Z(e$                                 ej&        dd !          dfded)e
edz   ej'        d*d+d,!          f         ddfd-            Z)e$                                 ej&        dd !          ddg g dfded.e
edz   ej'        d/d0d1!          f         de
edz   ej'        d"d#d$ddd%          f         de
e e          ej'        d&d'!          f         d2e
e e          ej'        d3d4d5!          f         d6e
edz   ej'        d7d8d9dddd:          f         ddfd;            Z*dS )?z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                      t           j        dk    r:dD ]5} 	 t          j        | dgddd           | c S # t          j        $ r Y 2w xY 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)cmds    _/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/mcp/cli/cli.py_get_npx_commandr   *   sx    
|w0 	 	C[1dZ^____


0   t5s   4AAenv_varreturnc                     d| vr2t                               d|  d           t          j        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values      r   _parse_env_varr*   8   sj    
'YWYYYZZZsA&&JC99;;%%    	file_specwith_editablewith_packagesc                     dg}|                     g d           |r$|                     dt          |          g           |r|D ]}|r|                     d|g           |                     dd| g           |S )zABuild the uv run command that runs an MCP server through mcp run.uv)r   --withr   --with-editabler1   r   r   )extendstr)r,   r-   r.   r   pkgs        r   _build_uv_commandr6   A   s     &CJJ'''((( <

%s='9'9:;;; ,  	, 	,C ,

Hc?+++ JJui()))Jr+   c                    t          |           dk    o| d         dk    }d|r
| dd         n| v r|                     dd          \  }}n| d}}t          |                                                                          }|                                s1t                              d|            t          j	        d           |
                                s1t                              d|            t          j	        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_paths        r   _parse_file_pathrD   X   s    I*By|s/B  1@y}}yAA"+"2"23":":--"+T- X))++3355I 3	33444 /I//000m##r+   filerB   c                 d   t          | j                  }|t          j        vr t          j                            d|           t
          j                            d|           }|r|j        s?t          
                    ddt          |           i           t          j        d           t
          j                            |          }|j                            |           dt          dt           fd	}|sd
D ]e}t          ||          rS |t!          ||          |  d|           s"t          
                    d|  d| d           St!          ||          c S ft          
                    d|  ddt          |           i           t          j        d           d|v r|                    dd          \  }}	 t          j        |          }	t!          |	|d          }
nd# t&          $ rF t          
                    d| ddt          |           i           t          j        d           Y nw xY wt!          ||d          }
|
Ct          
                    d| ddt          |           i           t          j        d            ||
|          st          j        d           |
S )zImport an 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 modulerE   extrar   rB   object_namec           	          t          | t                    sgt                              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)rB   rJ   s     r   _check_server_objectz,_import_server.<locals>._check_server_object   s     -11 	LLsksstMGZGZsshosssttt-88 n   5tr+   )r   serverappr8   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)r4   parentr   pathinsert	importlibutilspec_from_file_locationloaderr#   r$   r%   module_from_specexec_moduler   hasattrgetattrr&   import_moduleImportError)rE   rB   file_dirspecmodulerP   r   module_namerJ   rG   rQ   s              r   _import_serverre   w   s    4;Hsx8$$$ >11/4HHD t{ ,VSYY4GHHH^,,T22FKF###C c    &  , 	- 	-Dvt$$ -++GFD,A,AdCSCSTCSCSTT LL!eT!e!eD!e!e!efffvt,,,,,	- 	< < < < 3t99% 	 	
 	
 	
 	 m#0#6#6sA#>#> [	%3K@@M]K>>FF 	 	 	LL:K:::s4yy)     HQKKKKK	 55~8m8883t99% 	 	
 	
 	
 	66 Ms   %G' 'AH76H7c                      	 t           j                            d          } t          d|             dS # t           j        j        $ r' t          d           t          j        d           Y dS w xY w)zShow the MCP version.r   zMCP version z+MCP version unknown (package not installed)r   N)rW   metadataversionprintPackageNotFoundErrorr   r%   )rh   s    r   rh   rh      s}    $,,U33&W&&'''''2   ;<<<s   15 7A0/A0.z2Python file to run, optionally with :object suffix)r   r2   z-ez?Directory containing pyproject.toml to install in editable mode)r   r>   	file_okayresolve_pathr1   zAdditional packages to installc           	      .   t          |           \  }}t                              dt          |          ||rt          |          nd|d           	 t	          ||          }t          |d          r$t          t          ||j        z                       }t          | ||          }t                      }|s.t                              d           t          j        d           t          j        dk    }t          j        |d	g|z   d
|t#          t$          j                                                            }	t          j        |	j                   dS # t          j        $ rc}
t                              dt          |          t          |
          |
j        d           t          j        |
j                   Y d}
~
dS d}
~
wt.          $ rC t                              ddt          |          i           t          j        d           Y dS w xY w)z)Run an MCP server with the MCP Inspector.zStarting dev serverN)rE   rB   r-   r.   rH   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)rE   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.rE   )rD   r#   debugr4   re   r]   listsetrn   r6   r   r$   r   r%   r   r   r   dictosenvironitemsrp   r   FileNotFoundError)r,   r-   r.   rE   rB   rQ   uv_cmdnpx_cmdr   processes              r   devr}      s>   4 +955D-
LLII*3@JS///d*	
 
    *m446>** 	K ]V5H%H!I!IJJM"9m]KK #$$ 	LLt   HQKKK '.786ARZ%%''((	
 
 
 	#$$$$$( 	 	 	D		Ql  	 	
 	
 	
 	   " 3t99%	 	 	
 	
 	
 	s!   DE H&AGAHH	transportz--transportz-tz(Transport protocol to use (stdio or sse)c                 r   t          |           \  }}t                              dt          |          ||d           	 t	          ||          }i }|r||d<    |j        d	i | dS # t          $ rC t                              ddt          |          i           t          j	        d           Y dS w xY w)
a  Run an 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)rE   rB   r~   rH   r~   zFailed to run serverrE   r   N )
rD   r#   rq   r4   re   r   	Exception	exceptionr   r%   )r,   r~   rE   rB   rQ   kwargss         r   r   r   1  s    2 +955D-
LLII*"
 
    m44  	,"+F;
V   "D		 	 	
 	
 	
 	s   &A) )A	B65B6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   r>   rk   dir_okayrl   c           	         t          |           \  }}t                              dt          |          |||rt          |          nd|d           t	          j                    s.t                              d           t          j        d           |}d}	|si	 t          ||          }	|	j
        }nP# t          t          f$ r<}
t                              ddt          |
          i           |j        }Y d}
~
nd}
~
ww xY w|	rt          |	d	g           ng }|rt          t!          ||z                       }d}|s|ri }|rt"          rz	 |d
 t#          j        |                                          D             z  }ns# t(          t*          f$ r1 t                              d           t          j        d           Y n2w xY wt                              d           t          j        d           |D ]}t/          |          \  }}|||<   t	          j        | ||||          r t                              d| d           dS t                              d| d           t          j        d           dS )zInstall an 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)rE   r   rB   r-   r.   rH   zClaude app not foundr   zFCould not import server (likely missing dependencies), using file namer$   rn   c                     i | ]
\  }}|||S Nr   ).0kvs      r   
<dictcomp>zinstall.<locals>.<dictcomp>  s$     j j j$!Q\]\iA\i\i\ir+   zFailed to load .env filez6python-dotenv is not installed. Cannot load .env file.)r-   r.   r   zSuccessfully installed z in Claude appzFailed to install )rD   r#   rq   r4   r	   get_claude_config_pathr$   r   r%   re   r   r`   ModuleNotFoundErrorstemr^   rr   rs   dotenvdotenv_valuesrw   OSError
ValueErrorr   r*   update_claude_configinfo)r,   r   r-   r.   r   r   rE   rB   r   rQ   r|   server_dependenciesenv_dictr   r(   r)   s                   r   installr   j  s   t +955D-
LLII&*3@JS///d*
 
  	 	 	 (** +,,, DF 		#D-88F;DD01 	 	 	LLXA'     9DDDDDD	 BHO'&."===R GS1D!DEEFF '+H "8 " 		   j j&2Fx2P2P2V2V2X2X j j jjHH,      $$%?@@@HQKKKKK  UVVV   	" 	"G'00JC!HSMM"##   
 	BdBBBCCCCC>$>>>???s*   B3 3D 2C;;D 3E? ??G G)NNr   )r    N)+__doc__importlib.metadatarW   importlib.utilru   r   r   pathlibr   typingr   r   
mcp.serverr   r   rN   typerr`   ri   r%   mcp.clir	   $mcp.server.fastmcp.utilities.loggingr
   r   r#   TyperrR   r   r4   tupler*   rr   r6   rD   re   commandrh   ArgumentOptionr}   r   r   r   r+   r   <module>r      s             				     



       ! ! ! ! ! ! ! !       / / / / / /LLLL   	E
IJJJCHQKKKKK???????   	E
KLLLCHQKKKKKMMMM   FFF 
E		ek		 	    &C &E#s(O & & & & "&&* $; 9t# 
#Y	   .$ $dC$J.>(? $ $ $ $>Y Y YcDj Y Y Y Yx     #U^A   	 	-O OO
 tR	
 	
 	
	
O  S	1	
 	
 	
	!O. 
/O O O Od #U^A   	5 55
 d
;	
 	
 	
	5 
5 5 5 5p #U^A   	 	 	 	 	e} }}
 d
`	
 	
 	
	} tR	
 	
 	
	
}0 S	1	
 	
 	
	1}> S	<	
 	
 	
	?}N t>	
 	
 	
		O}f 
g} } } } } }s3   7  AAA+ + BBB B! B!