o
    i                     @   sB  U d Z ddlmZmZ ddlZddlmZ ddlmZ ddlmZ ej	r1ddl
mZ dd	lmZ n*eZejed
< eZejed< zddl
mZ dd	lmZ W n eyZ   dZdZY nw dedejfddZdee deej fddZdejdefddZdejdefddZdeeef ddfddZdejdejfddZdS )z!Utils for working with MCP tools.    )PackageNotFoundErrorversionN)Any   )_common)types)Tool)ClientSessionMcpClientSessionMcpTooltoolreturnc              	   C   s6   t j| j| jt jjt jdi t| jddgdS )z.Translates an MCP tool to a Google GenAI tool.)json_schema)namedescription
parameters)function_declarationsN )	r   r   r   r   Schemafrom_json_schema
JSONSchema_filter_to_supported_schemainputSchema)r   r   r   ]/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/google/genai/_mcp_utils.pymcp_to_gemini_tool'   s   r   toolsc                 C   s   dd | D S )z?Translates a list of MCP tools to a list of Google GenAI tools.c                 S      g | ]}t |qS r   )r   ).0r   r   r   r   
<listcomp>8   s    z'mcp_to_gemini_tools.<locals>.<listcomp>r   )r   r   r   r   mcp_to_gemini_tools6   s   r   c                 C   s4   t du rdS | D ]}t|tst|t r dS qdS )zDChecks whether the list of tools contains any MCP tools or sessions.NFT)r
   
isinstancer   r   r   r   r   r   has_mcp_tool_usage;   s   r"   c                 C   s*   t du rdS | D ]
}t|t r dS qdS )z;Checks whether the list of tools contains any MCP sessions.NFT)r
   r    r!   r   r   r   has_mcp_session_usageE   s   
r#   headersc                 C   sV   t du rdS ztd}W n ty   d}Y nw | dd}|d|   | 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)r$   version_labelexisting_headerr   r   r   set_mcp_usage_headerO   s   
r+   schemac                    s   t tjj  d}d}d}|  D ],\}}||v r!t|| |< q||v r/dd |D | |< q||v r>dd | D | |< q fdd|  D S )	zKFilters the schema to only include fields that are supported by JSONSchema.)items)any_of)
propertiesc                 S   r   r   r   )r   valuer   r   r   r   k   s    z/_filter_to_supported_schema.<locals>.<listcomp>c                 S   s   i | ]	\}}|t |qS r   r0   r   keyr1   r   r   r   
<dictcomp>o   s    z/_filter_to_supported_schema.<locals>.<dictcomp>c                    s   i | ]\}}| v r||qS r   r   r2   supported_fieldsr   r   r4   s   s    )setr   r   model_fieldskeysr-   r   )r,   schema_field_nameslist_schema_field_namesdict_schema_field_names
field_namefield_valuer   r5   r   r   ]   s&   

r   )__doc__importlib.metadatar   r   typingr   r&   r   r   TYPE_CHECKING	mcp.typesr   r   r%   r	   r
   Type__annotations__ImportErrorr   listr   ToolListUnionboolr"   r#   dictstrr+   
StringDictr   r   r   r   r   <module>   s8   

