
    {h~:                     H   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	 dd	lmZ dd
lmZ 	 ddlZ ed      Z ej4                  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jI                         d>d       Z%ejI                          ejL                  dd !      dg fdede
edz   ejN                  d"d#d$ddd%      f   de
e e    ejN                  d&d'!      f   ddfd(       Z(ejI                          ejL                  dd !      dfded)e
edz   ejN                  d*d+d,!      f   ddfd-       Z)ejI                          ejL                  dd !      ddg g dfded.e
edz   ejN                  d/d0d1!      f   de
edz   ejN                  d"d#d$ddd%      f   de
e e    ejN                  d&d'!      f   d2e
e e    ejN                  d3d4d5!      f   d6e
edz   ejN                  d7d8d9dddd:      f   ddfd;       Z*y# e$ r  ed        ej&                  d       Y 6w xY w# e$ r  ed        ej&                  d       Y Ow xY w# e$ r dZY Xw xY w)?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 ]!  } 	 t        j                  | dgddd       | c S  yy# t        j                  $ r Y ;w xY w)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    L/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/mcp/cli/cli.py_get_npx_commandr   *   sa    
||w0 	C[1dZ^_
	  00 s   <AAenv_varreturnc                     d| vr.t         j                  d|  d       t        j                  d       | j	                  dd      \  }}|j                         |j                         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   sW    
'<WIEXYZsA&JC99;%%    	file_specwith_editablewith_packagesc                     dg}|j                  g d       |r|j                  dt        |      g       |r|D ]  }|s|j                  d|g        |j                  dd| g       |S )z@Build the uv run command that runs a MCP server through mcp run.uv)r   --withr   --with-editabler1   r   r   )extendstr)r,   r-   r.   r   pkgs        r   _build_uv_commandr6   A   sq     &CJJ'(

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

Hc?+	,
 JJui()Jr+   c                    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:
        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  1y}yA"+"2"23":-"+T- X))+335I'	{34|I;/0m##r+   filerB   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       t
        j                  j                  |      }|j                  j                  |       dt        dt         fd	}|sd
D ]Q  }t        ||      s |t!        ||      |  d|       st        j                  d|  d| d       Et!        ||      c S  t        j                  d|  ddt        |       i       t        j                  d       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        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 modulerE   extrar   rB   object_namec           	          t        | t              sRt        j                  d| dt	        |        dt         d       t        | t
              rt        j                  d       yy)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LL-k],tMGZF[[ghogpprst-8n r+   )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   sE    4;;Hsxx8$ >>11/4HDt{{,VSY4GH^^,,T2FKKF#C c & , 	-Dvt$+GFD,AdV1TFCSTLL#4TF!D6Ad!efvt,,	- 	( /< < 3t9% 	 	
 	 m#0#6#6sA#> [	%33K@M]K>F 5~m_K83t9% 	 	
 	6M)  	LL+K=:s4y)   HHQK	s   $"H1 1AI87I8c                      	 t         j                  j                  d      } t        d|         y# t         j                  j                  $ r# t        d       t        j                  d       Y y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WI&'22 ;<s   -0 =A0/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        j                  dt        |      ||rt        |      nd|d       	 t	        ||      }t        |d      r!t        t        ||j                  z               }t        | ||      }t               }|s*t        j                  d       t        j                  d       t        j                  dk(  }t        j                   |d	g|z   d
|t#        t$        j&                  j)                                     }	t        j                  |	j*                         y# t        j,                  $ r`}
t        j                  dt        |      t        |
      |
j*                  d       t        j                  |
j*                         Y d}
~
yd}
~
wt.        $ r: t        j                  ddt        |      i       t        j                  d       Y yw xY w)z(Run a 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D-
LLI*3@S/d*	
  *m46>* ]V5H5H%H!IJM"9m]K #$LLt HHQK '..786ARZZ%%'(	
 	##$(( 	D	Qll 	 	
 	 " 3t9%	 	 	
 	s!   C1D4 4G(AF""AG('G(	transportz--transportz-tz(Transport protocol to use (stdio or sse)c                 D   t        |       \  }}t        j                  dt        |      ||d       	 t	        ||      }i }|r||d<    |j
                  d	i | y# t        $ r: t        j                  ddt        |      i       t        j                  d       Y yw xY 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)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D-
LLI*"
  m4 "+F;

V "D	 	 	
 	s   'A A BB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        j                  dt        |      |||rt        |      nd|d       t	        j
                         s*t        j                  d       t        j                  d       |}d}	|s	 t        ||      }	|	j                  }|	rt        |	d	g       ng }|rt        t!        ||z               }d}|s|ri }|rot"        r?	 |t#        j$                  |      j'                         D ci c]  \  }}|	|| c}}z  }n*t        j                  d       t        j                  d       |D ]  }t/        |      \  }}|||<    t	        j0                  | ||||      rt        j3                  d| d       yt        j                  d| d       t        j                  d       y# t        t        f$ r9}
t        j                  ddt        |
      i       |j                  }Y d}
~
cd}
~
ww xY wc c}}w # t(        t*        f$ r. t        j-                  d
       t        j                  d       Y 
w xY w)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)rE   r   rB   r-   r.   rH   zClaude app not foundr   zFCould not import server (likely missing dependencies), using file namer$   rn   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_dictkvr   r(   r)   s                     r   installr   j  s+   t +95D-
LLI&*3@S/d*
  	 ((*+, DF	#D-8F;;D BH'&."=RS1D!DEF '+H8 &2F2Fx2P2V2V2X j$!Q\]\iA jjH
 UV   	"G'0JC!HSM	" ""## 	-dV>BC)$~>?W 01 	LLXA'   99D	( !k,  $$%?@HHQK sB   F? 'H 
H
H
H ?H.HH
H 9II)NN)N)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'      	  
  !  /
?
 
E	ekk		 	&C &E#s(O & "&&*$; 9t# 
#Y	.$ $dC$J.>(? $>Y YcDj Yx   #U^^A 	 	-OO
 tR	
	
O  S	1	
	!O. 
/O Od #U^^A 	55
 d
;	
	5 
5 5p #U^^A 	 	 	 	 	e}}
 d
`	
	} tR	
	
}0 S	1	
	1}> S	<	
	?}N t>	
		O}f 
g} }w
  	
IJCHHQK  	
KLCHHQK  Fs4   I
 I0 J 
I-,I-0JJJ! J!