o
    Yi                     @   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.)Zjson_schema)namedescription
parameters)Zfunction_declarationsN )	r   r   r   r   ZSchemaZfrom_json_schema
JSONSchema_filter_to_supported_schemaZinputSchema)r   r   r   Q/home/kim/smarthome/.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   Zversion_labelZ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)Z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   r(   r   keyr)   r   r   r   
<dictcomp>o   s    z/_filter_to_supported_schema.<locals>.<dictcomp>c                    s   i | ]\}}| v r||qS r   r   r*   Zsupported_fieldsr   r   r,   s   s    )setr   r   Zmodel_fieldskeysr&   r   )r%   Zschema_field_namesZlist_schema_field_namesZdict_schema_field_names
field_nameZfield_valuer   r-   r   r   ]   s&   

r   )__doc__importlib.metadatar   r   typingr   r!   r   r   TYPE_CHECKINGZ	mcp.typesr   r   r    r	   r
   Type__annotations__ImportErrorr   listr   ZToolListUnionboolr   r   dictstrr$   Z
StringDictr   r   r   r   r   <module>   s8   

