
    i                    `
   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ZddlZddlmZ ddlmZmZmZmZ  ej        e          Zdaee         ed<    ej                    ZdefdZdeddfd	Zd
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# n # e+$ r e/                    d           Y nw xY w	 ddl.m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 dZ n # e+$ r e/                    d           Y nw xY w	 ddl.m7Z7m8Z8m9Z9m: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Z< e<            Z"ere"se/                    d           dZ=dZ>dZ?dZ@dZA eBh d          ZC e	jD        de	jE                  ZFd eeG         deGfd!ZHd"edefd#ZI e	jD        d$e	jJ                  d%f e	jD        d&e	jJ                  d'f e	jD        d(e	jJ                  d)f e	jD        d*e	jJ                  d+f e	jD        d,e	jJ                  d-f e	jD        d.e	jJ                  d/f e	jD        d0e	jJ                  d1f e	jD        d2e	jJ                  d3f e	jD        d4e	jJ                  d5f e	jD        d6e	jJ                  d7fg
ZKded8ed9edee         fd:ZLd;eGd<edeGfd=ZMd>ed;eGdeNeeGf         fd?ZOd@ePdefdAZQeRdBfdCZS G dD dE          ZT G dF dG          ZUi ZVeeeUf         edH<   i ZWeeeRf         edI<   i ZXeeeYf         edJ<   dZZdKZ[deddfdLZ\deddfdMZ]dNa^eNedO<   deNfdPZ_d@ePde;fdQZ`ded@ePdRefdSZadTZbeNedU<   d@ePde;fdVZcded@ePdRefdWZddaeeejf                 edX<   dageejh                 edY<    ej                    Zii ZjeeRef         edZ<    ek            Zleked[<   dekfd\Zmd] Znd^ Zodd`eYfdaZpdefdbZqdc ZrdeeeGf         fddZsdeedfeGdeUfdgZtded8edheYfdiZudedheYfdjZvdedheYfdkZwdedheYfdlZxdedheYfdmZydefdnZzdoeGdz  deGfdpZ{dqedefdrZ|dedeGfdsZ}dedeeG         fdtZ~dqeduedeke         fdvZddqedwe;de;fdxZdydzd{d|d}Zded~eUdfeGdeeG         fdZdee         fdZdeed~eUdfeGdee         fdZdeedfeGdee         fdZdeeeGf         dee         fdZdee         fdZdeeG         fdZdeeeeN         f         fdZd Zdde;ddfdZd ZdS )a  
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)datetime)AnyDictListOptional_mcp_stderr_log_fhreturnc                     t           5  t          t          cddd           S 	 ddlm}   |             dz  }|                    dd           |dz  }t          |dd	d
d          }|                                 |anl# t          $ r_}t          	                    d|           	 t          t          j        dd	          an# t          $ r t          j        aY nw xY wY d}~nd}~ww xY wt          cddd           S # 1 swxY w Y   dS )a^  Return a shared append-mode file handle for MCP subprocess stderr.

    Opened once per process and reused for every stdio server.  Must have a
    real OS-level file descriptor (``fileno()``) because asyncio's subprocess
    machinery wires the child's stderr directly to that fd.  Falls back to
    ``/dev/null`` if opening the log file fails.
    Nr   )get_hermes_homelogsT)parentsexist_okzmcp-stderr.logazutf-8replace   )encodingerrors	bufferingz0Failed to open MCP stderr log, using devnull: %sw)r   )_mcp_stderr_log_lockr   hermes_constantsr   mkdiropenfileno	Exceptionloggerdebugosdevnullsysstderr)r   log_dirlog_pathfhexcs        3/home/ubuntu/.hermes/hermes-agent/tools/mcp_tool.py_get_mcp_stderr_logr'   o   s    
 " ")%" " " " " " " "	0888888%o''&0GMM$M666!11H hgiSTUUUBIIKKK!# 	0 	0 	0LLKSQQQ0%)"*cG%L%L%L"" 0 0 0 &)Z"""0		0 "/" " " " " " " " " " " " " " " " " "s_   C=AA>=C=>
C'C"$C C"CC"CC"C="C''	C==DDserver_namec                     t                      }	 t          j                                        d          }|                    d| d|  d           |                                 dS # t          $ r Y dS w xY w)a  Write a human-readable session marker before launching a server.

    Gives operators a way to find each server's output in the shared
    ``mcp-stderr.log`` file without needing per-line prefixes (which would
    require a pipe + reader thread and complicate shutdown).
    z%Y-%m-%d %H:%M:%Sz
===== [z] starting MCP server 'z' =====
N)r'   r   nowstrftimewriteflushr   )r(   r$   tss      r&   _write_stderr_log_headerr/      s     
		B\^^$$%899
NRNNNNNOOO





   s   AA( (
A65A6Fz
2025-03-26)ClientSessionStdioServerParameters)stdio_clientT)streamablehttp_client)streamable_http_client)LATEST_PROTOCOL_VERSIONzRmcp.types.LATEST_PROTOCOL_VERSION not available -- using fallback protocol version)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 disabledc                      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	signaturer0   
parameters	TypeError
ValueError     r&   _check_message_handler_supportrK      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_)r   environitems_SAFE_ENV_KEYS
startswithupdate)rX   envkeyvalues       r&   _build_safe_envrc     so     Cj&&((  
U.  CNN6$:$: CH 

8JrJ   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rd   s    r&   _sanitize_errorri   '  s     ""<666rJ   z)ignore\s+(all\s+)?previous\s+instructionsz8prompt override attempt ('ignore previous instructions')zyou\s+are\s+now\s+az.identity override attempt ('you are now a...')z1your\s+new\s+(task|role|instructions?)\s+(is|are)ztask override attemptzsystem\s*:\s*zsystem prompt injection attemptz <\s*(system|human|assistant)\s*>zrole tag injection attemptz'do\s+not\s+(tell|inform|mention|reveal)zconcealment instructionz(curl|wget|fetch)\s+https?://znetwork command in descriptionzbase64\.(b64decode|decodebytes)zbase64 decode referencezexec\s*\(|eval\s*\(zcode execution referencez&import\s+(subprocess|os|shutil|socket)zdangerous import reference	tool_namedescriptionc                     g }|s|S t           D ]/\  }}|                    |          r|                    |           0|r1t                              d| |d                    |          |           |S )zxScan an MCP tool description for prompt injection patterns.

    Returns a list of finding strings (empty = clean).
    uU   MCP server '%s' tool '%s': suspicious description content — %s. Description: %.200s; )_MCP_INJECTION_PATTERNSsearchappendr   warningjoin)r(   rj   rk   findingspatternreasons         r&   _scan_mcp_descriptionrv   O  s    
 H 2 $ $>>+&& 	$OOF### 
"DIIh$7$7		
 	
 	
 OrJ   r`   	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.rR    c                     g | ]}||S rI   rI   ).0parts     r&   
<listcomp>z!_prepend_path.<locals>.<listcomp>k  s    AAAdDATAAArJ   )dictgetsplitr   pathseprr   )r`   rw   updatedexistingpartss        r&   _prepend_pathr   d  s    39"ooG {{62&&HAAhnnRZ88AAAE#U#05Dbjooe,,,9GFONrJ   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.
    rR   N)path>   npmnpxnodeHERMES_HOME~z.hermesr   binz.local)r   r   
expanduserstrstripr~   sepshutilwhichgetenvrr   isfileaccessX_OKdirnamer   )
r   r`   resolved_commandresolved_envpath_arg	which_hithermes_home
candidates	candidatecommand_dirs
             r&   _resolve_stdio_commandr   r  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 @$\;??\))rJ   r%   c                    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: '([^']+)'r   	__cause____context__)	getattr
isinstanceFileNotFoundErrorr   r   rero   groupBaseException)r   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trJ   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 )Nr   r   )	r   extendr   r   rp   r   r   	__class____name__)	r   r   	flattenedr   messagesrd   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7rJ   zmissing executable ''>   r   r   r   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)rm   NrO   )
r   r   r   r   r   r   basenameri   rp   rr   )r%   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2rJ   r   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*.
    )r   floatmathisfinitemaxrG   rH   OverflowError)rb   defaultcoerceminimumresults        r&   _safe_numericr     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_callsr(   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_roundsrN   r   )r   modelallowed_models)r   inforq   	log_levelr   )requestsr   tokens_usedtool_use_count)r(   r   r   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__  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rJ   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.rM   c                      g | ]
}|k    |S rI   rI   )r{   twindows     r&   r}   z5SamplingHandler._check_rate_limit.<locals>.<listcomp>  s    #S#S#S!F

A


rJ   NFT)timer   lenr   rp   )r   r*   r   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)))trJ   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$$$trJ   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.contentNry   
c              3   D   K   | ]}t          |d           |j        V  dS )rd   Nr   rd   r{   r   s     r&   	<genexpr>z<SamplingHandler._extract_tool_result_text.<locals>.<genexpr>)  s3      NNtf8M8MNNNNNNNrJ   )r   r  r   listrr   )blockr\   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NrJ   c                 H   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        d
          nt!          |
j                  dd           |j        |	d}d |D             }|rd                    |          |d<   |                    |           t|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   r{   bs     r&   r}   z5SamplingHandler._convert_messages.<locals>.<listcomp>:  s'    III!K1H1HIAIIIrJ   c                 r    g | ]4}t          |d           t          |d          "t          |d          2|5S )r   inputr  r  r  s     r&   r}   z5SamplingHandler._convert_messages.<locals>.<listcomp>;  sI    yyyqga.@.@yWQPWEXEXyahijlwaxaxyyyyrJ   c                 r    g | ]4}t          |d           t          |d          rt          |d          2|5S )r  r   r  r  r  s     r&   r}   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rJ   tool)roletool_call_idr  idcall_functionFensure_asciir   	arguments)r  typer  )r  r   c                 <    g | ]}t          |d           |j        S rh   r  r  s     r&   r}   z5SamplingHandler._convert_messages.<locals>.<listcomp>T  s)    SSS6@R@RSafSSSrJ   r  r  r   r   rd   r  r  r!  rd   datamimeType	image_urlurlzdata:z;base64,)r!  r'  z5Unsupported sampling content block type: %s (skipped))r   r   r  r   r  r
  rp   r  r  r   r   r   r  r~   jsondumpsr   r  rr   rd   r&  r%  r   rq   r!  r   )r   paramsr   msgblockstool_results	tool_usescontent_blockstrtc_listtumsg_dict
text_partsr   r  s                  r&   _convert_messagesz!SamplingHandler._convert_messages+  si     "? 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 *$&GU_`b`hjnUoUo  *CBH5)Q)Q)Q)Quxy{  zB  vC  vC% %$ $     +.('!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rJ   r   codec                 P    t           rt          ||           S t          |           )z1Return ErrorData (MCP spec) or raise as fallback.)r8  r   )_MCP_SAMPLING_TYPESr8   r   )r   r8  s     r&   _errorzSamplingHandler._errorr  s-      	9$8888   rJ   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   r   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   r   r   r)  loadsJSONDecodeErrorrH   r   rq   r~   rp   r<   r  r   logr   r   r   r   r7   )r   choiceresponser0  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   ry   z6MCP server '%s' sampling response: model=%s, tokens=%sr?  Nr@  rA  rB  rd   r$  r   rC  )r   r   r  r   rG  r   r(   r   r   r6   r;   ri   _STOP_REASON_MAPr   finish_reason)r   rH  rI  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	
 
 
 	
rJ   c                 @    | t          t                                dS )z<Return kwargs to pass to ClientSession for sampling support.)tools)sampling_callbacksampling_capabilities)r9   r:   r   s    r&   session_kwargszSamplingHandler.session_kwargs  s1     "&%7-//& & &
 
 	
rJ   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   r   z)Sampling rate limit exceeded for server 'z' (z requests/minute)modelPreferencesNr   )call_llmry   z:MCP server '%s' requested model '%s' not in allowed_modelszModel 'z' not allowed for server 'z'. Allowed: , systemPromptsystemr#  temperaturerT  c                     g | ]G}d t          |dd          t          |dd          pdt          t          |dd                    ddHS )r  r   ry   rk   inputSchemaNr   rk   rF   )r!  r  )r   _normalize_mcp_input_schema)r{   r   s     r&   r}   z,SamplingHandler.__call__.<locals>.<listcomp>
  s}         ' '62 6 6'.q-'D'D'J&A#A}d;;' '! !	 	  rJ   zFMCP server '%s' sampling request: model=%s, max_tokens=%d, messages=%dc            	      6      dpd j                   S )Nmcp)taskr   r   r_  
max_tokensrT  r   r   )r[  call_temperature
call_toolsrg  r   resolved_modelr   s   r&   
_sync_callz,SamplingHandler.__call__.<locals>._sync_call  s8    8$,!,%    rJ   rh  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   rq   r(   r   r   r;  r  r   agent.auxiliary_clientr[  r   r   rr   r6  r   r]  insertminr   r   r_  rG  r   r   asynciowait_for	to_threadr   TimeoutErrorr   ri   r   rm  r   r   rP  r   r   rM  rR  )r   contextr+  r   server_toolsrl  rI  r%   rH  r@  r[  ri  rj  rg  r   rk  s   `         @@@@@@r&   __call__zSamplingHandler.__call__  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)r7  )r   
__module____qualname____doc__rO  r   r~   r   boolr   r   r  staticmethodr  r   r6  r   r;  rM  rR  rX  rw  rI   rJ   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 }9rJ   r   c                       e Zd ZdZdZdefdZdefdZd Z	de
j        fdZd	 Zd
 Zdef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_reconnect_event_toolsr;  _config	_sampling_registered_tool_names
_auth_type_refresh_lock	_rpc_lock_pending_refresh_tasksr   c                    || _         d | _        t          | _        d | _        t          j                    | _        t          j                    | _        t          j                    | _	        g | _
        d | _        i | _        d | _        g | _        d| _        t          j                    | _        t          j                    | _        t'                      | _        d S )Nry   )r   r  _DEFAULT_TOOL_TIMEOUTr  r  rq  Eventr  r  r  r  r;  r  r  r  r  Lockr  r  setr  )r   r   s     r&   r   zMCPServerTask.__init__j  s    	&*#8-1
moo&} !(+/4813#!$\^^ !9<###rJ   r	   c                     d| j         v S )z)Check if this server uses HTTP transport.r(  )r  rW  s    r&   _is_httpzMCPServerTask._is_http  s    $$rJ   c                    K   	 |                                   d{V  dS # t          j        $ r  t          $ r$ t                              d| j                   Y dS w xY w)zBRun a dynamic tool refresh and log failures from background tasks.Nz,MCP server '%s': dynamic tool refresh failed)_refresh_toolsrq  CancelledErrorr   r   	exceptionr   rW  s    r&   _refresh_tools_taskz!MCPServerTask._refresh_tools_task  s      	X%%'''''''''''% 	 	 	 	X 	X 	XKTYWWWWWW	Xs     9AAc                     t          j        |                                           }| j                            |           |                    | j        j                   |S )zCSchedule a background tool refresh and keep it strongly referenced.)rq  create_taskr  r  addadd_done_callbackdiscard)r   rf  s     r&   _schedule_tools_refreshz%MCPServerTask._schedule_tools_refresh  sT    "4#;#;#=#=>>#''---t:BCCCrJ   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S\    t          	                    dj                   
                                 t          j        d           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: %srI   z9MCP server '%s': received tools/list_changed notificationr   z/MCP server '%s': prompts/list_changed (ignored)z1MCP server '%s': resources/list_changed (ignored)z%Error in MCP message handler for '%s')r   r   r   r   r   _MCP_NOTIFICATION_TYPESr=   rootr>   r   r  rq  sleepr?   r@   r  )r   r   s    r&   _handlerz5MCPServerTask._make_message_handler.<locals>._handler  s      Ugy11 LL!JDIW^___F* !z'CU/V/V !!,:8::::::"KK [ $	   !88::: #*-"2"2222222222% ;& =:<<<<<<"LL)Z\`\efffff =<>>>>>"LL)\^b^ghhhhh ? D5! ! ! !6  U U U  !H$)TTTTTTUs)   6D% A>D% =/D% .-D% D% %*EErI   )r   r  s   ` r&   _make_message_handlerz#MCPServerTask._make_message_handler  s)    !	U !	U !	U !	U !	UD rJ   c           	         K   ddl m}  j        4 d{V  t           j                  } j        4 d{V   j                                         d{V }ddd          d{V  n# 1 d{V swxY w Y   t          |d          r|j	        ng }| fd|D             z
  }|D ]}|
                    |           | _        t           j          j                   _        t           j                  }||z
  }||z
  }	g }
|r8|
                    dd                    t#          |                                |	r8|
                    dd                    t#          |	                                |
r5t$                              d	 j        d
                    |
                     n3t$                              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   registryNrT  c                 h    h | ].}d t          j                   dt          |j                   /S )mcp__)sanitize_mcp_name_componentr   )r{   r  r   s     r&   	<setcomp>z/MCPServerTask._refresh_tools.<locals>.<setcomp>  sY     1 1 1 <249== < <.ty99< <1 1 1rJ   zadded: r\  z	removed: uU   MCP server '%s': tools changed dynamically — %s. Verify these changes are expected.rm   z>MCP server '%s': dynamically refreshed %d tool(s) (no changes))tools.registryr  r  r  r  r  r  
list_toolsr   rT  
deregisterr  _register_server_toolsr   r  rp   rr   sortedr   rq   r   r   )r   r  old_tool_namestools_resultnew_mcp_toolsstale_tool_namesrj   new_tool_namesaddedremovedchangess   `          r&   r  zMCPServerTask._refresh_tools  sp      	,+++++% 1	 1	 1	 1	 1	 1	 1	 1	 !<==N ~ ? ? ? ? ? ? ? ?%)\%<%<%>%>>>>>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?29,2P2PXL..VXM  . 1 1 1 1 *1 1 1  
 . / /	##I.... (DK*@	4+ +D'
 !!<==N"^3E$~5GG EC6%==)A)ACCDDD IG499VG__+E+EGGHHH 
9Ityy11    TIs4#>??  ]1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	s5   "G8 A+G8+
A5	5G88A5	9E,G88
HHc                   K   t          j        | j                                                  }t          j        | j                                                  }	 t          j        ||ht           j                   d{V  ||fD ]P}|                                s:|                                 	 | d{V  4# t           j        t          f$ r Y Lw xY wQnZ# ||fD ]P}|                                s:|                                 	 | d{V  4# t           j        t          f$ r Y Lw xY wQw xY w| j        
                                rdS | j                                         dS )a<  Block until either _shutdown_event or _reconnect_event fires.

        Returns:
            "shutdown"  if the server should exit the run loop entirely.
            "reconnect" if the server should tear down the current MCP
                        session and re-enter the transport (fresh OAuth
                        tokens, new session ID, etc.). The reconnect event
                        is cleared before return so the next cycle starts
                        with a fresh signal.

        Shutdown takes precedence if both events are set simultaneously.
        )return_whenNshutdown	reconnect)rq  r  r  waitr  FIRST_COMPLETEDdonecancelr  r   is_setclear)r   shutdown_taskreconnect_taskr   s       r&   _wait_for_lifecycle_eventz'MCPServerTask._wait_for_lifecycle_event  s       +D,@,E,E,G,GHH ,T-B-G-G-I-IJJ	,/#3         
 $^4  vvxx HHJJJ#2I>   	m^4  vvxx HHJJJ#2I>   	 &&(( 	:##%%%{sB   (C 1B::CC/D/DD/D*	'D/)D*	*D/r   c           	      T  K   |                     d          }|                     dg           }|                     d          }|st          d| j         d          t          |          }t	          ||          \  }}ddlm}  |||          }|rt          d| j         d|           t          |||r|nd	
          }| j        r| j        	                                ni }	t          rt          r|                                 |	d<   t                      }
t                      }t          | j                   t!                      }	 t#          ||          4 d	{V \  }}t                      |
z
  }|r4t$          5  |D ]}| j        t&          |<   	 d	d	d	           n# 1 swxY w Y   t)          ||fi |	4 d	{V 	 }|                                 d	{V  || _        |                                  d	{V  | 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   |rt$          5  |D ]}t&                              |d	           |D ]P}	 t7          j        |d           n# t:          t<          t>          f$ r Y 2w xY wt@          !                    |           Q	 d	d	d	           d	S # 1 swxY w Y   d	S d	S # |rt$          5  |D ]}t&                              |d	           |D ]P}	 t7          j        |d           n# t:          t<          t>          f$ r Y 2w xY wt@          !                    |           Q	 d	d	d	           w # 1 swxY w Y   w w xY w)z%Run the server using stdio transport.r   rK  r`   MCP server 'z' has no 'command' in configr   )check_package_for_malwarez': N)r   rK  r`   rB   )errlog)"r   rH   r   rc   r   tools.osv_checkr  r1   r  rX  r  _MCP_MESSAGE_HANDLER_SUPPORTEDr  _snapshot_child_pidsr  r/   r'   r2   _lock_stdio_pidsr0   
initializer  _discover_toolsr  r  popr   killProcessLookupErrorPermissionErrorOSError_orphan_stdio_pidsr  )r   r   r   rK  rX   safe_envr  malware_errorserver_paramssampling_kwargspids_beforenew_pids_errlogread_streamwrite_stream_pidr  pids                     r&   
_run_stdiozMCPServerTask._run_stdio'  sQ     **Y''zz&"%%::e$$ 	FtyFFF   #8,,27HEE 	>=====11'4@@ 	<ty<<]<<   .$.$
 
 
 >B^S$.77999QS" 	N'E 	N151K1K1M1MO-. +,,
 	!+++%''%	4#M'BBB ; ; ; ; ; ; ; G 011K? : : :$, : :D04	K--:: : : : : : : : : : : : : : : ) 1@  
; 
; 
; 
; 
; 
; 
; 
;!,,.........#*DL..000000000KOO%%% 88:::::::::
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
; 
;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;6  	4 4 4 ( 4 4#d3333' 4 4%GCOOOO 2OWM % % %$H%*..s333344 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4	4 	4x 	4 4 4 ( 4 4#d3333' 4 4%GCOOOO 2OWM % % %$H%*..s333344 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4	4s   	L  I$>F I$ F$	$I$'F$	(I$ A/I/I$
I	I$I	I$L $
I..L 1I.2L >%L $J:9L :KL KL  LL
N'%N?MNM/,N.M//NN'NN'!N"N'c                 f  K   t           st          d| j         d          |d         }t          |                    d          pi           }t          d |D                       s
t          |d<   |                    dt                    }|                    dd	          }d
}| j        dk    rs	 ddl	m
}  |                                | j        ||                    d                    }n4# t          $ r'}t                              d| j        |            d
}~ww xY w| j        r| j                                        ni }	t"          rt$          r|                                 |	d<   t(          rdd
l}
|
                    |          fd}d	|
                    t1          |          d          |d|gid}|r||d<   |||d<    |
j        di |4 d
{V }t5          ||          4 d
{V \  }}}t7          ||fi |	4 d
{V }|                                 d
{V  || _        |                                  d
{V  | j                                          | !                                 d
{V }|dk    r t          "                    d| j                   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 |t1          |          |d}|||d<   tG          |fi |4 d
{V \  }}}t7          ||fi |	4 d
{V }|                                 d
{V  || _        |                                  d
{V  | j                                          | !                                 d
{V }|dk    r t          "                    d| j                   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(  headersc              3   F   K   | ]}|                                 d k    V  dS )mcp-protocol-versionN)r   )r{   ra   s     r&   r	  z*MCPServerTask._run_http.<locals>.<genexpr>  s0      LLS399;;"88LLLLLLrJ   r  connect_timeout
ssl_verifyTNoauthr   get_managerz#MCP OAuth setup failed for '%s': %srB   c                    K   | j         r| j        rz| j        j        }|j        |j        |j        fj        j        j        fk    rF| j        j                            dd           | j        j                            dd           dS dS dS dS )zBStrip Authorization headers when redirected to a different origin.authorizationNAuthorization)is_redirectnext_requestr(  schemehostportr  r  )rI  target_original_urls     r&   $_strip_auth_on_cross_origin_redirectzEMCPServerTask._run_http.<locals>._strip_auth_on_cross_origin_redirect  s      ' QH,A Q%26Fv{FK@%,m.@-BTE   !-599/4PPP -599/4PPPPPQ Q Q Q rJ   g     r@)readrI  )follow_redirectsr   verifyevent_hooksauth)http_clientr  uB   MCP server '%s': reconnect requested — tearing down HTTP session)r  r   r  uI   MCP server '%s': reconnect requested — tearing down legacy HTTP sessionrI   )$_MCP_HTTP_AVAILABLEImportErrorr   r~   r   anyr5   _DEFAULT_CONNECT_TIMEOUTr  tools.mcp_oauth_managerr  get_or_build_providerr   r   rq   r  rX  r  r  r  _MCP_NEW_HTTPhttpxURLTimeoutr   AsyncClientr4   r0   r  r  r  r  r  r  r   r3   )r   r   r(  r  r  r  _oauth_authr  r%   r  r  r  client_kwargsr  r  r  _get_session_idr  ru   _http_kwargsr  s                       @r&   	_run_httpzMCPServerTask._run_httpw  s     " 	?ty ? ? ?   Umvzz),,233
 LLGLLLLL 	F.EG*+ **%68PQQZZd33
 ?g%%??????)kmmAAIsFJJw$7$7     DdiQTUUU >B^S$.77999QS" 	N'E 	N151K1K1M1MO-. C	 LLL!IIcNNMQ Q Q Q Q %) ==)?)?e=LL$ *-Q,RS	# #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)))'+'E'E'G'G!G!G!G!G!G!G![00"KK!<=AY  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                                                       $ # 11$" "L
 &'2V$,SAALAA        F\?(lVVoVV 
 
 
 
 
 
 
Za!,,.........#*DL..000000000KOO%%%#'#A#A#C#CCCCCCCF,,?@D	  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                             s   1=C/ /
D 9"DD K<5KBJ5#K5
J??KJ?KK<
K"	"K<%K"	&K<<
L	L<P BO<*P <
P	P 	P	
P  
P*-P*c                    K   | j         dS | j        4 d{V  | j                                          d{V }ddd          d{V  n# 1 d{V swxY w Y   t          |d          r|j        ng | _        dS )z*Discover tools from the connected session.NrT  )r  r  r  r   rT  r  )r   r  s     r&   r  zMCPServerTask._discover_tools  s     <F> 	; 	; 	; 	; 	; 	; 	; 	;!%!8!8!:!:::::::L	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; |W--L 	s    A
AA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}d}	 	 |                                 r|                     |           d{V  n|                     |           d{V  | j                                        r
	 d| _        dS t                              d| j	                   d| _        	 d| _        # t(          $ rI}d| _        | j                                        s|dz  }|t,          k    rTt                              d| j	        t,          |           || _        | j                                         Y d}~d| _        dS t                              d| j	        |t,          ||           t3          j        |           d{V  t7          |dz  t8                    }| j                                        r-|| _        | j                                         Y d}~d| _        dS Y d}~d| _        | j                                        r.t                              d| j	        |           Y d}~d| _        dS |dz  }|t<          k    r4t                              d| j	        t<          |           Y d}~d| _        dS t                              d| j	        |t<          ||           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  ry   samplingenabledTNr(  r   zMCP server '%s' has both 'url' and 'command' in config. Using HTTP transport ('url'). Remove 'command' to silence this warning.r   g      ?z@MCP server '%s': reconnecting (OAuth recovery or manual refresh)r   zJMCP server '%s' failed initial connection after %d attempts, giving up: %szPMCP server '%s' initial connection failed (attempt %d/%d), retrying in %.0fs: %s   z0MCP server '%s' disconnected during shutdown: %szDMCP server '%s' failed after %d reconnection attempts, giving up: %szJMCP server '%s' connection lost (attempt %d/%d), reconnecting in %.0fs: %s)r  r   r  r  r   r   r  r:  r   r   r  r   rq   r  r  r  r  r  r  r   r   r  _MAX_INITIAL_CONNECT_RETRIESr;  r  rq  r  rp  _MAX_BACKOFF_SECONDSr   _MAX_RECONNECT_RETRIES)r   r   sampling_configretriesinitial_retriesbackoffr%   s          r&   runzMCPServerTask.run  s      "JJy2GHH!::f--3::<<BBDD !**Z44y$// 	"4G 	",TYHHDNN!DN F??yF22NN  		   Z	$Y$==?? 2..0000000000//&111111111 '..00 \  $[ &I    $ D  $C  ? ? ?# {))++ #q(O&)EEE9 I'CS  
 '*)))`  $] NNA	?4gs	   "-000000000!'A+/CDDG +2244 &))))B  $A HHH@  $; '..00 LLJ	3   FFF0  $- 1333NN(	#93  
 FFF  $ 0Iw(>S	   mG,,,,,,,,,gk+?@@ '..00 FFF#    }?|  $t####uZ	$sp   /A$F 'F O!A8OO- BO1O- >O- :OO- 7O	O- A4O
O- O- O!!O- -	O6c                    K   t          j        |                     |                    | _        | j                                         d{V  | j        r| j        dS )z<Create the background Task and wait until ready (or failed).N)rq  ensure_futurer  r  r  r  r;  )r   r   s     r&   startzMCPServerTask.startj  sf      *488F+;+;<<
k         ; 	+	 	rJ   c                 :  K   ddl m} | j                                         | 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| j        r`t!          | j                  D ]}|                                 t          j        | j        ddi d{V  | j                                         t!          t'          | d	g                     D ]}|                    |           g | _        d| _        dS )
z=Signal the Task to exit and wait for clean resource teardown.r   r  r   rh  Nz3MCP server '%s' shutdown timed out, cancelling taskreturn_exceptionsTr  )r  r  r  r  r  r  r  rq  rr  rt  r   rq   r   r  r  r  r
  gatherr  r   r  r  r  )r   r  rf  rj   s       r&   r  zMCPServerTask.shutdownq  s     ++++++  """ 	!!###: 	djoo// 	&tz2>>>>>>>>>>>' 	 	 	II   
!!###*$$$$$$$$-   D	 & 	0T899  .$"=VQUVVVVVVVVV'--///gd,DbIIJJ 	+ 	+I	****&(#s7   !A> >AC-CC-C'$C-&C''C-,C-N)r   rx  ry  rz  	__slots__r   r   r{  r  r  rq  Taskr  r  r  r  r~   r  r  r  r  r  r  rI   rJ   r&   r~  r~  X  sb        I?S ? ? ? ?:%$ % % % %X X X    ) ) )V; ; ;z         DN4t N4 N4 N4 N4`ld l l l l\

 

 

w$ w$ w$ w$ w$r$             rJ   r~  _servers_server_error_counts_server_breaker_opened_atg      N@c                     t                               | d          dz   }|t           | <   |t          k    rt          j                    t
          | <   dS dS )a  Increment the consecutive-failure count for ``server_name``.

    When the count crosses :data:`_CIRCUIT_BREAKER_THRESHOLD`, stamp the
    breaker-open timestamp so the cooldown clock starts (or re-starts,
    for probe failures in the half-open state).
    r   r   N)r%  r   _CIRCUIT_BREAKER_THRESHOLDr   	monotonicr&  )r(   ns     r&   _bump_server_errorr+    sV     	  a0014A()%&&&151A1A!+... '&rJ   c                 P    dt           | <   t                              | d           dS )zFully close the breaker for ``server_name``.

    Clears both the failure count and the breaker-open timestamp. Call
    this on any unambiguous success signal (successful tool call,
    successful reconnect, manual /mcp refresh).
    r   N)r%  r&  r  )r(   s    r&   _reset_server_errorr-    s+     )*%!!+t44444rJ   rI   _AUTH_ERROR_TYPESc                     t           rt           S g } 	 ddlm}m} |                     ||g           n# t
          $ r Y nw xY w	 ddlm} |                     |           n# t
          $ r Y nw xY w	 ddlm	} |                     |           n# t
          $ r Y nw xY w	 ddl
}|                     |j                   n# t
          $ r Y nw xY wt          |           a t           S )u  Return a tuple of exception types that indicate MCP OAuth failure.

    Cached after first call. Includes:
      - ``mcp.client.auth.OAuthFlowError`` / ``OAuthTokenError`` — raised by
        the SDK's auth flow when discovery, refresh, or full re-auth fails.
      - ``mcp.client.auth.UnauthorizedError`` (older MCP SDKs) — kept as an
        optional import for forward/backward compatibility.
      - ``tools.mcp_oauth.OAuthNonInteractiveError`` — raised by our callback
        handler when no user is present to complete a browser flow.
      - ``httpx.HTTPStatusError`` — caller must additionally check
        ``status_code == 401`` via :func:`_is_auth_error`.
    r   )OAuthFlowErrorOAuthTokenError)UnauthorizedError)OAuthNonInteractiveErrorN)r.  mcp.client.authr0  r1  r   r   r2  rp   tools.mcp_oauthr3  r  HTTPStatusErrortuple)typesr0  r1  r2  r3  r  s         r&   _get_auth_error_typesr9    s`     !  ECCCCCCCCno67777   555555&''''   <<<<<<-....   U*++++   esB   2 
??A 
A,+A,0B 
BBB< <
C	C	c                     t                      }|rt          | |          sdS 	 ddl}t          | |j                  rt	          | j        dd          dk    S n# t          $ r Y nw xY wdS )zReturn True if ``exc`` indicates an MCP OAuth failure.

    ``httpx.HTTPStatusError`` is only treated as auth-related when the
    response status code is 401. Other HTTP errors fall through to the
    generic error path in the tool handlers.
    Fr   Nstatus_codei  T)r9  r   r  r6  r   rI  r   )r%   r8  r  s      r&   _is_auth_errorr<    s     "##E 
3.. uc5011 	E3<==DD	E   4s   2A 
A%$A%op_descriptionc                     t          |          sdS ddlm}  |             fd}	 t           |            d          }n5# t          $ r(}t
                              d |           d}Y d}~nd}~ww xY w|rt          5  t          	                               }ddd           n# 1 swxY w Y   |t          |d	          rt          }	|	|	                                r|	                    |j        j                   t!          j                    d
z   }
t!          j                    |
k     rL|j        |j                                        rn+t!          j        d           t!          j                    |
k     Lt-                      	  |            }	 t/          j        |          }d|vrt-                      |S n-# t.          j        t4          f$ r t-                      |cY S w xY wn4# t          $ r'}t
                              d ||           Y d}~nd}~ww xY wt7                      t/          j        d  d  dd dd          S )aQ  Attempt auth recovery and one retry; return None to fall through.

    Called by the 5 MCP tool handlers when ``session.<op>()`` raises an
    auth-related exception. Workflow:

      1. Ask :class:`tools.mcp_oauth_manager.MCPOAuthManager.handle_401` if
         recovery is viable (i.e., disk has fresh tokens, or the SDK can
         refresh in-place).
      2. If yes, set the server's ``_reconnect_event`` so the server task
         tears down the current MCP session and rebuilds it with fresh
         credentials. Wait briefly for ``_ready`` to re-fire.
      3. Retry the operation once. Return the retry result if it produced
         a non-error JSON payload. Otherwise return the ``needs_reauth``
         error dict so the model stops hallucinating manual refresh.
      4. Return None if ``exc`` is not an auth error, signalling the
         caller to use the generic error path.

    Args:
        server_name: Name of the MCP server that raised.
        exc: The exception from the failed tool call.
        retry_call: Zero-arg callable that re-runs the tool call, returning
            the same JSON string format as the handler.
        op_description: Human-readable name of the operation (for logs).

    Returns:
        A JSON string if auth recovery was attempted, or None to fall
        through to the caller's generic error path.
    Nr   r  c                  @   K                         d            d {V S N)
handle_401)managerr(   s   r&   _recoverz._handle_auth_error_and_retry.<locals>._recover6  s/      ''T:::::::::rJ   r   rh  z+MCP OAuth '%s': recovery attempt failed: %sFr           ?errorz.MCP %s/%s retry after auth recovery failed: %sr  z4' requires re-authentication. Run `hermes mcp login u   ` (or delete the tokens file under ~/.hermes/mcp-tokens/ and restart). Do NOT retry this tool — ask the user to re-authenticate.T)rF  needs_reauthserverr  )r<  r  r  _run_on_mcp_loopr   r   rq   r  r$  r   r   	_mcp_loop
is_runningcall_soon_threadsafer  r  r   r)  r  r  r  r  r-  r)  rE  rF  rG   r+  r*  )r(   r%   
retry_callr=  r  rC  	recoveredrec_excsrvloopdeadliner   rL  	retry_excrB  s   `             @r&   _handle_auth_error_and_retryrT    sA   D # t333333kmmG; ; ; ; ; ;$XXZZ<<<		   9	
 	
 	
 						  ( 	, 	,,,{++C	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,?ws,>???DDOO$5$5))#*>*BCCC  >++b0n&&11{.3:3D3D3F3F.Jt$$$ n&&11 	K(((	Z\\FF++&(('444!M ) ()4   #K000 )  	 	 	NN@^Y       	 {###:>; > >%0> > >
 	 	 	 	 	 	se   A 
A7A22A7B++B/2B/
G0 (G G0 'G,)G0 +G,,G0 0
H!:HH!)zinvalid or expired sessionzexpired sessionzsession expiredzsession not foundzunknown session_SESSION_EXPIRED_MARKERSc                     t          | t                    rdS t          |                                           sdS t	          fdt
          D                       S )u  Return True if ``exc`` looks like an MCP transport session expiry.

    Streamable HTTP MCP servers may garbage-collect server-side session
    state while the OAuth token remains valid — idle TTL, server
    restart, horizontal-scaling pod rotation, etc.  The SDK surfaces
    this as a JSON-RPC error whose message contains phrases like
    ``"Invalid or expired session"``.  This class of failure is
    distinct from :func:`_is_auth_error`: re-running the OAuth refresh
    flow would be pointless because the access token is fine.  What's
    needed is a transport reconnect — tear down and rebuild the
    ``streamablehttp_client`` + ``ClientSession`` pair, which is
    exactly what ``MCPServerTask._reconnect_event`` triggers.
    Fc              3       K   | ]}|v V  	d S r@  rI   )r{   markerr,  s     r&   r	  z,_is_session_expired_error.<locals>.<genexpr>  s'      DDv}DDDDDDrJ   )r   InterruptedErrorr   r   r  rU  )r%   r,  s    @r&   _is_session_expired_errorrZ    se     #'(( u
 c((..

C uDDDD+CDDDDDDrJ   c                    t          |          sdS t          5  t                              |           }ddd           n# 1 swxY w Y   |t	          |d          sdS t
          }||                                sdS t                              d| ||           |	                    |j
        j                   t          j                    dz   }d}t          j                    |k     rN|j        |j                                        rd}n+t          j        d           t          j                    |k     N|st                              d|            dS 	  |            }	 t'          j        |          }	d	|	vrd
t*          | <   |S n(# t&          j        t.          f$ r d
t*          | <   |cY S w xY wn4# t0          $ r'}
t                              d| ||
           Y d}
~
nd}
~
ww xY wdS )uO  Trigger a transport reconnect and retry once on session expiry.

    Unlike :func:`_handle_auth_error_and_retry`, this does **not** call
    the OAuth manager's ``handle_401`` — the access token is still
    valid, only the server-side session state is stale.  Setting
    ``_reconnect_event`` causes the server task's lifecycle loop to
    tear down the current ``streamablehttp_client`` + ``ClientSession``
    and rebuild them, reusing the existing OAuth provider instance.
    See #13383.

    Args:
        server_name: Name of the MCP server that raised.
        exc: The exception from the failed call.
        retry_call: Zero-arg callable that re-runs the operation,
            returning the same JSON string format as the handler.
        op_description: Human-readable name of the operation (logs).

    Returns:
        A JSON string if reconnect + retry was attempted and produced
        a response, or ``None`` to fall through to the caller's
        generic error path (not a session-expired error, no server
        record, reconnect didn't ready in time, or retry also failed).
    Nr  zmMCP server '%s': %s failed with session-expired error (%s); signalling transport reconnect and retrying once.rD  FTrE  zsMCP server '%s': reconnect did not ready within 15s after session-expired error; falling through to error response.rF  r   z2MCP %s/%s retry after session reconnect failed: %s)rZ  r  r$  r   r   rJ  rK  r   r   rL  r  r  r   r)  r  r  r  r  rq   r)  rE  r%  rF  rG   r   )r(   r%   rM  r=  rP  rQ  rR  readyr   rL  rS  s              r&   !_handle_session_expired_and_retryr]    s~   : %S)) t	 ( (ll;''( ( ( ( ( ( ( ( ( ( ( ( ( ( (
{'#'9::{tD|4??,,|t
KK	<^S   	c26777~"$HE
.

X
%
%;"sz'8'8':':"E
4	 .

X
%
%
  H	
 	
 	

 t
	Z''Ff$$45$[1 % $i0 	 	 	01 -MMM	 %  
 
 
@	
 	
 	
 	
 	
 	
 	
 	


 4sM   A  AA
F0 "#F F0 "F,)F0 +F,,F0 0
G!:GG!rJ  _mcp_threadr  r  c                     t          j                    } 	 d|  d|  d}t          |          5 }d |                                                                D             cddd           S # 1 swxY w Y   n# t
          t          t          f$ r Y nw xY w	 ddl}d |	                    |           
                                D             S # t          $ r Y nw xY wt                      S )zReturn a set of current child process PIDs.

    Uses /proc on Linux, falls back to psutil, then empty set.
    Used by _run_stdio to identify the subprocess spawned by stdio_client.
    z/proc/z/task/z	/childrenc                 T    h | ]%}|                                 t          |          &S rI   )r   r   )r{   ps     r&   r  z'_snapshot_child_pids.<locals>.<setcomp>  s+    BBBq		BCFFBBBrJ   Nr   c                     h | ]	}|j         
S rI   )r  )r{   cs     r&   r  z'_snapshot_child_pids.<locals>.<setcomp>  s    AAA!AAArJ   )r   getpidr   r  r   r   r  rH   psutilProcesschildrenr   r  )my_pidchildren_pathfre  s       r&   r  r    sW    Y[[F@@@v@@@-   	CABBAFFHHNN$4$4BBB	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	Cw
3   AAv~~f55>>@@AAAA    55LsF   A6 0A*A6 *A..A6 1A.2A6 6BB4C	 	
CCc                     |                     d          }t          |t                    rdt          |          v rdS |                     |           dS )a  Suppress benign 'Event loop is closed' noise during shutdown.

    When the MCP event loop is stopped and closed, httpx/httpcore async
    transports may fire __del__ finalizers that call call_soon() on the
    dead loop.  asyncio catches that RuntimeError and routes it here.
    We silence it because the connection is being torn down anyway; all
    other exceptions are forwarded to the default handler.
    r  zEvent loop is closedN)r   r   RuntimeErrorr   default_exception_handler)rQ  ru  r%   s      r&   _mcp_loop_exception_handlerrn  %  sW     ++k
"
"C#|$$ )?3s88)K)K""7+++++rJ   c                  z   t           5  t          't                                          r	 ddd           dS t          j                    at                              t                     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)r  r   daemon)r  rJ  rK  rq  new_event_loopset_exception_handlerrn  	threadingThreadrun_foreverr^  r  rI   rJ   r&   _ensure_mcp_looprv  4  s    
 
 
 Y%9%9%;%; 
 
 
 
 
 
 
 
 *,,	''(CDDD&(!
 
 

 	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   "B0A,B00B47B4r   r   c                 L   ddl m} t          5  t          }ddd           n# 1 swxY w Y   ||                                st          d          t          j        | |          }|dnt          j	                    |z   }	  |            r#|
                                 t          d          d}|B|t          j	                    z
  }|dk    r|                    d          S t          ||          }	 |                    |          S # t          j        j        $ r Y w xY w)	zSchedule a coroutine on the MCP event loop and block until done.

    Poll in short intervals so the calling agent thread can honor user
    interrupts while the MCP work is still running on the background loop.
    r   )is_interruptedNzMCP event loop is not runningTzUser sent a new messageg?rh  )tools.interruptrx  r  rJ  rK  rl  rq  run_coroutine_threadsafer   r)  r  rY  r   rp  
concurrentfuturesrt  )coror   rx  rQ  futurerR  wait_timeout	remainings           r&   rI  rI  D  s}    /.....	                |4??,,|:;;;-dD99FttDN,<,<w,FH> 	>MMOOO"#<=== 4>#3#33IA~~}}Q}///|Y77L	===666!. 	 	 	H	s   "&&6D D#"D#c                  2    t          j        ddid          S )z=Standardized JSON error for a user-interrupted MCP tool call.rF  z-MCP call interrupted: user sent a new messageFr  )r)  r*  rI   rJ   r&   _interrupted_call_resultr  e  s)    :@   rJ   c                 
   t          | t                    rd }t          j        d||           S t          | t                    rd |                                 D             S t          | t                    rd | D             S | S )z@Recursively resolve ``${VAR}`` placeholders from ``os.environ``.c                     t           j                            |                     d          |                     d                    S )Nr   r   )r   r[   r   r   )ms    r&   _replacez'_interpolate_env_vars.<locals>._replaces  s,    :>>!''!**aggajj999rJ   z\$\{([^}]+)\}c                 4    i | ]\  }}|t          |          S rI   _interpolate_env_varsr{   kvs      r&   
<dictcomp>z)_interpolate_env_vars.<locals>.<dictcomp>w  s'    FFF1(++FFFrJ   c                 ,    g | ]}t          |          S rI   r  )r{   r  s     r&   r}   z)_interpolate_env_vars.<locals>.<listcomp>y  s!    888Q%a((888rJ   )r   r   r   rg   r~   r\   r
  )rb   r  s     r&   r  r  p  s    % 9	: 	: 	:v&%888% GFFFFFF% 988%8888LrJ   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 rI   r  r{   r   cfgs      r&   r  z$_load_mcp_config.<locals>.<dictcomp>  s'    RRRYT3+C00RRRrJ   zFailed to load MCP config: %sN)hermes_cli.configr  r   r   r~   hermes_cli.env_loaderr  r   r\   r   r   )r  r   serversr  r%   s        r&   _load_mcp_configr  }  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)r~  r  )r   r   rH  s      r&   _connect_serverr    s>       4  F
,,v

MrJ   r  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``
    rK  r	   c           
         
 t                               d          t          k    rt                              d          }t	          j                    |z
  }|t          k     rRt          dt          t          |z
                      }t          j
        dd dt                     d| did	
          S t          5  t                                        d d d            n# 1 swxY w Y   rj        s+t                     t          j
        dd did	
          S  fd

fd}	  |            }	 t          j        |          }d|v rt                     nt!                     n+# t          j        t$          f$ r t!                     Y nw xY w|S # t&          $ r t)                      cY S t*          $ r}t-          ||d           }	|	|	cY d }~S t/          ||d           }	|	|	cY d }~S t                     t0                              d|           t          j
        dt5          dt7          |          j         d|           id	
          cY d }~S d }~ww xY w)Nr   g        r   rF  r  z' is unreachable after z0 consecutive failures. Auto-retry available in ~ue   s. Do NOT retry this tool yet — use alternative approaches or ask the user to check the MCP server.Fr  ' is not connectedc                    K   j         4 d {V  j                                       d {V } d d d           d {V  n# 1 d {V swxY w Y   | j        rOd}| j        pg D ]}t          |d          r
||j        z  }t          j        dt          |pd          id          S g }| j        pg D ],}t          |d          r|
                    |j                   -|rd                    |          nd}t          | d	d           }|3|rt          j        ||d
d          S t          j        d|id          S t          j        d|id          S )Nr   ry   rd   rF  zMCP tool returned an errorFr  r  structuredContent)r   r  r   )r  r  	call_toolisErrorr  r   rd   r)  r*  ri   rp   rr   r   )	r   
error_textr  r   text_result
structuredrK  rH  rj   s	         r&   _callz3_make_tool_handler.<locals>._handler.<locals>._call  st     ' S S S S S S S S%~77	T7RRRRRRRRS S S S S S S S S S S S S S S S S S S S S S S S S S S ~ 	'
$n2 1 1Euf-- 1"ej0
z_"B&B # !&	' ' ' '  "E ..B - -5&)) -LL,,,.3;$))E***K !)<dCCJ% +:"--7' ' %*+ + + + z8Z"8uMMMM:x5EJJJJ   #A
AAc                  6    t                                  S Nrh  rI  r  r  s   r&   
_call_oncez8_make_tool_handler.<locals>._handler.<locals>._call_once       #EEGG\BBBBrJ   ztools/call zMCP tool %s/%s call failed: %sMCP call failed: : )r%  r   r(  r&  r   r)  _CIRCUIT_BREAKER_COOLDOWN_SECr   r   r)  r*  r  r$  r  r+  rE  r-  rF  rG   rY  r  r   rT  r]  r   rF  ri   r!  r   )rK  kwargs	opened_atager  r  r   rL  r%   rN  r  rH  r(   rj   r  s   `         @@r&   r  z$_make_tool_handler.<locals>._handler  s     ##K337QQQ155k3GGI.""Y.C2223'Ds'J#K#KLL	zO{ O O/<O O>GO O O# !&' ' ' '  	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	#V^ 	#{+++:GGGG!# # # #"	K "	K "	K "	K "	K "	K "	KH	C 	C 	C 	C 	C 	C,	#Z\\F1F++f$$&{3333'444()4 1 1 1#K000001M 	. 	. 	.+----- 	# 	# 	# 5S*)i)) I $       
 :S*)i)) I $       {+++LL0Y   :CS		(:CCcCC  "	# # # # # # # # #5	#ss   >C%%C),C)2
F" =7E5 4F" 5%FF" FF" "I4<	I4I/I4#I/;I4A(I/)I4/I4r~   r   )r(   rj   r  r  s   ``` r&   _make_tool_handlerr    sM    v#t v## v# v# v# v# v# v# v# v#p OrJ   c                 4     dt           dt          f fd}|S )z>Return a sync handler that lists resources from an MCP server.rK  r	   c           
      n   t           5  t                                        d d d            n# 1 swxY w Y   rj        st	          j        dd did          S fdfd}	  |            S # t          $ r t                      cY S t          $ r}t          ||d          }||cY d }~S t          ||d          }||cY d }~S t                              d	|           t	          j        dt          d
t          |          j         d|           id          cY d }~S d }~ww xY w)NrF  r  r  Fr  c                  H  K   j         4 d {V  j                                         d {V } d d d           d {V  n# 1 d {V swxY w Y   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d          S )N	resourcesurir   rk   r&  Fr  )r  r  list_resourcesr   r  r   r  r   rk   r&  rp   r)  r*  )r   r  rentryrH  s       r&   r  z=_make_list_resources_handler.<locals>._handler.<locals>._call?  s     ' ? ? ? ? ? ? ? ?%~<<>>>>>>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?I*1&+*F*FNf&&B 
( 
(1e$$ .#&qu::E%L1f%% +$%FE&M1m,, 9 9+,=E-(1j)) 3aj 3()
E*%  '''':{I6UKKKK    A
AAc                  6    t                                  S r  r  r  s   r&   r  zB_make_list_resources_handler.<locals>._handler.<locals>._call_onceP  r  rJ   zresources/listz MCP %s/list_resources failed: %sr  r  r  r$  r   r  r)  r*  rY  r  r   rT  r]  r   rF  ri   r!  r   	rK  r  r  r%   rN  r  rH  r(   r  s	        @@r&   r  z._make_list_resources_handler.<locals>._handler7  s<    	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	#V^ 	#:GGGG!# # # #	L 	L 	L 	L 	L"	C 	C 	C 	C 	C 	C	#:<< 	. 	. 	.+----- 	# 	# 	#4S*.> I $       9S*.> I $       LL2K   :CS		(:CCcCC  "	# # # # # # # # #	#I   266.	A8 8D4	D4D/0D46D/D4AD/)D4/D4r  r(   r  r  s   `` r&   _make_list_resources_handlerr  4  s=    2#t 2## 2# 2# 2# 2# 2# 2# 2#h OrJ   c                 4     dt           dt          f fd}|S )zFReturn a sync handler that reads a resource by URI from an MCP server.rK  r	   c           
         ddl m} t          5  t                              	          d d d            n# 1 swxY w Y   rj        st          j        dd	 did          S |                     d          s |d	          S fd

fd}	  |            S # t          $ r t                      cY S t          $ r}t          	||d          }||cY d }~S t          	||d          }||cY d }~S t                              d	|           t          j        dt          dt!          |          j         d|           id          cY d }~S d }~ww xY w)Nr   
tool_errorrF  r  r  Fr  r  z Missing required parameter 'uri'c                    K   j         4 d {V  j                                       d {V } d d d           d {V  n# 1 d {V swxY w Y   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d	
          S )Ncontentsrd   blobz[binary data, z bytes]r   r  ry   Fr  )r  r  read_resourcer   r  rp   rd   r   r  r)  r*  rr   )r   r   r  r  rH  r  s       r&   r  z<_make_read_resource_handler.<locals>._handler.<locals>._call  s     ' A A A A A A A A%~;;C@@@@@@@@A A A A A A A A A A A A A A A A A A A A A A A A A A A  "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KZ_````s   !A
AAc                  6    t                                  S r  r  r  s   r&   r  zA_make_read_resource_handler.<locals>._handler.<locals>._call_once  r  rJ   zresources/readzMCP %s/read_resource failed: %sr  r  r  r  r  r$  r   r  r)  r*  rY  r  r   rT  r]  r   rF  ri   r!  r   )rK  r  r  r  r%   rN  r  rH  r  r(   r  s         @@@r&   r  z-_make_read_resource_handler.<locals>._handlerq  s   ------ 	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	#V^ 	#:GGGG!# # # # hhuoo 	B:@AAA	a 	a 	a 	a 	a 	a	C 	C 	C 	C 	C 	C	#:<< 	. 	. 	.+----- 	# 	# 	#4S*.> I $       9S*.> I $       LL1;   :CS		(:CCcCC  "	# # # # # # # # #	#sJ   9= =	B" "E<	EEE E5E;AEEEr  r  s   `` r&   _make_read_resource_handlerr  n  s=    4#t 4## 4# 4# 4# 4# 4# 4# 4#l OrJ   c                 4     dt           dt          f fd}|S )z<Return a sync handler that lists prompts from an MCP server.rK  r	   c           
      n   t           5  t                                        d d d            n# 1 swxY w Y   rj        st	          j        dd did          S fdfd}	  |            S # t          $ r t                      cY S t          $ r}t          ||d          }||cY d }~S t          ||d          }||cY d }~S t                              d	|           t	          j        dt          d
t          |          j         d|           id          cY d }~S d }~ww xY w)NrF  r  r  Fr  c                    K   j         4 d {V  j                                         d {V } d d d           d {V  n# 1 d {V swxY w Y   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d          S )Npromptsr   rk   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   rk   required)r   r   rk   r  )r{   r   s     r&   r}   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* * *rJ   Fr  )r  r  list_promptsr   r  r   rk   r   rp   r)  r*  )r   r  ra  r  rH  s       r&   r  z;_make_list_prompts_handler.<locals>._handler.<locals>._call  s     ' = = = = = = = =%~::<<<<<<<<= = = = = = = = = = = = = = = = = = = = = = = = = = =G(/	(B(BJfnn & &1f%% +$%FE&M1m,, 9 9+,=E-(1k** q{ * * "#* * *E+& u%%%%:y'2GGGGr  c                  6    t                                  S r  r  r  s   r&   r  z@_make_list_prompts_handler.<locals>._handler.<locals>._call_once  r  rJ   zprompts/listzMCP %s/list_prompts failed: %sr  r  r  r  s	        @@r&   r  z,_make_list_prompts_handler.<locals>._handler  s:    	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	#V^ 	#:GGGG!# # # #	H 	H 	H 	H 	H,	C 	C 	C 	C 	C 	C	#:<< 	. 	. 	.+----- 	# 	# 	#4S*n I $       9S*n I $       LL0+s   :CS		(:CCcCC  "	# # # # # # # # #	#r  r  r  s   `` r&   _make_list_prompts_handlerr    s=    7#t 7## 7# 7# 7# 7# 7# 7# 7#r OrJ   c                 4     dt           dt          f fd}|S )zDReturn a sync handler that gets a prompt by name from an MCP server.rK  r	   c           
        	 ddl m} t          5  t                              
          	d d d            n# 1 swxY w Y   	r	j        st          j        dd
 did          S |                     d          s |d	          S |                     d
i           	fdfd}	  |            S # t          $ r t                      cY S t          $ r}t          
||d          }||cY d }~S t          
||d          }||cY d }~S t                              d
|           t          j        dt          dt!          |          j         d|           id          cY d }~S d }~ww xY w)Nr   r  rF  r  r  Fr  r   z!Missing required parameter 'name'r   c                    K   j         4 d {V  j                                       d {V } d d d           d {V  n# 1 d {V swxY w Y   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        |d          S )	Nr  r   r  r  rd   rk   Fr  )r  r  
get_promptr   r   r  r  rd   r   r   rp   rk   r)  r*  )	r   r   r,  r  r  respr   r   rH  s	         r&   r  z9_make_get_prompt_handler.<locals>._handler.<locals>._call  s!     ' T T T T T T T T%~888SSSSSSSST T T T T T T T T T T T T T T T T T T T T T T T T T T 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7777r  c                  6    t                                  S r  r  r  s   r&   r  z>_make_get_prompt_handler.<locals>._handler.<locals>._call_once	  r  rJ   zprompts/getzMCP %s/get_prompt failed: %sr  r  r  )rK  r  r  r  r%   rN  r  r   r   rH  r(   r  s         @@@@r&   r  z*_make_get_prompt_handler.<locals>._handler  s   ------ 	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	#V^ 	#:GGGG!# # # # xx 	C:ABBBHH["--		8 	8 	8 	8 	8 	8 	8.	C 	C 	C 	C 	C 	C	#:<< 	. 	. 	.+----- 	# 	# 	#4S*m I $       9S*m I $       LL.S   :CS		(:CCcCC  "	# # # # # # # # #	#sJ   :>>0	B: :E6	E6E12E68E1E6AE1+E61E6r  r  s   `` r&   _make_get_prompt_handlerr    s=    ?#t ?## ?# ?# ?# ?# ?# ?# ?#B OrJ   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 r@  )r  r$  r   r  )rH  r(   s    r&   _checkz_make_check_fn.<locals>._check3	  s     	/ 	/\\+..F	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/T!@fnD&@@s   044)r{  )r(   r  s   ` r&   _make_check_fnr  0	  s6    AD A A A A A A
 MrJ   schemac                     | sdi dS fdd }fd |           } ||          } |          }t          |t                    sdi dS |                    d          dk    rd|vri |di i}|S )ad  Normalize MCP input schemas for LLM tool-calling compatibility.

    MCP servers can emit plain JSON Schema with ``definitions`` /
    ``#/definitions/...`` references.  Kimi / Moonshot rejects that form and
    requires local refs to point into ``#/$defs/...`` instead.  Normalize the
    common draft-07 shape here so MCP tool schemas remain portable across
    OpenAI-compatible providers.

    Additional MCP-server robustness repairs applied recursively:

    * Missing or ``null`` ``type`` on an object-shaped node is coerced to
      ``"object"`` (some servers omit it).  See PR #4897.
    * When an ``object`` node lacks ``properties``, an empty ``properties``
      dict is added so ``required`` entries don't dangle.
    * ``required`` arrays are pruned to only names that exist in
      ``properties``; otherwise Google AI Studio / Gemini 400s with
      ``property is not defined``.  See PR #4651.
    * MCP/Pydantic optional fields commonly arrive as
      ``anyOf: [{...}, {"type": "null"}], default: null``.  Anthropic rejects
      nullable branches in tool input schemas, so nullable unions are collapsed
      to the non-null branch and optionality remains represented solely by the
      parent object's ``required`` list.

    All repairs are provider-agnostic and ideally produce a schema valid on
    OpenAI, Anthropic, Gemini, and Moonshot in one pass.
    objectr!  
propertiesc                    t          | t                    ri }|                                 D ]\  }}|dk    rdn|} |          ||<   |                    d          }t          |t                    r2|                    d          rd|t          d          d          z   |d<   |S t          | t                    rfd| D             S | S )Ndefinitionsz$defsz$refz#/definitions/z#/$defs/c                 &    g | ]} |          S rI   rI   )r{   r   _rewrite_local_refss     r&   r}   zL_normalize_mcp_input_schema.<locals>._rewrite_local_refs.<locals>.<listcomp>h	  s%    ???$''--???rJ   )r   r~   r\   r   r   r^   r   r
  )r   
normalizedra   rb   out_keyrefr  s         r&   r  z8_normalize_mcp_input_schema.<locals>._rewrite_local_refs]	  s    dD!! 	J"jjll A A
U%(M%9%9''s&9&9%&@&@
7##..((C#s## N7G(H(H N%/#c:J6K6K6L6L2M%M
6"dD!! 	@????$????rJ   c                 (    ddl m}  || d          S )a  Collapse JSON Schema nullable unions to provider-safe non-null schemas.

        Delegates to ``tools.schema_sanitizer.strip_nullable_unions`` so MCP
        ingestion, the Anthropic guard, and the global sanitizer all share one
        implementation. Keeps the ``nullable: true`` hint so runtime argument
        coercion can still map a model-emitted ``"null"`` string to Python
        ``None`` for this optional field.
        r   )strip_nullable_unionsT)keep_nullable_hint)tools.schema_sanitizerr  )r   r  s     r&   _strip_nullable_unionz:_normalize_mcp_input_schema.<locals>._strip_nullable_unionk	  s-     	A@@@@@$$TdCCCCrJ   c                 "   t          | t                    rfd| D             S t          | t                    s| S fd|                                 D             }|                    d          sd|v sd|v rd|d<   |                    d          dk    rd|vs(t          |                    d          t                    s>d|vri n|d         |d<   t          |                    d          t                    si |d<   |                    d          }t          |t                    rc|                    d          pi fd|D             }t          |          t          |          k    r|r||d<   n|                    dd           |S )	zBRecursively repair object-shaped nodes: fill type, prune required.c                 &    g | ]} |          S rI   rI   )r{   r   _repair_object_shapes     r&   r}   zM_normalize_mcp_input_schema.<locals>._repair_object_shape.<locals>.<listcomp>{	  s%    @@@4((..@@@rJ   c                 .    i | ]\  }}| |          S rI   rI   )r{   r  r  r  s      r&   r  zM_normalize_mcp_input_schema.<locals>._repair_object_shape.<locals>.<dictcomp>	  s+    HHH41aA++A..HHHrJ   r!  r  r  r  c                 F    g | ]}t          |t                    |v |S rI   )r   r   )r{   r  propss     r&   r}   zM_normalize_mcp_input_schema.<locals>._repair_object_shape.<locals>.<listcomp>	  s-    RRRq
1c0B0BRqEzzzzzrJ   N)r   r
  r~   r\   r   r   r  )r   repairedr  validr  r  s       @r&   r  z9_normalize_mcp_input_schema.<locals>._repair_object_shapex	  s   dD!! 	A@@@@4@@@@$%% 	KHHHH4::<<HHH ||F## 	(H$$
h(>(>'HV<<8++8++:\**D4 4+ 0<8/K/KQYZfQg&!(,,|"<"<dCC 0-/H\*  ||J//H(D)) 7 \228bRRRRHRRRu::X.. 7/4,, Z666rJ   r!  r  )r   r~   r   )r  r  r  r  r  s      @@r&   rc  rc  ?	  s    6  4 333    D D D$ $ $ $ $L %$V,,J&&z22J%%j11J j$'' 4 333~~f))l*.L.L5
5L"55
rJ   rb   c                 L    t          j        ddt          | pd                    S )a5  Return an MCP name component safe for tool and prefix generation.

    Preserves Hermes's historical behavior of converting hyphens to
    underscores, and also replaces any other character outside
    ``[A-Za-z0-9_]`` with ``_`` so generated tool names are compatible with
    provider validation rules.
    z[^A-Za-z0-9_]r  ry   )r   rg   r   )rb   s    r&   r  r  	  s%     6"CU[b)9)9:::rJ   c           	          t          |j                  }t          |           }d| d| }||j        pd|j         d|  t          t	          |dd                    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=...)``.
    r  r  z	MCP tool z from ra  Nrb  )r  r   rk   rc  r   )r(   mcp_toolsafe_tool_namesafe_server_nameprefixed_names        r&   _convert_mcp_schemar  	  s     1??N2;??>+>>n>>M+]/]8=/]/]P[/]/]1'(MSW2X2XYY  rJ   c                     t          |           }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i 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  _list_resourcesz*List available resources from MCP server 'r   r  r  rb  r  )r  handler_key_read_resourcez(Read a resource by URI from MCP server 'r  stringzURI of the resource to read)r!  rk   )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 promptT)r!  rk   r  additionalPropertiesr  r   r  )r  )r(   	safe_names     r&   _build_utility_schemasr  	  s    ,K88I :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*,48	& &# # "(  ( (+	
 	
Q? ?rJ   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 rI   )r   r  s     r&   r  z)_normalize_name_filter.<locals>.<setcomp>
  s    ,,,dD		,,,rJ   z>MCP config %s must be a string or list of strings; ignoring %r)r  r   r   r
  r7  r   rq   )rb   r  s     r&   _normalize_name_filterr  
  sx    }uu% w%$s+,, -,,e,,,,
NNSUZ\abbb55LrJ   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)r   r{  r   r   r   r   rq   )rb   r   lowereds      r&   _parse_boolishr   
  s    }% % ++--%%''00041115
NNVX]_fgggNrJ   r  r  r  r  r  r  r  r  rH  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.rT  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))	r   r  r  r   r   _UTILITY_CAPABILITY_METHODSr   r  rp   )
r(   rH  r   tools_filterresources_enabledprompts_enabledselectedr  r  required_methods
             r&   _select_utility_schemasr'  8
  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	   OrJ   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$  r\   r   r   r  r  r  r   rp   )names_snamerH  r   r  s        r&   _existing_tool_namesr+  U
  s    E"..** ) )6344 	LL6777 	) 	)H(h??FLL((((	) LrJ   c                 d   ddl 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                   5t          | |j        |j        pd           t          | |          }	|	d         }
|                    |
          }|r9|                    d          s$t                              d| |j        |
|           |                    |
||	t#          | |j        |j                  t'          |           d|	d                    |                    |
           t*          t,          t.          t0          d}t'          |           }t3          | ||          D ]}|d         }	|d         } ||         | |j                  }|	d         }|                    |          }|r3|                    d          st                              d| ||           {|                    |||	||d|	d                    |                    |           |r|                    | |           |S )a  Register tools from an already-connected server into the registry.

    Handles include/exclude filtering and utility tools. Toolset resolution
    for ``mcp-{server}`` and raw server-name aliases is derived from the live
    registry, rather than mutating ``toolsets.TOOLSETS`` at runtime.

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

    Returns:
        List of registered prefixed tool names.
    r   r  zmcp-rT  includezmcp_servers.z.tools.includeexcludez.tools.excluderj   r	   c                      r| v S r| vS dS )NTrI   )rj   exclude_setinclude_sets    r&   _should_registerz0_register_server_tools.<locals>._should_register}
  s.     	,++ 	0K//trJ   z8MCP server '%s': skipping tool '%s' (filtered by config)ry   r   us   MCP server '%s': tool '%s' (→ '%s') collides with built-in tool in toolset '%s' — skipping to preserve built-inFrk   )r   toolsetr  handlercheck_fnis_asyncrk   r  r  r  up   MCP server '%s': utility tool '%s' collides with built-in tool in toolset '%s' — skipping to preserve built-in)r  r  r   r  r   r{  r  r   r   r   rv   rk   r  get_toolset_for_toolr^   rq   registerr  r  r  rp   r  r  r  r  r'  register_toolset_alias)r   rH  r   r  registered_namestoolset_namer"  r2  r   r  tool_name_prefixedexisting_toolset_handler_factoriesr5  r  r  r4  	util_namer0  r1  s                     @@r&   r  r  b
  s]    ('''''"$ $==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 	dHM83G3M2NNN$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  
  }- 	 	
 	
 	
 		**** <''l;;;rJ   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  rh  Nr(  HTTPstdioz/MCP server '%s' (%s): registered %d tool(s): %sr\  )r   r  rq  rr  r  r  r$  r  r
  r  r   r   r   rr   )r   r   r  rH  r:  transport_types         r&   _discover_and_register_serverrD  
  s?     
 jj!24LMMO#f%%        F 
                                  .dFFCC$()9$:$:F!$VVGN
KK9nc"233		"##  
 s   A&&A*-A*r  c                 $   t           st                              d           g S | st                              d           g S t          5  d |                                 D             ddd           n# 1 swxY w Y   st                      S t                       dt          dt          dt          t                   fdfd	}t           |            d
           t          5  d D             }t          d |D                       }ddd           n# 1 swxY w Y   t                    t          |          z
  }|s|r;d| dt          |           d}|r	|d| dz  }t                              |           t                      S )a[  Connect to explicit MCP servers and register their tools.

    Idempotent for already-connected server names. Servers with
    ``enabled: false`` are skipped without disconnecting existing sessions.

    Args:
        servers: Mapping of ``{server_name: server_config}``.

    Returns:
        List of all currently registered MCP tool names.
    z;MCP SDK not available -- skipping explicit MCP registrationz No explicit MCP servers providedc                 v    i | ]6\  }}|t           vt          |                    d d          d          3||7S r  Tr   r$  r  r   r  s      r&   r  z(register_mcp_servers.<locals>.<dictcomp>  sT     
 
 
1  ^AEE)T4J4JTX%Y%Y%Y  q   rJ   Nr   r  r	   c                 2   K   t          | |           d{V S )z@Connect to a single server and return its registered tool names.N)rD  )r   r  s     r&   _discover_onez+register_mcp_servers.<locals>._discover_one  s(      24=========rJ   c            	        K   t                                                    } t          j        fd                                D             ddi d {V }t          | |          D ]u\  }}t          |t                    r[                    |i                               d          }t          
                    d||rd| dndt          |                     vd S )	Nc              3   6   K   | ]\  }} ||          V  d S r@  rI   )r{   r   r  rJ  s      r&   r	  z>register_mcp_servers.<locals>._discover_all.<locals>.<genexpr>  s3      LL94mmD#&&LLLLLLrJ   r   Tr   z*Failed to connect to MCP server '%s'%s: %sz
 (command=)ry   )r
  keysrq  r!  r\   zipr   r   r   r   rq   r   )server_namesresultsr   r   r   rJ  new_serverss        r&   _discover_allz+register_mcp_servers.<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	  	 	rJ   rL   rh  c                 $    g | ]}|t           v |S rI   r$  r{   r*  s     r&   r}   z(register_mcp_servers.<locals>.<listcomp>)  s    ===1qH}}Q}}}rJ   c              3   h   K   | ]-}t          t          t          |         d g                     V  .dS r  Nr   r   r$  rV  s     r&   r	  z'register_mcp_servers.<locals>.<genexpr>*  sO       
 
 %=rBBCC
 
 
 
 
 
rJ   zMCP: registered  tool(s) from 
 server(s) ( failed))rC   r   r   r  r\   r+  rv  r   r~   r   rI  sumr   r   )r  rS  	connectednew_tool_countfailedsummaryrJ  rR  s         @@r&   register_mcp_serversrc  
  sq     RSSS	 7888	 
 
 

 

 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
  &#%%% ># >D >T#Y > > > >     & ]]__c2222 
 
 
=====	 
 

 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 I.F  ]^]]3y>>]]] 	-,F,,,,GG!!!s$   A66A:=A:)&DD"Dc                     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   t          |           }|s|S t          5  d |D             }t          d |D                       }ddd           n# 1 swxY w Y   t          |          t          |          z
  }|s|r;d| dt          |           d	}|r	|d
| dz  }t          	                    |           |S )a  Entry point: load config, connect to MCP servers, register tools.

    Called from ``model_tools`` after ``discover_builtin_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                 t    g | ]5\  }}|t           vt          |                    d d          d          3|6S rG  rH  r  s      r&   r}   z&discover_mcp_tools.<locals>.<listcomp>N  sR     
 
 
c8##swwy$7O7OY](^(^(^# ###rJ   Nc                 $    g | ]}|t           v |S rI   rU  r{   r   s     r&   r}   z&discover_mcp_tools.<locals>.<listcomp>Y  s"    !X!X!X4txGWGW$GWGWGWrJ   c              3   h   K   | ]-}t          t          t          |         d g                     V  .dS rX  rY  rg  s     r&   r	  z%discover_mcp_tools.<locals>.<genexpr>Z  sO       
 
 (@"EEFF
 
 
 
 
 
rJ   z  MCP: rZ  r[  r\  r]  )
rC   r   r   r  r  r\   rc  r^  r   r   )r  new_server_names
tool_namesconnected_server_namesr`  failed_countrb  s          r&   discover_mcp_toolsrm  8  s     KLLL	  G 0111		 
 

 
$]]__
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 &g..J 	 
 
!X!X3C!X!X!X 
 
.
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 '((3/E+F+FFL  aNaa#>T:U:Uaaa 	32L2222GGs$   BB	B'&CC C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(  httprB  r  T)r   	transportrT  r_  r  r   F)r  r  r~   r$  r\   r   r  r   r   r  r  r  r   rp   )r   
configuredactive_serversr   r  rp  rH  r  s           r&   get_mcp_statusrs  i  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 rG  )r  r   r  s      r&   r  z*probe_mcp_server_tools.<locals>.<dictcomp>  sO       A!%%	400$???	1  rJ   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  rh  r   Tz$Probe: failed to connect to '%s': %srk   ry   c              3   >   K   | ]}|                                 V  d S r@  r  )r{   ss     r&   r	  z=probe_mcp_server_tools.<locals>._probe_all.<locals>.<genexpr>  s*      33qajjll333333rJ   )r
  rN  r\   r   r  rp   rq  rr  r  r!  rO  r   r   r   r   r  r   r   )r)  corosr   r  ctoutcomesoutcomerT  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
"
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
rJ   rL   rh  zMCP probe failed: %sN)	rC   r  r\   rv  rI  r   r   r   _stop_mcp_loop)servers_configr  r%   r  r  r   s      @@@r&   probe_mcp_server_toolsr    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@  rx  )r{   rH  s     r&   r	  z:shutdown_mcp_servers.<locals>._shutdown.<locals>.<genexpr>  s,      ??Ffoo??????rJ   r   Tz!Error closing MCP server '%s': %s)rq  r!  rO  r   r   r   r   r   r  r$  r  )rQ  rH  r   servers_snapshots      r&   	_shutdownz'shutdown_mcp_servers.<locals>._shutdown  s     ??.>???
"
 
 
 
 
 
 
 
 ""2G<< 	 	NFF&),, 7f    	 	NN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   8BB#&B#rD  rh  zError during MCP shutdown: %s)r  r
  r$  valuesr  rJ  rK  rq  rz  r   r   r   r   )r  rQ  r~  r%   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>include_activec                 b   ddl }ddl}t          5  i }t          D ]}d||<   t                                           | r@|                    t          t                               t                                           ddd           n# 1 swxY w Y   |sdS |                                D ]Y\  }}	 t          j
        ||j                   t                              d||           =# t          t          t           f$ r Y Vw xY w|                    d           t%          |d|j                  }|                                D ]i\  }}	 t          j
        |d           t          j
        ||           t                              d||           M# t          t          t           f$ r Y fw xY wdS )u5  Best-effort graceful shutdown of stdio MCP subprocesses to reap orphans.

    Orphans are PIDs that survived their session context exit (SDK teardown
    did not terminate the process — common on Linux when stdio children escape
    the parent cgroup on cancellation). By default only entries in
    ``_orphan_stdio_pids`` are reaped so concurrent cron jobs and live user
    sessions are not disrupted.

    Sends SIGTERM, waits 2 seconds, then escalates to SIGKILL for any
    survivors, avoiding shared-resource collisions when multiple hermes
    processes run on the same host (each has its own ``_stdio_pids`` dict).

    With ``include_active=True`` also kills every PID in ``_stdio_pids`` —
    used only at final shutdown, after the MCP event loop has stopped and no
    sessions can still be in flight.
    r   Norphanz,Sent SIGTERM to orphaned MCP process %d (%s)r  SIGKILLz6Force-killed MCP process %d (%s) after SIGTERM timeout)signalr   r  r  r  r_   r~   r  r\   r   r  SIGTERMr   r   r  r  r  r  r   rq   )r  _signal_timepidsopidr  r(   _sigkills           r&   _kill_orphaned_mcp_childrenr    s!   " 	    !& 	" 	"D!DJJ  """ 	 KK[))***                                !JJLL  [	GC)))LLGkZZZZ"OW= 	 	 	D	 
KKNNN w	7?;;H JJLL 	 	[	GCOOOGC"""NNH[    #OW= 	 	 	D		 	s7   A-B		BB26C))DDAFF,+F,c                  @   t           5  t          } t          }dadaddd           n# 1 swxY w Y   | j|                     | j                   ||                    d           	 |                                  n# t          $ r Y nw xY wt          d           dS dS )z3Stop the background event loop and join its thread.NrN   rh  T)r  )	r  rJ  r^  rL  r   rr   closer   r  )rQ  threads     r&   r  r  6  s     
  		              
 !!$),,,KKK"""	JJLLLL 	 	 	D	
 	$4888888 s   '++'A< <
B	B	)r   )T)F)rz  rq  concurrent.futuresr{  rD   r)  r   r   r   r   r   r    rs  r   r   typingr   r   r   r   	getLoggerr   r   r   __annotations__r  r   r'   r   r/   rC   r  r:  r  r  r5   re  r0   r1   mcp.client.stdior2   mcp.client.streamable_httpr3   r   r4   r  	mcp.typesr   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   r{  rK   r  r  r  r  r  	frozensetr]   compile
IGNORECASErf   r~   rc   ri   Irn   rv   r   r7  r   r   r   r   r   r   r~  r$  r%  r&  r   r(  r  r+  r-  r.  r9  r<  rT  rU  rZ  r]  rJ  AbstractEventLoopr^  rt  r  r  r  r  r  rn  rv  rI  r  r  r  r  r  r  r  r  r  r  rc  r  r  r  r  r  r!  r'  r+  r  rD  rc  rm  rs  r  r  r  r  rI   rJ   r&   <module>r     s  D D DL          				 				  



            , , , , , , , , , , , ,		8	$	$( %) HSM ( ( (%y~''  "S  "  "  "  "F# $    &    !&  ' .K88888888------N$DDDDDD" $ $ $#$EEEEEE   k5555555 k k kijjjjjk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" RZ<bdCC?ARZ&--57RZDbdKKRZ "$''&(RZ3RT::!#RZ:BDAA RZ0"$77%'RZ2BD99 RZ&--!RZ924@@!#' 0s s  QUVYQZ    *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y y y y y y y y@ &($sM!
" ' ' '& (* d38n ) ) ).0 4U
+ 0 0 0  $ 
BC 
BD 
B 
B 
B 
B5S 5T 5 5 5 5   5   'u ' ' ' 'T $    &kk	k 	k k k kd# %   E= ET E E E E4PP	P 	P P P Ph 26	8G-. 5 5 5*.Xi&' . . . 		 !T#s(^       #%% C   c    4, , ,    E    B#    
 
 
$sDy/    B T m    (C C u    D7c 7 7 7 7 7t9S 9 9 9 9 9x<C <u < < < <~D# DU D D D DN    it i i i i iX;s ;s ; ; ; ;S t    *F FT
 F F F FR	# 	c 	c#h 	 	 	 	 #      " '$"	   m T VZ[_V`    :
d3i 
 
 
 
i im iT idSVi i i i iXc 4 DI    :J"$sDy/ J"d3i J" J" J" J"Z.DI . . . .b%T
 % % % %P?S$u+%5 6 ? ? ? ?D% % %P8 8 8 8 8 8 8v9 9 9 9 9s   E B' &E 'B1.E 0B11E 5B> =E >CE CE C E C0-E /C00E 4D	 E 	D&#E %D&&E *D9 8E 9EE EE E76E7