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-   Z/home/ubuntu/veenaModal/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 }
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)
skip_namesr/   T)by_alias)r   r   r   r   r   r    r"   r#   r   r'   r)   )	__name__r   
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-   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    call_fn_with_arg_validationr   r"   r#   rB   r   	Exceptionr	   r   )r,   r?   r@   rB   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)r?   r   r@   rA   rB   r!   r*   r   )r4   
__module____qualname__r6   r   r   __annotations__r   r   r   r   r    r"   r#   r   r'   r)   r   r+   classmethodr>   rG   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)rL   r-   r-   r.   r7   x   s   
r7   )rL   r   r*   r!   )%
__future__r   _annotationsrO   rR   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.shared.exceptionsr   mcp.shared.tool_name_validationr   	mcp.typesr   r   mcp.server.fastmcp.serverr   mcp.server.sessionr   mcp.shared.contextr   r   r   r7   r-   r-   r-   r.   <module>   s&    `