o
    i                     @  s   d dl mZ d dl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mZ d dlmZ d dlmZ d d	lmZmZ d d
lmZmZ e	r\d dlmZ d dlmZ d dlmZmZ G dd deZdddZ dS )    )annotationsN)Callable)cached_property)TYPE_CHECKINGAny)	BaseModelField)	ToolError)find_context_parameter)FuncMetadatafunc_metadata)IconToolAnnotations)Context)ServerSessionT)LifespanContextTRequestTc                   @  s   e Zd ZU dZeddZded< eddZded	< ed
ddZded< eddZ	ded< eddZ
ded< eddZded< eddZded< ed
ddZded< ed
ddZded< ed
ddZd ed!< ed
d"dZd#ed$< ed4d&d'Ze	
	
	
	
	
	
	
	
d5d6d*d+Z	
	,d7d8d2d3Zd
S )9Toolz Internal tool registration info.T)excludeCallable[..., Any]fnzName of the tool)descriptionstrnameNz Human-readable title of the tool
str | Nonetitlez!Description of what the tool doesr   zJSON schema for tool parametersdict[str, Any]
parameterszIMetadata about the function including a pydantic model for tool argumentsr   fn_metadatazWhether the tool is asyncboolis_asyncz-Name of the kwarg that should receive contextcontext_kwargz!Optional annotations for the toolToolAnnotations | Noner   z$Optional list of icons for this tool)defaultr   list[Icon] | NoneiconszOptional metadata for this tooldict[str, Any] | Nonemetareturnc                 C  s   | j jS )N)r   output_schema)self r+   c/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/mcp/server/fastmcp/tools/base.pyr)   '   s   zTool.output_schemastructured_outputbool | Nonec
                 C  s   |p|j }
|
dkrtd|p|jpd}t|}|du r t|}t||dur)|gng |	d}|jjdd}| ||
|||||||||dS )	zCreate a Tool from a function.z<lambda>z,You must provide a name for lambda functions N)
skip_namesr-   T)by_alias)r   r   r   r   r   r   r    r!   r   r%   r'   )__name__
ValueError__doc___is_async_callabler
   r   	arg_modelmodel_json_schema)clsr   r   r   r   r!   r   r%   r'   r-   	func_namefunc_docr    func_arg_metadatar   r+   r+   r,   from_function+   s4   
zTool.from_functionF	argumentscontext:Context[ServerSessionT, LifespanContextT, RequestT] | Noneconvert_resultr   c              
     sz   z#| j | j| j|| jdur| j|indI dH }|r"| j |}|W S  ty< } ztd| j d| |d}~ww )zRun the tool with arguments.NzError executing tool z: )	r   call_fn_with_arg_validationr   r    r!   r@   	Exceptionr	   r   )r*   r=   r>   r@   resulter+   r+   r,   runY   s   
zTool.run)r(   r&   )NNNNNNNN)r   r   r   r   r   r   r   r   r!   r   r   r"   r%   r$   r'   r&   r-   r.   r(   r   )NF)r=   r   r>   r?   r@   r   r(   r   )r2   
__module____qualname__r4   r   r   __annotations__r   r   r   r   r   r    r!   r   r%   r'   r   r)   classmethodr<   rE   r+   r+   r+   r,   r      s<   
 0r   objr   r(   r   c                 C  sB   t | tjr| j} t | tjst| p t| o tt| dd S )N__call__)
isinstance	functoolspartialfuncinspectiscoroutinefunctioncallablegetattr)rJ   r+   r+   r,   r5   p   s   
r5   )rJ   r   r(   r   )!
__future__r   _annotationsrM   rP   collections.abcr   r   typingr   r   pydanticr   r   mcp.server.fastmcp.exceptionsr	   .mcp.server.fastmcp.utilities.context_injectionr
   *mcp.server.fastmcp.utilities.func_metadatar   r   	mcp.typesr   r   mcp.server.fastmcp.serverr   mcp.server.sessionr   mcp.shared.contextr   r   r   r5   r+   r+   r+   r,   <module>   s"    Z