
    KiS                        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e      Z y)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                     e Zd ZU dZ ed      Zded<    ed      Zded<    edd	
      Zded<    ed      Z	ded<    edd      Z
ded<    ed	d      Zded<    ed	d      Zded<    ed	d      Zded<    ed      Zded<    ed       Zd!ed"<    ed	d#      Zded$<   e	 	 	 	 	 	 	 	 d(	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d)d%       Zd*d&Z	 d+	 	 	 	 	 	 	 d,d'Zy	)-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_kwargc                   |xs |j                   }|dk(  rt        d      |
t        |      }
t        ||
|
gng       }|j                  j                         }t        |      } | ||||xs |j                  xs d|xs d|||	|||
      S )z"Create a template from a function.z<lambda>z,You must provide a name for lambda functions)
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)   s                 r/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/mcp/server/fastmcp/resources/templates.pyfrom_functionzResourceTemplate.from_function&   s     'BKK	
"KLL  226M **7*C
 '00BBD
 2%#7rzz7R/<#!'
 	
    c                    | j                   j                  dd      j                  dd      }t        j                  d| d|      }|r|j	                         S y)z5Check if URI matches template and extract parameters.{z(?P<}z>[^/]+)^$N)r   replacerematch	groupdict)selfuripatternr@   s       r6   matcheszResourceTemplate.matchesU   sT     ##++C8@@iP1WIQ-??$$r8   c                  K   	 t        | j                  ||| j                        } | j                  di |t        j                        r
 d{   t        || j                  | j                  | j                  | j                  | j                  | j                  | j                  fd	      S 7 a# t        $ r}t        d|       d}~ww xY ww)z>Create a resource from the template with the given parameters.Nc                      S N )results   r6   <lambda>z2ResourceTemplate.create_resource.<locals>.<lambda>w   s    6 r8   )	rC   r   r   r   r   r!   r   r$   r'   z'Error creating resource from template: rI   )r   r'   r*   inspectiscoroutiner
   r   r   r   r   r!   r   r$   	Exceptionr/   )rB   rC   paramscontexterJ   s        @r6   create_resourcez ResourceTemplate.create_resource^   s     	L#DGGVWd>P>PQF TWW&v&F""6*%#YYjj ,,..jj ,,YY!
 
 &  	LFqcJKK	Ls<   CAB7 B5A B7 4C5B7 7	C CCC)NNNNNNNN)r'   r&   r   r   r   r   r   r   r   r   r   r   r!   r    r   r"   r$   r#   r*   r   returnr   )rC   r   rS   r#   rH   )rC   r   rO   r(   rP   z:Context[ServerSessionT, LifespanContextT, RequestT] | NonerS   r   )r.   
__module____qualname__r2   r   r   __annotations__r   r   r   r   r!   r   r$   r'   r)   r*   classmethodr7   rE   rR   rI   r8   r6   r   r      s   8*hiL#i"89D#9*PZ^_E:_#0WXKX<=`aIsa$T?qrEr&+DFv&wK#w"'Bp"qD
q"40B0!&3X!YJY %d8g hM:h
   "& $#'*.&*$(,
,
 ,
 	,

 ,
  ,
 ,
 !,
 (,
 $,
 ",
 
,
 ,
\ OS	LL L L	L
 
Lr8   r   )!r2   
__future__r   rL   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   rI   r8   r6   <module>rc      sE    & "  	 $ % 4 4 I a D '11=cLy cLr8   