o
    iU                     @  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Zded< eddZded< ed	d dZded!< e														d/d0d#d$Zd1d'd(Z		d2d3d-d.Zd	S )4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   T)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&   )__name__
ValueErrorr   r   	arg_modelmodel_json_schemar   __doc__)clsr#   r   r   r   r   r   r   r   r&   	func_namefunc_arg_metadatar%    r2   l/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/mcp/server/fastmcp/resources/templates.pyfrom_function%   s.   

zResourceTemplate.from_functionuridict[str, Any] | Nonec                 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)selfr5   patternr=   r2   r2   r3   matchesR   s
   zResourceTemplate.matchesparamscontext:Context[ServerSessionT, LifespanContextT, RequestT] | Noner
   c                   s   z2t | j||| j}| jdi | t r I dH  t|| j| j| j| j	| j
| j fdddW S  tyF } ztd| d}~ww )z>Create a resource from the template with the given parameters.Nc                     s    S Nr2   r2   resultr2   r3   <lambda>s   s    z2ResourceTemplate.create_resource.<locals>.<lambda>)r5   r   r   r   r   r   r   r#   z'Error creating resource from template: r2   )r   r#   r&   inspectiscoroutiner	   r   r   r   r   r   r   	Exceptionr+   )r?   r5   rB   rC   er2   rF   r3   create_resource[   s(   



z ResourceTemplate.create_resource)NNNNNNN)r#   r"   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r&   r   r'   r   )r5   r   r'   r6   rE   )r5   r   rB   r$   rC   rD   r'   r
   )r*   
__module____qualname__r.   r   r   __annotations__r   r   r   r   r   r   r#   r%   r&   classmethodr4   rA   rM   r2   r2   r2   r3   r      s0   
 
,r   )!r.   
__future__r   rI   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   r2   r2   r2   r3   <module>   s     