
    CjE                    <   S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKJrJ	r	  S SK
Jr  S SKJrJrJrJr  S SKrS SKJrJr  SSKJr  SS	KJrJr  SS
KJrJrJrJr   S SKJ r!  SSK#J#r#  SSK$J%r%  SSK&J'r'  SSK(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0J1r1J2r2  SSK3J4r4  SSK5J6r6J7r7J8r8  SSK9J:r:  \(       a  \/\.-  r;\<\;-  \=\;   -  r>O\\/\.4   r;\\<\;\=\;   4   r>\(       a  S SK?J*r@  SSKAJBrB  SSKCJDrD   " S S\5      rE\ " S S5      5       rF\(       a  \	\F\@/\:\G   4   rHO\	\F\/\:\G   4   rH  " S S\5      rI\(       a	  \H\I-  S-  rJO\\H\IS4   rJ \ " S S5      5       rK\(       a  \	\K/\:\L\<\4   S-     4   rMO\	S\4   rM   S#     S$S  jjrN " S! S"5      rOg! \" a    Sr! GN8f = f)%    )annotationsN)	AwaitableCallable)	dataclass)TYPE_CHECKINGAnyProtocolUnion)NotRequired	TypedDict   )_debug)&resolve_mcp_tool_description_for_modelresolve_mcp_tool_title)AgentsExceptionMCPToolCancellationErrorModelBehaviorError	UserError)McpError)logger)RunContextWrapper)ensure_strict_json_schema)
FunctionToolToolToolErrorFunction
ToolOriginToolOriginTypeToolOutputImageDictToolOutputTextDict*_build_handled_function_tool_error_handler_build_wrapped_function_tooldefault_tool_error_function)ToolContext)FunctionSpanDataget_current_spanmcp_tools_span)MaybeAwaitable)r   )	AgentBase   )	MCPServerc                  :    \ rS rSrSr   S       SS jjrSrg)HttpClientFactory8   zProtocol for HTTP client factory functions.

This interface matches the MCP SDK's McpHttpClientFactory but is defined locally
to avoid accessing internal MCP SDK modules.
Nc                    g N )selfheaderstimeoutauths       a/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/agents/mcp/util.py__call__HttpClientFactory.__call__?   s    
      r0   )NNN)r2   zdict[str, str] | Noner3   zhttpx.Timeout | Noner4   zhttpx.Auth | Nonereturnzhttpx.AsyncClient)__name__
__module____qualname____firstlineno____doc__r6   __static_attributes__r0   r8   r5   r,   r,   8   sB     *.(,"&	 &  &   	 
 
   r8   r,   c                  <    \ rS rSr% SrS\S'    S\S'    S\S'   S	rg
)ToolFilterContextG   z7Context information available to tool filter functions.RunContextWrapper[Any]run_contextr(   agentstrserver_namer0   Nr:   r;   r<   r=   r>   __annotations__r?   r0   r8   r5   rA   rA   G   s     A''"5%r8   rA   c                  0    \ rS rSr% SrS\S'    S\S'   Srg)ToolFilterStaticd   zAStatic tool filter configuration using allowlists and blocklists.zNotRequired[list[str]]allowed_tool_namesblocked_tool_namesr0   NrH   r0   r8   r5   rK   rK   d   s    K..3 /.1r8   rK   c                  H    \ rS rSr% SrS\S'    S\S'    S\S'    S\S	'   S
rg)MCPToolMetaContextw   zBContext information available to MCP tool meta resolver functions.rC   rD   rF   rG   	tool_namedict[str, Any] | None	argumentsr0   NrH   r0   r8   r5   rP   rP   w   s(    L''"%N-$$$r8   rP   .c                4    U c  Uc  g0 nU b  XS'   Ub  XS'   U$ )as  Create a static tool filter from allowlist and blocklist parameters.

This is a convenience function for creating a ToolFilterStatic.

Args:
    allowed_tool_names: Optional list of tool names to allow (whitelist).
    blocked_tool_names: Optional list of tool names to exclude (blacklist).

Returns:
    A ToolFilterStatic if any filtering is specified, None otherwise.
NrM   rN   r0   )rM   rN   filter_dicts      r5   create_static_tool_filterrW      s<     !&8&@$&K%,>()%,>()r8   c                  8   \ rS rSrSr\SS j5       r\\4           SS jj5       r	\\4           SS jj5       r
\S\4           SS jj5       r\      SS j5       r\          SS	 j5       r\SS
.           SS jj5       rSrg)MCPUtil   z>Set of utilities for interop between MCP and Agents SDK tools.c                   [        U SS 5      n[        U[        5      (       a  [        R                  " U5      $ [        U SS 5      n[        U[        5      (       a<  UR                  S5      n[        U[        5      (       a  [        R                  " U5      $ [        U SS 5      n[        U5      (       aX  U" 5       n[        U[        5      (       a<  UR                  S5      n[        U[        5      (       a  [        R                  " U5      $ g )Nmetamodel_extra
model_dump)getattr
isinstancedictcopydeepcopygetcallable)toolr\   r]   
extra_metar^   dumpeddumped_metas          r5   _extract_static_metaMCPUtil._extract_static_meta   s    tVT*dD!!==&&dM48k4(($0J*d++}}Z00T<6
J\F&$''$jj0k400==55r8   c           	     8  #    / n[        5       nU H|  nU R                  UUUUUS9I Sh  vN n	U	 V
s1 s H  oR                  iM     nn
[        X-  5      S:  a  [	        SX-   35      eUR                  U5        UR                  U	5        M~     U$  Nhs  sn
f 7f)z2Get all function tools from a list of MCP servers.failure_error_functionNr   z/Duplicate tool names found across MCP servers: )setget_function_toolsnamelenr   updateextend)clsserversconvert_schemas_to_strictrD   rE   rn   tools
tool_namesserverserver_toolsrf   server_tool_namess               r5   get_all_function_toolsMCPUtil.get_all_function_tools   s      "u
F!$!7!7)'= "8 " L 8D D|t| D$12Q6E(568  /0LL& " ! !Es!   (BBBBABBc                L  #    [        UR                  S9 nUR                  X45      I Sh  vN nU Vs/ s H  oR                  PM     snUR                  l        SSS5        W Vs/ s H  nU R                  UUUUUS9PM     sn$  N[s  snf ! , (       d  f       N9= fs  snf 7f)z0Get all function tools from a single MCP server.)rz   Nrm   )r&   rq   
list_tools	span_dataresultto_function_tool)	ru   rz   rw   rD   rE   rn   spanrx   rf   s	            r5   rp   MCPUtil.get_function_tools   s      6;;/4 ++K??E;@$A54YY5$ADNN! 0 	
    )'= !  	
 		
 @$A 0/	
sI   B$BBBB		BB$'BB$B	B
BB$Nc                   U R                  U5      n[        R                  " U R                  UUUS9nUR	                  U5      nUR
                  SpSU	;  a  0 U	S'   U(       a   [        U	5      n	Sn
UR                  X5      n[        UR                  [        U5      U	U[        SSS	9UU
U[        U5      [!        ["        R$                  UR                  S
9S9
nU$ ! [         a#  n[        R                  " SU 35         SnANSnAff = f)a  Convert an MCP tool to an Agents SDK function tool.

The ``agent`` parameter is optional for backward compatibility with older
call sites that used ``MCPUtil.to_function_tool(tool, server, strict)``.
When omitted, this helper preserves the historical behavior for static
policies. If the server uses a callable approval policy, approvals default
to required to avoid bypassing dynamic checks.
r\   F
propertiesTz,Error converting MCP schema to strict mode: NzError running tool (non-fatal)zMCP tool)span_message	log_label)typemcp_server_name)
rq   descriptionparams_json_schemainvoke_tool_implon_handled_errorrn   strict_json_schemaneeds_approval	mcp_titletool_origin)rj   	functoolspartialinvoke_mcp_tool_get_failure_error_functioninputSchemar   	Exceptionr   info_get_needs_approval_for_toolr!   rq   r   r    r   r   r   MCP)ru   rf   rz   rw   rE   rn   static_metainvoke_func_impl effective_failure_error_functionschema	is_stricter   function_tools                 r5   r   MCPUtil.to_function_tool  s+   " ..t4$,,	
 ,2+M+M",
( !,,e	 v%#%F< $P26: 	 //< 	 5>tD%-G=$ $D(),T2"#'' &
$ 3  PJ1#NOOPs   $C 
D$DDc                h    U c  Uc  g 0 nU b  UR                  U 5        Ub  UR                  U5        U$ r/   )rs   )resolved_metaexplicit_metamergeds      r5   _merge_mcp_metaMCPUtil._merge_mcp_metaF  s@    
  ]%:!#$MM-($MM-(r8   c                8  #    [        USS 5      nUc  g Ub  [        R                  " U5      OS n[        UUR                  UUS9nU" U5      n[
        R                  " U5      (       a
  UI S h  vN nUc  g [        U[        5      (       d  [        S5      eU$  N*7f)Ntool_meta_resolver)rD   rG   rR   rT   z-MCP meta resolver must return a dict or None.)
r_   rb   rc   rP   rq   inspectisawaitabler`   ra   	TypeError)	ru   rz   contextrR   rT   meta_resolverarguments_copyresolver_contextr   s	            r5   _resolve_metaMCPUtil._resolve_metaT  s       (<dC 5>5Jy1PT-$	
 /0v&&!\F>&$''KLL "s   A+B-B.+Br   c                 #     U(       a  [         R                  " U5      O0 n[        R                  (       a$  [
        R                  " SUR                   35        O&[
        R                  " SUR                   SU 35         U R                  XUR                  U5      I Sh  vN nU R                  X5      n	[        R                  " U	c  UR                  UR                  U5      OUR                  UR                  XiS95      n
 [        R                  " U
1[        R                  S9I Sh  vN u  pUR!                  5       nUR#                  5       (       a&  [%        SUR                   S	UR                   S
35      eUR'                  5       n[        R                  (       a%  [
        R                  " SUR                   S35        O&[
        R                  " SUR                   SU 35        UR@                  (       a2  URB                  (       a!  [         RD                  " URB                  5      nO/ nURF                   H  nURH                  S:X  a%  URK                  [M        SURN                  S95        M8  URH                  S:X  a5  URK                  [Q        SSURR                   SURT                   3S95        M}  URK                  [M        S[;        URW                  SS95      S95        M     [Y        U5      S:X  a  US   nOUn[[        5       nU(       a  [3        UR\                  [^        5      (       am  [3        U[`        5      (       a(  URb                  b  URb                  Rd                  (       a  UUR\                  l3        SUR                  0UR\                  l4        U$ [
        R<                  " SU 35        U$ ! [         a  n[        R                  (       a$  [
        R                  " SUR                   35        O&[
        R                  " SUR                   SU 35        [        SUR                   SU 35      UeSnAff = f GN GN"! [        R(                   aS    U
R+                  5       (       d  U
R-                  5          U
I Sh  vN    e ! [        R(                  [        4 a     e f = ff = f! [.        [$        4 a    e [         a  n[0        b  [3        U[0        5      (       aw  [5        US5      (       a'  UR6                  (       a  UR6                  R8                  O
[;        U5      n[
        R<                  " SUR                   SUR                   SU 35        e [
        R6                  " SUR                   SUR                   SU 35        [?        SUR                   SUR                   SU 35      UeSnAff = f7f)z7Invoke an MCP tool and return the result as ToolOutput.zInvalid JSON input for tool z: NzInvoking MCP tool z with input r   )return_whenzFailed to call tool 'z' on MCP server 'z ': tool execution was cancelled.errorz	MCP tool z on server 'z' returned an error: zError invoking MCP tool z': z completed.z
 returned text)r   r   imagezdata:z;base64,)r   	image_urljson)moder)   r   rz   z>Current span is not a FunctionSpanData, skipping tool output: )5r   loadsr   r   DONT_LOG_TOOL_DATAr   debugrq   r   r   r   asynciocreate_task	call_toolwaitFIRST_COMPLETEDpop	cancelledr   r   CancelledErrordonecancelr   	_McpErrorr`   hasattrr   messagerF   warningr   use_structured_contentstructuredContentdumpscontentr   appendr   r   r   mimeTypedatar^   rr   r%   r   r$   r#   
run_configtrace_include_sensitive_dataoutputmcp_data)ru   rz   rf   r   
input_jsonr\   	json_datar   r   merged_meta	call_taskr   _finished_taskr   
error_texttool_outputtool_output_listitemcurrent_spans                       r5   r   MCPUtil.invoke_mcp_toolp  s    		BL

:(>RTI $$LL-dii[9:LL-dii[ZLQR.	"%"3"3FTYYPY"ZZM--mBK++&   I6%%dii%MI
 'i[gF]F] ^^ $
 **,,2/		{:KFKK= Y8 8  '--/@ $$LL9TYYK{;<LL9TYYKz&BC ((V-E-E**V%=%=>K5799&$++,>FQUQZQZ,[\YY')$+++!(eDMM?(SWS\S\R]4^ %++*SV\A]=^_ ' #$).q1.'),002BCC!';77&&.'2D2D2a2a4?L**1fkk3&&/ 	 TUaTbc W  	((;DII;GH;DII;bUV$.tyykJ<H	 [ _ ))  ~~''$$&#OO   ..	:  34 	  	$Ay)A)A 1870C0CQWW__UXYZU[
		{,v{{mCX!l$ LL3DII;l6;;-WZ[\Z]^_!*499+\&++cRSQTU	s   WO  AW R= #Q$A"R= 'Q .Q/A!Q H0W 
Q

A;QQ

WR= Q 9R:RRRR:R63R:5R66R::R= =WC/WWWr0   )rf   r   r9   rS   )rv   zlist[MCPServer]rw   boolrD   rC   rE   r(   rn   ToolErrorFunction | Noner9   
list[Tool])rz   r*   rw   r   rD   rC   rE   r(   rn   r   r9   r   )rf   MCPToolrz   r*   rw   r   rE   zAgentBase | Nonern   r   r9   r   )r   rS   r   rS   r9   rS   )
rz   r*   r   rC   rR   rF   rT   rS   r9   rS   )rz   r*   rf   r   r   rC   r   rF   r\   rS   r9   
ToolOutput)r:   r;   r<   r=   r>   staticmethodrj   classmethodr"   r}   rp   r   r   r   r   r?   r0   r8   r5   rY   rY      s   H *  <W  $( ,	
  !9 
 <  <W

 $(
 ,	

 
 !9
 

 
2  #';V== = $(	=
  = !9= 
= =~ ,, 
   ( 	
 ) 
 6  '+ww w (	w
 w $w 
w wr8   rY   )NN)rM   list[str] | NonerN   r   r9   zToolFilterStatic | None)P
__future__r   r   rb   r   r   r   collections.abcr   r   dataclassesr   typingr   r   r	   r
   httpxtyping_extensionsr   r    r   _mcp_tool_metadatar   r   
exceptionsr   r   r   r   mcp.shared.exceptionsr   r   ImportErrorr   rD   r   strict_schemar   rf   r   r   r   r   r   r   r   r    r!   r"   tool_contextr#   tracingr$   r%   r&   util._typesr'   ToolOutputItemrF   listr   	mcp.typesr   rE   r(   rz   r*   r,   rA   r   ToolFilterCallablerK   
ToolFilterrP   ra   MCPToolMetaResolverrW   rY   r0   r8   r5   <module>r     s   "      / ! 6 6  4  _ a a;  + 5   ' H H ('*==N~%^(<<J-/BBCNsND,@@AJ)!!    
& 
& 
& !#4g">t@T"TU!#4c":N4<P"PQ	1y 	1 #&66=J)+;TABJ ` % % %  "	tCH~,-	/
 #38, ,0+/(( 6t t  Is   F FF