
    +jk                        U d Z ddlZddlZddlmZmZ ddlZddlmZ ddlZ	ddl
mZ ddlmZ ddlmZ dd	lmZ ej        rdd
lmZ ddlmZ ddlmZ ddlmZ nSeZej        ed<   eZej        ed<   dZeed<   dZeed<   	 dd
lmZ ddlmZ ddlmZ ddlmZ n# e$ r dZdZdZdZY nw xY wdedej        fdZdedej        fdZ 	 d#de!e         de"de!ej                 fdZ#dej$        de"fdZ%dej$        de"fdZ&de'e(e(f         ddfdZ)dej*        dej*        fdZ+ej,        d ed!e(dej-        e         fd"            Z.dS )$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                     t          j        | j        | j        t           j                            t          j        di t          | j                            dg          S )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    [/home/wildlama/visual-decline/.venv/lib/python3.11/site-packages/google/genai/_mcp_utils.pymcp_to_gemini_toolr"   6   sw    	))55*  /0@AA  6    	

 

 

 
    c                 T    t          j        | j        | j        | j        dg          S )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   s=    	 i!-(,(8 
 
 
 r#   Ftoolsis_agent_platformc                 6    |rd | D             S d | D             S )z?Translates a list of MCP tools to a list of Google GenAI tools.c                 ,    g | ]}t          |          S r   )r&   .0r   s     r!   
<listcomp>z'mcp_to_gemini_tools.<locals>.<listcomp>X   s!    BBBD)$//BBBr#   c                 ,    g | ]}t          |          S r   )r"   r+   s     r!   r-   z'mcp_to_gemini_tools.<locals>.<listcomp>Y   s!    	5	5	5t
T
"
"	5	5	5r#   r   )r'   r(   s     r!   mcp_to_gemini_toolsr/   R   s5    
  CBBEBBBB	5	5u	5	5	55r#   c                 |    t           dS | D ]/}t          |t                    st          |t                     r dS 0dS )zDChecks whether the list of tools contains any MCP tools or sessions.NFT)r   
isinstancer   r'   r   s     r!   has_mcp_tool_usager3   \   sQ    5  d$   Jt5E$F$F TT	r#   c                 R    t           dS | D ]}t          |t                     r dS dS )z;Checks whether the list of tools contains any MCP sessions.NFT)r   r1   r2   s     r!   has_mcp_session_usager5   f   s@    5  d$()) TT	r#   headersc                     t           dS 	 t          d          }n# t          $ r d}Y nw xY w|                     dd          }|d| z                                   | d<   dS )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)r6   version_labelexisting_headers      r!   set_mcp_usage_headerr>   p   s    
FENNMM	   MMMKK 3R88/4]444
FHH 

s    **schemac                    t          t          j        j                                                  }|                    g d           d}d}d}i }|                                 D ]_\  }}||v rt          |          ||<   ||v rd |D             ||<   0||v r"d |                                D             ||<   V||v r|||<   `|S )zKFilters the schema to only include fields that are supported by JSONSchema.)additionalPropertiesanyOfoneOf$defsz$ref)itemsrA   additional_properties)rB   any_ofrC   one_of)
propertiesdefsrD   c                 ,    g | ]}t          |          S r   r   )r,   values     r!   r-   z/_filter_to_supported_schema.<locals>.<listcomp>   s.     % % %16
%e
,
,% % %r#   c                 4    i | ]\  }}|t          |          S r   rL   )r,   keyrM   s      r!   
<dictcomp>z/_filter_to_supported_schema.<locals>.<dictcomp>   s7     % % %c5 *511% % %r#   )setr	   r   model_fieldskeysupdaterE   r   )r?   supported_fieldsschema_field_nameslist_schema_field_namesdict_schema_field_namesfiltered_schema
field_namefield_values           r!   r   r   ~   s5     #5#3#@#E#E#G#GHH      
 C;$&/!' 0 0j+'''$?$L$Loj!!	.	.	.% %:E% % %oj!! 
.	.	.% %'--//% % %oj!! 
'	'	'$/oj!	r#   
api_clienttoolset_namec                  K   t           t          d          d}t          | d          r!t          | j        d          r| j        j        }|r'|                    d          r
|dd         }| d| }n7t          | dd	          }|d	k    rd
| }n|t          v r	d| d| }nd| d| }|                                  d{V }t          | dd          }i }t          | d          r,| j        r%| j        j	        rt          | j        j	                  }d| |d<   |r||d<   t          |           t          j        |d          }	 |4 d{V  t          ||          4 d{V 	 }	|	\  }
}}t          |
|          4 d{V }|                                 d{V  	 |W V  nD# t           $ r7 Y ddd          d{V  ddd          d{V  ddd          d{V  dS w xY w	 ddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  n# 1 d{V swxY w Y   ddd          d{V  dS # 1 d{V swxY w Y   dS # t"          $ rd}g dt$          ddffdt'          |          j        dv st          |d          r! |           t+          d| d           |Y d}~dS d}~ww xY w)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)r6   timeout)urlhttp_clientexcr   c           	         t          | d          r| j        D ]} |           d S t          |           j         dt	          |            }t          | d          r| j        t          | j        dt          | j        dd                    }t          | j        dt	          | j                            }t          |          rt	          | j                  }|d| d| d	z  }                    |           d S )
N
exceptionsz: responsestatus_codestatusUnknowntextz (HTTP ))	hasattrrl   type__name__strrm   getattrcallableappend)rj   emsgro   rq   _extract_errorserror_messagess        r!   r|   z4_connect_agent_platform_mcp.<locals>._extract_errors   s   	l	#	# # 	 	A
/!



	 	 c#11s3xx113
## 		-(@lclHi88 &
 vs3</@/@AA$d^^ %s|$$D
,6,,T,,,
,#c"""""r#   )ExceptionGroupBaseExceptionGrouprl   z2Failed to connect to Agent Platform MCP Server at z.
Underlying errors: )r   ImportErrorrs   r_   r`   endswithrw   r
   _async_access_tokenr6   dictr>   httpxAsyncClientr   
initializeGeneratorExitBaseExceptionr   rt   ru   
ValueError)r\   r]   r`   mcp_urlrc   tokenre   r6   ri   streamsread_streamwrite_stream_sessionegr|   r}   s                  @@r!   _connect_agent_platform_mcpr      s      #
H   (Z)) 1gj6NPZ.[.[ 1'0H S #2#h....GGz:x88H8GGGgg	.	.	.VhVVVVggR8RRLRRg..00
0
0
0
0
0
0%J	400''Z)) 5j.F 5:KcKk 5:+344G.u..'/ -%,G!"w!'4@@@++ 
 
 
 
 
 
 
 
';   	 	 	 	 	 	 	 	'.$\1#K>> 	 	 	 	 	 	 	'""$$
$
$
$
$
$
$
$MMMMM   	 	 	 	 	 	 	 	 	 	 	 			 	 	 	 	 	 	 	 	 	 	 	
 
 
 
 
 
 
 
 
 
 
 
 
 
 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 			 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
   N#S #T # # # # # # #& BxxDDD
LI ID ob1w 1 1 .1 1  	 EDDDDD/s   I I
-H&
H&F,+H,
G-	6H7H&I
I ,G-	-H1H&
HH&HH&I
&
H0	0I
3H0	4I
7I 

II II 
K'AKK)F)/__doc__
contextlibr   importlib.metadatar   r   typingr   google.authgooglegoogle.auth.transport.requestsr   r9   r   r	   _api_clientr
   TYPE_CHECKING	mcp.typesr   r   r8   r   r   mcp.client.streamable_httpr   mcp.shared._httpx_utilsr   Type__annotations__r   r"   r&   listboolr/   ToolListUnionr3   r5   r   rv   r>   
StringDictr   asynccontextmanagerAsyncIteratorr   r   r#   r!   <module>r      sP    ( ' '      < < < < < < < <            2 2 2 2 2 2             2 2 2 2 2 2	 "''''''333333??????<<<<<<<"%FK%%%'6; $#$$$ $#$$$	"))))))555555AAAAAA>>>>>>>	 " " "G!!	"W     
 
EJ 
 
 
 
 $6 6=66 
%*6 6 6 6e1 d    !4     $sCx. T    """ " " "J R# RS RVMabeMf R R R  R R Rs   B B*)B*