
    Ojk                        % S r SSKrSSKrSSKJrJr  SSKrSSKJr  SSKr	SSK
Jr  SSKJr  SSKJr  SS	KJr  \R"                  (       a  SS
KJr  SSKJr  SSKJr  SSKJr  OI\r\R8                  \S'   \r\R8                  \S'   Sr\\S'   Sr\\S'    SS
KJr  SSKJr  SSKJr  SSKJr  S\S\R&                  4S jrS\S\R&                  4S jr  S"S\!\   S\"S\!\R&                     4S jjr#S\RH                  S\"4S jr%S\RH                  S\"4S jr&S\'\(\(4   SS4S jr)S\RT                  S\RT                  4S jr+\RX                  S\S \(S\RZ                  \   4S! j5       r.g! \ a    SrSrSrSr Nf = f)#z!Utils for working with MCP tools.    N)PackageNotFoundErrorversion)Any)Request   )_common)types)_MULTI_REGIONAL_LOCATIONS)Tool)ClientSession)streamable_http_client)create_mcp_http_clientMcpClientSessionMcpToolr   r   toolreturnc                     [         R                  " U R                  U R                  [         R                  R                  [         R                  " S0 [        U R                  5      D6S9S./S9$ )z.Translates an MCP tool to a Google GenAI tool.)json_schema)namedescription
parametersfunction_declarations )	r	   r   r   r   Schemafrom_json_schema
JSONSchema_filter_to_supported_schemainputSchemar   s    Y/var/www/html/leaf-doctor-v1/venv/lib/python3.13/site-packages/google/genai/_mcp_utils.pymcp_to_gemini_toolr"   6   se    	))))55** /0@0@A 6  	

 
    c                 p    [         R                  " U R                  U R                  U R                  S./S9$ )z9Translates an Agent Platform tool to a Google GenAI tool.)r   r   parameters_json_schemar   )r	   r   r   r   r   r    s    r!   agent_platform_to_gemini_toolr&   E   s8    	 ii!--(,(8(8
 r#   toolsis_agent_platformc                     U(       a  U  Vs/ s H  n[        U5      PM     sn$ U  Vs/ s H  n[        U5      PM     sn$ s  snf s  snf )z?Translates a list of MCP tools to a list of Google GenAI tools.)r&   r"   )r'   r(   r   s      r!   mcp_to_gemini_toolsr*   R   sF    
 <ABED)$/EBB/4	5ut
T
"u	55 C	5s
   A Ac                 ~    [         c  gU  H/  n[        U[        5      (       d  [        U[         5      (       d  M/    g   g)zDChecks whether the list of tools contains any MCP tools or sessions.FT)r   
isinstancer   r'   r   s     r!   has_mcp_tool_usager.   \   s7    d$  Jt5E$F$F  
r#   c                 T    [         c  gU  H  n[        U[         5      (       d  M    g   g)z;Checks whether the list of tools contains any MCP sessions.FT)r   r,   r-   s     r!   has_mcp_session_usager0   f   s+    d$())  
r#   headersc                     [         c  g [        S5      nU R                  SS5      nUSU 3-   R	                  5       U S'   g! [         a    Sn N:f = f)z;Sets the MCP version label in the Google API client header.Nmcpz0.0.0zx-goog-api-client z
 mcp_used/)r   r   r   getlstrip)r1   version_labelexisting_headers      r!   set_mcp_usage_headerr9   p   sc    
ENM KK 3R8/*]O44
FH 

 
 Ms   A AAschemac           	         [        [        R                  R                  R	                  5       5      nUR                  / SQ5        SnSnSn0 nU R                  5        H  u  pgXb;   a  [        U5      XV'   M  Xc;   a   U Vs/ s H  n[        U5      PM     snXV'   M>  Xd;   a3  UR                  5        V	Vs0 s H  u  pU	[        U5      _M     snn	XV'   Mv  Xa;   d  M}  XuU'   M     U$ s  snf s  snn	f )zKFilters the schema to only include fields that are supported by JSONSchema.)additionalPropertiesanyOfoneOf$defsz$ref)itemsr<   additional_properties)r=   any_ofr>   one_of)
propertiesdefsr?   )setr	   r   model_fieldskeysupdater@   r   )
r:   supported_fieldsschema_field_nameslist_schema_field_namesdict_schema_field_namesfiltered_schema
field_namefield_valuevaluekeys
             r!   r   r   ~   s     #5#3#3#@#@#E#E#GH  
 C;$&/!'j'$?$Lo!		.:E%:E
%e
,+%o! 
	. (--/%/jc *51
1/%o! 
	'$/j! "0 
%%s   ?C$3C)
api_clienttoolset_namec           	       ^^#    [         c  [        S5      eSn[        U S5      (       a1  [        U R                  S5      (       a  U R                  R                  nU(       a#  UR                  S5      (       a  USS nU SU 3nO4[        U SS	5      nUS	:X  a  S
U 3nOU[        ;   a	  SU SU 3nOSU SU 3nU R                  5       I Sh  vN n[        U SS5      n0 n[        U S5      (       aK  U R                  (       a:  U R                  R                  (       a  [        U R                  R                  5      nSU 3US'   U(       a  XgS'   [        U5        [        R                  " USS9n U ISh  vN   [        X8S9 ISh  vN n	U	u  pn[        X5       ISh  vN nUR                  5       I Sh  vN    U7v   SSS5      ISh  vN   SSS5      ISh  vN   SSS5      ISh  vN   g GN) N Nq NZ ND! [          a6     SSS5      ISh  vN    SSS5      ISh  vN    SSS5      ISh  vN    gf = f Ns! , ISh  vN  (       d  f       N= f N! , ISh  vN  (       d  f       N= f N! , ISh  vN  (       d  f       g= f! ["         aa  n/ mS[$        SS4UU4S jjm['        U5      R(                  S;   d  [        US5      (       a  T" U5        [+        SU ST 35      Ue SnAgSnAff = f7f)zGInternal helper to manage the Agent Platform MCP lifecycle per request.Nz@The 'mcp' package is required to use Agent Platform MCP servers._http_optionsbase_url/z/mcp/locationglobalz&https://aiplatform.googleapis.com/mcp/zhttps://aiplatform.z.rep.googleapis.com/mcp/zhttps://z-aiplatform.googleapis.com/mcp/projectzBearer AuthorizationzX-Goog-User-Project)r1   timeout)urlhttp_clientexcr   c           	        > [        U S5      (       a  U R                   H  nT" U5        M     g [        U 5      R                   S[	        U 5       3n[        U S5      (       a  U R
                  b  [        U R
                  S[        U R
                  SS5      5      n[        U R
                  S[	        U R
                  5      5      n[        U5      (       a  [	        U R
                  5      nUSU SU S	3-  nTR                  U5        g )
N
exceptionsz: responsestatus_codestatusUnknowntextz (HTTP ))	hasattrrc   type__name__strrd   getattrcallableappend)ra   emsgrf   rh   _extract_errorserror_messagess        r!   rs   4_connect_agent_platform_mcp.<locals>._extract_errors   s    	l	#	#A
!
   c##$Bs3xj13
##(@llcllHi8&
 vs3<</@A$d^^s||$D
76("TF!,
,#c"r#   )ExceptionGroupBaseExceptionGrouprc   z2Failed to connect to Agent Platform MCP Server at z.
Underlying errors: )r   ImportErrorrj   rV   rW   endswithrn   r
   _async_access_tokenr1   dictr9   httpxAsyncClientr   
initializeGeneratorExitBaseExceptionr   rk   rl   
ValueError)rS   rT   rW   mcp_urlrZ   tokenr\   r1   r`   streamsread_streamwrite_stream_sessionegrs   rt   s                  @@r!   _connect_agent_platform_mcpr      s     #
H  (Z))gj6N6NPZ.[.[''00H#2h
%~.Gz:x8H88Gg	.	.%hZ/G~Vg8*$CL>Rg..0
0%J	40''Z))j.F.F:KcKcKkKk:++334G&ug.'/%,!"w!!'4@++{' '.$1#K>>'""$
$
$M ?>	  {{! 1   ?
$   ?>>	   {{	 ?>>>	    {{{ 
 N#S #T # #& BxDD
LI I b>wi H  ./1 	I/s  CLG5B"L0J 6G87J :I=G:	I=I!!G<
"I!%I9G>
:I?H I!II!I=I I=$J /I;0J 4L8J :I=<I!>I 
I 	
II!HI!I='H*(I=-J 8H;9J >L?I 	 II!IIII!I=!I8	'I*(I8	4I=;J =JJJJ LJ 
L!AK=8L=LL)F)/__doc__
contextlibr|   importlib.metadatar   r   typingr   google.authgooglegoogle.auth.transport.requestsr   r4   r   r	   _api_clientr
   TYPE_CHECKING	mcp.typesr   r   r3   r   r   mcp.client.streamable_httpr   mcp.shared._httpx_utilsr   Type__annotations__rx   r"   r&   listboolr*   ToolListUnionr.   r0   r{   rm   r9   
StringDictr   asynccontextmanagerAsyncIteratorr   r   r#   r!   <module>r      s    (   <    2   2	'3?<"%FKK%'6;; $#$ $#$	")5A>W  
 
EJJ 
 $6=66 
%**6e11 d !4!4  $sCx. T """J R# RS RVMaMabeMf R  Rk 
 "G!!	"s   E* *E;:E;