o
    i                      @  s   d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	 ddl
Z
ddlmZmZ ddlmZ ddlmZmZmZmZmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ er[	 G dd deZG dd deZ dS )z2Base classes and interfaces for FastMCP resources.    )annotationsN)Callable)TYPE_CHECKING	AnnotatedAny)AnnotationsIcon)Resource)AnyUrl
ConfigDictFieldUrlConstraintsfield_validatormodel_validator)Self)get_context)FastMCPComponent)find_kwarg_by_typeget_fn_namec                      s  e Zd ZU dZeddZedddZded< ed	d
dZ	ded< eddddZ
ded< dZded< dC fddZdC fddZe									dDdEd+d,Zedd-d.edFd/d0Zed1d.dGd3d4ZdHd6d7Zdd8dId=d>ZdJd?d@ZedJdAdBZ  ZS )Kr	   zBase class for all resources.T)validate_default.zURI of the resource)defaultdescriptionz6Annotated[AnyUrl, UrlConstraints(host_required=False)]uri zName of the resourcestrname
text/plainz!MIME type of the resource contentz^[a-zA-Z0-9]+/[a-zA-Z0-9\-+.]+$)r   r   pattern	mime_typeNzfAnnotated[Annotations | None, Field(description="Optional annotations about the resource's behavior")]r   returnNonec                   4   t    z
t }|  W d S  ty   Y d S w N)superenabler   _queue_resource_list_changedRuntimeErrorselfcontext	__class__ `/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/resources/resource.pyr$   4      
zResource.enablec                   r!   r"   )r#   disabler   r%   r&   r'   r*   r,   r-   r/   <   r.   zResource.disablefnCallable[..., Any]str | AnyUrl
str | Nonetitler   iconslist[Icon] | Nonetagsset[str] | Noneenabledbool | NoneAnnotations | Nonemetadict[str, Any] | NoneFunctionResourcec                 C  s    t j| |||||||||	|
dS )Nr0   r   r   r4   r   r5   r   r7   r9   r   r<   )r>   from_functionr?   r,   r,   r-   r@   D   s   zResource.from_functionbefore)modec                 C  s   |r|S dS )z&Set default MIME type if not provided.r   r,   )clsr   r,   r,   r-   set_default_mime_type`   s   zResource.set_default_mime_typeafterr   c                 C  s*   | j r	 | S | jrt| j| _ | S td)z*Set default name from URI if not provided.z#Either name or uri must be provided)r   r   r   
ValueErrorr(   r,   r,   r-   set_default_nameh   s   zResource.set_default_namestr | bytesc                   s
   t d)zRead the resource content.

        This method is not implemented in the base Resource class and must be
        implemented by subclasses.
        z Subclasses must implement read())NotImplementedErrorrG   r,   r,   r-   reads   s   zResource.readinclude_fastmcp_metarM   	overridesr   MCPResourcec                K  sn   t |d| j|d| j|d| j|d| j|d| j|d| j|d| j|d| j	|d	d
S )z'Convert the resource to an MCPResource.r   r   r   mimeTyper4   r5   r   _metarL   )r   r   r   rP   r4   r5   r   rQ   )
rO   getr   r   r   r   r4   r5   r   get_meta)r(   rM   rN   r,   r,   r-   to_mcp_resource{   s   zResource.to_mcp_resourcec              
   C  s.   | j j d| jd| jd| jd| j d
S )Nz(uri=z, name=z, description=z, tags=))r+   __name__r   r   r   r7   rG   r,   r,   r-   __repr__   s   .zResource.__repr__c                 C  s   | j pt| jS )a*  
        The key of the component. This is used for internal bookkeeping
        and may reflect e.g. prefixes or other identifiers. You should not depend on
        keys having a certain value, as the same tool loaded from different
        hierarchies of servers may have different keys.
        )_keyr   r   rG   r,   r,   r-   key   s   zResource.key)r   r    	NNNNNNNNNr0   r1   r   r2   r   r3   r4   r3   r   r3   r5   r6   r   r3   r7   r8   r9   r:   r   r;   r<   r=   r   r>   )r   r3   r   r   )r   r   r   rI   )rM   r:   rN   r   r   rO   )r   r   )rV   
__module____qualname____doc__r   model_configr   r   __annotations__r   r   r   r$   r/   staticmethodr@   r   classmethodrD   r   rH   rK   rT   rW   propertyrY   __classcell__r,   r,   r*   r-   r	   !   sL   
 





r	   c                   @  sF   e Zd ZU dZded< e									ddddZdddZdS )r>   a  A resource that defers data loading by wrapping a function.

    The function is only called when the resource is read, allowing for lazy loading
    of potentially expensive data. This is particularly useful when listing resources,
    as the function won't be called until the resource is actually accessed.

    The function can return:
    - str for text content (default)
    - bytes for binary content
    - other types will be converted to JSON
    r1   r0   Nr   r2   r   r3   r4   r   r5   r6   r   r7   r8   r9   r:   r   r;   r<   r=   r   c                 C  sX   t |tr	t|}| |||pt|||pt|||pd|p t |	dur&|	nd|
|dS )z*Create a FunctionResource from a function.r   NTr?   )
isinstancer   r
   r   inspectgetdocset)rC   r0   r   r   r4   r   r5   r   r7   r9   r   r<   r,   r,   r-   r@      s   

zFunctionResource.from_functionrI   c                   s   ddl m} i }t| j|d}|durt ||< | jdi |}t|r+|I dH }t|tr7|	 I dH S t|t
tB r@|S tj|td S )z2Read the resource by calling the wrapped function.r   )Context)
kwarg_typeN)fallbackr,   )fastmcp.server.contextrj   r   r0   r   rg   isawaitablerf   r	   rK   bytesr   pydantic_coreto_jsondecode)r(   rj   kwargscontext_kwargresultr,   r,   r-   rK      s   



zFunctionResource.readrZ   r[   r\   )rV   r]   r^   r_   ra   rc   r@   rK   r,   r,   r,   r-   r>      s   
 r>   )!r_   
__future__r   rg   collections.abcr   typingr   r   r   rp   	mcp.typesr   r   r	   rO   pydanticr
   r   r   r   r   r   typing_extensionsr   fastmcp.server.dependenciesr   fastmcp.utilities.componentsr   fastmcp.utilities.typesr   r   r>   r,   r,   r,   r-   <module>   s"     }