
    Ki                    r    d 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mZ ddlmZ  G d	 d
e      Zy)z2SamplingTool for use during LLM sampling requests.    )annotationsN)Callable)Any)Tool)	BaseModel
ConfigDict)ParsedFunctionc                      e Zd ZU dZded<   dZded<   ded<   d	ed
<    ed      ZdddZddZ	e
ddd	 	 	 	 	 	 	 dd       Zy)SamplingToola  A tool that can be used during LLM sampling.

    SamplingTools bundle a tool's schema (name, description, parameters) with
    an executor function, enabling servers to execute agentic workflows where
    the LLM can request tool calls during sampling.

    In most cases, pass functions directly to ctx.sample():

        def search(query: str) -> str:
            '''Search the web.'''
            return web_search(query)

        result = await context.sample(
            messages="Find info about Python",
            tools=[search],  # Plain functions work directly
        )

    Create a SamplingTool explicitly when you need custom name/description:

        tool = SamplingTool.from_function(search, name="web_search")
    strnameN
str | Nonedescriptionzdict[str, Any]
parametersCallable[..., Any]fnT)arbitrary_types_allowedc                |   K   |i } | j                   di |}t        j                  |      r
| d{   }|S 7 w)zExecute the tool with the given arguments.

        Args:
            arguments: Dictionary of arguments to pass to the tool function.

        Returns:
            The result of executing the tool function.
        N )r   inspectisawaitable)self	argumentsresults      q/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/server/sampling/sampling_tool.pyrunzSamplingTool.run-   sE      I%9%v&!\F "s   1<:<c                Z    t        | j                  | j                  | j                        S )zConvert to an mcp.types.Tool for SDK compatibility.

        This is used internally when passing tools to the MCP SDK's
        create_message() method.
        )r   r   inputSchema)SDKToolr   r   r   )r   s    r   _to_sdk_toolzSamplingTool._to_sdk_tool>   s(     ((
 	
    )r   r   c                   t        j                  |d      }||j                  dk(  rt        d       | |xs |j                  |xs |j                  |j
                  |j                        S )aZ  Create a SamplingTool from a function.

        The function's signature is analyzed to generate a JSON schema for
        the tool's parameters. Type hints are used to determine parameter types.

        Args:
            fn: The function to create a tool from.
            name: Optional name override. Defaults to the function's name.
            description: Optional description override. Defaults to the function's docstring.

        Returns:
            A SamplingTool wrapping the function.

        Raises:
            ValueError: If the function is a lambda without a name override.
        T)validatez<lambda>z,You must provide a name for lambda functions)r   r   r   r   )r	   from_functionr   
ValueErrorr   input_schemar   )clsr   r   r   parseds        r   r$   zSamplingTool.from_functionJ   si    0  --b4@<FKK:5KLL$#9v'9'9**yy	
 	
r!   )N)r   zdict[str, Any] | Nonereturnr   )r)   r   )r   r   r   r   r   r   r)   r   )__name__
__module____qualname____doc____annotations__r   r   model_configr   r    classmethodr$   r   r!   r   r   r      sz    , I"K"d;L"

 
  "&!
!
 	!

  !
 
!
 !
r!   r   )r-   
__future__r   r   collections.abcr   typingr   	mcp.typesr   r   pydanticr   r   fastmcp.tools.toolr	   r   r   r!   r   <module>r7      s+    8 "  $  % * -]
9 ]
r!   