o
    iS                     @  s   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 ddlmZmZ ddlmZmZ dd	lmZ dd
lmZmZ er\ddlmZ ddlmZ ddlmZmZ G dd deZ dS )z Resource template functionality.    )annotationsN)Callable)TYPE_CHECKINGAny)	BaseModelFieldvalidate_call)FunctionResourceResource)find_context_parameterinject_context)func_metadata)AnnotationsIcon)Context)ServerSessionT)LifespanContextTRequestTc                   @  s  e Zd ZU dZeddZded< eddZded< edd	d
Zded< eddZ	ded< edddZ
ded< ed	ddZded< ed	ddZded< ed	ddZded< eddZded< ed dZd!ed"< ed	d#dZded$< e																d1d2d&d'Zd3d)d*Z		d4d5d/d0Zd	S )6ResourceTemplatez.A template for dynamically creating resources.z<URI template with parameters (e.g. weather://{city}/current))descriptionstruri_templatezName of the resourcenamez$Human-readable title of the resourceN)r   default
str | Nonetitlez%Description of what the resource doesr   
text/plainz!MIME type of the resource content)r   r   	mime_typez0Optional list of icons for the resource templatelist[Icon] | Noneiconsz.Optional annotations for the resource templateAnnotations | Noner   z,Optional metadata for this resource templatedict[str, Any] | NonemetaT)excludeCallable[..., Any]fnz#JSON schema for function parametersdict[str, Any]
parametersz-Name of the kwarg that should receive contextcontext_kwargreturnc                 C  s   |p|j }|dkrtd|
du rt|}
t||
dur|
gng d}|j }t|}| ||||p5|jp5d|p8d|||	|||
dS )z"Create a template from a function.z<lambda>z,You must provide a name for lambda functionsN)Z
skip_names r   )r   r   r   r   r   r   r   r"   r%   r'   r(   )__name__
ValueErrorr   r   Z	arg_modelZmodel_json_schemar   __doc__)clsr%   r   r   r   r   r   r   r   r"   r(   	func_nameZfunc_arg_metadatar'    r0   ]/home/app/Keep/.python/lib/python3.10/site-packages/mcp/server/fastmcp/resources/templates.pyfrom_function&   s0   

zResourceTemplate.from_functionuric                 C  s:   | j dddd}td| d|}|r| S dS )z5Check if URI matches template and extract parameters.{z(?P<}z>[^/]+)^$N)r   replacerematch	groupdict)selfr3   patternr:   r0   r0   r1   matchesU   s
   zResourceTemplate.matchesparamscontext:Context[ServerSessionT, LifespanContextT, RequestT] | Noner
   c                   s   z4t | j||| j}| jdi | t r I dH  t|| j| j| j| j	| j
| j| j fddd	W S  tyH } ztd| d}~ww )z>Create a resource from the template with the given parameters.Nc                     s    S Nr0   r0   resultr0   r1   <lambda>w   s    z2ResourceTemplate.create_resource.<locals>.<lambda>)	r3   r   r   r   r   r   r   r"   r%   z'Error creating resource from template: r0   )r   r%   r(   inspectiscoroutiner	   r   r   r   r   r   r   r"   	Exceptionr,   )r<   r3   r?   r@   er0   rC   r1   create_resource^   s*   


z ResourceTemplate.create_resource)NNNNNNNN)r%   r$   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r"   r!   r(   r   r)   r   )r3   r   r)   r!   rB   )r3   r   r?   r&   r@   rA   r)   r
   )r+   
__module____qualname__r-   r   r   __annotations__r   r   r   r   r   r   r"   r%   r'   r(   classmethodr2   r>   rJ   r0   r0   r0   r1   r      s4   
 
.r   )!r-   
__future__r   rF   r9   collections.abcr   typingr   r   Zpydanticr   r   r   Z"mcp.server.fastmcp.resources.typesr	   r
   Z.mcp.server.fastmcp.utilities.context_injectionr   r   Z*mcp.server.fastmcp.utilities.func_metadatar   Z	mcp.typesr   r   Zmcp.server.fastmcp.serverr   Zmcp.server.sessionr   Zmcp.shared.contextr   r   r   r0   r0   r0   r1   <module>   s     