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 d dlmZ d dlmZmZ e	rhd 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)UrlElicitationRequiredError)validate_and_warn_tool_name)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-   T/home/app/Keep/.python/lib/python3.10/site-packages/mcp/server/fastmcp/tools/base.pyr+   )   s   zTool.output_schemastructured_outputbool | Nonec
                 C  s   |p|j }
t|
 |
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)Z
skip_namesr/   T)Zby_alias)r   r   r   r   r   r    r"   r#   r   r'   r)   )	__name__r   
ValueError__doc___is_async_callabler
   r   Z	arg_modelZmodel_json_schema)clsr   r   r   r   r#   r   r'   r)   r/   	func_nameZfunc_docr"   Zfunc_arg_metadatar   r-   r-   r.   from_function-   s6   
zTool.from_functionF	argumentscontext:Context[ServerSessionT, LifespanContextT, RequestT] | Noneconvert_resultr   c              
     s   z#| j | j| j|| jdur| j|indI dH }|r"| j |}|W S  ty,     tyC } ztd| j	 d| |d}~ww )zRun the tool with arguments.NzError executing tool z: )
r    Zcall_fn_with_arg_validationr   r"   r#   r<   r   	Exceptionr	   r   )r,   r9   r:   r<   resulter-   r-   r.   run]   s"   
zTool.run)r*   r(   )NNNNNNNN)r   r   r   r   r   r   r   r   r#   r   r   r$   r'   r&   r)   r(   r/   r0   r*   r   )NF)r9   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+   classmethodr8   r@   r-   r-   r-   r.   r      s<   
 2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)rE   r-   r-   r.   r5   x   s   
r5   )rE   r   r*   r!   )%
__future__r   Z_annotationsrH   rK   collections.abcr   r   typingr   r   Zpydanticr   r   Zmcp.server.fastmcp.exceptionsr	   Z.mcp.server.fastmcp.utilities.context_injectionr
   Z*mcp.server.fastmcp.utilities.func_metadatar   r   Zmcp.shared.exceptionsr   Zmcp.shared.tool_name_validationr   Z	mcp.typesr   r   Zmcp.server.fastmcp.serverr   Zmcp.server.sessionr   Zmcp.shared.contextr   r   r   r5   r-   r-   r-   r.   <module>   s&    `