
    Ki                        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	rd dlmZ d dlmZ d dl m!Z!m"Z"  G d de      Z#ddZ$y)    )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                     e Zd ZU dZ ed      Zded<    ed      Zded	<    ed
d      Zded<    ed      Z	ded<    ed      Z
ded<    ed      Zded<    e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%       Ze	 	 	 	 	 	 	 	 d)	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d*d&       Z	 	 d+	 	 	 	 	 	 	 d,d'Zy
)-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metac                .    | j                   j                  S )N)r"   output_schema)selfs    i/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/mcp/server/fastmcp/tools/base.pyr-   zTool.output_schema)   s    ---    c
                4   |xs |j                   }
t        |
       |
dk(  rt        d      |xs |j                  xs d}t	        |      }|t        |      }t        |||gng |	      }|j                  j                  d      } | ||
|||||||||      S )zCreate a Tool from a function.z<lambda>z,You must provide a name for lambda functions )
skip_namesstructured_outputT)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+   r4   	func_namefunc_docr$   func_arg_metadatar!   s                  r/   from_functionzTool.from_function-   s     'BKK	#I.
"KLL2"**2%b) 226M)*7*C/

 '00BBDBQ
 !)'#
 	
r0   c                b  K   	 | j                   j                  | j                  | j                  || j                  | j                  |ind       d{   }|r| j                   j                  |      }|S 7 ## t        $ r  t        $ r!}t        d| j                   d|       |d}~ww xY ww)zRun the tool with arguments.NzError executing tool z: )
r"   call_fn_with_arg_validationr   r$   r%   convert_resultr   	Exceptionr
   r   )r.   	argumentscontextrC   resultes         r/   runzTool.run]   s     	M++GG151C1C1O##W-UY	 F ))88@M + 	  	M3DII;bDE1L	Ms;   B/AA9 A7"A9 6B/7A9 9B,B''B,,B/)returnr*   )NNNNNNNN)r   r   r   r   r   r   r   r   r%   r   r   r&   r)   r(   r+   r*   r4   zbool | NonerJ   r   )NF)rE   r    rF   z:Context[ServerSessionT, LifespanContextT, RequestT] | NonerC   r#   rJ   r   )r6   
__module____qualname__r8   r	   r   __annotations__r   r   r   r!   r"   r$   r%   r   r)   r+   r   r-   classmethodr@   rI    r0   r/   r   r      s   *"40B0"45D#5d0RSE:S)LMKM!&3T!UJU %_!K  'BCHdC %d8g hM:h*/Be*fK'f$T?efEf"'Bc"dD
d. .    "&$(.2#'&*)--
-
 -
 	-

  -
 "-
 ,-
 !-
 $-
 '-
 
-
 -
d OS$	M!M LM 	M
 
Mr0   r   c                   t        | t        j                        r'| j                  } t        | t        j                        r't	        j
                  |       xs- t        |       xr  t	        j
                  t        | dd             S )N__call__)
isinstance	functoolspartialfuncinspectiscoroutinefunctioncallablegetattr)objs    r/   r9   r9   x   sg    
S)++
,hh S)++
, &&s+ U'55gc:t6TUr0   )rZ   r   rJ   r#   )%
__future__r   _annotationsrS   rV   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   r9   rO   r0   r/   <module>ri      sN    2   $ % % % 3 Q R = G +11=]M9 ]M@r0   