
    i-                       d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZmZmZmZ ddlmZmZ ddlmZ dd	l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#m$Z$ ddl%m&Z& ddl'm(Z( erddl)m*Z* ddl+m,Z,  ededef         Z-e G d de             Z. edd       G d d             Z/ G d de      Z0dddddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d dZ1y)!z+Standalone @resource decorator for FastMCP.    )annotationsN)Callable)	dataclassfield)TYPE_CHECKINGAnyLiteralProtocolTypeVarruntime_checkable)AnnotationsIcon)AnyUrl)SkipJsonSchema)resolve_task_config)ResourceResourceResult)resolve_ui_mime_type)	AuthCheck)transform_context_annotationswithout_injected_parameters)
TaskConfig)call_sync_fn_in_threadpool)DocketResourceTemplateF.)boundc                  $    e Zd ZU dZded<   ddZy)DecoratedResourcez0Protocol for functions decorated with @resource.ResourceMeta__fastmcp__c                     y N )selfargskwargss      s/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/resources/function_resource.py__call__zDecoratedResource.__call__)   s    #    N)r'   r   r(   r   returnr   )__name__
__module____qualname____doc____annotations__r*   r%   r+   r)   r    r    #   s    :=r+   r    T)frozenkw_onlyc                      e Zd ZU dZ edd      Zded<   ded<   d	Zd
ed<   d	Zded<   d	Z	d
ed<   d	Z
d
ed<   d	Zded<   d	Zded<   d	Zd
ed<   d	Zded<   d	Zded<   d	Zded<   d	Zded<   dZded<   y	) r!   z:Metadata attached to functions by the @resource decorator.resourceF)defaultinitzLiteral['resource']typestruriN
str | Nonenamestr | int | Noneversiontitledescriptionlist[Icon] | Noneiconsset[str] | Nonetags	mime_typeAnnotations | Noner   dict[str, Any] | Nonemetabool | TaskConfig | Nonetask"AuthCheck | list[AuthCheck] | NoneauthTboolenabled)r-   r.   r/   r0   r   r8   r1   r<   r>   r?   r@   rB   rD   rE   r   rH   rJ   rL   rN   r%   r+   r)   r!   r!   ,   s    D %ju ED
E	HD* $G$E:"K"#E# D/  Iz &*K#*"&D
&%)D
")/3D
,3GTr+   r!   c                      e Zd ZU dZded<   e	 d	ddddddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
d       Z	 	 ddZddZy)FunctionResourcea  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
    z"SkipJsonSchema[Callable[..., Any]]fnN)metadatar<   r>   r?   r@   rB   rE   rD   r   rH   rJ   rL   c               *   t        d ||||||	|
||||fD              xs |du}||rt        d      |-|t        d      t        t        |      ||||||
|	||||      }t	        |j
                        }|j                  xs% t        |dd      xs |j                  j                  }|j                  }|t        d      }n(t        |t              rt        j                  |      }n|}|j                  ||       t!        j"                  |      s|j$                  }t        |t&              r|j(                  }t+        |      }t-        |      }t/        |j
                  |j0                        } | ||||j2                  t        |j2                        nd|j4                  |j6                  xs t!        j8                  |      |j:                  |xs d	|j<                  xs
 t?               |j@                  |jB                  ||jD                  
      S )a  Create a FunctionResource from a function.

        Args:
            fn: The function to wrap
            uri: The URI for the resource (required if metadata not provided)
            metadata: ResourceMeta object with all configuration. If provided,
                individual parameters must not be passed.
            name, title, etc.: Individual parameters for backwards compatibility.
                Cannot be used together with metadata parameter.
        c              3  $   K   | ]  }|d u 
 y wr$   r%   ).0xs     r)   	<genexpr>z1FunctionResource.from_function.<locals>.<genexpr>o   s       A s   Nz|Cannot pass both 'metadata' and individual parameters to from_function(). Use metadata alone or individual parameters alone.z-uri is required when metadata is not providedr:   r<   r>   r?   r@   rB   rD   rE   r   rH   rJ   rL   r-   	forbidden)modez
text/plain)rQ   r:   r<   r>   r?   r@   rB   rE   rD   r   rH   task_configrL   )#any	TypeErrorr!   r9   r   r:   r<   getattr	__class__r-   rJ   r   
isinstancerM   	from_boolvalidate_functioninspect	isroutiner*   staticmethod__func__r   r   r   rE   r>   r?   r@   getdocrB   rD   setr   rH   rL   )clsrQ   r:   rR   r<   r>   r?   r@   rB   rE   rD   r   rH   rJ   rL   individual_params_provideduri_obj	func_name
task_valuer[   
wrapped_fnresolved_mimes                         r)   from_functionzFunctionResource.from_functionO   s"   @       $# 	#( $>E  { OPP#H'#'H & MMSWRT:Sbll>S>S 	
 ]]
$+6K
D)$..z:K$K%%b)4   $Bb,'B +2. 14
 -X\\8;M;MN-5-=-=-IC(()t.. ,,Br0B..#3|'#% ,,#
 	
r+   c                V  K   t        j                  | j                        r| j                          d{   }n<t        | j                         d{   }t        j                  |      r
| d{   }t        |t              r|j                          d{   S |S 7 k7 O7 27 w)z2Read the resource by calling the wrapped function.N)rc   iscoroutinefunctionrQ   r   isawaitabler`   r   read)r&   results     r)   rt   zFunctionResource.read   s      &&tww/779_F 6dgg>>F""6*% fh'&& % ? & 'sE   3B)B!B)B#B)2B%3'B)B'B)#B)%B)'B)c                    | j                   j                         sy|j                  | j                  | j                  g       y)zRegister this resource with docket for background execution.

        FunctionResource registers the underlying function, which has the user's
        Depends parameters for docket to resolve.
        N)names)r[   supports_tasksregisterrQ   key)r&   dockets     r)   register_with_docketz%FunctionResource.register_with_docket   s3     ..0z2r+   r$   )rQ   Callable[..., Any]r:   zstr | AnyUrl | NonerR   zResourceMeta | Noner<   r;   r>   r=   r?   r;   r@   r;   rB   rA   rE   r;   rD   rC   r   rF   rH   rG   rJ   rI   rL   rK   r,   rP   )r,   zstr | bytes | ResourceResult)r{   r   r,   None)	r-   r.   r/   r0   r1   classmethodrp   rt   r|   r%   r+   r)   rP   rP   @   s
   
 	+* $(z

 )-$( "&#' $ $*.&*)-37#z
z
 !z

 &z
 z
 "z
 z
  z
 !z
 z
 z
 (z
 $z
  '!z
" 1#z
$ 
%z
 z
x	%*3r+   rP   )r<   r>   r?   r@   rB   rE   rD   r   rH   rJ   rL   c                   	
 t        t              rt        di t        j                         rt        d      d	
 fdd	
 fddfd}|S )zStandalone decorator to mark a function as an MCP resource.

    Returns the original function with metadata attached. Register with a server
    using mcp.add_resource().
    zQThe @resource decorator requires a URI. Use @resource('uri') instead of @resourcec                H   ddl m} ddlm} t	              }dv xr dv } ||       }t        t        j                  |      j                        }t        
|	      }|s|r |j                  | 
|	      S t        j                  | |      S )	Nr   r   )r   {}rX   )rQ   uri_templater<   r>   r?   r@   rB   rE   rD   r   rH   rJ   rL   )rR   )fastmcp.resources.templater   fastmcp.server.dependenciesr   r   rM   rc   	signature
parametersr!   rp   rP   )rQ   r   r   resolvedhas_uri_params
wrapper_fnhas_func_paramsresource_metar   rL   r@   rB   rH   rE   r<   rD   rJ   r?   r:   r>   s           r)   create_resourcez!resource.<locals>.create_resource	  s    ?K&t,2s
04
w00<GGH %##
 _1#11 '#'   $11"}1MMr+   c                x    t        	
      }t        | d      r| j                  n| }||_        | S )NrX   rf   )r!   hasattrrf   r"   )rQ   rR   targetr   rL   r@   rB   rH   rE   r<   rD   rJ   r?   r:   r>   s      r)   attach_metadataz!resource.<locals>.attach_metadata6  sT    ##
 !(J 7R%	r+   c                    t         j                  j                  dk(  r$t        j                  dt
        d        |       S  |       S )Nobjectzdecorator_mode='object' is deprecated and will be removed in a future version. Decorators now return the original function with metadata attached.   )
stacklevel)fastmcpsettingsdecorator_modewarningswarnDeprecationWarning)rQ   r   r   s    r)   	decoratorzresource.<locals>.decoratorI  sG    **h6MMV"	 #2&&r""r+   r%   )rQ   r}   r,   z#FunctionResource | ResourceTemplate)rQ   r   r,   r   )r`   dictr   rc   rd   r]   )r:   r<   r>   r?   r@   rB   rE   rD   r   rH   rJ   rL   r   r   r   s   ```````````` @@r)   r5   r5      sl    ( +t$!0K08
 	

+N +N +NZ  &	# r+   )r:   r9   r<   r;   r>   r=   r?   r;   r@   r;   rB   rA   rE   r;   rD   rC   r   z#Annotations | dict[str, Any] | NonerH   rG   rJ   rI   rL   rK   r,   zCallable[[F], F])2r0   
__future__r   rc   r   collections.abcr   dataclassesr   r   typingr   r   r	   r
   r   r   	mcp.typesr   r   pydanticr   pydantic.json_schemar   r   fastmcp.decoratorsr   fastmcp.resources.resourcer   r   fastmcp.server.appsr   !fastmcp.server.auth.authorizationr   r   r   r   fastmcp.server.tasks.configr   fastmcp.utilities.async_utilsr   r{   r   r   r   r   r    r!   rP   r5   r%   r+   r)   <module>r      s`   1 "   $ ( T T '  /  2 ? 4 7 3 D;CxS)* > > > $%  &&i3x i3^  $"#  7;"&%)/3h	h h 	h
 h h h h h 5h  h #h -h hr+   