
    "<i<                       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 ddlmZmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddZ#d dZ$d!dZ% G d de          Z G d de          Z&dS )"z Resource template functionality.    )annotationsN)Callable)	AnnotatedAny)parse_qsunquote)AnnotationsIcon)ResourceTemplate)Fieldfield_validatorvalidate_call)Resource)get_contextwithout_injected_parameters
TaskConfig)FastMCPComponent)compress_schema)get_cached_typeadapteruri_templatestrreturnset[str]c                    t          j        d|           }|r2d |                    d                              d          D             S t	                      S )zFExtract query parameter names from RFC 6570 `{?param1,param2}` syntax.z\{\?([^}]+)\}c                6    h | ]}|                                 S  )strip).0ps     /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/fastmcp/resources/template.py	<setcomp>z'extract_query_params.<locals>.<setcomp>   s     ===a		===       ,)researchgroupsplitset)r   matchs     r!   extract_query_paramsr,      sQ    I&55E >==5;;q>>#7#7#<#<====55Lr#   template
re.Patternc                   t          j        dd|           }t          j        d|          }d}|D ]}|                    d          rR|                    d          r=|dd         }|                    d          r|d	d         }|d
| dz  }_|d
| dz  }i|t          j        |          z  }t          j        d| d          S )a  Build regex pattern for URI template, handling RFC 6570 syntax.

    Supports:
    - `{var}` - simple path parameter
    - `{var*}` - wildcard path parameter (captures multiple segments)
    - `{?var1,var2}` - query parameters (ignored in path matching)
    z\{\?[^}]+\} z(\{[^}]+\}){}r$   *Nz(?P<z>.+)z>[^/]+)^$)r&   subr)   
startswithendswithescapecompile)r-   template_without_querypartspatternpartnames         r!   build_regexrA   #   s      VNBAAH^%;<<EG 	' 	'??3 	'DMM#$6$6 	'":D}}S!! 0CRCy,$,,,,/$////ry&GG:n'nnn%%%r#   uridict[str, str] | Nonec                \   |                      d          \  }}}t          |          }|                    |          }|sdS d |                                                                D             }|r8t          |          }t          |          }	|D ]}
|
|	v r|	|
         d         ||
<   |S )zMatch URI against template and extract both path and query parameters.

    Supports RFC 6570 URI templates:
    - Path params: `{var}`, `{var*}`
    - Query params: `{?var1,var2}`
    ?Nc                4    i | ]\  }}|t          |          S r   )r   )r   kvs      r!   
<dictcomp>z&match_uri_template.<locals>.<dictcomp>M   s$    BBB1aBBBr#   r   )	partitionrA   r+   	groupdictitemsr,   r   )rB   r   uri_path_query_stringregexr+   paramsquery_param_namesparsed_queryr@   s              r!   match_uri_templaterT   =   s     !$c 2 2Ha %%EKK!!E tBB(9(9(?(?(A(ABBBF  50>>--% 	5 	5D|##+D1!4tMr#   c                      e Zd ZU dZ 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<   d<dZ
d= fdZd= fdZe	 	 	 	 	 	 	 	 	 	 d>d?d'            Z ed	d()          ed@d*                        ZdAd,ZdBd/ZdCd2Zdd3dDd8ZedEd:            Zed<d;            Z xZS )Fr   .A template for dynamically creating resources.z<URI template with parameters (e.g. weather://{city}/current))descriptionr   r   
text/plainz!MIME type of the resource content)defaultrW   	mime_typez#JSON schema for function parametersdict[str, Any]
parametersNz2Optional annotations about the resource's behaviorAnnotations | Noner   r   c           
     `    | j         j         d| j        d| j        d| j        d| j         d
S )Nz(uri_template=z, name=z, description=z, tags=))	__class____name__r   r@   rW   tagsselfs    r!   __repr__zResourceTemplate.__repr__l   su    .)  X  X9J  X  XUYU^  X  Xpt  qA  X  X  LP  LU  X  X  X  	Xr#   Nonec                    t                                                       	 t                      }|                                 d S # t          $ r Y d S w xY wN)superenabler   _queue_resource_list_changedRuntimeErrorrd   contextr`   s     r!   rj   zResourceTemplate.enableo   s_    	!mmG0022222 	 	 	DD	   "A 
AAc                    t                                                       	 t                      }|                                 d S # t          $ r Y d S w xY wrh   )ri   disabler   rk   rl   rm   s     r!   rq   zResourceTemplate.disablew   s_    	!mmG0022222 	 	 	DD	ro   fnCallable[..., Any]r@   
str | NonetitlerW   iconslist[Icon] | Nonerb   set[str] | Noneenabledbool | Nonemetadict[str, Any] | Nonetaskbool | TaskConfig | NoneFunctionResourceTemplatec                N    t                               | |||||||||	|
|          S )Nrr   r   r@   ru   rW   rv   rZ   rb   ry   r   r{   r}   )r   from_functionr   s               r!   r   zResourceTemplate.from_function   sE     (55%## 6 
 
 	
r#   beforemodec                    |r|S dS )z&Set default MIME type if not provided.rX   r   )clsrZ   s     r!   set_default_mime_typez&ResourceTemplate.set_default_mime_type   s      	|r#   rB   c                ,    t          || j                  S )z5Check if URI matches template and extract parameters.)rT   r   )rd   rB   s     r!   matcheszResourceTemplate.matches   s    !#t'8999r#   	argumentsstr | bytesc                $   K   t          d          )Read the resource content.z>Subclasses must implement read() or override create_resource()NotImplementedError)rd   r   s     r!   readzResourceTemplate.read   s      !L
 
 	
r#   rQ   r   c                $   K   t          d          )zCreate a resource from the template with the given parameters.

        The base implementation does not support background tasks.
        Use FunctionResourceTemplate for task support.
        z[Subclasses must implement create_resource(). Use FunctionResourceTemplate for task support.r   )rd   rB   rQ   s      r!   create_resourcez ResourceTemplate.create_resource   s       "=
 
 	
r#   include_fastmcp_metar   	overridesr   MCPResourceTemplatec                  t          |                    d| j                  |                    d| j                  |                    d| j                  |                    d| j                  |                    d| j                  |                    d| j                  |                    d| j                  |                    d| 	                    |	                    
          S )z8Convert the resource template to an MCPResourceTemplate.r@   uriTemplaterW   mimeTyperu   rv   r   _metar   )r@   r   rW   r   ru   rv   r   r   )
r   getr@   r   rW   rZ   ru   rv   r   get_meta)rd   r   r   s      r!   to_mcp_templatez ResourceTemplate.to_mcp_template   s     #vty11!mT5FGG!mT5EFF]]:t~>>--44--44!mT5EFF--<PQQ 
 
 
 	
r#   mcp_templatec                N     | |j         |j        |j        |j        pdi           S )zJCreates a FastMCP ResourceTemplate from a raw MCP ResourceTemplate object.rX   )r   r@   rW   rZ   r\   )r   r@   rW   r   )r   r   s     r!   from_mcp_templatez"ResourceTemplate.from_mcp_template   s=    
 s%1"$0"+;|
 
 
 	
r#   c                    | j         p| j        S )a*  
        The key of the component. This is used for internal bookkeeping
        and may reflect e.g. prefixes or other identifiers. You should not depend on
        keys having a certain value, as the same tool loaded from different
        hierarchies of servers may have different keys.
        )_keyr   rc   s    r!   keyzResourceTemplate.key   s     y-D--r#   )r   r   )r   rf   
NNNNNNNNNNrr   rs   r   r   r@   rt   ru   rt   rW   rt   rv   rw   rZ   rt   rb   rx   ry   rz   r   r]   r{   r|   r}   r~   r   r   )rZ   rt   r   r   )rB   r   r   r|   r   r[   r   r   rB   r   rQ   r[   r   r   )r   rz   r   r   r   r   )r   r   r   r   )ra   
__module____qualname____doc__r   r   __annotations__rZ   r\   r   re   rj   rq   staticmethodr   r   classmethodr   r   r   r   r   r   propertyr   __classcell__)r`   s   @r!   r   r   \   sY        88R  L     U*M  I     "'9" " "J     ',e"V' ' 'K    X X X X              "&#' $ $#*.&*)-
 
 
 
 \
: _[x000   [ 10: : : :
 
 
 
	
 	
 	
 	
 -1
 
 
 
 
 
* 

 

 

 [

 . . . X. . . . .r#   r   c                      e Zd ZU dZded<    ed           Zded<   d&dZd'dZe		 	 	 	 	 	 	 	 	 	 d(d)d%            Z
dS )*r   rV   rs   rr   c                 "    t          d          S )N	forbiddenr   r   r   r#   r!   <lambda>z!FunctionResourceTemplate.<lambda>   s    jk&B&B&B r#   )default_factoryz_Annotated[TaskConfig, Field(description='Background task execution configuration (SEP-1686).')]task_configrB   r   rQ   r[   r   r   c           
         K   d fd}t          j        || j         j         j         j         j         j                  S )z>Create a resource from the template with the given parameters.r   r   c                 D   K                                   d {V } | S )N)r   )r   )resultrQ   rd   s    r!   resource_read_fnzBFunctionResourceTemplate.create_resource.<locals>.resource_read_fn   s1      99v966666666FMr#   )rr   rB   r@   rW   rZ   rb   ry   r}   )r   r   )r   r   r@   rW   rZ   rb   ry   r   )rd   rB   rQ   r   s   ` ` r!   r   z(FunctionResourceTemplate.create_resource   sm      	 	 	 	 	 	 	
 %(nL!	
 	
 	
 		
r#   r   r   c                  K   |                                 }t          j        | j                  }t	          |                                          D ]\  }}||j        v rt          |t                    r|j        |         }|j	        }|t          j
        j        u s	|t          u rT	 |t          u rt          |          ||<   n>|t          u rt          |          ||<   n"|t          u r|                                dv ||<   # t           t"          f$ r Y w xY w | j        di |}t          j        |          r| d{V }|S )r   )true1yesNr   )copyinspect	signaturerr   listrL   r\   
isinstancer   
annotation	Parameteremptyintfloatboollower
ValueErrorAttributeErrorisawaitable)	rd   r   kwargssig
param_nameparam_valueparamr   r   s	            r!   r   zFunctionResourceTemplate.read  sp      !!(('+FLLNN';'; 	 	#JS^++
;0L0L+z2"-
!2!888J#<M<M!S((-0-=-=z**#u,,-2;-?-?z**#t++-8->->-@-@DX-Xz*"N3   D
 ""6""v&& 	"!\\\\\\Fs   &ADDDNr   r@   rt   ru   rW   rv   rw   rZ   rb   rx   ry   rz   r   r]   r{   r|   r}   r~   c                   |pt          |dd          p|j        j        }|dk    rt          d          t	          j        |          }|j                                        D ]+}|j        t          j	        j
        k    rt          d          ,t          t          j        d|                    }t          |          }||z  }|st          d          t          |          }t	          j        |          t          j                                                  }fd|D             }fd	|D             }|r||z
  }|rt          d
| d          |                    |          st          d| d|           t%          d |j                                        D                       s*|                    |          st          d| d|           |pt	          j        |          }|t)          d          }n,t+          |t,                    rt)          j        |          }n|}|                    ||           t	          j        |          s|j        }t+          |t6                    r|j        }t          |          }t;          |          }|                                }t?          |d          }tA          |          } | ||||||pd|||pt                      |	|	nd|
||          S )z"Create a template from a function.ra   Nz<lambda>z,You must provide a name for lambda functionsz<Functions with *args are not supported as resource templatesz{(\w+)(?:\*)?}z0URI template must contain at least one parameterc                    h | ]L}j         |         j        t          j        j        u r'j         |         j        t          j        j        k    J|MS r   r\   rY   r   r   r   kindVAR_KEYWORDr   r    user_sigs     r!   r"   z9FunctionResourceTemplate.from_function.<locals>.<setcomp>O  s^     
 
 
"1%-1B1HHH#A&+w/@/LLL  MLLr#   c                    h | ]L}j         |         j        t          j        j        ur'j         |         j        t          j        j        k    J|MS r   r   r   s     r!   r"   z9FunctionResourceTemplate.from_function.<locals>.<setcomp>U  s^     
 
 
"1%-W5F5LLL#A&+w/@/LLL  MLLr#   zQuery parameters z9 must be optional function parameters with default valueszRequired function arguments z- must be a subset of the URI path parameters c              3  J   K   | ]}|j         t          j        j        k    V  d S rh   )r   r   r   r   )r   r   s     r!   	<genexpr>z9FunctionResourceTemplate.from_function.<locals>.<genexpr>l  sC       
 
 J'+77
 
 
 
 
 
r#   zURI parameters z- must be a subset of the function arguments: r   r   T)prune_titlesrX   )r   r@   ru   rW   rv   rZ   rr   r\   rb   ry   r   r{   r   )!getattrr`   ra   r   r   r   r\   valuesr   r   VAR_POSITIONALr*   r&   findallr,   r   keysissubsetanygetdocr   r   r   	from_boolvalidate_function	isroutine__call__r   __func__r   json_schemar   r   )r   rr   r   r@   ru   rW   rv   rZ   rb   ry   r   r{   r}   	func_namer   r   path_paramsquery_paramsall_uri_params
wrapper_fnfunc_paramsrequired_paramsoptional_paramsinvalid_query_paramsr   type_adapterr\   r   s                              @r!   r   z&FunctionResourceTemplate.from_function"  s   $ RGB
D99RR\=R	
""KLLL ##^**,, 	 	EzW.=== R   > "*%6EEFF+L99$|3 	QOPPP 144
$Z00(-224455
 
 
 
 
 
 

 
 
 
 
 
 
  	#//#A #  w(<www  
 ''44 	zzzmxzz  
  
 
..00
 
 
 
 
 	 "**;77  pnppcnpp   "7W^B%7%7 <$+666KKd## 	$.t44KKK%%b)444  $$ 	Bb,'' 	B044
-j99!--//
$ZdCCC
 :&&s%#/<!&2GG##
 
 
 	
r#   r   r   r   r   )ra   r   r   r   r   r   r   r   r   r   r   r   r#   r!   r   r      s         88 	BBCCC  D D D D

 
 
 
&   > 
   "&#' $ $#*.&*)-z
 z
 z
 z
 [z
 z
 z
r#   r   )r   r   r   r   )r-   r   r   r.   )rB   r   r   r   r   rC   )'r   
__future__r   r   r&   collections.abcr   typingr   r   urllib.parser   r   	mcp.typesr	   r
   r   r   pydanticr   r   r   fastmcp.resources.resourcer   fastmcp.server.dependenciesr   r   fastmcp.server.tasks.configr   fastmcp.utilities.componentsr   fastmcp.utilities.json_schemar   fastmcp.utilities.typesr   r,   rA   rT   r   r   r#   r!   <module>r     s   & & " " " " " "  				 $ $ $ $ $ $ ! ! ! ! ! ! ! ! * * * * * * * * ' ' ' ' ' ' ' ' = = = = = =          0 / / / / / P P P P P P P P 2 2 2 2 2 2 9 9 9 9 9 9 9 9 9 9 9 9 : : : : : :   & & & &4   >H. H. H. H. H.' H. H. H.Vv
 v
 v
 v
 v
/ v
 v
 v
 v
 v
r#   