o
    i                     @   s   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 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 	ddededee dB fddZdd ZdefddZ	ddee fddZdefddZdS )z
Routes and helpers for managing tools, resources, and prompts in FastMCP.
Provides endpoints for enabling/disabling components via HTTP, with optional authentication scopes.
    )Any)RequireAuthMiddleware)	Starlette)HTTPExceptionRequest)JSONResponse)MountRoute)ComponentService)NotFoundError)FastMCP/Nserverpathrequired_scopesc                 C   s   t | }g }g }d|j|jdd|j|jdd|j|jdd}|du r-|t|| n5|dkr;|	t
||| n'|	t
d|d id	| |	t
d
|d
 id| |	t
d|d id| | j| | j| dS )aA  Set up routes for enabling/disabling tools, resources, and prompts.
    Args:
        server: The FastMCP server instance
        path: Path used to mount all component-related routes on the server
        required_scopes: Optional list of scopes required for these routes. Applies only if authentication is enabled.
    	tool_name)paramenabledisablezuri:pathprompt_name)toolresourcepromptNr   r   /toolsr   
/resourcesr   /prompts)r   _enable_tool_disable_tool_enable_resource_disable_resource_enable_prompt_disable_promptextend!build_component_manager_endpointsappendbuild_component_manager_mount_additional_http_routes)r   r   r   serviceroutesmountsroute_configs r,   y/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/contrib/component_manager/component_manager.pyset_up_component_manager   sR   


r.   c                    s   dt f fdd}|S )ad  
    Factory for creating Starlette endpoint functions for enabling/disabling a component.
    Args:
        action: 'enable' or 'disable'
        component: The component type (e.g., 'tool', 'resource', or 'prompt')
        config: Dict with param and handler functions for the component
    Returns:
        An async endpoint function for Starlette.
    requestc              
      s   | j d dd  }z  |I d H  td   d d| iW S  ty@ } ztdd d| d	|d }~ww )
Nr   :r   messagezd z: i  zUnknown )status_codedetail)path_paramssplitr   
capitalizer   StarletteHTTPException)r/   nameeaction	componentconfigr,   r-   endpoint\   s    zmake_endpoint.<locals>.endpointr   )r;   r<   r=   r>   r,   r:   r-   make_endpointQ   s   r?   returnc                 C   s   t | ||}|dur|dv rd|d  d|  }n%|dkr1|du r1| d| d|d  d|  }nd| d|d  d|  }t||dgd	S )
ap  
    Creates a Starlette Route for enabling/disabling a component.
    Args:
        action: 'enable' or 'disable'
        component: The component type
        config: Dict with param and handler functions
        required_scopes: Optional list of required auth scopes
        root_path: The base path for the route
    Returns:
        A Starlette Route object.
    N)r   r   r   z/{r   z}/r   zs/{POST)r>   methods)r?   r
   )r;   r<   r=   r   	root_pathr>   r   r,   r,   r-   
make_routem   s    rD   c              
   C   s:   g }| D ]}| | }dD ]}| t||||| qq|S )a_  
    Build a list of Starlette Route objects for all components/actions.
    Args:
        route_configs: Dict describing component types and their handlers
        root_path: The base path for the routes
        required_scopes: Optional list of required auth scopes
    Returns:
        List of Starlette Route objects for component management.
    r   r   )r%   rD   r+   rC   r   component_management_routesr<   r=   r;   r,   r,   r-   r$      s   r$   c              
   C   sP   g }| D ]}| | }dD ]}| t||||| qqt| tt|d|dS )a]  
    Build a Starlette Mount with authentication for component management routes.
    Args:
        route_configs: Dict describing component types and their handlers
        root_path: The base path for the mount
        required_scopes: List of required auth scopes
    Returns:
        A Starlette Mount object with authentication middleware.
    rE   )r)   )app)r%   rD   r	   r   r   rF   r,   r,   r-   r&      s   

r&   )r   N)N)__doc__typingr   &mcp.server.auth.middleware.bearer_authr   starlette.applicationsr   starlette.exceptionsr   r7   starlette.requestsr   starlette.responsesr   starlette.routingr	   r
   3fastmcp.contrib.component_manager.component_servicer   fastmcp.exceptionsr   fastmcp.server.serverr   strlistr.   r?   rD   r$   r&   r,   r,   r,   r-   <module>   s4    

=
