
    $j%                       d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
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 d dlmZ erd d	lmZ d d
lmZ  ede          Z G d de	d          Zd dZd!dZd"dZ G d de          Z dS )#    )annotations)Sequence)TYPE_CHECKING	AnnotatedAnyClassVar	TypedDictcast)Icon)BeforeValidatorField)SelfTypeVar
TaskConfig)FastMCPBaseModel)Docket)	ExecutionT)defaultc                  .    e Zd ZU ded<   ded<   ded<   dS )FastMCPMetaz	list[str]tagsstrversionversionsN)__name__
__module____qualname____annotations__     d/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/fastmcp/utilities/components.pyr   r      s0         OOOLLLr"   r   F)totalmetadict[str, Any] | Nonereturnc                    | si S dD ]C}|                      |          }t          |t                    rt          t          |          c S Di S )zExtract FastMCP metadata from a component's meta dict.

    Handles both the current `fastmcp` namespace and the legacy `_fastmcp`
    namespace for compatibility with older FastMCP servers.
    )fastmcp_fastmcp)get
isinstancedictr
   r   )r%   keymetadatas      r#   get_fastmcp_metadatar0      sc      	& / /88C==h%% 	/X.....	/ Ir"   	maybe_setset[T] | Sequence[T] | Noneset[T]c                n    | t                      S t          | t                     r| S t          |           S )z@Convert a sequence to a set, defaulting to an empty set if None.)setr,   )r1   s    r#   _convert_set_default_noner6   +   s5    uu)S!! y>>r"   vstr | int | float | None
str | Nonec                8   | dS t          | t                    rt          d|           t          | t          t          t
          f          s't          dt          |           j         d|           t          |           }d|v rt          d|          |S )zCoerce version to string, accepting int, float, or str.

    Raises TypeError for non-scalar types (list, dict, set, etc.).
    Raises ValueError if version contains '@' (used as key delimiter).
    Nz3Version must be a string, int, or float, got bool: z-Version must be a string, int, or float, got z: @z;Version string cannot contain '@' (used as key delimiter): )	r,   bool	TypeErrorr   intfloattyper   
ValueError)r7   r   s     r#   _coerce_versionrB   4   s     	yt!T USaSSTTTa#sE*++ 
UDGG<LUUPQUU
 
 	
 !ffG
g~~U'UU
 
 	
 Nr"   c                      e Zd ZU dZdZded<   d; fd	Z 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ed          Zded<    edd          Zd ed!<    ed" #          Zd$ed%<   ed<d'            Zed=d(            Zd>d*Zd?d.Zd=d/Zd@d0Zd@d1ZdAd3ZdBd6ZdCd9Zd>d:Z xZS )DFastMCPComponentzIBase class for FastMCP tools, prompts, resources, and resource templates. zClassVar[str]
KEY_PREFIXkwargsr   r'   Nonec                     t                      j        di | | j        s+dd l}|                    | j         dt          d           d S d S )Nr   zb does not define KEY_PREFIX. Component keys will not be type-prefixed, which may cause collisions.   )
stacklevelr!   )super__init_subclass__rF   warningswarnr   UserWarning)clsrG   rN   	__class__s      r#   rM   z"FastMCPComponent.__init_subclass__O   s    !!++F+++~ 	OOOMM< Y Y Y	      	 	r"   zThe name of the component.)descriptionr   nameNzpOptional version identifier for this component. Multiple versions of the same component (same name) can coexist.)r   rS   z7Annotated[str | None, BeforeValidator(_coerce_version)]r   z0The title of the component for display purposes.r9   titlez!The description of the component.rS   zHOptional list of icons for this component to display in user interfaces.zlist[Icon] | NoneiconszTags for the component.)default_factoryrS   z?Annotated[set[str], BeforeValidator(_convert_set_default_none)]r   z$Meta information about the componentr&   r%   c                 "    t          d          S )N	forbidden)moder   r!   r"   r#   <lambda>zFastMCPComponent.<lambda>z   s    jk&B&B&B r"   )rW   z_Annotated[TaskConfig, Field(description='Background task execution configuration (SEP-1686).')]task_config
identifierc                ,    | j         r| j          d| S |S )zConstruct the lookup key for this component type.

        Args:
            identifier: The raw identifier (name for tools/prompts, uri for resources)

        Returns:
            A prefixed key like "tool:name" or "resource:uri"
        :)rF   )rQ   r]   s     r#   make_keyzFastMCPComponent.make_key|   s*     > 	4n33z333r"   c                R    |                      | j                  }| d| j        pd S )a  The globally unique lookup key for this component.

        Format: "{key_prefix}:{identifier}@{version}" or "{key_prefix}:{identifier}@"
        e.g. "tool:my_tool@v2", "tool:my_tool@", "resource:file://x.txt@"

        The @ suffix is ALWAYS present to enable unambiguous parsing of keys
        (URIs may contain @ characters, so we always include the delimiter).

        Subclasses should override this to use their specific identifier.
        Base implementation uses name.

        Prefer `.key` over ad-hoc `name or uri or uri_template` logic for any
        cross-component identity work (dedupe, grouping, collision detection,
        lookup tables). It encodes type, identifier, and version, so variants
        of the same component don't falsely collide with each other, and
        cross-type identifiers (e.g. a tool and a resource both named "foo")
        can't clash.
        r;   rE   )r`   rT   r   )selfbase_keys     r#   r.   zFastMCPComponent.key   s2    ( ==++11T\/R111r"   dict[str, Any]c                z   | j         rt          | j                   ni }dt          | j                  i}| j        
| j        |d<   |                    d          x}Zt          |t                    st          d          d |                                D             }t          t          ||z            }||d<   |S )a8  Get the meta information about the component.

        Returns a dict that always includes a `fastmcp` key containing:
        - `tags`: sorted list of component tags
        - `version`: component version (only if set)

        Internal keys (prefixed with `_`) are stripped from the fastmcp namespace.
        r   Nr   r)   zmeta['fastmcp'] must be a dictc                D    i | ]\  }}|                     d           ||S )_)
startswith).0kr7   s      r#   
<dictcomp>z-FastMCPComponent.get_meta.<locals>.<dictcomp>   s?       Aall3>O>O1  r"   )r%   r-   sortedr   r   r+   r,   r=   itemsr
   r   )rb   r%   fastmcp_metaupstream_metapublic_upstreams        r#   get_metazFastMCPComponent.get_meta   s     #')3tDI%+VDI->->$?<#&*lL# "XXi000M=mT22 B @AAA !.!4!4!6!6  O  _|-KLLL&Yr"   otherobjectr<   c                    t          |           t          |          urdS t          |t          |                     sdS |                                 |                                k    S )NF)r@   r,   
model_dump)rb   rr   s     r#   __eq__zFastMCPComponent.__eq__   s\    ::T%[[((5%d,, 	5  E$4$4$6$666r"   c                
   d| j         g}| j        r|                    d| j                   |                    d| j        d| j        d| j         g           | j        j         dd	                    |           dS )	Nzname=zversion=ztitle=zdescription=ztags=(z, ))
rT   r   appendextendrU   rS   r   rR   r   join)rb   partss     r#   __repr__zFastMCPComponent.__repr__   s    &&&'< 	6LL4DL44555'''3t/33#	##	
 	
 	
 .)??DIIe,<,<????r"   c                2    t          d| j         d          )z6Removed in 3.0. Use server.enable(keys=[...]) instead.zHComponent.enable() was removed in FastMCP 3.0. Use server.enable(keys=['']) instead.NotImplementedErrorr.   rb   s    r#   enablezFastMCPComponent.enable   s-    !?(,? ? ?
 
 	
r"   c                2    t          d| j         d          )z7Removed in 3.0. Use server.disable(keys=[...]) instead.zJComponent.disable() was removed in FastMCP 3.0. Use server.disable(keys=['r   r   r   s    r#   disablezFastMCPComponent.disable   s1    !@)-@ @ @
 
 	
r"   r   c                *    |                                  S )zCreate a copy of the component.)
model_copyr   s    r#   copyzFastMCPComponent.copy   s       r"   docketr   c                    dS )zRegister this component with docket for background execution.

        No-ops if task_config.mode is "forbidden". Subclasses override to
        register their callable (self.run, self.read, self.render, or self.fn).
        Nr!   )rb   r   s     r#   register_with_docketz%FastMCPComponent.register_with_docket   s      r"   argsr   c                   K   | j                                         s%t          d| j        j         d| j         d          t          | j        j         d          )a  Schedule this component for background execution via docket.

        Subclasses override this to handle their specific calling conventions:
        - Tool: add_to_docket(docket, arguments: dict, **kwargs)
        - Resource: add_to_docket(docket, **kwargs)
        - ResourceTemplate: add_to_docket(docket, params: dict, **kwargs)
        - Prompt: add_to_docket(docket, arguments: dict | None, **kwargs)

        The **kwargs are passed through to docket.add() (e.g., key=task_key).
        zCannot add z 'z)' to docket: task execution not supportedz# does not implement add_to_docket())r\   supports_tasksRuntimeErrorrR   r   rT   r   )rb   r   r   rG   s       r#   add_to_docketzFastMCPComponent.add_to_docket   s       ..00 	0dn5 0 0 0 0 0   "~&KKK
 
 	
r"   c                    d| j         iS )z{Return span attributes for telemetry.

        Subclasses should call super() and merge their specific attributes.
        zfastmcp.component.key)r.   r   s    r#   get_span_attributesz$FastMCPComponent.get_span_attributes  s    
 (22r"   )rG   r   r'   rH   )r]   r   r'   r   )r'   r   )r'   rd   )rr   rs   r'   r<   )r'   rH   )r'   r   )r   r   r'   rH   )r   r   r   r   rG   r   r'   r   )r   r   r   __doc__rF   r    rM   r   rT   r   rU   rS   rV   r5   r   r%   r\   classmethodr`   propertyr.   rq   rv   r~   r   r   r   r   r   r   __classcell__)rR   s   @r#   rD   rD   J   s        SS "J""""      0  D     HMuKH H HG    
 F  E     $e7  K      %u^     E     MRE-M M MD     #(%"H# # #D     	BBCCC  D D D D
    [ 2 2 2 X2,   87 7 7 7@ @ @ @
 
 
 

 
 
 
! ! ! !   
 
 
 
,3 3 3 3 3 3 3 3r"   rD   N)r%   r&   r'   r   )r1   r2   r'   r3   )r7   r8   r'   r9   )!
__future__r   collections.abcr   typingr   r   r   r   r	   r
   	mcp.typesr   pydanticr   r   typing_extensionsr   r   fastmcp.server.tasks.configr   fastmcp.utilities.typesr   r   r   docket.executionr   r   r   r0   r6   rB   rD   r!   r"   r#   <module>r      s   " " " " " " $ $ $ $ $ $ K K K K K K K K K K K K K K K K       + + + + + + + + + + + + + + + + 2 2 2 2 2 2 4 4 4 4 4 4 +******GC    )5       "      ,|3 |3 |3 |3 |3' |3 |3 |3 |3 |3r"   