o
    iV)                     @   s8  d 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 ddlmZ er8dd	lmZ d
ZdZdZdZdZdZ								d&dedB dedB dee dB deeeef B dB dee dB deegef dB deeef dB dedB deedef gedef f fddZddddddddddededB dedB dedB dedB dee dB dedB deeef dB dedB deedef gedef f fd d!Z						d'dedB dedB dedB dee dB deeef dB dedB deedef gedef f fd"d#Z G d$d% d%Z!dS )(z_Provides a base mixin class and decorators for easy registration of class methods with FastMCP.    )Callable)TYPE_CHECKINGAny)AnnotationsToolAnnotations)Prompt)Resource)Tool)get_fn_name)FastMCP_mcp_tool_registration_mcp_resource_registration_mcp_prompt_registration_+Nnamedescriptiontagsannotationsexclude_args
serializermetaenabledreturn.c           	   	      s8   dt dtf dt dtf f fdd}|S )zADecorator to mark a method as an MCP tool for later registration.func.r   c              	      s@   pt |  d}dd | D }t| t| | S )N)r   r   r   r   r   r   r   r   c                 S      i | ]\}}|d ur||qS N .0kvr   r   i/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/contrib/mcp_mixin/mcp_mixin.py
<dictcomp>0       z/mcp_tool.<locals>.decorator.<locals>.<dictcomp>)r
   itemssetattr_MCP_REGISTRATION_TOOL_ATTRr   	call_argsr   r   r   r   r   r   r   r   r   r"   	decorator%   s   

zmcp_tool.<locals>.decoratorr   r   )	r   r   r   r   r   r   r   r   r+   r   r*   r"   mcp_tool   s   4r-   )r   titler   	mime_typer   r   r   r   urir.   r/   c          
   
      s:   dt dtf dt dtf f f	dd}	|	S )zEDecorator to mark a method as an MCP resource for later registration.r   .r   c              
      sB   pt |  d	}dd | D }t| t| | S )N)	r0   r   r.   r   r/   r   r   r   r   c                 S   r   r   r   r   r   r   r"   r#   Q   r$   z3mcp_resource.<locals>.decorator.<locals>.<dictcomp>)r
   r%   r&   _MCP_REGISTRATION_RESOURCE_ATTRr(   	r   r   r   r   r/   r   r   r.   r0   r   r"   r+   E   s   
zmcp_resource.<locals>.decoratorr,   )
r0   r   r.   r   r/   r   r   r   r   r+   r   r2   r"   mcp_resource7   s   6r3   c                    s4   dt dtf dt dtf f fdd}|S )zCDecorator to mark a method as an MCP prompt for later registration.r   .r   c                    s<   pt |  d}dd | D }t| t| | S )N)r   r.   r   r   r   r   c                 S   r   r   r   r   r   r   r"   r#   n   r$   z1mcp_prompt.<locals>.decorator.<locals>.<dictcomp>)r
   r%   r&   _MCP_REGISTRATION_PROMPT_ATTRr(   r   r   r   r   r   r.   r   r"   r+   d   s   
	zmcp_prompt.<locals>.decoratorr,   )r   r.   r   r   r   r   r+   r   r5   r"   
mcp_promptZ   s   0
r6   c                   @   s   e Zd ZdZdefddZdefdddedB d	ed
dfddZdefdddedB d	ed
dfddZ	de
fdddedB d	ed
dfddZdeee
fdddedB dededed
dfddZdS )MCPMixina  Base mixin class for objects that can register tools, resources, and prompts
    with a FastMCP server instance using decorators.

    This mixin provides methods like `register_all`, `register_tools`, etc.,
    which iterate over the methods of the inheriting class, find methods
    decorated with `@mcp_tool`, `@mcp_resource`, or `@mcp_prompt`, and
    register them with the provided FastMCP server instance.
    registration_typec                    s    fddt D S )z>Retrieves all methods marked for a specific registration type.c                    sH   g | ] }t t|rtt| rt|tt|  fqS r   )callablegetattrhasattrcopy)r   method_namer8   selfr   r"   
<listcomp>   s    z5MCPMixin._get_methods_to_register.<locals>.<listcomp>)dir)r?   r8   r   r>   r"   _get_methods_to_register   s   z!MCPMixin._get_methods_to_registerN
mcp_serverr   prefix	separatorr   c                 C   s   |  tD ]A\}}|r| | |d  |d< tj||d|d|d|d|d|d|d|d|d	d

}|| qdS )a  Registers all methods marked with @mcp_tool with the FastMCP server.

        Args:
            mcp_server: The FastMCP server instance to register tools with.
            prefix: Optional prefix to prepend to tool names. If provided, the
                final name will be f"{prefix}{separator}{original_name}".
            separator: The separator string used between prefix and original name.
                Defaults to '_'.
        r   r   r   r   r   r   output_schemar   r   )
fnr   r   r   r   r   r   rF   r   r   N)rB   r'   r	   from_functiongetadd_tool)r?   rC   rD   rE   methodregistration_infotoolr   r   r"   register_tools   s(   zMCPMixin.register_toolsc                 C   s   |  tD ]L\}}|r#| | |d  |d< | | |d  |d< tj||d |d|d|d|d|d|d|d|d	d

}|| qdS )a  Registers all methods marked with @mcp_resource with the FastMCP server.

        Args:
            mcp_server: The FastMCP server instance to register resources with.
            prefix: Optional prefix to prepend to resource names and URIs. If provided,
                the final name will be f"{prefix}{separator}{original_name}" and the
                final URI will be f"{prefix}{separator}{original_uri}".
            separator: The separator string used between prefix and original name/URI.
                Defaults to '+'.
        r   r0   r.   r   r/   r   r   r   r   )
rG   r0   r   r.   r   r/   r   r   r   r   N)rB   r1   r   rH   rI   add_resource)r?   rC   rD   rE   rK   rL   resourcer   r   r"   register_resources   s,   zMCPMixin.register_resourcesc                 C   sz   |  tD ]5\}}|r| | |d  |d< tj||d|d|d|d|d|dd}|| qdS )	a  Registers all methods marked with @mcp_prompt with the FastMCP server.

        Args:
            mcp_server: The FastMCP server instance to register prompts with.
            prefix: Optional prefix to prepend to prompt names. If provided, the
                final name will be f"{prefix}{separator}{original_name}".
            separator: The separator string used between prefix and original name.
                Defaults to '_'.
        r   r.   r   r   r   r   )rG   r   r.   r   r   r   r   N)rB   r4   r   rH   rI   
add_prompt)r?   rC   rD   rE   rK   rL   promptr   r   r"   register_prompts   s"   	zMCPMixin.register_promptstool_separatorresource_separatorprompt_separatorc                 C   s4   | j |||d | j|||d | j|||d dS )a  Registers all marked tools, resources, and prompts with the server.

        This method calls `register_tools`, `register_resources`, and `register_prompts`
        internally, passing the provided prefix and separators.

        Args:
            mcp_server: The FastMCP server instance to register with.
            prefix: Optional prefix applied to all registered items unless overridden
                by a specific separator argument.
            tool_separator: Separator for tool names (defaults to '_').
            resource_separator: Separator for resource names/URIs (defaults to '+').
            prompt_separator: Separator for prompt names (defaults to '_').
        )rD   rE   N)rN   rQ   rT   )r?   rC   rD   rU   rV   rW   r   r   r"   register_all   s   zMCPMixin.register_all)__name__
__module____qualname____doc__strrB   _DEFAULT_SEPARATOR_TOOLrN   _DEFAULT_SEPARATOR_RESOURCErQ   _DEFAULT_SEPARATOR_PROMPTrT   rX   r   r   r   r"   r7   v   sj    	
)
-
$r7   )NNNNNNNN)NNNNNN)"r\   collections.abcr   typingr   r   	mcp.typesr   r   fastmcp.prompts.promptr   fastmcp.resources.resourcer   fastmcp.tools.toolr	   fastmcp.utilities.typesr
   fastmcp.serverr   r'   r1   r4   r^   r_   r`   r]   setdictlistboolr-   r3   r6   r7   r   r   r   r"   <module>   s    

	
!
	

$

