o
    i                  
   @   s  U d Z ddlmZ ddlmZ ddl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mZ ddlmZmZmZ ddlmZmZ ddlmZ eedZeed< G dd deZdede e fddZ!ej"e!dZ#	d'dedee$df dee%e$ef dB df de	j&j'fddZ(ej"e(dZ)G dd deZ*dede e	j&j+ fdd Z,ej"e,dZ-ded!eee$B d"f de e fd#d$Z.ej"e.dZ/G d%d& d&eZ0dS )(z=A middleware for injecting tools into the MCP server context.    )Sequence)Logger)	AnnotatedAnyN)ReadResourceContents)Prompt)AnyUrl)override)Context)CallNext
MiddlewareMiddlewareContext)Tool
ToolResult)
get_loggernameloggerc                   @   s   e Zd ZdZdee fddZedee	j
j dee	j
jee f dee fdd	Zedee	j
j dee	j
jef defd
dZdS )ToolInjectionMiddlewarez2A middleware for injecting tools into the context.toolsc                 C   s   || _ dd |D | _dS )z)Initialize the tool injection middleware.c                 S   s   i | ]}|j |qS  r   ).0toolr   r   n/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/server/middleware/tool_injection.py
<dictcomp>   s    z4ToolInjectionMiddleware.__init__.<locals>.<dictcomp>N)_tools_to_inject_tools_to_inject_by_nameselfr   r   r   r   __init__   s   z ToolInjectionMiddleware.__init__context	call_nextreturnc                    s   g | j ||I dH S )zInject tools into the response.N)r   )r   r    r!   r   r   r   on_list_tools   s   z%ToolInjectionMiddleware.on_list_toolsc                    sF   |j j| jv r| j|j j }|j|j jpi dI dH S ||I dH S )z'Intercept tool calls to injected tools.)	argumentsN)messager   r   runr$   )r   r    r!   r   r   r   r   on_call_tool(   s
   z$ToolInjectionMiddleware.on_call_toolN)__name__
__module____qualname____doc__r   r   r   r	   r   mcptypesListToolsRequestr   r#   CallToolRequestParamsr   r'   r   r   r   r   r      s&    

r   r    r"   c                       |   I dH S )z%List prompts available on the server.N)list_promptsr    r   r   r   r1   6      r1   )fnr   z!The name of the prompt to render.r$   z$The arguments to pass to the prompt.c                    s   | j ||dI dH S )z(Render a prompt available on the server.)r   r$   N)
get_prompt)r    r   r$   r   r   r   r5   @   s   r5   c                       "   e Zd ZdZd fddZ  ZS )PromptToolMiddlewarez=A middleware for injecting prompts as tools into the context.r"   Nc                       t tg}t j|d d S N)r   )list_prompts_toolget_prompt_toolsuperr   r   	__class__r   r   r   S      zPromptToolMiddleware.__init__r"   Nr(   r)   r*   r+   r   __classcell__r   r   r=   r   r7   P       r7   c                    r0   )z'List resources available on the server.N)list_resourcesr2   r   r   r   rD   X   r3   rD   uriz The URI of the resource to read.c                    s   | j |dI dH S )z(Read a resource available on the server.)rE   N)read_resource)r    rE   r   r   r   rF   b   s   rF   c                       r6   )ResourceToolMiddlewarez?A middleware for injecting resources as tools into the context.r"   Nc                    r8   r9   )list_resources_toolread_resource_toolr<   r   r   r=   r   r   r   r   r?   zResourceToolMiddleware.__init__r@   rA   r   r   r=   r   rG   o   rC   rG   )N)1r+   collections.abcr   loggingr   typingr   r   	mcp.typesr,    mcp.server.lowlevel.helper_typesr   r   pydanticr   typing_extensionsr	   fastmcp.server.contextr
   $fastmcp.server.middleware.middlewarer   r   r   fastmcp.tools.toolr   r   fastmcp.utilities.loggingr   r(   r   __annotations__r   listr1   from_functionr:   strdictr-   GetPromptResultr5   r;   r7   ResourcerD   rH   rF   rI   rG   r   r   r   r   <module>   sd    !



