
    k7iN                    T   % S SK Jr  S SKrS SKrS SKJr  S SKJr  S SKJ	r	J
r
JrJrJrJrJr  S SKrS SKrS SKJrJrJrJr  S SKJr  S SKJrJr  S S	KJr  S SKrS S
KJ r   S SK!J"r"  S SK#J$r$  S SK%J&r&  S SK'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/  \	(       a  S SK0J1r1J2r2  \&" \35      r4\" S\S9r5\ " S S\\5   5      5       r6 " S S5      r7\\/\84   r9S\:S'   S$S jr; " S S5      r< " S S\"5      r " S S\5      r=\ " S S 5      5       r> S%     S&S! jjr? S%     S'S" jjr@ S%     S(S# jjrAg))    )annotationsN)Callable)	dataclass)TYPE_CHECKING	AnnotatedAnyGenericLiteral	TypeAliasget_type_hints)ContentBlockIconTextContentToolAnnotations)Tool)FieldPydanticSchemaGenerationError)TypeVar)get_context)FastMCPComponent)compress_schema)
get_logger)AudioFileImageNotSetNotSetTfind_kwarg_by_typeget_cached_typeadapterreplace_type)ArgTransformTransformedToolT)defaultc                  $    \ rS rSr% SrS\S'   Srg)_WrappedResult0   z,Generic wrapper for non-object return types.r#   result N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r)       L/home/james-whalen/.local/lib/python3.13/site-packages/fastmcp/tools/tool.pyr&   r&   0   s
    6Ir1   r&   c                      \ rS rSrSrg)_UnserializableType7   r)   N)r*   r+   r,   r-   r0   r)   r1   r2   r4   r4   7   s    r1   r4   r   ToolResultSerializerTypec                P    [         R                  " U [        S9R                  5       $ )N)fallback)pydantic_coreto_jsonstrdecode)datas    r2   default_serializerr>   >   s      4;;==r1   c                  :    \ rS rSr  S   SS jjr  SS jrSrg)	
ToolResultB   Nc                h   Uc  Uc  [        S5      eUc  Un[        US9U l        UbP   [        R                  " US9n[        U[        5      (       d&  [        S[        U5      R                   SU< S35      eX l        g ! [        R
                   a  n[        R                  SU 35        e S nAff = f)Nz5Either content or structured_content must be provided)r(   )valuezCould not serialize structured content. If this is unexpected, set your tool's output_schema to None to disable automatic serialization: z/structured_content must be a dict or None. Got z: zA. Tools should wrap non-dict values based on their output_schema.)
ValueError_convert_to_contentcontentr9   to_jsonable_pythonPydanticSerializationErrorloggererror
isinstancedicttyper*   structured_content)selfrF   rN   es       r2   __init__ToolResult.__init__C   s    
 ?19TUU_(G+>g+N)%2%E%E,&" 0$77  23<<=R@R?U VVV 
 :L !;;  `  ab  `c  d 	s   A? ?B1B,,B1c                d    U R                   c  U R                  $ U R                  U R                   4$ N)rN   rF   )rO   s    r2   to_mcp_resultToolResult.to_mcp_resulta   s/     ""*<<||T4444r1   rF   rN   )NN)rF   zlist[ContentBlock] | Any | NonerN   zdict[str, Any] | Any | None)returnz>list[ContentBlock] | tuple[list[ContentBlock], dict[str, Any]])r*   r+   r,   r-   rQ   rU   r0   r)   r1   r2   r@   r@   B   s1     48:>L0L 8L<5	G5r1   r@   c                  ^  ^  \ rS rSr% SrS\S'   SrS\S'   SrS\S	'   SrS
\S'   SU 4S jjr	SU 4S jjr
SS.     SS jjr\SSSSSSS\SSS4                         SS jj5       rSS jr\S\\S\\S\SSSS.                         SS jj5       rSrU =r$ )r   i   z Internal tool registration info.zOAnnotated[dict[str, Any], Field(description='JSON schema for tool parameters')]
parametersNzRAnnotated[dict[str, Any] | None, Field(description='JSON schema for tool output')]output_schemaz]Annotated[ToolAnnotations | None, Field(description='Additional annotations about the tool')]r   zlAnnotated[ToolResultSerializerType | None, Field(description='Optional custom serializer for tool results')]
serializerc                x   > [         TU ]  5          [        5       nUR                  5         g ! [         a     g f = frT   )superenabler   _queue_tool_list_changedRuntimeErrorrO   context	__class__s     r2   r`   Tool.enable{   s7    	!mG,,. 		   , 
99c                x   > [         TU ]  5          [        5       nUR                  5         g ! [         a     g f = frT   )r_   disabler   ra   rb   rc   s     r2   ri   Tool.disable   s7    	!mG,,. 		rg   include_fastmcp_metac               x   SnU R                   (       a  U R                   nOBU R                  (       a1  U R                  R                   (       a  U R                  R                   n[        UR                  SU R                  5      UR                  SU5      UR                  SU R
                  5      UR                  SU R                  5      UR                  SU R                  5      UR                  SU R                  5      UR                  SU R                  5      UR                  S	U R                  US
95      S9$ )z(Convert the FastMCP tool to an MCP tool.NnametitledescriptioninputSchemaoutputSchemaiconsr   _metark   )rn   ro   rp   rq   rr   rs   r   rt   )
ro   r   MCPToolgetrn   rp   r[   r\   rs   get_meta)rO   rl   	overridesro   s       r2   to_mcp_toolTool.to_mcp_tool   s     ::JJE$"2"2"8"8$$**Evtyy1--/!mT5E5EF!mT__E"~t7I7IJ--4!mT5E5EF--<PQ
 	
r1   c                >    [         R                  U UUUUUUUUU	U
US9$ )Create a Tool from a function.fnrn   ro   rp   rs   tagsr   exclude_argsr\   r]   metaenabled)FunctionToolfrom_functionr}   s               r2   r   Tool.from_function   s@      ))##%'! * 
 	
r1   c                    #    [        S5      e7f)z
Run the tool with arguments.

This method is not implemented in the base Tool class and must be
implemented by subclasses.

`run()` can EITHER return a list of ContentBlocks, or a tuple of
(list of ContentBlocks, dict of structured output).
zSubclasses must implement run())NotImplementedError)rO   	argumentss     r2   runTool.run   s      ""CDDs   )rn   ro   rp   r   r   r\   r]   r   transform_argsr   transform_fnc               D    SSK Jn  UR                  " UUUUU
UUUUUU	US9$ )Nr   )r"   )toolr   rn   ro   r   rp   r   r   r\   r]   r   r   )fastmcp.tools.tool_transformr"   	from_tool)clsr   rn   ro   rp   r   r   r\   r]   r   r   r   r   r"   s                 r2   r   Tool.from_tool   s?    " 	A((%)##'!
 	
r1   r)   )rX   None)rl   bool | Nonerx   r   rX   ru   r~   Callable[..., Any]rn   
str | Nonero   r   rp   r   rs   zlist[Icon] | Noner   set[str] | Noner   zToolAnnotations | Noner   list[str] | Noner\   0dict[str, Any] | Literal[False] | NotSetT | Noner]   ToolResultSerializerType | Noner   dict[str, Any] | Noner   r   rX   r   r   dict[str, Any]rX   r@   )r   r   rn   r   ro   str | NotSetT | Nonerp   r   r   r   r   z ToolAnnotations | NotSetT | Noner\   r   r]   r   r   zdict[str, Any] | NotSetT | Noner   zdict[str, ArgTransform] | Noner   r   r   zCallable[..., Any] | NonerX   r"   )r*   r+   r,   r-   r.   r/   r\   r   r]   r`   ri   ry   staticmethodr   r   r   classmethodr   r0   __classcell__)re   s   @r2   r   r   i   s   * 
 	    	    	   
 -1
 *
 	

 

6    "&#' $.2)-JP6:&*#


 
  	

 !
 
 ,
 '
 H
 4
 $
 
 

 
<
E 
  &,,2 $8>JP6:069=#26

 	

 $
 *
 
 6
 H
 4
 .
 7
 
 0
 

 
r1   r   c                      \ rS rSr% S\S'   \SSSSSSS\SSS4                         SS jj5       rS	S jrSr	g)
r      r   r~   Nc                l   [         R                  XS9nUc  UR                  S:X  a  [        S5      e[	        U	[
        5      (       a  UR                  nOCU	SL a<  [        R                  R                  (       a  [        R                  " S[        SS9  SnOU	nUb9  [	        U[        5      (       a$  UR                  S	5      S
:w  a  [        SU< 35      eU " UR                  U=(       d    UR                  UU=(       d    UR                   UUR"                  UUU=(       d
    [%        5       U
UUb  US9$ SS9$ )r|   )r   Nz<lambda>z,You must provide a name for lambda functionsFzJPassing output_schema=False is deprecated. Use output_schema=None instead.   )
stacklevelrM   objectzWOutput schemas must have "type" set to "object" due to MCP spec limitations. Received: T)r~   rn   ro   rp   rs   r[   r\   r   r   r]   r   r   )ParsedFunctionr   rn   rD   rK   r   r\   fastmcpsettingsdeprecation_warningswarningswarnDeprecationWarningrL   rv   r~   rp   input_schemaset)r   r~   rn   ro   rp   rs   r   r   r   r\   r]   r   r   	parsed_fnfinal_output_schemas                  r2   r   FunctionTool.from_function   s?   $ #000O	<INNj8KLLmW--"+"9"9e#44`& 
 #' #0 *z:Mt/T/T"&&v.(: m  oB  nE  F  ||'#<y'<'< ---#!&2G
 	
 9=
 	
r1   c                |  #    SSK Jn  UR                  5       n[        U R                  US9nU(       a  X1;  a  [        5       X'   [        U R                  5      nUR                  U5      n[        R                  " U5      (       a
  UI Sh  vN n[        U[        5      (       a  U$ [        XPR                  S9nU R                  c  [        U[        [         -  ["        -  [$        -  5      (       d3  [        U[&        [(        -  5      (       a   [+        S U 5       5      (       a	  [        US9$  [,        R.                  " U5      n[        U[0        5      (       a
  [        UUS9$  [        US9$ U R                  R5                  S	5      n[        UU(       a  S
U0S9$ US9$  GN! [,        R2                   a     NTf = f7f)zRun the tool with arguments.r   Context
kwarg_typeN)r]   c              3  B   #    U  H  n[        U[        5      v   M     g 7frT   rK   r   .0items     r2   	<genexpr>#FunctionTool.run.<locals>.<genexpr>M  s     J64
4666   )rF   rW   x-fastmcp-wrap-resultr(   )fastmcp.server.contextr   copyr   r~   r   r   validate_pythoninspectisawaitablerK   r@   rE   r]   r\   r   r   r   r   listtupleanyr9   rG   rL   rH   rv   )	rO   r   r   context_kwargtype_adapterr(   unstructured_resultrN   wrap_results	            r2   r   FunctionTool.run4  s    2NN$	*477wG];'2}I$-dgg6--i8v&&!\Ffj))M1&__U%&,"6">"EFF64%<00J6JJJ!*=>>	%2%E%Ef%M"0$77% 3+=  8 &9::((,,-DE'5@&1
 	
FL
 	
? "0 !;; s7   B	F<FB!F<.4F" ">F<"F96F<8F99F<r)   r   r   )
r*   r+   r,   r-   r/   r   r   r   r   r0   r)   r1   r2   r   r      s       "&#' $.2)-JP6:&*#:
:
 :
 	:

  :
 !:
 :
 ,:
 ':
 H:
 4:
 $:
 :
 
:
 :
x0
r1   r   c                  x    \ rS rSr% S\S'   S\S'   S\S'   S\S	'   S
\S'   \   S         SS jj5       rSrg)r   ig  r   r~   r;   rn   r   rp   r   r   r   r\   Nc                   SSK Jn  U(       Ga  [        R                  " U5      nUR                  R                  5        Hi  nUR                  [        R                  R                  :X  a  [        S5      eUR                  [        R                  R                  :X  d  M`  [        S5      e   U(       am  U Hg  nXR                  ;  a  [        SU S35      eUR                  U   nUR                  [        R                  R                  :X  d  MZ  [        SU S35      e   [        USS 5      =(       d    UR                  R                  n	[        R                   " U5      n
[        R"                  " U5      (       d  UR$                  n['        U[(        5      (       a  UR*                  n/ n[-        XS	9nU(       a  UR/                  U5        U(       a  UR1                  U5        [3        U5      nUR5                  5       n[7        XS
S9nS n[        R                  " U5      nUR8                  n['        U[:        5      (       a   [=        US
S9nUR?                  SU5      nU[        RB                  S [D        S4;  Ga!  [G        U[H        RK                  [L        [N        [P        [R        [T        RV                  RX                  [T        RV                  RZ                  [T        RV                  R\                  [T        RV                  R^                  [T        RV                  R`                  4	[b        5      5      n [3        U5      nUR5                  SS9nU(       a>  UR?                  S5      S:w  a)  [d        U   n[3        U5      nUR5                  SS9nS
US'   OUn[7        US
S9nU " UU	U
UU=(       d    S S9$ ! [@         a     GN^f = f! [f         a2  nS[;        U5      ;  a  [h        Rk                  SU< 35         S nAN[S nAff = f)Nr   r   z/Functions with *args are not supported as toolsz2Functions with **kwargs are not supported as toolszParameter 'z-' in exclude_args does not exist in function.z,' in exclude_args must have a default value.r*   r   T)prune_paramsprune_titles)include_extrasrX   .serialization)moderM   r   r   )r   r4   z#Unable to generate schema for type )r~   rn   rp   r   r\   )6r   r   r   	signaturer[   valueskind	ParameterVAR_POSITIONALrD   VAR_KEYWORDr$   emptygetattrre   r*   getdoc	isroutine__call__rK   r   __func__r   appendextendr   json_schemar   return_annotationr;   r   rv   	Exception_emptyr   r    rL   fromkeysr   r   r   r@   mcptypesr   ImageContentAudioContentResourceLinkEmbeddedResourcer4   r&   r   rI   debug)r   r~   r   validatewrap_non_object_output_schemar   sigparamarg_namefn_namefn_docr   r   input_type_adapterr   r\   output_type
type_hintsclean_output_typer   base_schemawrapped_typewrapped_adapterrP   s                           r2   r   ParsedFunction.from_functiono  su    	3##B'C..0::!2!2!A!AA$%VWW::!2!2!>!>>$L 	 1  ,H~~5()(3`a   NN84E}}(9(9(?(??()(3_`  !- "j$/H2<<3H3H#   $$Bb,''B"$*2B.-3B7)557&$
 #++ k3'' ,BtD
(nnX{C
 w~~tS#>> !-"		--		..		..		..		22
 (!$X56GH*66O6L
 2#/8; $22C#DL&<\&JO$3$?$?_$?$UM=AM"9:$/M /D Q %'/4
 	
i  ` 1 X(A6LL#F{o!VWXs+   N/ 1A+O  /
N=<N= 
O<
(O77O<r)   )NTT)
r~   r   r   r   r   boolr   r   rX   r   )r*   r+   r,   r-   r/   r   r   r0   r)   r1   r2   r   r   g  ss    
I  (( *..2

 '
 	

 (,
 

 
r1   r   c                    Ub	   U" U 5      $ [        U 5      $ ! [          a  n[        R                  SUSS9   S nAN/S nAff = f)Nz!Error serializing tool result: %sT)exc_info)r   rI   warningr>   )r(   r]   rP   s      r2   _serialize_with_fallbackr    sS     	f%% f%%  	NN3  	s    
A<Ac                r   [        U [        5      (       a  U $ [        U [        5      (       a  U R                  5       $ [        U [        5      (       a  U R                  5       $ [        U [        5      (       a  U R                  5       $ [        U [        5      (       a
  [        SU S9$ [        S[        X5      S9$ )NtextrM   r  )rK   r   r   to_image_contentr   to_audio_contentr   to_resource_contentr;   r   r  )r   r]   s     r2    _convert_to_single_content_blockr    s     $%%$$$&&$$$&&$''))$T22F)A$)STTr1   c                T   U c  / $ [        U [        [        -  5      (       d  [        X5      /$ [	        S U  5       5      (       a  U $ [        S U  5       5      (       a3  U  Vs/ s H%  n[        U[        5      (       d  [        X!5      OUPM'     sn$ [        S[        X5      S9/$ s  snf )z2Convert a result to a sequence of content objects.c              3  B   #    U  H  n[        U[        5      v   M     g 7frT   r   r   s     r2   r   &_convert_to_content.<locals>.<genexpr>%  s     
=fd:dL))fr   c              3  l   #    U  H*  n[        U[        [        -  [        -  [        -  5      v   M,     g 7frT   )rK   r   r   r   r   r   s     r2   r   r  *  s)     
TVT:dL50584?@@Vs   24r  r  )	rK   r   r   r  allr   r   r   r  )r(   r]   r   s      r2   rE   rE     s     ~	fte|--0DEE 
=f
=== 
TV
TTT
 	
  dL11 -T> 	
 	
 V*B6*VWXX
s   ",B%)r=   r   rX   r;   rT   )r(   r   r]   r   rX   r;   )r   r   r]   r   rX   r   )r(   r   r]   r   rX   zlist[ContentBlock])B
__future__r   r   r   collections.abcr   dataclassesr   typingr   r   r   r	   r
   r   r   	mcp.typesr   r9   r   r   r   r   r   ru   pydanticr   r   typing_extensionsr   r   fastmcp.server.dependenciesr   fastmcp.utilities.componentsr   fastmcp.utilities.json_schemar   fastmcp.utilities.loggingr   fastmcp.utilities.typesr   r   r   r   r   r   r   r    r   r!   r"   r*   rI   r#   r&   r4   r;   r6   r/   r>   r@   r   r   r  r  rE   r)   r1   r2   <module>r     sw   "   $ !     F F % 9 %  3 9 9 0	 	 	 J	H	C WQZ  	 	 '/ucz&: ) :>$5 $5NH
 H
Vp
4 p
f G
 G
 G
V @D&&<&&$ 37U
U/U U0 37YY/Y Yr1   