
    Ki/             	       R   U d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZmZmZmZ  ej        e          ZdZdZdZdZdZ	 ddlmZmZ ddlmZ dZ	 ddlmZ dZn# e$ r dZY nw xY w	 dd	lm Z  dZ!n# e$ r dZ!Y nw xY w	 dd
l"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) dZn # e$ r e*                    d           Y nw xY w	 ddl"m+Z+m,Z,m-Z-m.Z. dZn # e$ r e*                    d           Y nw xY wn # e$ r e*                    d           Y nw xY wde/fdZ0 e0            Zerese*                    d           dZ1dZ2dZ3dZ4 e5h d          Z6 ej7        dej8                  Z9dee:         de:fdZ;de<de<fdZ=de:de<de:fdZ>de<de:de?e<e:f         fdZ@d eAde<fd!ZBeCd"fd#ZD G d$ d%          ZE G d& d'          ZFi ZGee<eFf         eHd(<   daIeejJ                 eHd)<   daKee	jL                 eHd*<    e	jM                    ZNd+ ZOdWd-ePfd.ZQd/ ZRdee<e:f         fd0ZSd1e<d2e:deFfd3ZTd4e<d5e<d6ePfd7ZUd4e<d6ePfd8ZVd4e<d6ePfd9ZWd4e<d6ePfd:ZXd4e<d6ePfd;ZYd4e<fd<ZZd=e:dz  de:fd>Z[d4e<de:fd?Z\dXd@eee<                  ddfdAZ]d4e<dee:         fdBZ^dCedDe<de_e<         fdEZ`dYdCedFe/de/fdGZadHdIdJdKdLZbd4e<dMeFd2e:dee:         fdNZcdee<         fdOZdd1e<dMeFd2e:dee<         fdPZed1e<d2e:dee<         fdQZfdee<         fdRZgdee:         fdSZhdee<ee?         f         fdTZidU ZjdV ZkdS )Za  
MCP (Model Context Protocol) Client Support

Connects to external MCP servers via stdio or HTTP/StreamableHTTP transport,
discovers their tools, and registers them into the hermes-agent tool registry
so the agent can call them like any built-in tool.

Configuration is read from ~/.hermes/config.yaml under the ``mcp_servers`` key.
The ``mcp`` Python package is optional -- if not installed, this module is a
no-op and logs a debug message.

Example config::

    mcp_servers:
      filesystem:
        command: "npx"
        args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
        env: {}
        timeout: 120         # per-tool-call timeout in seconds (default: 120)
        connect_timeout: 60  # initial connection timeout (default: 60)
      github:
        command: "npx"
        args: ["-y", "@modelcontextprotocol/server-github"]
        env:
          GITHUB_PERSONAL_ACCESS_TOKEN: "ghp_..."
      remote_api:
        url: "https://my-mcp-server.example.com/mcp"
        headers:
          Authorization: "Bearer sk-..."
        timeout: 180
      analysis:
        command: "npx"
        args: ["-y", "analysis-server"]
        sampling:                    # server-initiated LLM requests
          enabled: true              # default: true
          model: "gemini-3-flash"    # override model (optional)
          max_tokens_cap: 4096       # max tokens per request
          timeout: 30                # LLM call timeout (seconds)
          max_rpm: 10                # max requests per minute
          allowed_models: []         # model whitelist (empty = all)
          max_tool_rounds: 5         # tool loop limit (0 = disable)
          log_level: "info"          # audit verbosity

Features:
    - Stdio transport (command + args) and HTTP/StreamableHTTP transport (url)
    - Automatic reconnection with exponential backoff (up to 5 retries)
    - Environment variable filtering for stdio subprocesses (security)
    - Credential stripping in error messages returned to the LLM
    - Configurable per-server timeouts for tool calls and connections
    - Thread-safe architecture with dedicated background event loop
    - Sampling support: MCP servers can request LLM completions via
      sampling/createMessage (text and tool-use responses)

Architecture:
    A dedicated background event loop (_mcp_loop) runs in a daemon thread.
    Each MCP server runs as a long-lived asyncio Task on this loop, keeping
    its transport context alive. Tool call coroutines are scheduled onto the
    loop via ``run_coroutine_threadsafe()``.

    On shutdown, each server Task is signalled to exit its ``async with``
    block, ensuring the anyio cancel-scope cleanup happens in the *same*
    Task that opened the connection (required by anyio).

Thread safety:
    _servers and _mcp_loop/_mcp_thread are accessed from both the MCP
    background thread and caller threads.  All mutations are protected by
    _lock so the code is safe regardless of GIL presence (e.g. Python 3.13+
    free-threading).
    N)AnyDictListOptionalF)ClientSessionStdioServerParameters)stdio_clientT)streamablehttp_client)streamable_http_client)CreateMessageResultCreateMessageResultWithTools	ErrorDataSamplingCapabilitySamplingToolsCapabilityTextContentToolUseContentz5MCP sampling types not available -- sampling disabled)ServerNotificationToolListChangedNotificationPromptListChangedNotificationResourceListChangedNotificationzGMCP notification types not available -- dynamic tool discovery disabledz6mcp package not installed -- MCP tool support disabledreturnc                      t           sdS 	 dt          j        t                    j        v S # t
          t          f$ r Y dS w xY w)zCheck if ClientSession accepts ``message_handler`` kwarg.

    Inspects the constructor signature for backward compatibility with older
    MCP SDK versions that don't support notification handlers.
    Fmessage_handler)_MCP_AVAILABLEinspect	signaturer   
parameters	TypeError
ValueError     +/home/ubuntu/hermes-agent/tools/mcp_tool.py_check_message_handler_supportr#      sV      u G$5m$D$D$OOOz"   uus   + A A zKMCP SDK does not support message_handler -- dynamic tool discovery disabledx   <      >   HOMELANGPATHTERMUSERSHELLLC_ALLTMPDIRz(?:ghp_[A-Za-z0-9_]{1,255}|sk-[A-Za-z0-9_]{1,255}|Bearer\s+\S+|token=[^\s&,;\"']{1,255}|key=[^\s&,;\"']{1,255}|API_KEY=[^\s&,;\"']{1,255}|password=[^\s&,;\"']{1,255}|secret=[^\s&,;\"']{1,255})user_envc                     i }t           j                                        D ](\  }}|t          v s|                    d          r|||<   )| r|                    |            |S )a  Build a filtered environment dict for stdio subprocesses.

    Only passes through safe baseline variables (PATH, HOME, etc.) and XDG_*
    variables from the current process environment, plus any variables
    explicitly specified by the user in the server config.

    This prevents accidentally leaking secrets like API keys, tokens, or
    credentials to MCP server subprocesses.
    XDG_)osenvironitems_SAFE_ENV_KEYS
startswithupdate)r/   envkeyvalues       r"   _build_safe_envr;      so     Cj&&((  
U.  CNN6$:$: CH 

8Jr!   textc                 8    t                               d|           S )zStrip credential-like patterns from error text before returning to LLM.

    Replaces tokens, keys, and other secrets with [REDACTED] to prevent
    accidental credential exposure in tool error responses.
    z
[REDACTED])_CREDENTIAL_PATTERNsubr<   s    r"   _sanitize_errorrA      s     ""<666r!   r8   	directoryc                    t          | pi           }|s|S |                    dd          }d |                    t          j                  D             }||vr|g|}|rt          j                            |          n||d<   |S )z=Prepend *directory* to env PATH if it is not already present.r)    c                     g | ]}||S r    r    ).0parts     r"   
<listcomp>z!_prepend_path.<locals>.<listcomp>   s    AAAdDATAAAr!   )dictgetsplitr2   pathsepjoin)r8   rB   updatedexistingpartss        r"   _prepend_pathrQ      s    39"ooG {{62&&HAAhnnRZ88AAAE#U#05Dbjooe,,,9GFONr!   commandc           
         t           j                            t          |                                                     }t          |pi           }t           j        |vrDd|v r|d         nd}t          j        ||          }|r|}n|dv rt           j                            t          j	        dt           j        
                    t           j                            d          d                              }t           j        
                    |dd	|          t           j        
                    t           j                            d          d
d	|          g}|D ]D}t           j                            |          r#t          j        |t           j                  r|} nEt           j                            |          }	|	rt          ||	          }||fS )zResolve a stdio MCP command against the exact subprocess environment.

    This primarily exists to make bare ``npx``/``npm``/``node`` commands work
    reliably even when MCP subprocesses run under a filtered PATH.
    r)   N)path>   npmnpxnodeHERMES_HOME~z.hermesrW   binz.local)r2   rT   
expanduserstrstriprI   sepshutilwhichgetenvrM   isfileaccessX_OKdirnamerQ   )
rR   r8   resolved_commandresolved_envpath_arg	which_hithermes_home
candidates	candidatecommand_dirs
             r"   _resolve_stdio_commandrn      s    w))#g,,*<*<*>*>??	r??L	v%%%+1\+A+A<''tL!1AAA	 	(!777',,	!27<<0B0B30G0G#S#S  K [&%9IJJRW//44hGWXXJ (  	7>>),, 9bg1N1N '0$E'//"233K @$\;??\))r!   excc                    dt           dt          t                   ffddt           dt          t                   ffd |           }|r;d| d}t          j                            |          dv r|dz  }t          |          S g } |           D ]}||vr|                    |           t          d		                    |d
d                             S )zERender nested MCP connection errors into an actionable short message.currentr   c                    t          | dd           }|r|D ]} |          }|r|c S d S t          | t                    r^t          | dd           rt          | j                  S t          j        dt          |                     }|r|                    d          S dD ]9}t          | |d           }t          |t                    r |          }|r|c S :d S )N
exceptionsfilenamez$No such file or directory: '([^']+)'   	__cause____context__)	getattr
isinstanceFileNotFoundErrorr\   rt   researchgroupBaseException)rq   nestedchildmissingmatchattr
nested_exc_find_missings          r"   r   z,_format_connect_error.<locals>._find_missing  s   ,55 	 # #'-.. #"NNN#4g011 	&w
D11 -7+,,,IEs7||TTE &{{1~~%0 	# 	#D $55J*m44 #'-
33 #"NNNtr!   c                    t          | dd           }|r'g }|D ] }|                     |                     !|S g }t          |                                           }|r|                    |           dD ]F}t          | |d           }t          |t                    r|                     |                     G|p| j        j        gS )Nrs   rv   )	ry   extendr\   r]   appendrz   r   	__class____name__)	rq   r   	flattenedr   messagesr<   r   r   _flatten_messagess	           r"   r   z0_format_connect_error.<locals>._flatten_messages'  s    ,55 	#%I ; ;  !2!25!9!9::::7||!!## 	"OOD!!!0 	? 	?D $55J*m44 ? 1 1* = =>>>7G-677r!   zmissing executable ''>   rU   rV   rW   z (ensure Node.js is installed and PATH includes its bin directory, or set mcp_servers.<name>.command to an absolute path and include that directory in mcp_servers.<name>.env.PATH)z; N   )
r   r   r\   r   r2   rT   basenamerA   r   rM   )ro   r   messagededupeditemr   r   s        @@r"   _format_connect_errorr     s(   } #      ,8= 8T#Y 8 8 8 8 8 8" mC  G (33337G$$(>>>AG
 w'''G!!#&& ! !wNN4   499WRaR[11222r!   ru   c                     	  ||           }t          |t                    rt          j        |          s|S t	          ||          S # t
          t          t          f$ r |cY S w xY w)zCoerce a config value to a numeric type, returning *default* on failure.

    Handles string values from YAML (e.g. ``"10"`` instead of ``10``),
    non-finite floats, and values below *minimum*.
    )rz   floatmathisfinitemaxr   r   OverflowError)r:   defaultcoerceminimumresults        r"   _safe_numericr   N  sw    fe$$ 	T]6-B-B 	N67###z=1   s   5A A A$#A$c                       e Zd ZdZddddZdedefdZd	efd
Z	d	e
e         fdZed	efd            Zd	ee         fdZeddedefd            Zd Zd Zd	efdZd ZdS )SamplingHandlera+  Handles sampling/createMessage requests for a single MCP server.

    Each MCPServerTask that has sampling enabled creates one SamplingHandler.
    The handler is callable and passed directly to ``ClientSession`` as
    the ``sampling_callback``.  All state (rate-limit timestamps, metrics,
    tool-loop counters) lives on the instance -- no module-level globals.

    The callback is async and runs on the MCP background event loop.  The
    sync LLM call is offloaded to a thread via ``asyncio.to_thread()`` so
    it doesn't block the event loop.
    endTurn	maxTokenstoolUse)stoplength
tool_callsserver_nameconfigc                 &   || _         t          |                    dd          dt                    | _        t          |                    dd          dt
                    | _        t          |                    dd          dt                    | _        t          |                    dd          dt          d	
          | _        |                    d          | _	        |                    dg           | _
        t          j        t          j        t          j        d}|                    t          |                    dd                                                    t          j                  | _        g | _        d	| _        d	d	d	d	d| _        d S )Nmax_rpm
   timeout   max_tokens_capi   max_tool_roundsr&   r   )r   modelallowed_models)debuginfowarning	log_levelr   )requestserrorstokens_usedtool_use_count)r   r   rJ   intr   r   r   r   r   model_overrider   loggingDEBUGINFOWARNINGr\   loweraudit_level_rate_timestamps_tool_loop_countmetrics)selfr   r   _log_levelss       r"   __init__zSamplingHandler.__init__l  sM   &$VZZ	2%>%>CHH$VZZ	2%>%>EJJ+FJJ7G,N,NPTVYZZ,JJ(!,,aa 
  
  
 %jj11$jj)92>> 'w|PWP_``&??

;//006688',
 

 .0 !$%1XYZZr!   r   c                     t          j                     }|dz
  fd| j        D             | j        dd<   t          | j                  | j        k    rdS | j                            |           dS )zASliding-window rate limiter.  Returns True if request is allowed.r%   c                      g | ]
}|k    |S r    r    )rF   twindows     r"   rH   z5SamplingHandler._check_rate_limit.<locals>.<listcomp>  s    #S#S#S!F

A


r!   NFT)timer   lenr   r   )r   nowr   s     @r"   _check_rate_limitz!SamplingHandler._check_rate_limit  sx    ikkr#S#S#S#St/D#S#S#Saaa t$%%555$$S)))tr!   c                     | j         r| j         S |rAt          |d          r1|j        r*|j        D ]"}t          |d          r|j        r	|j        c S #dS )z3Config override > server hint > None (use default).hintsnameN)r   hasattrr   r   )r   preferenceshints      r"   _resolve_modelzSamplingHandler._resolve_model  sz     	'&& 	%7;88 	%[=N 	%#) % %4(( %TY %9$$$tr!   c                     t          | d          r| j        dS t          | j        t                    r| j        n| j        g}d                    d |D                       S )z,Extract text from a ToolResultContent block.contentNrD   
c              3   D   K   | ]}t          |d           |j        V  dS )r<   Nr   r<   rF   r   s     r"   	<genexpr>z<SamplingHandler._extract_tool_result_text.<locals>.<genexpr>  s3      NNtf8M8MNNNNNNNr!   )r   r   rz   listrM   )blockr4   s     r"   _extract_tool_result_textz)SamplingHandler._extract_tool_result_text  sc     ui(( 	EM,A2!+EM4!@!@Uu}oyyNNuNNNNNNr!   c                 D   g }|j         D ]}t          |d          r|j        n(t          |j        t
                    r|j        n|j        g}d |D             }d |D             }d |D             }|D ]3}|                    d|j        |                     |          d           4|rg }	|D ]}
|	                    t          |
ddt          |	                     d	|
j        t          |
j        t                    rt          j        |
j                  nt!          |
j                  d
d           |j        |	d}d |D             }|rd                    |          |d<   |                    |           r|rt          |          dk    r@t          |d         d          r*|                    |j        |d         j        d           g }|D ]}t          |d          r|                    d|j        d           0t          |d          r;t          |d          r+|                    ddd|j         d|j         id           {t,                              dt1          |          j                   |r|                    |j        |d           |S )aL  Convert MCP SamplingMessages to OpenAI format.

        Uses ``msg.content_as_list`` (SDK helper) so single-block and
        list-of-blocks are handled uniformly.  Dispatches per block type
        with ``isinstance`` on real SDK types when available, falling back
        to duck-typing via ``hasattr`` for compatibility.
        content_as_listc                 2    g | ]}t          |d           |S )	toolUseIdr   rF   bs     r"   rH   z5SamplingHandler._convert_messages.<locals>.<listcomp>  s'    III!K1H1HIAIIIr!   c                 r    g | ]4}t          |d           t          |d          "t          |d          2|5S )r   inputr   r   r   s     r"   rH   z5SamplingHandler._convert_messages.<locals>.<listcomp>  sI    yyyqga.@.@yWQPWEXEXyahijlwaxaxyyyyr!   c                 r    g | ]4}t          |d           t          |d          rt          |d          2|5S )r   r   r   r   r   s     r"   rH   z5SamplingHandler._convert_messages.<locals>.<listcomp>  sr      E  E  EAwq+7N7N  EX_`aciXjXj  Eovwx  {B  pC  pC  Ea  E  E  Er!   tool)roletool_call_idr   idcall_functionr   	arguments)r   typer   )r   r   c                 <    g | ]}t          |d           |j        S r@   r   r   s     r"   rH   z5SamplingHandler._convert_messages.<locals>.<listcomp>  s)    SSS6@R@RSafSSSr!   r   r   ru   r   r<   r   r   r   r<   datamimeType	image_urlurlzdata:z;base64,)r   r   z5Unsupported sampling content block type: %s (skipped))r   r   r   rz   r   r   r   r   r   ry   r   r   r   rI   jsondumpsr\   r   rM   r<   r   r   loggerr   r   r   )r   paramsr   msgblockstool_results	tool_usescontent_blockstrtc_listtumsg_dict
text_partsrP   r   s                  r"   _convert_messagesz!SamplingHandler._convert_messages  sS     "? 8	N 8	NC,3C9J,K,K S(()#+t<<O3;- 
 JIvIIILyyFyyyI E  E  E  E  EN #  "$&L#==bAA! !      %N#  BNN%b$0FG0F0FGG *$&GAKBHVZA[A[)nBH)=)=)=adegemanan% %$ $     +.('!J!JSSnSSS
 @*.))J*?*?HY')))) N~&&!++q8I60R0R+OOSX.QRBSBX$Y$YZZZZE!/  "5&11 !LL&%*)M)MNNNN$UF33 	z8R8R 	!LL(3.35aU^5a5aUZU_5a5a-b* *    
 #NN W $U 4     N e(L(LMMMr!   r   codec                 P    t           rt          ||           S t          |           )z1Return ErrorData (MCP spec) or raise as fallback.)r  r   )_MCP_SAMPLING_TYPESr   	Exception)r   r  s     r"   _errorzSamplingHandler._error  s-      	9$8888   r!   c                    | j         dxx         dz  cc<   | j        dk    r%d| _        |                     d| j         d          S | xj        dz  c_        | j        | j        k    r-d| _        |                     d| j         d| j         d          S g }|j        j        D ]}|j        j        }t          |t                    rW	 t          j        |          }ni# t          j        t          f$ r( t                              d	| j        |           d
|i}Y n,w xY wt          |t"                    r|nd
t          |          i}|                    t'          d|j        |j        j        |                     t                              | j        d| j        |j        t3          t3          |dd          dd          t5          |                     t7          d||j        d          S )zEBuild a CreateMessageResultWithTools from an LLM tool_calls response.r   ru   r   z Tool loops disabled for server 'z' (max_tool_rounds=0)z%Tool loop limit exceeded for server 'z' (max z rounds)zRMCP server '%s': malformed tool_calls arguments from LLM (wrapping as raw): %.100s_rawtool_use)r   r   r   r   zEMCP server '%s' sampling response: model=%s, tokens=%s, tool_calls=%dusageNtotal_tokens?	assistantr   r   r   r   
stopReason)r   r   r   r  r   r   r   r   r   rz   r\   r   loadsJSONDecodeErrorr   r   r   rI   r   r   r   r   logr   r   ry   r   r   )r   choiceresponser  tcargsparseds          r"   _build_tool_use_resultz&SamplingHandler._build_tool_use_result  sO   %&&&!+&&& 1$$$%D!;;Z43CZZZ   	" 4#777$%D!;;78H 7 7,7 7 7  
 .+ 	 	B;(D$$$ Q,!Z--FF,j9 , , ,NN=($  
 %d^FFF, ",D$!7!7Pfc$ii=P!!.5[%	# # #     	

ShnGHgt44ncJJ	
 	
 	
 ,". 	
 
 
 	
s   C;DDc                 f   d| _         |j        j        pd}t                              | j        d| j        |j        t          t          |dd          dd                     t          dt          d	t          |          
          |j        | j                            |j        d                    S )z8Build a CreateMessageResult from a normal text response.r   rD   z6MCP server '%s' sampling response: model=%s, tokens=%sr  Nr  r  r  r<   r   r   r  )r   r   r   r   r  r   r   r   ry   r   r   rA   _STOP_REASON_MAPrJ   finish_reason)r   r  r  response_texts       r"   _build_text_resultz"SamplingHandler._build_text_result,  s     !.4"

DhnGHgt44ncJJ		
 	
 	
 #V/-2P2PQQQ.,001EyQQ	
 
 
 	
r!   c                 @    | t          t                                dS )z<Return kwargs to pass to ClientSession for sampling support.)tools)sampling_callbacksampling_capabilities)r   r   r   s    r"   session_kwargszSamplingHandler.session_kwargsA  s1     "&%7-//& & &
 
 	
r!   c           
         
K                                     sat                              d j         j                    j        dxx         dz  cc<                        d j         d j         d          S                      t          |dd                    }d	d
l	m

 |p j        pd j        r|rz j        vrqt                              d j                    j        dxx         dz  cc<                        d d j         dd                     j                             S                      |          t          |d          r%|j        r                    d	d|j        d           t%          |j         j                  dt          |d          r|j        |j        dt          |dd          }|rd |D             t                               j        d j        t1                               
 fd}	 t3          j        t3          j        |           j                   d{V }n# t2          j        $ r>  j        dxx         dz  cc<                        d j         d j         d          cY S t<          $ rQ} j        dxx         dz  cc<                        dt?          tA          |                               cY d}~S d}~ww xY wt          |dd          s3 j        dxx         dz  cc<                        d j         d          S |j!        d	         } j        d xx         dz  cc<   t          t          |d!d          d"d	          }	tE          |	tF                    r j        d#xx         |	z  cc<   |j$        d$k    r7t          |j%        d$          r"|j%        j&        r '                    ||          S  (                    ||          S )%zSampling callback invoked by the MCP SDK.

        Conforms to ``SamplingFnT`` protocol.  Returns
        ``CreateMessageResult``, ``CreateMessageResultWithTools``, or
        ``ErrorData``.
        z5MCP server '%s' sampling rate limit exceeded (%d/min)r   ru   z)Sampling rate limit exceeded for server 'z' (z requests/minute)modelPreferencesNr   )call_llmrD   z:MCP server '%s' requested model '%s' not in allowed_modelszModel 'z' not allowed for server 'z'. Allowed: , systemPromptsystemr   temperaturer*  c                     g | ]G}d t          |dd          t          |dd          pdt          t          |dd                    ddHS )r   r   rD   descriptioninputSchemaNr   r7  r   )r   r   )ry   _normalize_mcp_input_schema)rF   r   s     r"   rH   z,SamplingHandler.__call__.<locals>.<listcomp>  s}         ' '62 6 6'.q-'D'D'J&A#A}d;;' '! !	 	  r!   zFMCP server '%s' sampling request: model=%s, max_tokens=%d, messages=%dc            	      6      dpd j                   S )Nmcp)taskr   r   r5  
max_tokensr*  r   r   )r1  call_temperature
call_toolsr>  r   resolved_modelr   s   r"   
_sync_callz,SamplingHandler.__call__.<locals>._sync_call  s8    8$,!,%    r!   r?  z"Sampling LLM call timed out after zs for server 'r   zSampling LLM call failed: choicesz5LLM returned empty response (no choices) for server 'r   r  r  r   r   ))r   r   r   r   r   r   r  r   ry   agent.auxiliary_clientr1  r   r   rM   r  r   r3  insertminr   r   r5  r  r   r   asynciowait_for	to_threadr   TimeoutErrorr  rA   r\   rD  rz   r   r&  r   r   r#  r(  )r   contextr   r   server_toolsrC  r  ro   r  r  r1  r@  rA  r>  r   rB  s   `         @@@@@@r"   __call__zSamplingHandler.__call__L  st      %%'' 		NNG $,   L"""a'""";;4D<L 4 4L4 4 4   ##GF4F$M$MNN 	433333 ;$"5; 		> 		nDL_6_6_NNL .   L"""a'""";;S. S S$S S26))D<O2P2PS S   ))&116>** 	Sv/B 	SOOAV=PQQRRR )4+>??
6=)) 	2f.@.L%1 
vw55 	  &  J 	

Tnj#h--	
 	
 	
		 		 		 		 		 		 		 		 		 		 			$-!*--t|        HH # 	 	 	L"""a'""";;3T\ 3 3#/3 3 3      	 	 	L"""a'""";;H_SXX-F-FHH       	 xD11 	L"""a'""";;($( ( (   !!$Z   A%   wx$??QRSSlC(( 	8L'''<7'''  L0055 1) 1 ..vx@@@&&vx888s&   3I A
K8	K8'AK3-K83K8N)r  )r   
__module____qualname____doc__r%  r\   rI   r   boolr   r   r   staticmethodr   r   r  r   r  r#  r(  r.  rN  r    r!   r"   r   r   ]  se       
 
 !*[PYZZ[C [ [ [ [ [.4    Xc]     OC O O O \OC4: C C C CN ! ! !3 ! ! ! \!7
 7
 7
r
 
 
*
 
 
 
 
}9 }9 }9 }9 }9r!   r   c                   v    e Zd ZdZdZdefdZdefdZd Z	d Z
d	efd
Zd	efdZd Zd	efdZd	efdZd ZdS )MCPServerTaskac  Manages a single MCP server connection in a dedicated asyncio Task.

    The entire connection lifecycle (connect, discover, serve, disconnect)
    runs inside one asyncio Task so that anyio cancel-scopes created by
    the transport client are entered and exited in the same Task context.

    Supports both stdio and HTTP/StreamableHTTP transports.
    )r   sessiontool_timeout_task_ready_shutdown_event_toolsr  _config	_sampling_registered_tool_names
_auth_type_refresh_lockr   c                 ,   || _         d | _        t          | _        d | _        t          j                    | _        t          j                    | _        g | _	        d | _
        i | _        d | _        g | _        d| _        t          j                    | _        d S )NrD   )r   rV  _DEFAULT_TOOL_TIMEOUTrW  rX  rH  EventrY  rZ  r[  r  r\  r]  r^  r_  Lockr`  )r   r   s     r"   r   zMCPServerTask.__init__  sy    	&*#8-1
moo&}+/4813#!$\^^r!   r   c                     d| j         v S )z)Check if this server uses HTTP transport.r   )r\  r-  s    r"   _is_httpzMCPServerTask._is_http  s    $$r!   c                       fd}|S )a  Build a ``message_handler`` callback for ``ClientSession``.

        Dispatches on notification type.  Only ``ToolListChangedNotification``
        triggers a refresh; prompt and resource change notifications are
        logged as stubs for future work.
        c                   K   	 t          | t                    r#t                              dj        |            d S t
          rt          | t                    r| j        xt          d x?\    t          	                    dj                   
                                 d {V  d S  xt          d x%\    t                              dj                   d S  t          d x$\   t                              dj                   d S  	 d S d S d S # t          $ r$ t                              dj                   Y d S w xY w)Nz'MCP message handler (%s): exception: %sr    z9MCP server '%s': received tools/list_changed notificationz/MCP server '%s': prompts/list_changed (ignored)z1MCP server '%s': resources/list_changed (ignored)z%Error in MCP message handler for '%s')rz   r  r   r   r   _MCP_NOTIFICATION_TYPESr   rootr   r   _refresh_toolsr   r   	exception)r   r   s    r"   _handlerz5MCPServerTask._make_message_handler.<locals>._handler  s     Ugy11 LL!JDIW^___F* !z'CU/V/V !!,:8::::::"KK [ $	   #'"5"5"7"7777777777 ; =:<<<<<<"LL)Z\`\efffff =<>>>>>"LL)\^b^ghhhhh ? D! ! ! !  U U U  !H$)TTTTTTUs)   6D A*D )/D -D 	D *D?>D?r    )r   rm  s   ` r"   _make_message_handlerz#MCPServerTask._make_message_handler  s(    	U 	U 	U 	U 	U* r!   c                 v   K   ddl m} ddlm}  j        4 d{V   j                                         d{V }t          |d          r|j        ng }|	                                D ]1\  }}|
                    d          r fd|d         D             |d<   2 j        D ]}|                    |           | _        t           j          j                   _        t"                              d j        t'           j                             ddd          d{V  dS # 1 d{V swxY w Y   dS )	ua  Re-fetch tools from the server and update the registry.

        Called when the server sends ``notifications/tools/list_changed``.
        The lock prevents overlapping refreshes from rapid-fire notifications.
        After the initial ``await`` (list_tools), all mutations are synchronous
        — atomic from the event loop's perspective.
        r   registryTOOLSETSNr*  hermes-c                 &    g | ]}|j         v|S r    )r^  )rF   r   r   s     r"   rH   z0MCPServerTask._refresh_tools.<locals>.<listcomp>&  s&    "b"b"bQdFa=a=a1=a=a=ar!   z1MCP server '%s': dynamically refreshed %d tool(s))tools.registryrq  toolsetsrs  r`  rV  
list_toolsr   r*  r4   r6   r^  
deregisterr[  _register_server_toolsr   r\  r   r   r   )r   rq  rs  tools_resultnew_mcp_toolsts_nametsprefixed_names   `       r"   rk  zMCPServerTask._refresh_tools  s      	,+++++%%%%%%% 	 	 	 	 	 	 	 	!%!8!8!:!:::::::L29,2P2PXL..VXM  (~~// c c%%i00 c"b"b"b"bbk"b"b"bBwK "&!< 3 3##M2222 (DK*@	4+ +D' KKC	3t:;;  )	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   C8D((
D25D2r   c                   K   |                     d          }|                     dg           }|                     d          }|st          d| j         d          t          |          }t	          ||          \  }}t          |||r|nd          }| j        r| j                                        ni }t          rt          r| 
                                |d<   t          |          4 d{V \  }}	t          ||	fi |4 d{V }
|
                                 d{V  |
| _        |                                  d{V  | j                                         | j                                         d{V  ddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  dS # 1 d{V swxY w Y   dS )	z%Run the server using stdio transport.rR   r!  r8   MCP server 'z' has no 'command' in configN)rR   r!  r8   r   )rJ   r   r   r;   rn   r   r]  r.  ri  _MCP_MESSAGE_HANDLER_SUPPORTEDrn  r	   r   
initializerV  _discover_toolsrY  setrZ  wait)r   r   rR   r!  r/   safe_envserver_paramssampling_kwargsread_streamwrite_streamrV  s              r"   
_run_stdiozMCPServerTask._run_stdio7  s     **Y''zz&"%%::e$$ 	FtyFFF   #8,,27HEE-$.$
 
 
 >B^S$.77999QS" 	N'E 	N151K1K1M1MO-... 	2 	2 	2 	2 	2 	2 	22M;$[,RR/RR 2 2 2 2 2 2 2V]((*********&**,,,,,,,,,!!!*//1111111112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2s7   1F3	A4F=F3
F	F3F	F33
F= F=c                   K   t           st          d| j         d          |d         }t          |                    d          pi           }|                    dt
                    }d}| j        dk    rQ	 dd	lm}  || j        |          }n8# t          $ r+}t                              d
| j        |           Y d}~nd}~ww xY w| j        r| j                                        ni }t          rt          r|                                 |d<   t"          r]ddl}	d|	                    t)          |          d          d}
|r||
d<   |||
d<    |	j        di |
4 d{V }t-          ||          4 d{V \  }}}t/          ||fi |4 d{V }|                                 d{V  || _        |                                  d{V  | j                                         | j                                         d{V  ddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  dS # 1 d{V swxY w Y   dS |t)          |          d}|||d<   t?          |fi |4 d{V \  }}}t/          ||fi |4 d{V }|                                 d{V  || _        |                                  d{V  | j                                         | j                                         d{V  ddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  dS # 1 d{V swxY w Y   dS )z3Run the server using HTTP/StreamableHTTP transport.r  zw' requires HTTP transport but mcp.client.streamable_http is not available. Upgrade the mcp package to get HTTP support.r   headersconnect_timeoutNoauthr   )build_oauth_authz#MCP OAuth setup failed for '%s': %sr   Tg     r@)read)follow_redirectsr   auth)http_client)r  r   r    ) _MCP_HTTP_AVAILABLEImportErrorr   rI   rJ   _DEFAULT_CONNECT_TIMEOUTr_  tools.mcp_oauthr  r  r   r   r]  r.  ri  r  rn  _MCP_NEW_HTTPhttpxTimeoutr   AsyncClientr   r   r  rV  r  rY  r  rZ  r  r
   )r   r   r   r  r  _oauth_authr  ro   r  r  client_kwargsr  r  r  _get_session_idrV  _http_kwargss                    r"   	_run_httpzMCPServerTask._run_httpU  s     " 	?ty ? ? ?   Umvzz),,233 **%68PQQ ?g%%V<<<<<<..ty#>> V V VDdiQTUUUUUUUUV >B^S$.77999QS" 	N'E 	N151K1K1M1MO-. *	6 LLL %) ==)?)?e=LL# #M  3+2i(&(3f% )u(99=99 	: 	: 	: 	: 	: 	: 	:[1#;OOO : : : : : : : T,[,ZZ/ZZ : : : : : : :^e%00222222222'."22444444444)))"277999999999: : : : : : : : : : : : : : : : : : : : : : : : : : :: : : : : : : : : : : : : : : : : : : : : : : : : : :	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: # 11" "L &'2V$,SAALAA 6 6 6 6 6 6 6 F\?(lVVoVV 6 6 6 6 6 6 6Za!,,.........#*DL..000000000KOO%%%.335555555556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 66 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6s   7B 
C!B??C"I :H<A4HH<
H##H<&H#'H<*I <
I	I 	I	
I  
I*-I*M"8A4L>,M">
M	M"M	M""
M,/M,c                    K   | j         dS | j                                          d{V }t          |d          r|j        ng | _        dS )z*Discover tools from the connected session.Nr*  )rV  rx  r   r*  r[  )r   r{  s     r"   r  zMCPServerTask._discover_tools  sb      <F!\4466666666 |W--L 	r!   c           	        K   || _         |                    dt                    | _        |                    d          pd                                                                | _        |                    di           }|                    dd          r"t          rt          | j	        |          | _
        nd| _
        d|v r$d	|v r t                              d
| j	                   d}d}	 	 |                                 r|                     |           d{V  n|                     |           d{V  	 d| _        dS # t"          $ rc}d| _        | j                                        s-|| _        | j                                         Y d}~d| _        dS | j                                        r.t                              d| j	        |           Y d}~d| _        dS |dz  }|t0          k    r4t                              d| j	        t0          |           Y d}~d| _        dS t                              d| j	        |t0          ||           t3          j        |           d{V  t7          |dz  t8                    }| j                                        rY d}~d| _        dS Y d}~nd}~ww xY w	 d| _        n# d| _        w xY w)zLong-lived coroutine: connect, discover tools, wait, disconnect.

        Includes automatic reconnection with exponential backoff if the
        connection drops unexpectedly (unless shutdown was requested).
        r   r  rD   samplingenabledTNr   rR   zMCP server '%s' has both 'url' and 'command' in config. Using HTTP transport ('url'). Remove 'command' to silence this warning.r   g      ?z0MCP server '%s' disconnected during shutdown: %sru   zDMCP server '%s' failed after %d reconnection attempts, giving up: %szJMCP server '%s' connection lost (attempt %d/%d), reconnecting in %.0fs: %s   )r\  rJ   rb  rW  r   r]   r_  r  r   r   r]  r   r   rf  r  r  rV  r  rY  is_setr  r  rZ  r   _MAX_RECONNECT_RETRIESrH  sleeprG  _MAX_BACKOFF_SECONDS)r   r   sampling_configretriesbackoffro   s         r"   runzMCPServerTask.run  sK      "JJy2GHH!::f--3::<<BBDD !**Z44y$// 	"4G 	",TYHHDNN!DN F??yF22NN  		   /	$.$==?? 2..0000000000//&111111111P  $O  % % %# {))++ "%DKKOO%%%FFF@  $; '..00 LLJ	3   FFF0  $- 1333NN(	#93  
 FFF  $ 0Iw(>S	   mG,,,,,,,,,gk+?@@ '..00 FFF#    I%H  $t####_/	$sW   -AE J0A J+J< :J+J< !7J+J< %A4J+J< &J< +J00J< <	Kc                    K   t          j        |                     |                    | _        | j                                         d{V  | j        r| j        dS )z<Create the background Task and wait until ready (or failed).N)rH  ensure_futurer  rX  rY  r  r  )r   r   s     r"   startzMCPServerTask.start  sf      *488F+;+;<<
k         ; 	+	 	r!   c                   K   | j                                          | j        r| j                                        s	 t	          j        | j        d           d{V  nr# t          j        $ r` t                              d| j	                   | j        
                                 	 | j         d{V  n# t          j        $ r Y nw xY wY nw xY wd| _        dS )z=Signal the Task to exit and wait for clean resource teardown.r   r?  Nz3MCP server '%s' shutdown timed out, cancelling task)rZ  r  rX  donerH  rI  rK  r   r   r   cancelCancelledErrorrV  r-  s    r"   shutdownzMCPServerTask.shutdown  s       """: 	djoo// 	&tz2>>>>>>>>>>>' 	 	 	II   
!!###*$$$$$$$$-   D	 s6   !A AC(B65C6CCCCCN)r   rO  rP  rQ  	__slots__r\   r   rR  rf  rn  rk  rI   r  r  r  r  r  r  r    r!   r"   rU  rU    s        I,S , , , ,%$ % % % %  <" " "H2t 2 2 2 2<D6d D6 D6 D6 D6L	
 	
 	
K$ K$ K$ K$ K$Z$        r!   rU  _servers	_mcp_loop_mcp_threadc                  <   t           5  t          't                                          r	 ddd           dS t          j                    at          j        t          j        dd          at          	                                 ddd           dS # 1 swxY w Y   dS )z>Start the background event loop thread if not already running.Nzmcp-event-loopT)targetr   daemon)
_lockr  
is_runningrH  new_event_loop	threadingThreadrun_foreverr  r  r    r!   r"   _ensure_mcp_loopr    s     
 	 	 Y%9%9%;%; 	 	 	 	 	 	 	 	 *,,	&(!
 
 

 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   "BABBBr   r   c                     t           5  t          }ddd           n# 1 swxY w Y   ||                                st          d          t	          j        | |          }|                    |          S )z@Schedule a coroutine on the MCP event loop and block until done.NzMCP event loop is not runningr?  )r  r  r  RuntimeErrorrH  run_coroutine_threadsafer   )coror   loopfutures       r"   _run_on_mcp_loopr  *  s    	                |4??,,|:;;;-dD99F===)))s     c                 
   t          | t                    rddl}d } |j        d||           S t          | t                    rd |                                 D             S t          | t                    rd | D             S | S )z@Recursively resolve ``${VAR}`` placeholders from ``os.environ``.r   Nc                     t           j                            |                     d          |                     d                    S )Nru   r   )r2   r3   rJ   r~   )ms    r"   _replacez'_interpolate_env_vars.<locals>._replace<  s,    :>>!''!**aggajj999r!   z\$\{([^}]+)\}c                 4    i | ]\  }}|t          |          S r    _interpolate_env_varsrF   kvs      r"   
<dictcomp>z)_interpolate_env_vars.<locals>.<dictcomp>@  s'    FFF1(++FFFr!   c                 ,    g | ]}t          |          S r    r  )rF   r  s     r"   rH   z)_interpolate_env_vars.<locals>.<listcomp>B  s!    888Q%a((888r!   )rz   r\   r|   r?   rI   r4   r   )r:   r|   r  s      r"   r  r  8  s    % 9				: 	: 	:rv&%888% GFFFFFF% 988%8888Lr!   c                  h   	 ddl m}   |             }|                    d          }|rt          |t                    si S 	 ddlm}  |             n# t          $ r Y nw xY wd |                                D             S # t          $ r'}t          
                    d|           i cY d}~S d}~ww xY w)a  Read ``mcp_servers`` from the Hermes config file.

    Returns a dict of ``{server_name: server_config}`` or empty dict.
    Server config can contain either ``command``/``args``/``env`` for stdio
    transport or ``url``/``headers`` for HTTP transport, plus optional
    ``timeout``, ``connect_timeout``, and ``auth`` overrides.

    ``${ENV_VAR}`` placeholders in string values are resolved from
    ``os.environ`` (which includes ``~/.hermes/.env`` loaded at startup).
    r   )load_configmcp_servers)load_hermes_dotenvc                 4    i | ]\  }}|t          |          S r    r  )rF   r   cfgs      r"   r  z$_load_mcp_config.<locals>.<dictcomp>]  s'    RRRYT3+C00RRRr!   zFailed to load MCP config: %sN)hermes_cli.configr  rJ   rz   rI   hermes_cli.env_loaderr  r  r4   r   r   )r  r   serversr  ro   s        r"   _load_mcp_configr  F  s    111111**]++ 	j$77 	I	@@@@@@     	 	 	D	RR'--//RRRR   4c:::						sA   =B  A B  
AB  A B   
B1
B,&B1,B1r   r   c                 ^   K   t          |           }|                    |           d{V  |S )a  Create an MCPServerTask, start it, and return when ready.

    The server Task keeps the connection alive in the background.
    Call ``server.shutdown()`` (on the same event loop) to tear it down.

    Raises:
        ValueError: if required config keys are missing.
        ImportError: if HTTP transport is needed but not available.
        Exception: on connection or initialization failure.
    N)rU  r  )r   r   servers      r"   _connect_serverr  g  s>       4  F
,,v

Mr!   r   	tool_namerW  c                 8     dt           dt          f fd}|S )zReturn a sync handler that calls an MCP tool via the background loop.

    The handler conforms to the registry's dispatch interface:
    ``handler(args_dict, **kwargs) -> str``
    r!  r   c           
          t           5  t                                        d d d            n# 1 swxY w Y   rj        st	          j        dd di          S  fd}	 t           |                      S # t          $ rb}t          	                    d|           t	          j        dt          dt          |          j         d|           i          cY d }~S d }~ww xY w)	Nerrorr  ' is not connectedc                    K   j                                        d {V } | j        rMd}| j        pg D ]}t	          |d          r
||j        z  }t          j        dt          |pd          i          S g }| j        pg D ],}t	          |d          r|	                    |j                   -t          j        d|rd
                    |          ndi          S )Nr   rD   r<   r  zMCP tool returned an errorr   r   )rV  	call_toolisErrorr   r   r<   r   r   rA   r   rM   )r   
error_textr   rP   r!  r  r  s       r"   _callz3_make_tool_handler.<locals>._handler.<locals>._call  s     !>33I3NNNNNNNNF~ 	
$n2 1 1Euf-- 1"ej0
z_"B&B #     "E ..B - -5&)) -LL,,,:xU)J5)9)9)9KLLLr!   r?  zMCP tool %s/%s call failed: %sMCP call failed: : r  r  rJ   rV  r   r   r  r  r   r  rA   r   r   )r!  kwargsr  ro   r  r   r  rW  s   `   @r"   rm  z$_make_tool_handler.<locals>._handler  s    	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	V^ 	:GGGG   	M 	M 	M 	M 	M 	M 	M*	#EEGG\BBBB 		 		 		LL0Y   :CS		(:CCcCC         		s+   266(B 
C-AC("C-(C-rI   r\   )r   r  rW  rm  s   ``` r"   _make_tool_handlerr  {  sC    (t (# ( ( ( ( ( ( ( (T Or!   c                 4     dt           dt          f fd}|S )z>Return a sync handler that lists resources from an MCP server.r!  r   c           
         t           5  t                                        d d d            n# 1 swxY w Y   rj        st	          j        dd di          S fd}	 t           |                      S # t          $ ra}t          	                    d|           t	          j        dt          dt          |          j         d|           i          cY d }~S d }~ww xY w)	Nr  r  r  c                    K   j                                          d {V } g }t          | d          r| j        ng D ]}i }t          |d          rt	          |j                  |d<   t          |d          r
|j        |d<   t          |d          r|j        r
|j        |d<   t          |d          r|j        r
|j        |d<   |	                    |           t          j        d|i          S )N	resourcesurir   r7  r   )rV  list_resourcesr   r  r\   r  r   r7  r   r   r   r   )r   r  rentryr  s       r"   r  z=_make_list_resources_handler.<locals>._handler.<locals>._call  s     !>88::::::::FI*1&+*F*FNf&&B 
( 
(1e$$ .#&qu::E%L1f%% +$%FE&M1m,, 9 9+,=E-(1j)) 3aj 3()
E*%  '''':{I6777r!   r?  z MCP %s/list_resources failed: %sr  r  r  r!  r  r  ro   r  r   rW  s       @r"   rm  z._make_list_resources_handler.<locals>._handler  s}    	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	V^ 	:GGGG   	8 	8 	8 	8 	8 
	#EEGG\BBBB 	 	 	LL2K   :CS		(:CCcCC         		+   155%A> >
C)AC$C)$C)r  r   rW  rm  s   `` r"   _make_list_resources_handlerr    =    "t "# " " " " " " "H Or!   c                 4     dt           dt          f fd}|S )zFReturn a sync handler that reads a resource by URI from an MCP server.r!  r   c           
      6   t           5  t                                        d d d            n# 1 swxY w Y   rj        st	          j        dd di          S |                     d          st	          j        ddi          S fd}	 t           |                      S # t          $ ra}t          	                    d|           t	          j        dt          d	t          |          j         d
|           i          cY d }~S d }~ww xY w)Nr  r  r  r  z Missing required parameter 'uri'c                    K   j                                        d {V } g }t          | d          r| j        ng }|D ]h}t          |d          r|                    |j                   -t          |d          r+|                    dt          |j                   d           it          j	        d|rd
                    |          ndi          S )	Ncontentsr<   blobz[binary data, z bytes]r   r   rD   )rV  read_resourcer   r  r   r<   r   r  r   r   rM   )r   rP   r  r   r  r  s       r"   r  z<_make_read_resource_handler.<locals>._handler.<locals>._call  s      !>77<<<<<<<<F!E*1&**E*EMv2H! L L5&)) LLL,,,,UF++ LLL!J#ej//!J!J!JKKK:xU)J5)9)9)9KLLLr!   r?  zMCP %s/read_resource failed: %sr  r  r  )r!  r  r  ro   r  r  r   rW  s       @@r"   rm  z-_make_read_resource_handler.<locals>._handler  s    	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	V^ 	:GGGG    hhuoo 	M:w(JKLLL
	M 
	M 
	M 
	M 
	M 
	M
	#EEGG\BBBB 	 	 	LL1;   :CS		(:CCcCC         		s+   266B- -
D7ADDDr  r  s   `` r"   _make_read_resource_handlerr
    r  r!   c                 4     dt           dt          f fd}|S )z<Return a sync handler that lists prompts from an MCP server.r!  r   c           
         t           5  t                                        d d d            n# 1 swxY w Y   rj        st	          j        dd di          S fd}	 t           |                      S # t          $ ra}t          	                    d|           t	          j        dt          dt          |          j         d|           i          cY d }~S d }~ww xY w)	Nr  r  r  c                    K   j                                          d {V } g }t          | d          r| j        ng D ]}i }t          |d          r
|j        |d<   t          |d          r|j        r
|j        |d<   t          |d          r|j        rd |j        D             |d<   |                    |           t          j	        d|i          S )Npromptsr   r7  r   c                     g | ]H}d |j         it          |d          r|j        r	d|j        ini t          |d          r	d|j        ini IS )r   r7  required)r   r   r7  r  )rF   as     r"   rH   zO_make_list_prompts_handler.<locals>._handler.<locals>._call.<locals>.<listcomp>  s     * * * 	 #AFAHMAZAZt_`_ltq}==rt <C1j;Q;QY
AJ77WY* * *r!   )
rV  list_promptsr   r  r   r7  r   r   r   r   )r   r  pr  r  s       r"   r  z;_make_list_prompts_handler.<locals>._handler.<locals>._call  s     !>6688888888FG(/	(B(BJfnn & &1f%% +$%FE&M1m,, 9 9+,=E-(1k** q{ * * "#* * *E+& u%%%%:y'2333r!   r?  zMCP %s/list_prompts failed: %sr  r  r  r  s       @r"   rm  z,_make_list_prompts_handler.<locals>._handler  s}    	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	V^ 	:GGGG   	4 	4 	4 	4 	4*
	#EEGG\BBBB 	 	 	LL0+s   :CS		(:CCcCC         		r   r  r  s   `` r"   _make_list_prompts_handlerr    s=    't '# ' ' ' ' ' ' 'R Or!   c                 4     dt           dt          f fd}|S )zDReturn a sync handler that gets a prompt by name from an MCP server.r!  r   c           
      f   t           5  t                                        d d d            n# 1 swxY w Y   rj        st	          j        dd di          S |                     d          st	          j        ddi          S |                     di           fd}	 t           |                      S # t          $ ra}t          	                    d	|           t	          j        dt          d
t          |          j         d|           i          cY d }~S d }~ww xY w)Nr  r  r  r   z!Missing required parameter 'name'r   c                    K   j                                        d {V } g }t          | d          r| j        ng D ]}i }t          |d          r
|j        |d<   t          |d          rO|j        }t          |d          r|j        |d<   n-t          |t                    r||d<   nt          |          |d<   |	                    |           d|i}t          | d          r| j
        r
| j
        |d<   t          j        |          S )Nr  r   r   r   r<   r7  )rV  
get_promptr   r   r   r   r<   rz   r\   r   r7  r   r   )	r   r   r  r  r   respr   r   r  s	         r"   r  z9_make_get_prompt_handler.<locals>._handler.<locals>._callB  s?     !>44TY4OOOOOOOOFH+26:+F+FNB ' '3'' -$'HE&M3	** 8!kGw// 8+2<i((#GS11 8+2i((+.w<<i(&&&&)Dv}-- 9&2D 9&,&8]#:d###r!   r?  zMCP %s/get_prompt failed: %sr  r  r  )	r!  r  r  ro   r   r   r  r   rW  s	       @@@r"   rm  z*_make_get_prompt_handler.<locals>._handler5  s    	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	V^ 	:GGGG    xx 	N:w(KLMMMHH["--		$ 	$ 	$ 	$ 	$ 	$ 	$,
	#EEGG\BBBB 	 	 	LL.S   :CS		(:CCcCC         		s+   377,C 
D0AD+%D0+D0r  r  s   `` r"   _make_get_prompt_handlerr  2  s=    -t -# - - - - - - -^ Or!   c                 "     dt           f fd}|S )zBReturn a check function that verifies the MCP connection is alive.r   c                      t           5  t                                        } d d d            n# 1 swxY w Y   | d uo| j        d uS N)r  r  rJ   rV  )r  r   s    r"   _checkz_make_check_fn.<locals>._checkj  s     	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/T!@fnD&@@s   044)rR  )r   r  s   ` r"   _make_check_fnr  g  s6    AD A A A A A A
 Mr!   schemac                 ^    | sdi dS |                      d          dk    rd| vri | di iS | S )z?Normalize MCP input schemas for LLM tool-calling compatibility.objectr   
propertiesr   r$  )rJ   )r   s    r"   r:  r:  v  sS     4 333zz&X%%,f*D*D+&+,+++Mr!   c                    |j                             dd                              dd          }|                     dd                              dd          }d| d| }||j        pd|j          d|  t          |j                  dS )aS  Convert an MCP tool listing to the Hermes registry schema format.

    Args:
        server_name: The logical server name for prefixing.
        mcp_tool:    An MCP ``Tool`` object with ``.name``, ``.description``,
                     and ``.inputSchema``.

    Returns:
        A dict suitable for ``registry.register(schema=...)``.
    -_.mcp_z	MCP tool z from r9  )r   replacer7  r:  r8  )r   mcp_toolsafe_tool_namesafe_server_namer  s        r"   _convert_mcp_schemar.    s     ]**344<<S#FFN"**344<<S#FF>+>>n>>M+]/]8=/]/]P[/]/]1(2FGG  r!   server_namesc                   
 ddl m} | -t          t                                                                } t                      }g }| D ]}d|                    dd                              dd           d
t          
fd|D                       }|                    |           |	                    |          }|rRt          |	                    d	d
                                        d          st                              d|           d| d|g d||<   |                                D ]E\  }}|                    d          s|D ]'}	|	|d         vr|d                             |	           (FdS )a  Expose each MCP server as a standalone toolset and inject into hermes-* sets.

    Creates a real toolset entry in TOOLSETS for each server name (e.g.
    TOOLSETS["github"] = {"tools": ["mcp_github_list_files", ...]}). This
    makes raw server names resolvable in platform_toolsets overrides.

    Also injects all MCP tools into hermes-* umbrella toolsets for the
    default behavior.

    Skips server names that collide with built-in toolsets.
    r   rr  Nr)  r&  r'  r(  c              3   F   K   | ]}|                               |V  d S r  )r6   )rF   r   safe_prefixs     r"   r   z%_sync_mcp_toolsets.<locals>.<genexpr>  sH       
 
1<<#<#<

 
 
 
 
 
r!   r7  rD   r  uM   Skipping MCP toolset alias '%s' — a built-in toolset already uses that namez' tools)r7  r*  includesrt  r*  )rw  rs  r   r  keys_existing_tool_namesr*  sortedr   rJ   r\   r6   r   r   r4   r   )r/  rs  rO   all_mcp_toolsr   rM  existing_tsr}  r~  r  r2  s             @r"   _sync_mcp_toolsetsr9    s    "!!!!!,..335566#%%H!M# 
 
O[00c::BB3LLOOO 
 
 
 

 
 
 
 
 	\*** ll;// 	s;??="#E#EFFQQR`aa 	NN_    ?+>>>!!
 !
  ~~'' . .!!),, 	& 	. 	.I7++7""9---	.. .r!   c           
         |                      dd                               dd          }d| dd|  ddi d	d
ddd| dd|  ddddddidgdd
ddd| dd|  ddi d	d
ddd| dd|  ddddddddddgdd
ddgS )zBuild schemas for the MCP utility tools (resources & prompts).

    Returns a list of (schema, handler_factory_name) tuples encoded as dicts
    with keys: schema, handler_key.
    r&  r'  r(  r)  _list_resourcesz*List available resources from MCP server 'r   r"  r#  r9  r  )r   handler_key_read_resourcez(Read a resource by URI from MCP server 'r  stringzURI of the resource to read)r   r7  )r   r$  r  r	  _list_promptsz(List available prompts from MCP server 'r  _get_promptz&Get a prompt by name from MCP server 'zName of the prompt to retrievez(Optional arguments to pass to the promptr   r   r  )r*  )r   	safe_names     r"   _build_utility_schemasrB    s    ##C--55c3??I :y999ZKZZZ$"$   ,
	
 
	
 9y888X+XXX$$,+H   # "'	 	  +	
 	
& 8y777X+XXX$"$   *
	
 
	
 6y555VVVV$ %-+K! !
 %-+U& &	# 	# "(  $ ('	
 	
Q= =r!   r:   labelc                    | t                      S t          | t                    r| hS t          | t          t          t           f          rd | D             S t
                              d||            t                      S )z8Normalize include/exclude config to a set of tool names.Nc                 ,    h | ]}t          |          S r    )r\   r   s     r"   	<setcomp>z)_normalize_name_filter.<locals>.<setcomp>  s    ,,,dD		,,,r!   z>MCP config %s must be a string or list of strings; ignoring %r)r  rz   r\   r   tupler   r   )r:   rC  s     r"   _normalize_name_filterrH    sx    }uu% w%$s+,, -,,e,,,,
NNSUZ\abbb55Lr!   r   c                    | |S t          | t                    r| S t          | t                    r2|                                                                 }|dv rdS |dv rdS t
                              d| |           |S )z2Parse a bool-like config value with safe fallback.N>   1onyestrueT>   0noofffalseFzAMCP config expected a boolean-ish value, got %r; using default=%s)rz   rR  r\   r]   r   r   r   )r:   r   lowereds      r"   _parse_boolishrS    s    }% % ++--%%''00041115
NNVX]_fgggNr!   r  r	  r  r  r  r	  r  r  r  c                 2   |                     d          pi }t          |                     d          d          }t          |                     d          d          }g }t          |           D ]}|d         }|dv r|st                              d| |           -|d	v r|st                              d
| |           Pt
          |         }	t          |j        |	          st                              d| ||	           |                    |           |S )z?Select utility schemas based on config and server capabilities.r*  r  Tr   r  r<  >   r	  r  z;MCP server '%s': skipping utility '%s' (resources disabled)>   r  r  z9MCP server '%s': skipping utility '%s' (prompts disabled)z9MCP server '%s': skipping utility '%s' (session lacks %s))	rJ   rS  rB  r   r   _UTILITY_CAPABILITY_METHODSr   rV  r   )
r   r  r   tools_filterresources_enabledprompts_enabledselectedr  r<  required_methods
             r"   _select_utility_schemasr]  5  s9   ::g&&,"L&|'7'7'D'DdSSS$\%5%5i%@%@$OOOOH'44  M*===FW=LLVXcepqqq8888LLTVacnooo5kBv~77 	LLK	   Or!   c                     g } t                                           D ]j\  }}t          |d          r|                     |j                   0|j        D ]2}t          |j        |          }|                     |d                    3k| S )z6Return tool names for all currently connected servers.r^  r   )	r  r4   r   r   r^  r[  r.  r   r   )names_snamer  r+  r   s        r"   r5  r5  R  s    E"..** ) )6344 	LL6777 	) 	)H(h??FLL((((	) Lr!   c                    ddl m} ddlm}m} g }d|  }|                    d          pi }t          |                    d          d|  d          t          |                    d	          d|  d
          dt          dt          ffd}	|j	        D ]}
 |	|
j
                  s"t                              d| |
j
                   4t          | |
          }|d         }|                    |          }|r9|                    d          s$t                              d| |
j
        ||           |                    |||t%          | |
j
        |j                  t)          |           d|d                    |                    |           t,          t.          t0          t2          d}t)          |           }t5          | ||          D ]}|d         }|d         } ||         | |j                  }|d         }|                    |          }|r3|                    d          st                              d| ||           {|                    |||||d|d                    |                    |           |rk ||d|  d|           |                                D ]D\  }}|                    d          r*|D ]'}||d         vr|d                             |           (E|S )aC  Register tools from an already-connected server into the registry.

    Handles include/exclude filtering, utility tools, toolset creation,
    and hermes-* umbrella toolset injection.

    Used by both initial discovery and dynamic refresh (list_changed).

    Returns:
        List of registered prefixed tool names.
    r   rp  )create_custom_toolsetrs  zmcp-r*  includezmcp_servers.z.tools.includeexcludez.tools.excluder  r   c                      r| v S r| vS dS )NTr    )r  exclude_setinclude_sets    r"   _should_registerz0_register_server_tools.<locals>._should_registerz  s.     	,++ 	0K//tr!   z8MCP server '%s': skipping tool '%s' (filtered by config)r   us   MCP server '%s': tool '%s' (→ '%s') collides with built-in tool in toolset '%s' — skipping to preserve built-inFr7  )r   toolsetr   handlercheck_fnis_asyncr7  rT  r   r<  up   MCP server '%s': utility tool '%s' collides with built-in tool in toolset '%s' — skipping to preserve built-inzMCP tools from z server)r   r7  r*  rt  )rv  rq  rw  rb  rs  rJ   rH  r\   rR  r[  r   r   r   r.  get_toolset_for_toolr6   r   registerr  rW  r  r   r  r
  r  r  r]  r4   )r   r  r   rq  rb  rs  registered_namestoolset_namerX  rh  r+  r   tool_name_prefixedexisting_toolset_handler_factoriesrk  r  r<  rj  	util_namer}  r~  r  rf  rg  s                          @@r"   rz  rz  _  s    ('''''88888888"$ $==L ::g&&,"L()9)9))D)DFiUYFiFiFijjK()9)9))D)DFiUYFiFiFijjKC D        M 4 4.. 	LLSUY[c[hiii$T844#F^ $889KLL 	$4$?$?$G$G 	NNIhm%79I  
 # &tX]F<OPP#D))}- 	 	
 	
 	
 	 23333
 742.	  d##H(vv>> + +xM*1$[1$8KLL6N	 $88CC 	$4$?$?$G$G 	NNIi!1  
  }- 	 	
 	
 	
 		****  67$777"	
 	
 	
 	
 $>>++ 	6 	6KGR!!),, 6!1 6 6I 7337**9555r!   c           	        K   |                     dt                    }t          j        t	          | |          |           d{V }t
          5  |t          | <   ddd           n# 1 swxY w Y   t          | ||          }t          |          |_	        d|v rdnd}t                              d| |t          |          d                    |                     |S )	zsConnect to a single MCP server, discover tools, and register them.

    Returns list of registered tool names.
    r  r?  Nr   HTTPstdioz/MCP server '%s' (%s): registered %d tool(s): %sr2  )rJ   r  rH  rI  r  r  r  rz  r   r^  r   r   r   rM   )r   r   r  r  ro  transport_types         r"   _discover_and_register_serverry    s?     
 jj!24LMMO#f%%        F 
                                  .dFFCC$()9$:$:F!$VVGN
KK9nc"233		"##  
 s   A&&A*-A*c                  j   t           st                              d           g S t                      } | st                              d           g S t          5  d |                                 D             ddd           n# 1 swxY w Y   s<t          t          |                                                      t                      S t                       g ddt          dt          dt          t                   fd	fd
}t           |            d           t          t          |                                                      t                    }|z
  }sr;dt                     d| d}r	|d dz  }t                              |           t                      S )a  Entry point: load config, connect to MCP servers, register tools.

    Called from ``model_tools._discover_tools()``. Safe to call even when
    the ``mcp`` package is not installed (returns empty list).

    Idempotent for already-connected servers. If some servers failed on a
    previous call, only the missing ones are retried.

    Returns:
        List of all registered MCP tool names.
    z4MCP SDK not available -- skipping MCP tool discoveryzNo MCP servers configuredc                 v    i | ]6\  }}|t           vt          |                    d d          d          3||7S r  TrV  )r  rS  rJ   r  s      r"   r  z&discover_mcp_tools.<locals>.<dictcomp>  sT     
 
 
1  ^AEE)T4J4JTX%Y%Y%Y  q   r!   Nr   r   r  r   c                 2   K   t          | |           d{V S )z@Connect to a single server and return its registered tool names.N)ry  )r   r  s     r"   _discover_onez)discover_mcp_tools.<locals>._discover_one  s(      24=========r!   c            	      0  K   t                                                    } t          j        fd                                D             ddi d {V }t          | |          D ]\  }}t          |t                    radz                      |i                               d          }t          
                    d||rd| dnd	t          |                     {t          |t                     r                    |           dz  d S )
Nc              3   6   K   | ]\  }} ||          V  d S r  r    )rF   r   r  r~  s      r"   r   z<discover_mcp_tools.<locals>._discover_all.<locals>.<genexpr>   s3      LL94mmD#&&LLLLLLr!   return_exceptionsTru   rR   z*Failed to connect to MCP server '%s'%s: %sz
 (command=)rD   )r   r4  rH  gatherr4   ziprz   r  rJ   r   r   r   r   )	r/  resultsr   r   rR   r~  	all_toolsfailed_countnew_serverss	        r"   _discover_allz)discover_mcp_tools.<locals>._discover_all  s\     K,,..//LLLL8I8I8K8KLLL
"
 
 
 
 
 
 
 
  g66 	" 	"LD&&),, "!%//$3377	BB@/6>+++++B)&11	    FD)) "  ((((!	" 	"r!   r$   r?  z  MCP: z tool(s) from z
 server(s)z (z failed))r   r   r   r  r  r4   r9  r   r4  r5  r  r\   rI   r   r  r   r   )	r  r  total_servers
ok_serverssummaryr~  r  r  r  s	        @@@@r"   discover_mcp_toolsr    s4     KLLL	  G 0111	 
 
 

 

 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
  &4//000#%%% IL># >D >T#Y > > > >" " " " " " " "2 ]]__c2222tGLLNN++,,, $$M-J L PC	NNPP*PPP 	32L2222GG  !!!s   BB
B
c                  4   g } t                      }|s| S t          5  t          t                    }ddd           n# 1 swxY w Y   |                                D ]\  }}d|v rdnd}|                    |          }|r}|j        v||t          |d          rt          |j	                  nt          |j
                  dd}|j        rt          |j        j                  |d<   |                     |           |                     ||d	d
d           | S )zReturn status of all configured MCP servers for banner display.

    Returns a list of dicts with keys: name, transport, tools, connected.
    Includes both successfully connected servers and configured-but-failed ones.
    Nr   httprw  r^  T)r   	transportr*  	connectedr  r   F)r  r  rI   r  r4   rJ   rV  r   r   r^  r[  r]  r   r   )r   
configuredactive_serversr   r  r  r  r  s           r"   get_mcp_statusr  E  s    F "##J 	 ( (h( ( ( ( ( ( ( ( ( ( ( ( ( ( (  %%''  	c#sllFF	##D)) 	fn0&?FvOg?h?h  AV:;;;nqrxr  oA  oA!	 E  C$()9)A$B$Bj!MM%    MM&"	      Ms   =AAc                     t           si S t                      } | si S d |                                 D             si S t                       i g fd}	 t	           |            d           n2# t
          $ r%}t                              d|           Y d}~nd}~ww xY wt                       n# t                       w xY wS )u  Temporarily connect to configured MCP servers and list their tools.

    Designed for ``hermes tools`` interactive configuration — connects to each
    enabled server, grabs tool names and descriptions, then disconnects.
    Does NOT register tools in the Hermes registry.

    Returns:
        Dict mapping server name to list of (tool_name, description) tuples.
        Servers that fail to connect are omitted from the result.
    c                 d    i | ]-\  }}t          |                    d d          d          *||.S r|  )rS  rJ   r  s      r"   r  z*probe_mcp_server_tools.<locals>.<dictcomp>  sO       A!%%	400$???	1  r!   c            	        K   t          
                                          } g }
                                D ]W\  }}|                    dt                    }|                    t          j        t          ||          |                     Xt          j	        |ddi d {V }t          | |          D ]\  }}t          |t                    rt                              d||           7                    |           g }|j        D ]1}t!          |dd          pd}	|                    |j        |	f           2||<   t          j	        d D             ddi d {V  d S )	Nr  r?  r  Tz$Probe: failed to connect to '%s': %sr7  rD   c              3   >   K   | ]}|                                 V  d S r  r  )rF   ss     r"   r   z=probe_mcp_server_tools.<locals>._probe_all.<locals>.<genexpr>  s*      33qajjll333333r!   )r   r4  r4   rJ   r  r   rH  rI  r  r  r  rz   r  r   r   r[  ry   r   )r_  corosr   r  ctoutcomesoutcomer*  r   descr  probed_serversr   s             r"   
_probe_allz*probe_mcp_server_tools.<locals>._probe_all  s     W\\^^$$  	S 	SID#*,DEEBLL)/$*D*DbQQQRRRR G$GGGGGGGG 11 		! 		!MD''9-- CT7SSS!!'***E^ - -q-44:afd^,,,, F4LL n33N333
"
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r!   r$   r?  zMCP probe failed: %sN)	r   r  r4   r  r  r  r   r   _stop_mcp_loop)servers_configr  ro   r  r  r   s      @@@r"   probe_mcp_server_toolsr  m  s*     	%''N 	 '--//  G  	%'F*,N
 
 
 
 
 
 
4s33333 2 2 2+S111111112 	Ms0   A5 4B6 5
B$?BB6 B$$B6 6Cc                  "   t           5  t          t                                                    ddd           n# 1 swxY w Y   st	                       dS fd} t           5  t
          }ddd           n# 1 swxY w Y   |{|                                rg	 t          j         |             |          }|	                    d           n2# t          $ r%}t                              d|           Y d}~nd}~ww xY wt	                       dS )a  Close all MCP server connections and stop the background loop.

    Each server Task is signalled to exit its ``async with`` block so that
    the anyio cancel-scope cleanup happens in the same Task that opened it.
    All servers are shut down in parallel via ``asyncio.gather``.
    Nc                  X  K   t          j        d D             ddi d {V } t          |           D ];\  }}t          |t                    r!t
                              d|j        |           <t          5  t          
                                 d d d            d S # 1 swxY w Y   d S )Nc              3   >   K   | ]}|                                 V  d S r  r  )rF   r  s     r"   r   z:shutdown_mcp_servers.<locals>._shutdown.<locals>.<genexpr>  s,      ??Ffoo??????r!   r  Tz!Error closing MCP server '%s': %s)rH  r  r  rz   r  r   r   r   r  r  clear)r  r  r   servers_snapshots      r"   	_shutdownz'shutdown_mcp_servers.<locals>._shutdown  s     ??.>???
"
 
 
 
 
 
 
 
 ""2G<< 	 	NFF&),, 7f    	 	NN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   8BB#&B#   r?  zError during MCP shutdown: %s)r  r   r  valuesr  r  r  rH  r  r   r  r   r   )r  r  r  ro   r  s       @r"   shutdown_mcp_serversr    s    
 3 3 1 1223 3 3 3 3 3 3 3 3 3 3 3 3 3 3       
                DOO--	?5iikk4HHFMM"M%%%% 	? 	? 	?LL8#>>>>>>>>	? s9   '<A A %A99A= A=3C 
C>C99C>c                      t           5  t          } t          }dadaddd           n# 1 swxY w Y   | H|                     | j                   ||                    d           |                                  dS dS )z3Stop the background event loop and join its thread.Nr&   r?  )r  r  r  call_soon_threadsafer   rM   close)r  threads     r"   r  r    s     
  		              
 !!$),,,KKK"""

	 s   '++)r   r  )T)lrQ  rH  r   r   r   r   r2   r|   r_   r  r   typingr   r   r   r   	getLoggerr   r   r   r  r  ri  r  r<  r   r   mcp.client.stdior	   mcp.client.streamable_httpr
   r  r   r  	mcp.typesr   r   r   r   r   r   r   r   r   r   r   r   rR  r#   rb  r  r  r  	frozensetr5   compile
IGNORECASEr>   rI   r;   r\   rA   rQ   rG  rn   r   r   r   r   r   rU  r  __annotations__r  AbstractEventLoopr  r  rd  r  r  r   r  r  r  r  r  r  r
  r  r  r  r:  r.  r9  rB  r  rH  rS  rW  r]  r5  rz  ry  r  r  r  r  r  r    r!   r"   <module>r     s	  D D DL      				 				       , , , , , , , , , , , ,		8	$	$    !& *K88888888------N$DDDDDD" $ $ $#$EEEEEE   N	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 # N N NLMMMMMN	`	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 #' ` ` `^_____` K K K
LLIJJJJJK     "@!?!A!A  `8 `
LL^___           
 !bj		 M  &htn     &7# 7# 7 7 7 7t      !*C !*d !*uS$Y7G !* !* !* !*H93} 93 93 93 93 93@ *-a    l9 l9 l9 l9 l9 l9 l9 l9fz z z z z z z zB
 &($sM!
" ' ' ' 26	8G-. 5 5 5*.Xi&' . . . 		  * *E * * * *  $sDy/    B T m    (1C 1C 1u 1 1 1 1h'c ' ' ' ' 'T'S ' ' ' ' 'T,C ,u , , , ,^2# 2U 2 2 2 2j    t     S t    ,0. 0.Xd3i%8 0.D 0. 0. 0. 0.fD DT
 D D D DN	# 	c 	c#h 	 	 	 	 #      " '$"	   m T VZ[_V`    :
d3i 
 
 
 
p pm pT pdSVi p p p pfc 4 DI    :S"DI S" S" S" S"l%T
 % % % %P?S$u+%5 6 ? ? ? ?D% % %P    s   C8 $A- ,C8 -A74C8 6A77C8 ;B C8 BC8 BC8 B' &C8 'CC8 CC8 C C8 C41C8 3C44C8 8DD