
    Ki              
          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      Zeed<    G d de      Zdede e   fdZ! ejD                  e!      Z#	 d#dedee$df   dee%e$ef   dz  df   de	jL                  jN                  fdZ( ejD                  e(      Z) G d de      Z*dede e	jL                  jV                     fdZ, ejD                  e,      Z-dedeee$z  df   de e   fd Z. ejD                  e.      Z/ G d! d"e      Z0y)$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                   *   e Zd ZdZdee   fdZedee	j                  j                     dee	j                  j                  ee   f   dee   fd       Zedee	j                  j                     dee	j                  j                  ef   defd       Zy	)
ToolInjectionMiddlewarez2A middleware for injecting tools into the context.toolsc                 \    || _         |D ci c]  }|j                  | c}| _        yc c}w )z)Initialize the tool injection middleware.N)_tools_to_injectr   _tools_to_inject_by_name)selfr   tools      t/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/server/middleware/tool_injection.py__init__z ToolInjectionMiddleware.__init__   s/    05(-:
 $DIItO:
% :
s   )context	call_nextreturnc                 J   K   g | j                    ||       d{   S 7 w)zInject tools into the response.N)r   )r   r   r   s      r   on_list_toolsz%ToolInjectionMiddleware.on_list_tools   s*      C&&By/A)ABB)As   #!#c                    K   |j                   j                  | j                  v rU| j                  |j                   j                     }|j                  |j                   j                  xs i        d{   S  ||       d{   S 7 7 w)z'Intercept tool calls to injected tools.)	argumentsN)messager   r   runr$   )r   r   r   r   s       r   on_call_toolz$ToolInjectionMiddleware.on_call_tool(   su      ??4#@#@@001E1EFDGOO,E,E,KLLLw''' M's$   A3B5B
6BBBBN)__name__
__module____qualname____doc__r   r   r   r
   r   mcptypesListToolsRequestr   r"   CallToolRequestParamsr   r'        r   r   r      s    <
htn 
 C"399#=#=>C CII66FGC 
$	C C 
("399#B#BC
( CII;;ZGH
( 
	
( 
(r1   r   r   r    c                 >   K   | j                          d{   S 7 w)z%List prompts available on the server.N)list_promptsr   s    r   r3   r3   6   s     %%''''   )fnr   z!The name of the prompt to render.r$   z$The arguments to pass to the prompt.c                 D   K   | j                  ||       d{   S 7 w)z(Render a prompt available on the server.)r   r$   N)
get_prompt)r   r   r$   s      r   r8   r8   @   s$      ###CCCCs     c                   $     e Zd ZdZd fdZ xZS )PromptToolMiddlewarez=A middleware for injecting prompts as tools into the context.c                 >    t         t        g}t        |   |       y N)r   )list_prompts_toolget_prompt_toolsuperr   r   r   	__class__s     r   r   zPromptToolMiddleware.__init__S   s    .@u%r1   r    Nr(   r)   r*   r+   r   __classcell__rA   s   @r   r:   r:   P   s    G& &r1   r:   c                 >   K   | j                          d{   S 7 w)z'List resources available on the server.N)list_resourcesr4   s    r   rG   rG   X   s     ''))))r5   uriz The URI of the resource to read.c                 B   K   | j                  |       d{   S 7 w)z(Read a resource available on the server.)rH   N)read_resource)r   rH   s     r   rJ   rJ   b   s"     
 &&3&////s   c                   $     e Zd ZdZd fdZ xZS )ResourceToolMiddlewarez?A middleware for injecting resources as tools into the context.c                 >    t         t        g}t        |   |       y r<   )list_resources_toolread_resource_toolr?   r   r@   s     r   r   zResourceToolMiddleware.__init__r   s    02DEu%r1   rB   rC   rE   s   @r   rL   rL   o   s    I& &r1   rL   )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   listr3   from_functionr=   strdictr-   GetPromptResultr8   r>   r:   ResourcerG   rN   rJ   rO   rL   r0   r1   r   <module>rb      s   C $  !  A   & * X X / 0* *(j (B( (DL (
 'D&&  	DD
C<<
=D S#XEED 	YYD %$$$
&2 &*' *d3993E3E.F *
 )d(( 
00	6C<!CC	D0 

0 (T'' 
&4 &r1   