o
    i"                     @   sz   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ eeZG d	d
 d
ZdS )z
ComponentService: Provides async management of tools, resources, and prompts for FastMCP servers.
Handles enabling/disabling components both locally and across mounted servers.
    )NotFoundError)Prompt)Resource)ResourceTemplate)FastMCPhas_resource_prefixremove_resource_prefix)Tool)
get_loggerc                   @   s   e Zd ZdZdefddZdedefddZdedefd	d
Z	dede
eB fddZdede
eB fddZdedefddZdedefddZdS )ComponentServicezCService for managing components like tools, resources, and prompts.serverc                 C   s"   || _ |j| _|j| _|j| _d S )N)_server_tool_manager_resource_manager_prompt_manager)selfr    r   y/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/contrib/component_manager/component_service.py__init__   s   zComponentService.__init__keyreturnc                       t d| || jjjv r| j|I dH }|  |S t| jjD ])}|j	rL|
|j	 drK||j	 d}t|j}||I dH }|  S q#q#td| )zHandle 'enableTool' requests.

        Args:
            key: The key of the tool to enable

        Returns:
            The tool that was enabled
        zEnabling tool: %sN_Unknown tool: )loggerdebugr   r   _toolsget_toolenablereversed_mounted_serversprefix
startswithremoveprefixr   r   _enable_toolr   r   r   toolmountedtool_keymounted_servicer   r   r   r$          	
zComponentService._enable_toolc                    r   )zHandle 'disableTool' requests.

        Args:
            key: The key of the tool to disable

        Returns:
            The tool that was disabled
        zDisable tool: %sNr   r   )r   r   r   r   r   r   disabler   r    r!   r"   r#   r   r   _disable_toolr   r%   r   r   r   r,   7   r*   zComponentService._disable_toolc                       t d| || jjv r| j|I dH }|  |S || jjv r1| j|I dH }|  |S t	| jj
D ]'}|jr^t||j|jr]t||j|j}t|j}||I dH }|  S q7q7td| )zHandle 'enableResource' requests.

        Args:
            key: The key of the resource to enable

        Returns:
            The resource that was enabled
        zEnabling resource: %sNUnknown resource: )r   r   r   
_resourcesr   get_resourcer   
_templatesget_resource_templater   r    r!   r   resource_prefix_formatr   r   r   _enable_resourcer   r   r   resourcetemplater'   r)   mounted_resourcer   r   r   r4   T   :   	
z!ComponentService._enable_resourcec                    r-   )zHandle 'disableResource' requests.

        Args:
            key: The key of the resource to disable

        Returns:
            The resource that was disabled
        zDisable resource: %sNr.   )r   r   r   r/   r   r0   r+   r1   r2   r   r    r!   r   r3   r   r   r   _disable_resourcer   r5   r   r   r   r:      r9   z"ComponentService._disable_resourcec                    r   )zHandle 'enablePrompt' requests.

        Args:
            key: The key of the prompt to enable

        Returns:
            The prompt that was enabled
        zEnabling prompt: %sNr   Unknown prompt: )r   r   r   r   _prompts
get_promptr   r   r    r!   r"   r#   r   r   _enable_promptr   r   r   promptr'   
prompt_keyr)   r   r   r   r>      r*   zComponentService._enable_promptc                    s   || j jjv r| j |I dH }|  |S t| j jD ])}|jrF||j drE|	|j d}t
|j}||I dH }|  S qqtd| )zHandle 'disablePrompt' requests.

        Args:
            key: The key of the prompt to disable

        Returns:
            The prompt that was disabled
        Nr   r;   )r   r   r<   r=   r+   r   r    r!   r"   r#   r   r   _disable_promptr   r?   r   r   r   rB      s   
z ComponentService._disable_promptN)__name__
__module____qualname____doc__r   r   strr	   r$   r,   r   r   r4   r:   r   r>   rB   r   r   r   r   r      s    ++r   N)rF   fastmcp.exceptionsr   fastmcp.prompts.promptr   fastmcp.resources.resourcer   fastmcp.resources.templater   fastmcp.server.serverr   r   r   fastmcp.tools.toolr	   fastmcp.utilities.loggingr
   rC   r   r   r   r   r   r   <module>   s    