
    Ki)                        d 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	m
Z
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edefdZdedefdZddddddddededz  de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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dz  defd Z!dddddddddd!	d"edeedz   ejD                  d#d$gd%&      f   deee   dz   ejD                  d'd(d)*      f   deee   dz   ejD                  d+d,d)*      f   deee   dz   ejD                  d-d.d)*      f   d/eedz   ejD                  d0d12      f   d3eedz   ejD                  d4d52      f   deedz   ejD                  d6d72      f   deedz   ejD                  d8d92      f   deedz   ejD                  d:d;2      f   ddfd<Z#y)=z6Cursor integration for FastMCP install using Cyclopts.    N)Path)	Annotated)quoteurlparse)print)StdioMCPServerupdate_config_file)
get_logger)UVEnvironment   )process_common_argsserver_nameserver_configreturnc                     |j                  d      }t        j                  |j                               j	                         }t        | d      }d| d| }|S )zGenerate a Cursor deeplink for installing the MCP server.

    Args:
        server_name: Name of the server
        server_config: Server configuration

    Returns:
        Deeplink URL that can be clicked to install the server
    T)exclude_none )safez4cursor://anysphere.cursor-deeplink/mcp/install?name=z&config=)model_dump_jsonbase64urlsafe_b64encodeencodedecoder   )r   r   config_json
config_b64encoded_namedeeplinks         f/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/cli/install/cursor.pygenerate_cursor_deeplinkr      sa      //T/BK))+*<*<*>?FFHJ 2.LEl^S[\f[ghHO    r   c                    t        |       }|j                  dk7  r#t        j                  d|j                          y	 t        j
                  dk(  rt        j                  d| gdd       yt        j
                  dk(  rt        j                  |        yt        j                  d	| gdd       y# t        j                  t        t        f$ r Y yw xY w)
zAttempt to open a deeplink URL using the system's default handler.

    Args:
        deeplink: The deeplink URL to open

    Returns:
        True if the command succeeded, False otherwise
    cursorzInvalid deeplink scheme: FdarwinopenT)checkcapture_outputwin32zxdg-open)r   schemeloggerwarningsysplatform
subprocessrunos	startfileCalledProcessErrorFileNotFoundErrorOSError)r   parseds     r   open_deeplinkr5   0   s     hF}} 26==/BC	<<8#NNFH-T$O
 	 \\W$LL"  NNJ1dS))+<gF s   -B1 -(B1 B1 1CC)with_editablewith_packagesenv_varspython_versionwith_requirementsprojectfileserver_objectnameworkspace_pathr6   r7   r8   r9   r:   r;   c                R   |j                         }|j                         st        d| d       y|dz  }
|
j                  d       |
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                         s|j                  d       t        |||       t        d| d| d       y# t        $ r}t        d| d       Y d}~yd}~ww xY w)a  Install FastMCP server to workspace-specific Cursor configuration.

    Args:
        file: Path to the server file
        server_object: Optional server object name (for :object suffix)
        name: Name for the server in Cursor
        workspace_path: Path to the workspace directory
        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]Workspace directory does not exist: z[/red]Fz.cursorT)exist_okzmcp.jsonfastmcppythondependenciesrequirementsr;   editable:r.   r   r   Ncommandargsenvz{"mcpServers": {}}z[green]Successfully installed 'z' to workspace at z[/green]z,[red]Failed to install server to workspace: )resolveexistsr   mkdirr   strbuild_commandr   
write_textr	   	Exception)r<   r=   r>   r?   r6   r7   r8   r9   r:   r;   
cursor_dirconfig_file
env_configserver_specfull_commandr   es                    r   install_cursor_workspacerZ   J   s^   > $++-N  "9.9IPQ  )+Jd#z)K#)ri[8&J (-9$,,.) ++Y{,KLL #Q!"NM!!#""#78 	;m<-dV3EnEUU]^	
  <QCvFGs   A D 	D&D!!D&)r6   r7   r8   r9   r:   r;   	workspacer[   c                   t        ||xs g dgz   |||      }
|r| j                          d| }nt        | j                               }|
j                  dd|g      }|	rt	        | |||	||||||
      S t        |d   |dd |xs i 	      }t        ||      }t        d
| d       t        |      rt        d       yt        d| d       y)a  Install FastMCP server in Cursor.

    Args:
        file: Path to the server file
        server_object: Optional server object name (for :object suffix)
        name: Name for the server in Cursor
        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
        workspace: Optional workspace directory for project-specific installation

    Returns:
        True if installation was successful, False otherwise
    rB   rC   rH   r.   )
r<   r=   r>   r?   r6   r7   r8   r9   r:   r;   r   r   NrI   z![blue]Opening Cursor to install 'z'[/blue]zB[green]Cursor should now open with the installation dialog[/green]Tzc[red]Could not open Cursor automatically.[/red]
[blue]Please copy this link and open it in Cursor: z[/blue]F)	r   rM   rP   rQ   rZ   r   r   r   r5   )r<   r=   r>   r6   r7   r8   r9   r:   r;   r[   rV   rW   rX   r   r   s                  r   install_cursorr]      s   > #)ri[8&J (-9$,,.) ++Y{,KLL ''$'')/
 	
 #Q!"NM (m<H	-dV8
<=XRSBBJ7T	
 r    )	r   r6   r7   r8   env_filerD   r:   r;   r[   rW   z--namez-nz$Custom name for the server in Cursor)r>   helpz--with-editablezVDirectory with pyproject.toml to install in editable mode (can be used multiple times)r   )r_   negativez--withz;Additional packages to install (can be used multiple times)z--envzFEnvironment variables in KEY=VALUE format (can be used multiple times)r^   z
--env-filez)Load environment variables from .env file)r_   rD   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 directoryz--workspacezYInstall to workspace directory (will create .cursor/ inside it) instead of using deeplinkc       	            K   |xs g }|xs g }|xs g }t        | ||||       d{   \  }
}}}}t        |
|||||||||	
      }|st        j                  d       yy7 8w)z|Install an MCP server in Cursor.

    Args:
        server_spec: Python file to install, optionally with :object suffix
    N)
r<   r=   r>   r6   r7   r8   r9   r:   r;   r[   r   )r   r]   r+   exit)rW   r   r6   r7   r8   r^   rD   r:   r;   r[   r<   r=   r>   env_dictsuccesss                  r   cursor_commandre      s     X "'RM!'RM~2H?R[-8@ :6D-}h ###+G  #:s   %A"A 9A")$__doc__r   r/   r-   r+   pathlibr   typingr   urllib.parser   r   cycloptsrichr   fastmcp.mcp_configr   r	   fastmcp.utilities.loggingr
   6fastmcp.utilities.mcp_server_config.v1.environments.uvr   sharedr   __name__r)   rP   r   boolr5   listdictrZ   r]   	Parameterre    r    r   <module>rv      s$   <  	  
   (   A 0 P '	H	! 	2C D @ (,&*&*!%%)N
N:N N 	N :$N 9t#N 38nt#N $JN d{N D[N 
Nl (,&*&*!%%)!R
R:R R
 :$R 9t#R 38nt#R $JR d{R D[R d{R 
R| 	 	 	 	 	 	 	 	 	Iaa d
D!7	
	a T
Ti	
	a$ S	DN	
	%a4 S	DY	
	5aD t<	
	EaR d
;	
	Sa` !t!A	
	aan tE	
	oa| tl	
	}aJ 
Kar    