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)
skip_names r   )r   r   r   r   r   r   r   r"   r%   r'   r(   )__name__
ValueErrorr   r   	arg_modelmodel_json_schemar   __doc__)clsr%   r   r   r   r   r   r   r   r"   r(   	func_namefunc_arg_metadatar'    r4   c/home/ubuntu/veenaModal/venv/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)selfr7   patternr>   r4   r4   r5   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 Nr4   r4   resultr4   r5   <lambda>w   s    z2ResourceTemplate.create_resource.<locals>.<lambda>)	r7   r   r   r   r   r   r   r"   r%   z'Error creating resource from template: r4   )r   r%   r(   inspectiscoroutiner	   r   r   r   r   r   r   r"   	Exceptionr-   )r@   r7   rC   rD   er4   rG   r5   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   )r7   r   r)   r!   rF   )r7   r   rC   r&   rD   rE   r)   r
   )r,   
__module____qualname__r0   r   r   __annotations__r   r   r   r   r   r   r"   r%   r'   r(   classmethodr6   rB   rN   r4   r4   r4   r5   r      s4   
 
.r   )!r0   
__future__r   rJ   r=   collections.abcr   typingr   r   pydanticr   r   r   "mcp.server.fastmcp.resources.typesr	   r
   .mcp.server.fastmcp.utilities.context_injectionr   r   *mcp.server.fastmcp.utilities.func_metadatar   	mcp.typesr   r   mcp.server.fastmcp.serverr   mcp.server.sessionr   mcp.shared.contextr   r   r   r4   r4   r4   r5   <module>   s     