
    "<i)                     f   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j"        d#d$gd%&          f         deee         dz   ej"        d'd(d)*          f         deee         dz   ej"        d+d,d)*          f         deee         dz   ej"        d-d.d)*          f         d/eedz   ej"        d0d12          f         d3eedz   ej"        d4d52          f         deedz   ej"        d6d72          f         deedz   ej"        d8d92          f         deedz   ej"        d:d;2          f         ddfd<Z#dS )=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                     |                     d          }t          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         /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/fastmcp/cli/install/cursor.pygenerate_cursor_deeplinkr      sq      //T/BBK)+*<*<*>*>??FFHHJ 2...Lhlhh\fhhHO    r   c                    t          |           }|j        dk    r$t                              d|j                    dS 	 t          j        dk    rt          j        d| gdd           n>t          j        dk    rt          j	        |            nt          j        d	| gdd           dS # t          j
        t          t          f$ r Y dS 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}  B6=BBCCCu	<8##NFH-T$OOOOO\W$$L""""NJ1dSSSSt)+<gF   uus    A(B* *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                   |                                 }|                                st          d| d           dS |dz  }
|
                    d           |
dz  }t	          ||pg dgz   ||	|	          }|r|                                   d
| }n!t          |                                            }|                    dd|g          }t          |d         |dd         |pi           }	 |                                s|                    d           t          |||           t          d| d| d           dS # t          $ r}t          d| d           Y d}~dS 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  "" P.PPPQQQu  )+Jd###z)K#)ri[8&  J  *99-99$,,..)) ++Y{,KLLL #Q!""N  M!!## 	9""#7888 	;m<<<^d^^n^^^	
 	
 	
 t   FQFFFGGGuuuuus   0AE 
E*E%%E*)r6   r7   r8   r9   r:   r;   	workspacer[   c                
   t          ||pg dgz   |||          }
|r|                                  d| }n!t          |                                           }|
                    dd|g          }|	rt	          | |||	||||||
  
        S t          |d         |dd         |pi 	          }t          ||          }t          d
| d           t          |          rt          d           dS t          d| d           dS )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9-99$,,..)) ++Y{,KLLL  
''$'')/
 
 
 	
 #Q!""N  M (m<<H	
<d
<
<
<===X RSSStTBJT T T	
 	
 	
 ur    )	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   |pg }|pg }|pg }t          | ||||           d{V \  }
}}}}t          |
|||||||||	
  
        }|st          j        d           dS dS )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   r    )$__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 A A A A A A A 0 0 0 0 0 0 P P P P P P ' ' ' ' ' '	H		! 	   2C D    @ (,&*&*!%%)N N N
N:N N 	N :$N 9t#N 38nt#N $JN d{N D[N 
N N N Nl (,&*&*!%%)!R R R
R:R R
 :$R 9t#R 38nt#R $JR d{R D[R d{R 
R R R R| 	 	 	 	 	 	 	 	 	Ia a 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 a a a a ar    