
    Ki                        d 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m	Z	 ddl
mZmZ ddlmZ ddlmZ d	d
lmZ  ee      Zdedz  fdZddddddddededz  dedee   dz  dee   dz  deeef   dz  dedz  dedz  dedz  defdZddddddddddedeedz   ej6                  ddgd      f   deee   dz   ej6                  dd d!"      f   deee   dz   ej6                  d#d$d!"      f   deee   dz   ej6                  d%d&d!"      f   d'eedz   ej6                  d(d)*      f   d+eedz   ej6                  d,d-*      f   deedz   ej6                  d.d/*      f   deedz   ej6                  d0d1*      f   ddfd2Zy)3z>Claude Desktop integration for FastMCP install using Cyclopts.    N)Path)	Annotated)print)StdioMCPServerupdate_config_file)
get_logger)UVEnvironment   )process_common_argsreturnc                     t         j                  dk(  r!t        t        j                         ddd      } nt         j                  dk(  r!t        t        j                         ddd      } n`t         j                  j	                  d      r@t        t
        j                  j                  d	t        j                         d
z        d      } ny| j                         r| S y)z2Get the Claude config directory based on platform.win32AppDataRoamingClaudedarwinLibraryzApplication SupportlinuxXDG_CONFIG_HOMEz.configN)	sysplatformr   home
startswithosenvirongetexists)paths    n/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/cli/install/claude_desktop.pyget_claude_config_pathr       s    
||wDIIKIx@		!DIIK,A8L		 	 	)JJNN,diikI.EF
 {{}    )with_editablewith_packagesenv_varspython_versionwith_requirementsprojectfileserver_objectnamer"   r#   r$   r%   r&   r'   c                   t               }	|	st        d       y|	dz  }
t        ||xs g dgz   |||      }|r| j                          d| }nt	        | j                               }|j                  dd|g      }t        |d   |d	d
 |xs i       }	 |
j                         rjdd
l}|
j                         j                         }|rF|j                  |      }d|v r1||d   v r*|d   |   j                  di       }|ri ||}n|}||_        t        |
||       t        d| d       y# t        $ r}t        d| d       Y d
}~yd
}~ww xY w)a  Install FastMCP server in Claude Desktop.

    Args:
        file: Path to the server file
        server_object: Optional server object name (for :object suffix)
        name: Name for the server in Claude's config
        with_editable: Optional list of directories to install in editable mode
        with_packages: Optional list of additional packages to install
        env_vars: Optional dictionary of environment variables
        python_version: Optional Python version to use
        with_requirements: Optional requirements file to install from
        project: Optional project directory to run within

    Returns:
        True if installation was successful, False otherwise
    z[red]Claude Desktop config directory not found.[/red]
[blue]Please ensure Claude Desktop is installed and has been run at least once to initialize its config.[/blue]Fzclaude_desktop_config.jsonfastmcp)pythondependenciesrequirementsr'   editable:runr   r
   N)commandargsenv
mcpServersr5   z[green]Successfully installed 'z' in Claude Desktop[/green]Tz[red]Failed to install server: z[/red])r    r   r	   resolvestrbuild_commandr   r   json	read_textstriploadsr   r5   r   	Exception)r(   r)   r*   r"   r#   r$   r%   r&   r'   
config_dirconfig_file
env_configserver_specfull_commandserver_configr:   contentconfigexisting_env
merged_enves                        r   install_claude_desktoprJ   &   s   8 ()J~	
 ;;K#)ri[8&J (-9$,,.) ++Y{,KLL #Q!"NM!++-335GG,6)df\6J.J#),#7#=#A#A%#LL%A%A%A
%1
(2M% 	;m</v5PQR /s&9:s   BD+ +	E4EE)server_namer"   r#   r$   env_filer-   r&   r'   rB   rK   z--namez-nz5Custom name for the server in Claude Desktop's config)r*   helpz--with-editablezVDirectory with pyproject.toml to install in editable mode (can be used multiple times) )rM   negativez--withz;Additional packages to install (can be used multiple times)z--envzFEnvironment variables in KEY=VALUE format (can be used multiple times)rL   z
--env-filez)Load environment variables from .env file)rM   r-   z--pythonz(Python version to use (e.g., 3.10, 3.11)z--with-requirementsz.Requirements file to install dependencies fromz	--projectz2Run the command within the given project directoryc                   K   |xs g }|xs g }|xs g }t        | ||||       d{   \  }	}
}}}t        |	|
|||||||	      }|st        j                  d       yy7 7w)zInstall an MCP server in Claude Desktop.

    Args:
        server_spec: Python file to install, optionally with :object suffix
    N)	r(   r)   r*   r"   r#   r$   r%   r&   r'   r
   )r   rJ   r   exit)rB   rK   r"   r#   r$   rL   r-   r&   r'   r(   r)   r*   env_dictsuccesss                 r   claude_desktop_commandrT   }   s     J "'RM!'RM~2H?R[-8@ :6D-}h %###+
G  !:s   %A!A8A!)__doc__r   r   pathlibr   typingr   cycloptsrichr   fastmcp.mcp_configr   r   fastmcp.utilities.loggingr   6fastmcp.utilities.mcp_server_config.v1.environments.uvr	   sharedr   __name__loggerr    r8   listdictboolrJ   	ParameterrT    r!   r   <module>re      s   D 	 
     A 0 P '	H	t . (,&*&*!%%)T
T:T T
 :$T 9t#T 38nt#T $JT d{T D[T 
T@ 	 	 	 	 	 	 	 	{YY d
D!H	
	Y T
Ti	
	Y$ S	DN	
	%Y4 S	DY	
	5YD t<	
	EYR d
;	
	SY` !t!A	
	aYn tE	
	oY| 
}Yr!   