
    iA                    D   d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZmZmZmZmZmZ ddlZddlZddlmZ ddlmZmZmZmZ dd	lmZ dd
lmZ ddl Z ddl!m"Z" ddl#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.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7  e3e8      Z9erddl:m;Z; ddl<m=Z=  ededef         Z>e G d de             Z? edd       G d d             Z@ G d d e-      ZAed'd!       ZBeddddde5dddddddd"	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d(d#       ZBe	 d)dddddde5dddddddd$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d*d%       ZB	 d)dddddde5dddddddd$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+d&ZBy),z'Standalone @tool decorator for FastMCP.    )annotationsN)Callable)	dataclassfield)TYPE_CHECKING	AnnotatedAnyLiteralProtocolTypeVaroverloadruntime_checkable)McpError)	ErrorDataIconToolAnnotationsToolExecution)Field)SkipJsonSchema)resolve_task_config)	AuthCheck)without_injected_parameters)
TaskConfig)ParsedFunction_is_object_schema)Tool
ToolResultToolResultSerializerType)call_sync_fn_in_threadpool)
get_logger)NotSetNotSetTget_cached_typeadapter)Docket)	ExecutionF.)boundc                  $    e Zd ZU dZded<   ddZy)DecoratedToolz,Protocol for functions decorated with @tool.ToolMeta__fastmcp__c                     y N )selfargskwargss      k/Users/bowang/.openclaw/workspace/ChatDev/.venv/lib/python3.12/site-packages/fastmcp/tools/function_tool.py__call__zDecoratedTool.__call__=   s    #    N)r0   r	   r1   r	   returnr	   )__name__
__module____qualname____doc____annotations__r3   r.   r4   r2   r)   r)   7   s    6=r4   r)   T)frozenkw_onlyc                     e Zd ZU dZ edd      Zded<   dZded	<   dZd
ed<   dZ	ded<   dZ
ded<   dZded<   dZded<   eZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZded<   dZd ed!<   dZd"ed#<   d$Zd%ed&<   y)'r*   z6Metadata attached to functions by the @tool decorator.toolF)defaultinitzLiteral['tool']typeN
str | Nonenamestr | int | Noneversiontitledescriptionlist[Icon] | Noneiconsset[str] | Nonetagsdict[str, Any] | NotSetT | Noneoutput_schemaToolAnnotations | Noner   dict[str, Any] | Nonemetar	   appbool | TaskConfig | Nonetasklist[str] | Noneexclude_args
Any | None
serializerfloat | Nonetimeout"AuthCheck | list[AuthCheck] | NoneauthTboolenabled)r6   r7   r8   r9   r   rA   r:   rC   rE   rF   rG   rI   rK   r!   rM   r   rP   rQ   rS   rU   rW   rY   r[   r]   r.   r4   r2   r*   r*   @   s    @!&u=D/=D* $G$E:"K"#E# D/ 5;M2;*.K'."&D
&CO%)D
")%)L")!J
! G\ /3D
,3GTr4   r*   c                       e Zd ZU ded<   dZded<   	 	 	 	 d fdZedddddddddedddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       Zdd	Z	dd
Z
ddd	 	 	 	 	 	 	 	 	 	 	 ddZ xZS )FunctionToolz"SkipJsonSchema[Callable[..., Any]]fnNz3Annotated[SkipJsonSchema[Any], Field(exclude=True)]return_typec                    t        |   di |}| j                  j                         r)d|vr%t	        | j                  j
                        |_        |S )zConvert the FastMCP tool to an MCP tool.

        Extends the base implementation to add task execution mode if enabled.
        	execution)taskSupportr.   )superto_mcp_tooltask_configsupports_tasksr   moderc   )r/   	overridesmcp_tool	__class__s      r2   rf   zFunctionTool.to_mcp_tool[   sP     7&33 **,I1M!.4;K;K;P;P!QHr4   )metadatarC   rE   rF   rG   rI   rK   r   rU   rM   rW   rP   rS   rY   r[   c                  t        d |||||||	|||||fD              xs |t        uxs |
du}||rt        d      |t        ||||||||	|||
|||      }|j                  6t
        j                  j                  rt        j                  dt        d       |j                  r6t
        j                  j                  rt        j                  dt        d       t        j                  ||j                  	      }|j                  xs |j                  }|d
k(  rt        d      |j                   }|t#        d      }n(t%        |t&              rt#        j(                  |      }n|}|j+                  ||       t%        |j,                  t.              r|j,                  }n|j,                  }|)t%        |t0              rt3        |      st        d|       | di d|j4                  d|j6                  d|j                  xs |j                  d|j8                  t;        |j8                        ndd|j<                  d|j>                  xs |j>                  d|j@                  d|jB                  d|d|jD                  d|jF                  xs
 tI               d|j                  d|jJ                  d|d|jL                  d|jN                  S ) a{  Create a FunctionTool from a function.

        Args:
            fn: The function to wrap
            metadata: ToolMeta object with all configuration. If provided,
                individual parameters must not be passed.
            name, title, etc.: Individual parameters for backwards compatibility.
                Cannot be used together with metadata parameter.
        c              3  8   K   | ]  }|d uxr |t         u  y wr-   )r!   ).0xs     r2   	<genexpr>z-FunctionTool.from_function.<locals>.<genexpr>   s,      A 1!6/1s   Nz|Cannot pass both 'metadata' and individual parameters to from_function(). Use metadata alone or individual parameters alone.rC   rE   rF   rG   rI   rK   rM   r   rP   rS   rU   rW   rY   r[   zThe `serializer` parameter is deprecated. Return ToolResult from your tools for full control over serialization. See https://gofastmcp.com/servers/tools#custom-serialization for migration examples.   
stacklevelzThe `exclude_args` parameter is deprecated as of FastMCP 2.14. Use dependency injection with `Depends()` instead for better lifecycle management. See https://gofastmcp.com/servers/dependency-injection#using-depends for examples.)rU   z<lambda>z,You must provide a name for lambda functions	forbidden)ri   zROutput schemas must represent object types due to MCP spec limitations. Received: r`   ra   rC   rE   rF   rG   rI   
parametersrM   r   rK   rW   rP   rg   rY   r[   r.   )(anyr!   	TypeErrorr*   rW   fastmcpsettingsdeprecation_warningswarningswarnDeprecationWarningrU   r   from_functionrC   
ValueErrorrS   r   
isinstancer\   	from_boolvalidate_functionrM   r"   dictr   r`   ra   rE   strrF   rG   rI   input_schemar   rK   setrP   rY   r[   )clsr`   rm   rC   rE   rF   rG   rI   rK   r   rU   rM   rW   rP   rS   rY   r[   individual_params_provided	parsed_fn	func_name
task_valuerg   final_output_schemas                          r2   r   zFunctionTool.from_functionm   s4   B     (" F*#($ 4'' 	#, $>E  '+')%H" *w/?/?/T/TMMg #   W%5%5%J%JMMe # #00(BWBWX	MM3Y^^	
"KLL ]]
$+6K
D)$..z:K$K%%b)4 h,,g6"+"9"9"*"8"8*z:Mt/T$%89 !!4 79 
  
||
!--
 0)..
 .6-=-=-IC(()t	

 ..
 !,,E	0E0E
 ..
 !--
 .
 !,,
 '#%
  **
 
 $
 $$
  !
 	
r4   c                  K   t        | j                        }t        |      }| j                  	 t	        j
                  | j                        5  t        j                  |      r|j                  |       d{   }n=t        |j                  |       d{   }t        j                  |      r
| d{   }ddd       nlt        j                  |      r|j                  |       d{   }n=t        |j                  |       d{   }t        j                  |      r
| d{   }| j#                        S 7 7 7 # 1 sw Y   xY w# t        $ rc t        j                  d| j                   d| j                   d       t        t!        dd| j                   d| j                   d            dw xY w7 7 7 w)	zRun the tool with arguments.NzTool 'z' timed out after z`s. Consider using task=True for long-running operations. See https://gofastmcp.com/servers/tasksi z' execution timed out after s)codemessage)r   r`   r#   rY   anyio
fail_afterinspectiscoroutinefunctionvalidate_pythonr   isawaitableTimeoutErrorloggerwarningrC   r   r   convert_result)r/   	arguments
wrapper_fntype_adapterresults        r2   runzFunctionTool.run   s    09
-j9 <<#%%dll322:>'3'C'CI'N!N (B(88)( " #..v6+1\F 44 **:6+;;IFF9 00)   &&v.#)\F""6**A "O"
 &2 43   TYYK'9$,, H> ?
 #"(3OPTP\P\~]^ _
  G *s   -G	E *E9E:EEE7E8E<E *G	.G/G	GG	,G-G	EEEEE A,G  G	G	G	c                    | j                   j                         sy|j                  | j                  | j                  g       y)zRegister this tool with docket for background execution.

        FunctionTool registers the underlying function, which has the user's
        Depends parameters for docket to resolve.
        N)names)rg   rh   registerr`   key)r/   dockets     r2   register_with_docketz!FunctionTool.register_with_docket*  s3     ..0z2r4   )fn_keytask_keyc               ~   K   |xs | j                   }|r||d<     |j                  |fi |di | d{   S 7 w)a  Schedule this tool for background execution via docket.

        FunctionTool splats the arguments dict since .fn expects **kwargs.

        Args:
            docket: The Docket instance
            arguments: Tool arguments
            fn_key: Function lookup key in Docket registry (defaults to self.key)
            task_key: Redis storage key for the result
            **kwargs: Additional kwargs passed to docket.add()
        r   Nr.   )r   add)r/   r   r   r   r   r1   
lookup_keys          r2   add_to_docketzFunctionTool.add_to_docket4  sH     ( 'txx
$F5M5ZVZZ
5f5B	BBBBs   4=;=)rj   r	   r5   zmcp.types.Tool)"r`   Callable[..., Any]rm   zToolMeta | NonerC   rB   rE   rD   rF   rB   rG   rB   rI   rH   rK   rJ   r   rN   rU   rT   rM   rL   rW   zToolResultSerializerType | NonerP   rO   rS   rR   rY   rX   r[   rZ   r5   r_   )r   dict[str, Any]r5   r   )r   r$   r5   None)r   r$   r   r   r   rB   r   rB   r1   r	   r5   r%   )r6   r7   r8   r:   ra   rf   classmethodr!   r   r   r   r   __classcell__)rl   s   @r2   r_   r_   W   s   **GKKDK 
$ 
 %)$( "&#' $.2)-9?6:&*)- $37'M
M
 "	M
 M
 "M
 M
  M
 !M
 M
 ,M
 'M
 7M
 4M
  $!M
" '#M
$ %M
& 1'M
( 
)M
 M
^++Z3 "#CC "C
 C C C 
Cr4   r_   c                     y r-   r.   )r`   s    r2   r>   r>   N  s    r4   )rE   rF   rG   rI   rK   rM   r   rP   rS   rU   rW   rY   r[   c                    y r-   r.   )
name_or_fnrE   rF   rG   rI   rK   rM   r   rP   rS   rU   rW   rY   r[   s                 r2   r>   r>   P  s    " r4   rs   c                    y r-   r.   )r   rC   rE   rF   rG   rI   rK   rM   r   rP   rS   rU   rW   rY   r[   s                  r2   r>   r>   b  s    $ r4   c                 	
 t        t              rt        di t        | t              rt	        d      d		
fdd
	
fdd
fdt        j                  |       r	 | |      S t        | t              r|t	        d      | n| |nt	        dt        |              dfd}|S )zStandalone decorator to mark a function as an MCP tool.

    Returns the original function with metadata attached. Register with a server
    using mcp.add_tool().
    zsTo decorate a classmethod, use @classmethod above @tool. See https://gofastmcp.com/servers/tools#using-with-methodsc                v    t        |	t              
      }t        j                  | |      S )Nrs   )rm   )r*   r   r_   r   )r`   	tool_name	tool_metar   r[   rG   rU   rI   rP   rM   rW   rK   rS   rY   rF   rE   s      r2   create_toolztool.<locals>.create_tool  sT    #'#$T*%!
	  ))"y)AAr4   c                |    t        |
	      }t        | d      r| j                  n| }||_        | S )Nrs   __func__)r*   hasattrr   r+   )r`   r   rm   targetr   r[   rG   rU   rI   rP   rM   rW   rK   rS   rY   rF   rE   s       r2   attach_metadataztool.<locals>.attach_metadata  sZ    #'#%!
  !(J 7R%	r4   c                    t         j                  j                  dk(  r%t        j                  dt
        d        | |      S  | |      S )Nobjectzdecorator_mode='object' is deprecated and will be removed in a future version. Decorators now return the original function with metadata attached.   ru   )r{   r|   decorator_moder~   r   r   )r`   r   r   r   s     r2   	decoratorztool.<locals>.decorator  sK    **h6MMV"	 r9--r9--r4   z6Cannot specify name both as first argument and keywordzInvalid first argument: c                     |       S r-   r.   )r`   r   r   s    r2   wrapperztool.<locals>.wrapper  s    Y''r4   r.   )r`   r   r   rB   r5   r_   )r`   r&   r   rB   r5   r&   r`   r&   r5   r&   )	r   r   r   r   rz   r   	isroutiner   rA   )r   rC   rE   rF   rG   rI   rK   rM   r   rP   rS   rU   rW   rY   r[   r   r   r   r   r   s     ````````````` @@@@r2   r>   r>   w  s    . +t$%44*k*I
 	

B B B(  *	. $T**	J	$TUU				24
3C2DEFF( Nr4   r   )r   r   rE   rD   rF   rB   rG   rB   rI   rH   rK   rJ   rM   rL   r   'ToolAnnotations | dict[str, Any] | NonerP   rO   rS   rR   rU   rT   rW   rV   rY   rX   r[   rZ   r5   Callable[[F], F]r-   ) r   r   rC   rB   rE   rD   rF   rB   rG   rB   rI   rH   rK   rJ   rM   rL   r   r   rP   rO   rS   rR   rU   rT   rW   rV   rY   rX   r[   rZ   r5   r   ) r   zstr | Callable[..., Any] | NonerC   rB   rE   rD   rF   rB   rG   rB   rI   rH   rK   rJ   rM   rL   r   r   rP   rO   rS   rR   rU   rT   rW   rV   rY   rX   r[   rZ   r5   r	   )Cr9   
__future__r   r   r~   collections.abcr   dataclassesr   r   typingr   r   r	   r
   r   r   r   r   r   	mcp.typesmcpmcp.shared.exceptionsr   r   r   r   r   pydanticr   pydantic.json_schemar   r{   fastmcp.decoratorsr   !fastmcp.server.auth.authorizationr   fastmcp.server.dependenciesr   fastmcp.server.tasks.configr   fastmcp.tools.function_parsingr   r   fastmcp.tools.toolr   r   r   fastmcp.utilities.async_utilsr   fastmcp.utilities.loggingr    fastmcp.utilities.typesr!   r"   r#   r6   r   r   r$   docket.executionr%   r&   r)   r*   r_   r>   r.   r4   r2   <module>r      s   - "   $ (	 	 	   * E E  /  2 7 C 2 L 
 E 0  
H	*CxS)* >H > > $%  &,tC4 tCn 
  
 	 !%"# 5;;?"&%)%)! /3  	
    3 9   # #   -  ! 
" 
  $"# 5;;?"&%)%)! /3!  	
     3 9   # #    -!" # 
* 37b  $"# 5;;?"&%)%)! /3!b/b b 	b
 b b b b 3b 9b  b #b #b b b  -!b" 	#br4   