o
    hM                     @  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 erZd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)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Zded< eddZded< ed	ddZded< e												d,d-d d!Zd.d$d%Z		d/d0d*d+Zd	S )1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iconsT)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$   )__name__
ValueErrorr   r   	arg_modelmodel_json_schemar   __doc__)clsr!   r   r   r   r   r   r   r$   	func_namefunc_arg_metadatar#    r0   g/var/www/html/openai_agents/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)selfr3   patternr;   r0   r0   r1   matchesO   s
   zResourceTemplate.matchesparamscontext:Context[ServerSessionT, LifespanContextT, RequestT] | Noner
   c              
     s   z0t | j||| j}| jdi | t r I dH  t|| j| j| j| j	| j
 fdddW S  tyD } 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>o   s    z2ResourceTemplate.create_resource.<locals>.<lambda>)r3   r   r   r   r   r   r!   z'Error creating resource from template: r0   )r   r!   r$   inspectiscoroutiner	   r   r   r   r   r   	Exceptionr)   )r=   r3   r@   rA   er0   rD   r1   create_resourceX   s&   


	z ResourceTemplate.create_resource)NNNNNN)r!   r    r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r%   r   )r3   r   r%   r4   rC   )r3   r   r@   r"   rA   rB   r%   r
   )r(   
__module____qualname__r,   r   r   __annotations__r   r   r   r   r   r!   r#   r$   classmethodr2   r?   rK   r0   r0   r0   r1   r      s,   
 
*r   ) r,   
__future__r   rG   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   mcp.server.fastmcp.serverr   mcp.server.sessionr   mcp.shared.contextr   r   r   r0   r0   r0   r1   <module>   s     