
    Ki                         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z  fdZd ZdefdZ	 ddee   fdZdefdZy)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)FastMCPNserverpathrequired_scopesc                 p   t        |       }g }g }d|j                  |j                  dd|j                  |j                  dd|j
                  |j                  dd}||j                  t        ||             n|dk7  r|j                  t        |||             nc|j                  t        d|d   id	|             |j                  t        d
|d
   id|             |j                  t        d|d   id|             | j                  j                  |       | j                  j                  |       y)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promptN/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_configss          /home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/contrib/component_manager/component_manager.pyset_up_component_managerr/      sO    v&GFF !**,,
  ..00
 #,,..
M$ 7tLM3;MM-mT?S MM-]623X
 MM-z!:; # MM-}X67_ ""))&1
""))&1    c                 (     dt         f f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                   K   | j                   d   j                  d      d      }	     |       d {    t        dj                          d d| i      S 7 '# t        $ r}t        dd d| 	      |d }~ww xY ww)
Nr   :r   messagezd z: i  zUnknown )status_codedetail)path_paramssplitr	   
capitalizer   StarletteHTTPException)r2   nameeaction	componentconfigs      r.   endpointzmake_endpoint.<locals>.endpoint\   s     ""6'?#8#8#=a#@A		 &.&&&v00232i[4&IJ  '  	(!)Btf5 	s8   %BA! A&A! BA! !	B*A>>BBr   )r>   r?   r@   rA   s   ``` r.   make_endpointrB   Q   s      Or0   returnc                     t        | ||      }||dv rd|d    d|  }n&|dk7  r|| d| d|d    d|  }nd| d|d    d|  }t        ||dg      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.
    )r   r   r   z/{r   z}/r   zs/{POST)rA   methods)rB   r   )r>   r?   r@   r   	root_pathrA   r   s          r.   
make_routerH   m   s     VY7H"y 5 (
 VG_%S1 7[)D0AVHMDykfWo%6c&BD6(;;r0   c                 l    g }| D ],  }| |   }dD ]   }|j                  t        |||||             " . |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'   rH   r-   rG   r   component_management_routesr?   r@   r>   s          r.   r&   r&      sX     02" 	!.y!9+ 	F'..69foyQ	 '&r0   c                     g }| D ],  }| |   }dD ]   }|j                  t        |||||             " . t        | t        t	        |      |            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.
    rJ   )r+   )app)r'   rH   r
   r   r   rK   s          r.   r(   r(      sw     02" 	!.y!9+ 	F'..69foyQ	 +!89?
 r0   )r   N)N)__doc__typingr   &mcp.server.auth.middleware.bearer_authr   starlette.applicationsr   starlette.exceptionsr   r;   starlette.requestsr   starlette.responsesr	   starlette.routingr
   r   3fastmcp.contrib.component_manager.component_servicer   fastmcp.exceptionsr   fastmcp.server.serverr   strlistr/   rB   rH   r&   r(    r0   r.   <module>r]      s   
  H , H & , * P , ) KO:2:2:27;Cy47G:2z8< << /3'	%['0PU r0   