o
    i>                     @  st  U 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	 d dl
mZmZmZ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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"m#Z# d dl$m%Z%m&Z&m'Z'm(Z( e#e)Z*edddZ+de,d< d+ddZ-d+ddZ.e	ddG dd dZ/G dd  d e%Z0G d!d" d"eZ1G d#d$ d$e%Z2d,d)d*Z3dS )-    )annotationsN)Callable)
ContextVar)	dataclass)	AnnotatedAnyLiteralcast)ToolAnnotations)
ConfigDict)Field)BeforeValidator)ParsedFunctionTool
ToolResult_convert_to_content)_convert_set_default_none)compress_schema)
get_logger)FastMCPBaseModelNotSetNotSetTget_cached_typeadapter_current_tool)defaultz"ContextVar[TransformedTool | None]kwargsr   returnr   c                    s0   t  }|du rtd|jdi | I dH S )aG  Forward to parent tool with argument transformation applied.

    This function can only be called from within a transformed tool's custom
    function. It applies argument transformation (renaming, validation) before
    calling the parent tool.

    For example, if the parent tool has args `x` and `y`, but the transformed
    tool has args `a` and `b`, and an `transform_args` was provided that maps `x` to
    `a` and `y` to `b`, then `forward(a=1, b=2)` will call the parent tool with
    `x=1` and `y=2`.

    Args:
        **kwargs: Arguments to forward to the parent tool (using transformed names).

    Returns:
        The ToolResult from the parent tool execution.

    Raises:
        RuntimeError: If called outside a transformed tool context.
        TypeError: If provided arguments don't match the transformed schema.
    Nz6forward() can only be called within a transformed tool )r   getRuntimeErrorforwarding_fnr   toolr   r   b/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/tools/tool_transform.pyforward%   s
   r$   c                    s,   t  }|du rtd|j| I dH S )a  Forward directly to parent tool without transformation.

    This function bypasses all argument transformation and validation, calling the parent
    tool directly with the provided arguments. Use this when you need to call the parent
    with its original parameter names and structure.

    For example, if the parent tool has args `x` and `y`, then `forward_raw(x=1,
    y=2)` will call the parent tool with `x=1` and `y=2`.

    Args:
        **kwargs: Arguments to pass directly to the parent tool (using original names).

    Returns:
        The ToolResult from the parent tool execution.

    Raises:
        RuntimeError: If called outside a transformed tool context.
    Nz:forward_raw() can only be called within a transformed tool)r   r   r   parent_toolrunr!   r   r   r#   forward_rawC   s
   r'   T)kw_onlyc                   @  sz   e Zd ZU dZeZded< eZded< eZded< eZ	ded< eZ
ded	< d
Zded< eZded< eZded< dd ZdS )ArgTransforma4	  Configuration for transforming a parent tool's argument.

    This class allows fine-grained control over how individual arguments are transformed
    when creating a new tool from an existing one. You can rename arguments, change their
    descriptions, add default values, or hide them from clients while passing constants.

    Attributes:
        name: New name for the argument. Use None to keep original name, or ... for no change.
        description: New description for the argument. Use None to remove description, or ... for no change.
        default: New default value for the argument. Use ... for no change.
        default_factory: Callable that returns a default value. Cannot be used with default.
        type: New type for the argument. Use ... for no change.
        hide: If True, hide this argument from clients but pass a constant value to parent.
        required: If True, make argument required (remove default). Use ... for no change.
        examples: Examples for the argument. Use ... for no change.

    Examples:
        Rename argument 'old_name' to 'new_name'
        ```python
        ArgTransform(name="new_name")
        ```

        Change description only
        ```python
        ArgTransform(description="Updated description")
        ```

        Add a default value (makes argument optional)
        ```python
        ArgTransform(default=42)
        ```

        Add a default factory (makes argument optional)
        ```python
        ArgTransform(default_factory=lambda: time.time())
        ```

        Change the type
        ```python
        ArgTransform(type=str)
        ```

        Hide the argument entirely from clients
        ```python
        ArgTransform(hide=True)
        ```

        Hide argument but pass a constant value to parent
        ```python
        ArgTransform(hide=True, default="constant_value")
        ```

        Hide argument but pass a factory-generated value to parent
        ```python
        ArgTransform(hide=True, default_factory=lambda: uuid.uuid4().hex)
        ```

        Make an optional parameter required (removes any default)
        ```python
        ArgTransform(required=True)
        ```

        Combine multiple transformations
        ```python
        ArgTransform(name="new_name", description="New desc", default=None, type=int)
        ```
    zstr | NotSetTnamedescriptionzAny | NotSetTr   zCallable[[], Any] | NotSetTdefault_factorytypeFboolhidezLiteral[True] | NotSetTrequiredexamplesc                 C  s~   | j tu}| jtu}|r|rtd|r| jstd| jdu r(|s$|r(td| jr4| jdu r4td| jdu r=tddS )	zAValidate that only one of default or default_factory is provided.zCannot specify both 'default' and 'default_factory' in ArgTransform. Use either 'default' for a static value or 'default_factory' for a callable.zdefault_factory can only be used with hide=True. Visible parameters must use static 'default' values since JSON schema cannot represent dynamic factories.TzmCannot specify 'required=True' with 'default' or 'default_factory'. Required parameters cannot have defaults.z|Cannot specify both 'hide=True' and 'required=True'. Hidden parameters cannot be required since clients cannot provide them.Fz=Cannot specify 'required=False'. Set a default value instead.N)r   r   r,   
ValueErrorr/   r0   )selfhas_defaulthas_factoryr   r   r#   __post_init__   s.   



zArgTransform.__post_init__N)__name__
__module____qualname____doc__r   r*   __annotations__r+   r   r,   r-   r/   r0   r1   r6   r   r   r   r#   r)   ]   s   
 Dr)   c                   @  s   e Zd ZU dZ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dddZ
ded< edddZded< dddZdS )ArgTransformConfigz3A model for requesting a single argument transform.NzThe new name for the argument.r   r+   
str | Noner*   z%The new description for the argument.r+   z'The new default value for the argument.zstr | int | float | bool | Noner   Fz+Whether to hide the argument from the tool.r.   r/   z!Whether the argument is required.zLiteral[True] | Noner0   zExamples of the argument.z
Any | Noner1   r   r)   c                 C  s   t di | jddS )z?Convert the argument transform to a FastMCP argument transform.T)exclude_unsetNr   )r)   
model_dump)r3   r   r   r#   to_arg_transform   s   z#ArgTransformConfig.to_arg_transform)r   r)   )r7   r8   r9   r:   r   r*   r;   r+   r   r/   r0   r1   rA   r   r   r   r#   r<      s"   
 r<   c                   @  s   e Zd ZU dZedddZded< ded< ded	< d
ed< d=ddZede	e	ddde	e	e	e	dfd>d(d)Z
ed?d+d,Zed@d5d6ZedAd9d:ZedBd;d<ZdS )CTransformedToola  A tool that is transformed from another tool.

    This class represents a tool that has been created by transforming another tool.
    It supports argument renaming, schema modification, custom function injection,
    structured output control, and provides context for the forward() and forward_raw() functions.

    The transformation can be purely schema-based (argument renaming, dropping, etc.)
    or can include a custom function that uses forward() to call the parent tool
    with transformed arguments. Output schemas and structured outputs are automatically
    inherited from the parent tool but can be overridden or disabled.

    Attributes:
        parent_tool: The original tool that this tool was transformed from.
        fn: The function to execute when this tool is called (either the forwarding
            function for pure transformations or a custom user function).
        forwarding_fn: Internal function that handles argument transformation and
            validation when forward() is called from custom functions.
    allowT)extraarbitrary_types_allowedr   r%   Callable[..., Any]fnr    zdict[str, ArgTransform]transform_args	argumentsdict[str, Any]r   r   c              	     s  |  }| jdi }| D ]E\}}||vrUd|v rUd}| jrM| j D ]&\}}|jtur2|jn|}||krL|jturLt|jrL| ||< d} nq&|sU|d ||< qt	
| }	z| jdi |I dH }
t|
tr| jdu rt| jj}|tu r|
W t	|	 S |
W t	|	 S | jddkr| jdst|
jdd	W t	|	 S |
W t	|	 S t|
| jd
}d}| jdur| jdrd|
i}n|
}|du rzt|
}t|tsd}W n	 ty   Y nw t||d	W t	|	 S t	|	 w )a  Run the tool with context set for forward() functions.

        This method executes the tool's function while setting up the context
        that allows forward() and forward_raw() to work correctly within custom
        functions.

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

        Returns:
            ToolResult object containing content and optional structured output.
        
propertiesr   FTNr-   objectzx-fastmcp-wrap-result)contentstructured_content)
serializerresultr   )copy
parametersr   itemsrH   r*   r   r,   callabler   setrG   
isinstancer   output_schemainspect	signaturereturn_annotationresetrM   r   rO   pydantic_coreto_jsonable_pythondict	Exception)r3   rI   rK   
param_nameparam_schemahas_factory_default	orig_name	transformtransform_nametokenrP   rZ   unstructured_resultstructured_outputr   r   r#   r&     s   





-)
$



zTransformedTool.runNr"   r*   r>   titlestr | NotSetT | Noner+   tagsset[str] | Nonetransform_fnCallable[..., Any] | Nonedict[str, ArgTransform] | Noner    ToolAnnotations | NotSetT | NonerW   0dict[str, Any] | Literal[False] | NotSetT | NonerO   %Callable[[Any], str] | NotSetT | Nonemetadict[str, Any] | NotSetT | Noneenabledbool | Nonec           (      C  s  |pi }|durt j|dd}nd}t|jdi  }t| | }|r?tddt| d|j	 ddt| | 
||\}}|	tu rn|durjtt |j}|du rit|j}|tu rfd}n"|j}n|j}n|	du rtjjr~tjd	td
d d}nttdB |	}|du r|}|}nLtt |}|}| |}t|jdi  }t|di  }|s|| }|rtddt| ddt| | |j|}n| |j|}|r)g }| D ]\}}|js|j	tur||j	 q|| qi }|D ]}||dd ||< qdd | D }|r)tddt| |p.|j	} t|t s7|n|j!}!t|t sB|n|j"}"t|t sM|n|j#}#t|t sX|n|j$}$t|
t sc|
n|j%}%|durm|n|j&}&| |||| |"|!|||p~|j'|$|%|#||&d}'|'S )a  Create a transformed tool from a parent tool.

        Args:
            tool: The parent tool to transform.
            transform_fn: Optional custom function. Can use forward() and forward_raw()
                to call the parent tool. Functions with **kwargs receive transformed
                argument names.
            name: New name for the tool. Defaults to parent tool's name.
            title: New title for the tool. Defaults to parent tool's title.
            transform_args: Optional transformations for parent tool arguments.
                Only specified arguments are transformed, others pass through unchanged:
                - Simple rename (str)
                - Complex transformation (rename/description/default/drop) (ArgTransform)
                - Drop the argument (None)
            description: New description. Defaults to parent's description.
            tags: New tags. Defaults to parent's tags.
            annotations: New annotations. Defaults to parent's annotations.
            output_schema: Control output schema for structured outputs:
                - None (default): Inherit from transform_fn if available, then parent tool
                - dict: Use custom output schema
                - False: Disable output schema and structured outputs
            serializer: New serializer. Defaults to parent's serializer.
            meta: Control meta information:
                - NotSet (default): Inherit from parent tool
                - dict: Use custom meta information
                - None: Remove meta information

        Returns:
            TransformedTool with the specified transformations.

        Examples:
            # Transform specific arguments only
            ```python
            Tool.from_tool(parent, transform_args={"old": "new"})  # Others unchanged
            ```

            # Custom function with partial transforms
            ```python
            async def custom(x: int, y: int) -> str:
                result = await forward(x=x, y=y)
                return f"Custom: {result}"

            Tool.from_tool(parent, transform_fn=custom, transform_args={"a": "x", "b": "y"})
            ```

            # Using **kwargs (gets all args, transformed and untransformed)
            ```python
            async def flexible(**kwargs) -> str:
                result = await forward(**kwargs)
                return f"Got: {kwargs}"

            Tool.from_tool(parent, transform_fn=flexible, transform_args={"a": "x"})
            ```

            # Control structured outputs and schemas
            ```python
            # Custom output schema
            Tool.from_tool(parent, output_schema={
                "type": "object",
                "properties": {"status": {"type": "string"}}
            })

            # Disable structured outputs
            Tool.from_tool(parent, output_schema=None)

            # Return ToolResult for full control
            async def custom_output(**kwargs) -> ToolResult:
                result = await forward(**kwargs)
                return ToolResult(
                    content=[TextContent(text="Summary")],
                    structured_content={"processed": True}
                )
            ```
        NF)validaterK   z%Unknown arguments in transform_args: , z. Parent tool `z` has: zJPassing output_schema=False is deprecated. Use output_schema=None instead.   )
stacklevelz;Function missing parameters required after transformation: z. Function declares: r      c                 S  s   g | ]
\}}|d kr|qS )r{   r   ).0arg_namecountr   r   r#   
<listcomp>.  s    z-TransformedTool.from_tool.<locals>.<listcomp>z6Multiple arguments would be mapped to the same names: )rG   r    r%   r*   ri   r+   rR   rW   rk   r   rO   rs   rH   ru   )(r   from_functionrU   rR   r   keysr2   joinsortedr*   _create_forwarding_transformr   r	   rW   rX   rY   rZ   r   fastmcpsettingsdeprecation_warningswarningswarnDeprecationWarningr^   _function_has_kwargsinput_schema_merge_schema_with_precedencerS   r/   appendrV   r   r+   ri   rs   r   rO   ru   rk   )(clsr"   r*   ri   r+   rk   rm   rH   r   rW   rO   rs   ru   	parsed_fnparent_paramsunknown_argsschemar    final_output_schemarZ   final_fnfinal_schema
has_kwargs	fn_paramstransformed_paramsmissing_params	new_namesold_namerd   name_countsr}   
duplicates
final_namefinal_descriptionfinal_title
final_metafinal_annotationsfinal_serializerfinal_enabledtransformed_toolr   r   r#   	from_toolk  s   Z

	


zTransformedTool.from_tool)tuple[dict[str, Any], Callable[..., Any]]c                   s<  j di }j di  }tj dg }i t i i  | D ]T\}}|r5||v r5|| }nt }|jrZ|jtupD|j	tu}	|	sS||v rSt
d| d|	rY| |< q&| |||||v }
|
rz|
\}}}||< ||< |rz| q&dtd}|r||d< t|dd	}d fdd}||fS )a  Create schema and forwarding function that encapsulates all transformation logic.

        This method builds a new JSON schema for the transformed tool and creates a
        forwarding function that validates arguments against the new schema and maps
        them back to the parent tool's expected arguments.

        Args:
            parent_tool: The original tool to transform.
            transform_args: Dictionary defining how to transform each argument.

        Returns:
            A tuple containing:
            - The new JSON schema for the transformed tool as a dictionary
            - Async function that validates and forwards calls to the parent tool
        $defsrK   r0   zHidden parameter 'z' has no default value in parent tool and no default or default_factory provided in ArgTransform. Either provide a default or default_factory in ArgTransform or don't hide required parameters.rL   r-   rK   r0   T
prune_defsr   r   c            
        s   t  }t |  }|| }|rtddt| | }|r1tddt| i }|  D ]\}}||}|||< q7  D ]\}}	|	jturY|	j||< qJ|	j	turit
|	j	ri|		 ||< qJ|I d H S )Nz$Got unexpected keyword argument(s): rx   zMissing required argument(s): )rU   r   	TypeErrorr   r   rS   r   r   r   r,   rT   r&   )
r   
valid_argsprovided_argsr   missing_argsparent_argsnew_namevaluer   rd   hidden_defaults	new_propsnew_required
new_to_oldr%   r   r#   _forward  s2   



z>TransformedTool._create_forwarding_transform.<locals>._forwardN)r   r   )rR   r   rQ   rU   rS   r)   r/   r   r   r,   r2   _apply_single_transformaddlistr   )r   r%   rH   parent_defsparent_propsparent_requiredr   
old_schemard   has_user_defaulttransform_resultr   
new_schemais_requiredr   r   r   r   r#   r   X  sX   




#z,TransformedTool._create_forwarding_transformr   strr   rd   r)   r   r.   'tuple[str, dict[str, Any], bool] | Nonec                 C  s   |j rdS |jtur|jdur|jn| }n| }t|ts| }| }|jtur8|jdu r3|dd n|j|d< |jturMt	|j}|jdu rM|dd |j
tur^|jdur^|j
|d< d}|jturot|j }|| |jtury|j|d< |||fS )ap  Apply transformation to a single parameter.

        This method handles the transformation of a single argument according to
        the specified transformation rules.

        Args:
            old_name: Original name of the parameter.
            old_schema: Original JSON schema for the parameter.
            transform: ArgTransform object specifying how to transform the parameter.
            is_required: Whether the original parameter was required.

        Returns:
            Tuple of (new_name, new_schema, new_is_required) if parameter should be kept,
            None if parameter should be dropped.
        Nr+   Tr   Fr1   )r/   r*   r   rV   r   rQ   r+   popr0   r.   r   r-   r   json_schemaupdater1   )r   r   rd   r   r   r   type_schemar   r   r#   r     s2   













z'TransformedTool._apply_single_transformbase_schemaoverride_schemac                 C  s  |  di  }t|  dg }| di }t| dg }| D ]\}}||v r:||  }|| |||< q"| ||< q"| }	|D ]}||vrS|	| qG||v rfd|| vrf||vrf|	| qG| D ]\}}d|v rx|	| qk|  di  }
| di }| D ]\}}||
v r|
|  }|| ||
|< q| |
|< qd|t|	d}|
r|
|d< t|dd}|S )	a*  Merge two schemas, with the override schema taking precedence.

        Args:
            base_schema: Base schema to start with
            override_schema: Schema that takes precedence for overlapping properties

        Returns:
            Merged schema with override taking precedence
        rK   r0   r   r   rL   r   Tr   )	r   rQ   rU   rS   r   r   discardr   r   )r   r   merged_propsmerged_requiredoverride_propsoverride_requiredr`   ra   
base_paramfinal_requiredmerged_defsoverride_defsdef_name
def_schemabase_defrP   r   r   r#   r     sN   





z-TransformedTool._merge_schema_with_precedencec                 C  s"   t | }tdd |j D S )ab  Check if function accepts **kwargs.

        This determines whether a custom function can accept arbitrary keyword arguments,
        which affects how schemas are merged during tool transformation.

        Args:
            fn: Function to inspect.

        Returns:
            True if the function has a **kwargs parameter, False otherwise.
        c                 s  s    | ]
}|j tjjkV  qd S )N)kindrX   	ParameterVAR_KEYWORD)r|   pr   r   r#   	<genexpr>r  s    
z7TransformedTool._function_has_kwargs.<locals>.<genexpr>)rX   rY   anyrR   values)rG   sigr   r   r#   r   d  s   
z$TransformedTool._function_has_kwargs)rI   rJ   r   r   )r"   r   r*   r>   ri   rj   r+   rj   rk   rl   rm   rn   rH   ro   r   rp   rW   rq   rO   rr   rs   rt   ru   rv   r   rB   )r%   r   rH   ro   r   r   )
r   r   r   rJ   rd   r)   r   r.   r   r   )r   rJ   r   rJ   r   rJ   )rG   rF   r   r.   )r7   r8   r9   r:   r   model_configr;   r&   classmethodr   r   r   staticmethodr   r   r   r   r   r   r#   rB      s<   
 
h mxDMrB   c                   @  s   e Zd ZU dZ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e	ddZ
ded< edddZded< edddZded< eeddZded< dddZdS ) ToolTransformConfigz#Provides a way to transform a tool.NzThe new name for the tool.r=   r>   r*   zThe new title of the tool.ri   z The new description of the tool.r+   zThe new tags for the tool.)r,   r+   z?Annotated[set[str], BeforeValidator(_convert_set_default_none)]rk   z&The new meta information for the tool.zdict[str, Any] | Noners   TzWhether the tool is enabled.r.   ru   z9A dictionary of argument transforms to apply to the tool.zdict[str, ArgTransformConfig]rI   r"   r   r   rB   c                 C  s<   | j ddhd}tjd	d|i|ddd | j D iS )
zTCreate a TransformedTool from a provided tool and this transformation configuration.TrI   )r?   excluder"   rH   c                 S  s   i | ]	\}}||  qS r   )rA   )r|   kvr   r   r#   
<dictcomp>  s    z-ToolTransformConfig.apply.<locals>.<dictcomp>Nr   )r@   rB   r   rI   rS   )r3   r"   tool_changesr   r   r#   apply  s   zToolTransformConfig.apply)r"   r   r   rB   )r7   r8   r9   r:   r   r*   r;   ri   r+   rU   rk   rs   ru   r^   rI   r   r   r   r   r#   r   w  s8   
 r   toolsdict[str, Tool]transformationsdict[str, ToolTransformConfig]c                 C  sF   i }|   D ]\}}|| }r||||jp|< q|||< q|S )z{Apply a list of transformations to a list of tools. Tools that do not have any transformations
    are left unchanged.
    )rS   r   r   r*   )r   r   transformed_tools	tool_namer"   transformationr   r   r#   apply_transformations_to_tools  s   
r   )r   r   r   r   )r   r   r   r   r   r   )4
__future__r   rX   r   collections.abcr   contextvarsr   dataclassesr   typingr   r   r   r	   r\   	mcp.typesr
   pydanticr   pydantic.fieldsr   pydantic.functional_validatorsr   r   fastmcp.tools.toolr   r   r   r   fastmcp.utilities.componentsr   fastmcp.utilities.json_schemar   fastmcp.utilities.loggingr   fastmcp.utilities.typesr   r   r   r   r7   loggerr   r;   r$   r'   r)   r<   rB   r   r   r   r   r   r#   <module>   sF    

r     .