o
    i                  
   @  s  d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZmZmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ d dlmZmZmZ d dlmZ d dlm Z  z@d dl!m"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z* d dl+m*Z, d dl-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 d dl6m7Z7 W n e8y Z9 ze8de9dZ9[9ww erd dl:m;Z; ej<e=e>ef B e>B dB Z?ed Z@dZAe	ddG dd dee ZBd)d"d#ZCd*d'd(ZDdS )+    )annotationsN)Lock)AsyncExitStack)KW_ONLY	dataclass)Path)TYPE_CHECKINGAnyLiteral)AnyUrl)Selfassert_never)messages)
ModelRetry)
AgentDepsT
RunContextToolDefinition)AbstractToolset)ToolsetTool)Client)ClientTransport)	ToolError)	MCPConfig)FastMCP)AudioContentBlobResourceContentsContentBlockEmbeddedResourceImageContentResourceLinkTextContentTextResourceContents)TOOL_SCHEMA_VALIDATORu   Please install the `fastmcp` package to use the FastMCP server, you can use the `fastmcp` optional group — `pip install "pydantic-ai-slim[fastmcp]"`)CallToolResult)model_retryerrorzapplication/octet-streamF)initc                   @  s   e Zd ZU dZded< 	 ded< ded< 	 ded	< 	 d
ed< ddddd1ddZed2ddZd3ddZd4ddZ	d5d#d$Z
d6d+d,Zd7d/d0ZdS )8FastMCPToolsetaE  A FastMCP Toolset that uses the FastMCP Client to call tools from a local or remote MCP Server.

    The Toolset can accept a FastMCP Client, a FastMCP Transport, or any other object which a FastMCP Transport can be created from.

    See https://gofastmcp.com/clients/transports for a full list of transports available.
    zClient[Any]clientr   _Literal['model_retry', 'error']tool_error_behaviorintmax_retries
str | None_id   r$   N)r-   r+   idkClient[Any] | ClientTransport | FastMCP | FastMCP1Server | AnyUrl | Path | MCPConfig | dict[str, Any] | strr1   returnNonec                C  sL   t |tr	|| _ntt |d| _|| _|| _|| _t | _d| _	d | _
d S )N)	transportr   )
isinstancer   r(   r	   r/   r-   r+   r   _enter_lock_running_count_exit_stack)selfr(   r-   r+   r1    r;   b/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/toolsets/fastmcp.py__init__O   s   

zFastMCPToolset.__init__c                 C  s   | j S )N)r/   r:   r;   r;   r<   r1   l   s   zFastMCPToolset.idr   c              	     sv   | j 4 I d H & | jdkrt | _| j| jI d H  |  jd7  _W d   I d H  | S 1 I d H s4w   Y  | S )Nr   r0   )r7   r8   r   r9   enter_async_contextr(   r>   r;   r;   r<   
__aenter__p   s   
zFastMCPToolset.__aenter__argsr	   bool | Nonec              	     sv   | j 4 I d H & |  jd8  _| jdkr#| jr#| j I d H  d | _W d   I d H  d S 1 I d H s4w   Y  d S )Nr0   r   )r7   r8   r9   aclose)r:   rA   r;   r;   r<   	__aexit__z   s   zFastMCPToolset.__aexit__ctxRunContext[AgentDepsT]"dict[str, ToolsetTool[AgentDepsT]]c              	     s\    4 I d H   fdd j  I d H D W  d   I d H  S 1 I d H s'w   Y  d S )Nc                   sL   i | ]"}|j  t|j |j|j|j|jr|j nd |jpd ddqS )N)metar   output_schema)namedescriptionparameters_json_schemametadata)	rJ   tool_for_tool_defr   rK   inputSchemarH   r   
model_dumpoutputSchema).0mcp_toolr>   r;   r<   
<dictcomp>   s    z,FastMCPToolset.get_tools.<locals>.<dictcomp>)r(   
list_tools)r:   rE   r;   r>   r<   	get_tools   s   
0zFastMCPToolset.get_toolsrJ   str	tool_argsdict[str, Any]toolToolsetTool[AgentDepsT]c                   s   | 4 I d H 4 z| j j||dI d H }W n ty0 } z| jdkr*tt|d||d }~ww W d   I d H  n1 I d H sAw   Y  |jrL|jS t|jdS )N)rJ   	argumentsr$   )message)parts)	r(   	call_toolr   r+   r   rW   structured_content_map_fastmcp_tool_resultscontent)r:   rJ   rX   rE   rZ   call_tool_resulter;   r;   r<   r_      s   
(
zFastMCPToolset.call_tooltool_defr   c                 C  s   t t || | jtdS )N)re   toolsetr-   args_validator)r   r   r-   r"   )r:   re   r;   r;   r<   rN      s   z FastMCPToolset.tool_for_tool_def)
r(   r2   r-   r,   r+   r*   r1   r.   r3   r4   )r3   r.   )r3   r   )rA   r	   r3   rB   )rE   rF   r3   rG   )
rJ   rW   rX   rY   rE   rF   rZ   r[   r3   r	   )re   r   r3   r[   )__name__
__module____qualname____doc____annotations__r=   propertyr1   r@   rD   rV   r_   rN   r;   r;   r;   r<   r'   9   s*   
 



	
r'   r^   list[ContentBlock]r3   +list[FastMCPToolResult] | FastMCPToolResultc                 C  s&   dd | D }t |dkr|d S |S )z1Map FastMCP tool results to toolset tool results.c                 S  s   g | ]}t |qS r;   )_map_fastmcp_tool_result)rR   partr;   r;   r<   
<listcomp>   s    z-_map_fastmcp_tool_results.<locals>.<listcomp>r0   r   )len)r^   mapped_resultsr;   r;   r<   ra      s   ra   rq   r   FastMCPToolResultc                 C  s   t | tr| jS t | ttB rtjt| j	| j
dS t | trGt | jtr6tjt| jj| jj
p3tdS t | jtr@| jjS t| j d S t | trPtdt|  d S )N)data
media_typez_ResourceLink is not supported by the FastMCP toolset as reading resources is not yet supported.)r6   r    textr   r   r   BinaryContentbase64	b64decoderv   mimeTyper   resourcer   blobUNKNOWN_BINARY_MEDIA_TYPEr!   r   r   NotImplementedError)rq   r;   r;   r<   rp      s$   



rp   )r^   rn   r3   ro   )rq   r   r3   ru   )E
__future__r   rz   asyncior   
contextlibr   dataclassesr   r   pathlibr   typingr   r	   r
   pydanticr   typing_extensionsr   r   pydantic_air   pydantic_ai.exceptionsr   pydantic_ai.toolsr   r   r   pydantic_ai.toolsetsr   pydantic_ai.toolsets.abstractr   fastmcp.clientr   fastmcp.client.transportsr   fastmcp.exceptionsr   fastmcp.mcp_configr   fastmcp.serverr   mcp.server.fastmcpFastMCP1Server	mcp.typesr   r   r   r   r   r   r    r!   pydantic_ai.mcpr"   ImportError_import_errorfastmcp.client.clientr#   ry   dictrW   ru   ToolErrorBehaviorr   r'   ra   rp   r;   r;   r;   r<   <module>   sP    (
w
