o
    i)                     @   s  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dZdedefddZddddddddededB dededee dB dee dB deeef dB dedB dedB dedB defdd Z dddddddd!dededB dedee dB dee dB deeef dB dedB dedB dedB d"edB defd#d$Z!dddddddddd%	d&edeedB ej"d'd(gd)d*f deee dB ej"d+d,d-d.f deee dB ej"d/d0d-d.f deee dB ej"d1d2d-d.f d3eedB ej"d4d5d6f d7eedB ej"d8d9d6f deedB ej"d:d;d6f deedB ej"d<d=d6f d"eedB ej"d>d?d6f ddfd@dAZ#dS )Bz6Cursor 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                 C   s>   |j dd}t|  }t| d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deeplink r   `/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/cli/install/cursor.pygenerate_cursor_deeplink   s
   r   r   c              
   C   s   t | }|jdkrtd|j  dS z,tjdkr'tjd| gddd W dS tjdkr4t	|  W dS tjd	| gddd W dS  tj
ttfyN   Y dS 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   parsedr   r   r   open_deeplink0   s    	



r2   )with_editablewith_packagesenv_varspython_versionwith_requirementsprojectfileserver_objectnameworkspace_pathr3   r4   r5   r6   r7   r8   c             
   C   s   |  }| std| d dS |d }
|
jdd |
d }t||p%g dg ||	|d	}|r:|    d
| }nt|   }|dd|g}t|d |dd |pTi d}z| sa|d t	||| td| d| d W dS  t
y } ztd| d W Y d}~dS d}~w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requirementsr8   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)r9   r:   r;   r<   r3   r4   r5   r6   r7   r8   
cursor_dirconfig_file
env_configserver_specfull_commandr   er   r   r   install_cursor_workspaceJ   sH   

rV   )r3   r4   r5   r6   r7   r8   	workspacerW   c                C   s   t ||pg dg |||d}
|r|   d| }nt|  }|
dd|g}|	r8t| |||	||||||d
S t|d |dd |pDi d	}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
    r>   r?   rD   r+   )
r9   r:   r;   r<   r3   r4   r5   r6   r7   r8   r   r   NrE   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
   rI   rL   rM   rV   r   r   r   r2   )r9   r:   r;   r3   r4   r5   r6   r7   r8   rW   rR   rS   rT   r   r   r   r   r   install_cursor   sP   

rX   )	r   r3   r4   r5   env_filer@   r7   r8   rW   rS   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   )rZ   negativez--withz;Additional packages to install (can be used multiple times)z--envzFEnvironment variables in KEY=VALUE format (can be used multiple times)rY   z
--env-filez)Load environment variables from .env file)rZ   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 directoryz--workspacezYInstall to workspace directory (will create .cursor/ inside it) instead of using deeplinkc       	            sl   |pg }|pg }|pg }t | ||||I dH \}
}}}}t|
|||||||||	d
}|s4td dS dS )z|Install an MCP server in Cursor.

    Args:
        server_spec: Python file to install, optionally with :object suffix
    N)
r9   r:   r;   r3   r4   r5   r6   r7   r8   rW   r   )r   rX   r(   exit)rS   r   r3   r4   r5   rY   r@   r7   r8   rW   r9   r:   r;   env_dictsuccessr   r   r   cursor_command   s,   L
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&   rL   r   boolr2   listdictrV   rX   	Parameterr_   r   r   r   r   <module>   sh   
 

	

V

	

^



")07>E