
    ~h1                        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Zddlm	Z	 ddl
mZ ddlZddlZddlmZ ddlmZ ddlmZmZ ddl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       Z ej>                  dddd      Z d Z!de"de#e"e"f   fdZ$	 	 dPde"de	dz  de%e"   dz  de%e"   fdZ&e jO                         defd       Z(e jO                          ejR                  dd      dg dddfde"dee	dz   ejT                  dd d!ddd"      f   dee%e"    ejT                  d#d$      f   d%ee"dz   ejT                  d&d'      f   d(ee+dz   ejT                  d)d*      f   d+ee+dz   ejT                  d,d-      f   ddfd.       Z,e jO                  d/di0       ejR                  dd1      ddddfdej$                  de"d2ee"dz   ejT                  d3d4d5      f   d6ee"dz   ejT                  d7d8      f   d9ee+dz   ejT                  d:d;d<      f   d=ee"dz   ejT                  d>d?d@      f   ddfdA       Ze jO                          ejR                  dd      ddg g dfde"dBee"dz   ejT                  dCdDdE      f   dee	dz   ejT                  dd d!ddd"      f   dee%e"    ejT                  d#d$      f   dFee%e"    ejT                  dGdHdI      f   dJee	dz   ejT                  dKdLdMddddN      f   ddfdO       Z-y)QzFastMCP CLI tools.    N)Path)	Annotated)Console)Table)ContextExit)claude)run)FastMCP)
get_loggerclifastmcpzFastMCP CLIFT)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
subprocessr
   CalledProcessError)cmds    K/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/fastmcp/cli/cli.py_get_npx_commandr   #   se    
||w0 	C+&d4t 
	  00 s   <AAenv_varreturnc                     d| vr*t         j                  d       t        j                  d       | j	                  dd      \  }}|j                         |j                         fS )z6Parse environment variable string in format KEY=VALUE.=z6Invalid environment variable format. Must be KEY=VALUE   )loggererrorr   exitsplitstrip)r    keyvalues      r   _parse_env_varr,   3   sM    
'MNsA&JC99;%%    server_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-editabler3   r   r
   )extendstr)r.   r/   r0   r   pkgs        r   _build_uv_commandr8   <   sq     &CJJ+,

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

Hc?+	,
 JJ	5+./Jr-   ctxc                 p   | j                   ry t        j                  t        j                  j                  d      t        j                         t        j                         t        t        j                        j                         j                  d   d}t        j                  d      }|j                  dd       |j                  d	d
       |j                         D ]3  \  }}|j!                  |dz   t#        |      j%                  dd             5 t&        j)                  |       t+               )Nmcpr$   )zFastMCP versionzMCP versionzPython versionPlatformzFastMCP root path)r   r$   )paddingboldleft)stylejustifycyanright:
 )resilient_parsingr   __version__	importlibmetadataversionr   python_versionr   __file__resolveparentsr   grid
add_columnitemsadd_rowr6   replaceconsoleprintr   )r9   infogkvs        r   rK   rK   S   s    
 #.. ))11%8"113%%'!'"2"23;;=EEaHD 	

6"ALLvvL.LLvwL/

 61			!c'3q6>>$456MM!
&Lr-   .z2Python file to run, optionally with :object suffix)r   r4   z-ez?Directory containing pyproject.toml to install in editable mode)r   exists	file_okayresolve_pathr3   zAdditional packages to installinspector_versionz--inspector-versionz#Version of the MCP Inspector to useui_portz	--ui-portzPort for the MCP Inspector UIserver_portz--server-portz'Port for the MCP Inspector Proxy serverc           
      d   t        j                  |       \  }}t        j                  dt	        |      ||rt	        |      nd|||d       	 t        j
                  ||      }|j                  !t        t        ||j                  z               }i }	|rt	        |      |	d<   |rt	        |      |	d<   t               }
|
s*t        j                  d       t        j                  d       d	}|r|d
| z  }t        | ||      }t        j                  dk(  }t        j                   |
|g|z   d|t#        t$        j&                  j)                               |	z        }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)fileserver_objectr/   r0   r_   r`   extraCLIENT_PORTSERVER_PORTzbnpx not found. Please ensure Node.js and npm are properly installed and added to your system PATH.r$   z@modelcontextprotocol/inspector@r   T)r   r   envzDev server failed)rb   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.rb   )
run_moduleparse_file_pathr%   debugr6   import_serverdependencieslistsetr   r&   r   r'   r8   r   r   r
   dictosenvironrR   rj   r   FileNotFoundError)r.   r/   r0   r^   r_   r`   rb   rc   serverenv_varsnpx_cmdinspector_cmduv_cmdr   processes                   r   devr}   j   s   ^ %44[AD-
LLI*3@S/d*&
  
5$224G* ]V5H5H%H!IJM&)'lH]#&)+&6H]# #$LL1 HHQK9q!2 344M";}M '..m$v-RZZ%%'(83	
 	##$(( 	D	Qll 	 	
 	 " 3t9%	 	 	
 	s!   D,E; ;H/AG))AH/.H/allow_extra_args)context_settingsz4Python file, object specification (file:obj), or URL	transportz--transportz-tz:Transport protocol to use (stdio, streamable-http, or sse)hostz--hostz>Host to bind to when using http transport (default: 127.0.0.1)portz--portz-pz9Port to bind to when using http transport (default: 8000)	log_levelz--log-levelz-lz1Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)c           
      2   | j                   }t        j                  d||||||d       	 t        j                  ||||||       y# t
        $ rE}t        j                  d| |t        |      d       t        j                  d       Y d}~yd}~ww xY w)a_  Run a MCP server or connect to a remote one.

    The server can be specified in three ways:
    1. Module approach: server.py - runs the module directly, looking for an object named mcp/server/app.

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

    3. URL approach: http://server-url - connects to a remote server and creates a proxy.



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

    Server arguments can be passed after -- :
    fastmcp run server.py -- --config config.json --debug
    zRunning server or client)r.   r   r   r   r   server_argsrd   zFailed to run: )r.   r&   r$   N)
argsr%   rm   rk   run_command	Exceptionr&   r6   r   r'   )r9   r.   r   r   r   r   r   r|   s           r   r
   r
      s    h ((K
LL"&""&
  
##	
  aS!*Q 	 	
 	s   A 	B;BBserver_namez--namez-nzMCustom name for the server (defaults to server's name attribute or file name)rw   z	--env-varz-vz)Environment variables in KEY=VALUE formatenv_filez
--env-filez-fz+Load environment variables from a .env file)r   r[   r\   dir_okayr]   c           	      l   t        j                  |       \  }}t        j                  dt	        |      |||rt	        |      nd|d       t        j                         s*t        j                  d       t        j                  d       |}d}	|s#	 t        j                  ||      }	|	j                  }|	rt        |	d	g       ng }|rt!        t#        ||z               }d}|s|r[i }|r=	 |t%        j&                  |      j)                         D ci c]
  \  }}||| c}}z  }|D ]  }t-        |      \  }}|||<    t        j.                  | ||||      rt        j1                  d| d       yt        j                  d| d       t        j                  d       y# t        t        f$ r9}
t        j                  ddt	        |
      i       |j                  }Y d}
~
1d}
~
ww xY wc c}}w # t*        $ r8}
t        j                  d
|
        t        j                  d       Y d}
~

d}
~
w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)rb   r   rc   r/   r0   rd   zClaude app not foundr$   zFCould not import server (likely missing dependencies), using file namer&   ro   zFailed to load .env file: )r/   r0   rw   zSuccessfully installed z in Claude appzFailed to install )rk   rl   r%   rm   r6   r	   get_claude_config_pathr&   r   r'   rn   r   ImportErrorModuleNotFoundErrorstemgetattrrp   rq   dotenvdotenv_valuesrR   r   r,   update_claude_configrW   )r.   r   r/   r0   rw   r   rb   rc   r   rv   r|   server_dependenciesenv_dictrY   rZ   r    r*   r+   s                     r   installr   5  s*   v %44[AD-
LLI&*3@S/d*
  	 ((*+, DF		--dMBF;;D BH'&."=RS1D!DEF '+H8 & 4 4X > D D F1} qD    	"G'0JC!HSM	" ""## 	-dV>BC)$~>?Y 01 	LLA'  
 99D	(
  9!=>sB   "F! ('G2 G,G2 !G)0.G$$G),G2 2	H3;-H..H3)NN).__doc__importlib.metadatarI   importlib.utilrs   r   r   r   pathlibr   typingr   r   typerrich.consoler   
rich.tabler   r   r   r   fastmcp.clir	   r
   rk   fastmcp.server.serverr   fastmcp.utilities.loggingr   r%   rU   Typerappr   r6   tupler,   rp   r8   commandrK   ArgumentOptionintr}   r    r-   r   <module>r      s      	   
           ) ) 0	E	
)ekk			 &C &E#s(O & "&&*$; 9t# 
#Y	.   , %u~~A 	 	 	 	 	Wqq
 tR	
	
q  S	1	
	!q. !d
!6	
	/q< d
0	
	=qJ d
:	
	KqX 
Yq qh 1489 &u~~C 	 	 	 	IR	RR d
M	
	R d
Q	
	R* d
L	
	+R: d
D	
	;RJ 
KR :Rj %u~~A 	 	 	 	 	g
 d
	
	 tR	
	
2 S	1	
	3@ S	<	
	AP t>	
		Qh 
i r-   