
    kKj3<                       d Z ddlmZ ddlmZmZ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mZ dd
lmZ ddlmZ ddlmZ ddlmZmZ erddlm Z   ee!      Z" e
d      Z#	 	 	 	 	 	 	 	 ddZ$ G d d      Z%y)zGMCP protocol handler setup and wire-format handlers for FastMCP Server.    )annotations)	AwaitableCallableSequence)TYPE_CHECKINGAnyTypeVarcastN)McpError)ContentBlock)AnyUrl)DisabledErrorNotFoundError)TaskMeta)
get_logger)paginate_sequence)VersionSpecdedupe_with_versions)FastMCP	PaginateTc           	         |t        |       dfS 	 t        | ||      S # t        $ r9}t        t        j
                  j                  dt        |                  |d}~ww xY w)zApply pagination to items, raising McpError for invalid cursors.

    If page_size is None, returns all items without pagination.
    Nicodemessage)listr   
ValueErrorr   mcptypes	ErrorDatastr)itemscursor	page_sizees       u/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/server/mixins/mcp_operations.py_apply_paginationr&      se     E{D  P 	:: Psyy**A*GHaOPs    	A 4AA c                      e Zd ZdZddZ	 	 	 	 	 	 ddZ	 	 	 	 ddZ	 	 	 	 ddZ	 	 	 	 ddZ	 	 	 	 ddZ		 	 	 	 	 	 ddZ
	 	 	 	 dd	Z	 	 	 	 	 	 dd
ZddZy)MCPOperationsMixina  Mixin providing MCP protocol handler setup and wire-format handlers.

    Note: Methods registered with SDK decorators (e.g., _list_tools_mcp, _call_tool_mcp)
    cannot use `self: FastMCP` type hints because the SDK's `get_type_hints()` fails
    to resolve FastMCP at runtime (it's only available under TYPE_CHECKING). When
    type hints fail to resolve, the SDK falls back to calling handlers with no arguments.
    These methods use untyped `self` to avoid this issue.
    c                    | j                   j                         | j                          | j                   j                         | j                          | j                   j                         | j                         | j                  | j                        | j                   j                  t        j                  j                  <    | j                   j                  | j                        | j                          | j                   j!                         | j"                          | j                   j%                         | j&                          | j                   j)                         | j*                         | j-                          y)aR  Set up core MCP protocol handlers.

        List handlers use SDK decorators that pass the request object to our handler
        (needed for pagination cursor). The SDK also populates caches like _tool_cache.

        Exception: list_resource_templates SDK decorator doesn't pass the request,
        so we register that handler directly.

        The call_tool decorator is from the SDK (supports CreateTaskResult + validate_input).
        The read_resource and get_prompt decorators are from LowLevelServer to add
        CreateTaskResult support until the SDK provides it natively.
        )validate_inputN)_mcp_server
list_tools_list_tools_mcplist_resources_list_resources_mcplist_prompts_list_prompts_mcp_wrap_list_handler_list_resource_templates_mcprequest_handlersr   r   ListResourceTemplatesRequest	call_toolstrict_input_validation_call_tool_mcpread_resource_read_resource_mcp
get_prompt_get_prompt_mcpset_logging_level_set_logging_level_mcp_setup_task_protocol_handlers)selfs    r%   _setup_handlersz"MCPOperationsMixin._setup_handlers6   s4    	&##%d&:&:;)'')$*B*BC'%%'(>(>?
 ##D$E$EF 	))#))*P*PQ 	P""$2N2N"O	
 	)&&()@)@A%##%d&:&:;,**,T-H-HI 	**,    c                    dfd}|S )z@Wrap a list handler to pass the request and return ServerResult.c                n   K    |        d {   }t         j                  j                  |      S 7 #wN)r   r   ServerResult)requestresulthandlers     r%   wrapperz6MCPOperationsMixin._wrap_list_handler.<locals>.wrapper\   s.     "7++F99))&11 ,s   53$5)rG   r   returnzmcp.types.ServerResult )r@   rI   rJ   s    ` r%   r2   z%MCPOperationsMixin._wrap_list_handlerW   s    
	2 rB   c                  K   t        d|       }t        j                  d|j                   d       t	        t        |j                          d{         d       }|D cg c]  }|j                  |j                          }}|"|j                  r|j                  j                  nd}t        |||j                        \  }}t        j                  j                  ||      S 7 c c}w w)z
        List all available tools, in the format expected by the low-level MCP
        server. Supports pagination when list_page_size is configured.
        r   [z] Handler called: list_toolsNc                    | j                   S rE   namets    r%   <lambda>z4MCPOperationsMixin._list_tools_mcp.<locals>.<lambda>n   s    PQPVPVrB   rP   )tools
nextCursor)r
   loggerdebugrQ   r   r   r,   to_mcp_toolparamsr"   r&   _list_page_sizer   r   ListToolsResult)	r@   rG   serverrU   tool	sdk_toolsr"   pagenext_cursors	            r%   r-   z"MCPOperationsMixin._list_tools_mcpb   s      i&q%ABC$T0A0A0C*C%DFVWBGH%$T%%499%5%	H &-%8W^^GNN!!QU 	 .iAWAWXkyy((t(LL +DHs%   AC/C(C/"#C*A$C/*C/c                  K   t        d|       }t        j                  d|j                   d       t	        t        |j                          d{         d       }|D cg c]'  }|j                  t        |j                              ) }}|j                  r|j                  j                  nd}t        |||j                        \  }}t        j                  j!                  ||      S 7 c c}w w)z
        List all available resources, in the format expected by the low-level MCP
        server. Supports pagination when list_page_size is configured.
        r   rN   z ] Handler called: list_resourcesNc                ,    t        | j                        S rE   )r    uri)rs    r%   rT   z8MCPOperationsMixin._list_resources_mcp.<locals>.<lambda>   s    3quu:rB   )rd   )	resourcesrV   )r
   rW   rX   rQ   r   r   r.   to_mcp_resourcer    rd   rZ   r"   r&   r[   r   r   ListResourcesResult)	r@   rG   r]   rf   resourcesdk_resourcesr"   r`   ra   s	            r%   r/   z&MCPOperationsMixin._list_resources_mcpx   s      i&q%EFG(v,,../1E
	 MV
LUH$$X\\):$;I 	 
 +2..&&d-66#9#9
k yy,,t,TT /
s%   AC6C/C6",C1A"C61C6c                  K   t        d|       }t        j                  d|j                   d       t	        t        |j                          d{         d       }|D cg c]  }|j                  |j                          }}|j                  r|j                  j                  nd}t        |||j                        \  }}t        j                  j                  ||      S 7 c c}w w)z
        List all available resource templates, in the format expected by the low-level MCP
        server. Supports pagination when list_page_size is configured.
        r   rN   z)] Handler called: list_resource_templatesNc                    | j                   S rE   )uri_templaterR   s    r%   rT   zAMCPOperationsMixin._list_resource_templates_mcp.<locals>.<lambda>   s    ANNrB   )uriTemplate)resourceTemplatesrV   )r
   rW   rX   rQ   r   r   list_resource_templatesto_mcp_templaterm   rZ   r"   r&   r[   r   r   ListResourceTemplatesResult)	r@   rG   r]   	templatestemplatesdk_templatesr"   r`   ra   s	            r%   r3   z/MCPOperationsMixin._list_resource_templates_mcp   s      i&q%NOP(v55778:R
	
 &
% $$1F1F$G% 	 
 +2..&&d-66#9#9
k yy44"{ 5 
 	
 8
%   AC-C&C-"#C(A"C-(C-c                  K   t        d|       }t        j                  d|j                   d       t	        t        |j                          d{         d       }|D cg c]  }|j                  |j                          }}|j                  r|j                  j                  nd}t        |||j                        \  }}t        j                  j                  ||      S 7 c c}w w)z
        List all available prompts, in the format expected by the low-level MCP
        server. Supports pagination when list_page_size is configured.
        r   rN   z] Handler called: list_promptsNc                    | j                   S rE   rP   )ps    r%   rT   z6MCPOperationsMixin._list_prompts_mcp.<locals>.<lambda>   s    rB   rP   )promptsrV   )r
   rW   rX   rQ   r   r   r0   to_mcp_promptrZ   r"   r&   r[   r   r   ListPromptsResult)	r@   rG   r]   rz   promptsdk_promptsr"   r`   ra   s	            r%   r1   z$MCPOperationsMixin._list_prompts_mcp   s      i&q%CDE&v**,,-/?
 MTTG&v+++=GT*1..&&d-!7!7
k yy**4K*PP -Trv   c                R  K   t        d|       }t        j                  d|j                   d||       	 d}d}	 |j                  j
                  }|j                  r=|j                  j                  d      }|j                  di       j                  d      }|j                  j                  rC|j                  j                  }|j                  d      }	t        |	j                  d	      
      }|rt        |      nd}
|j!                  |||
|       d{   }t#        |t$        j&                  j(                        r|S |j+                         S # t        t        f$ r Y tw xY w7 O# t,        $ r}t/        d|      |d}~wt.        $ r}t/        d|      |d}~ww xY ww)a  
        Handle MCP 'callTool' requests.

        Extracts task metadata from MCP request context and passes it explicitly
        to call_tool(). The tool's _run() method handles the backgrounding decision,
        ensuring middleware runs before Docket.

        Args:
            key: The name of the tool to call
            arguments: Arguments to pass to the tool

        Returns:
            Tool result or CreateTaskResult for background execution
        r   rN   z&] Handler called: call_tool %s with %sNTexclude_nonefastmcpversionttlr   eqr   	task_metazUnknown tool: )r
   rW   rX   rQ   r+   request_contextmeta
model_dumpgetexperimentalis_tasktask_metadatar   AttributeErrorLookupErrorr   r6   
isinstancer   r   CreateTaskResultto_mcp_resultr   r   )r@   key	argumentsr]   version_strr   ctx	meta_dictmcp_task_metatask_meta_dictr   rH   r$   s                r%   r8   z!MCPOperationsMixin._call_tool_mcp   s    , i&}BCS)	
	A '+K)-I((8888 # 3 3 3 FI"+--	2">"B"B9"MK##++$'$4$4$B$BM%2%=%=4%=%PN (^-?-?-F GI 6Ak[1dG!++Y9 ,  F &#))"<"<='')) #K0   	A. 89q@ 	A. 89q@	Asp   2F'E, B8E 2(E, E*)E, F'E, F'E'$E, &E''E, ,	F$5FF$FF$$F'c           
       K   t        d|       }t        j                  d|j                   d|       	 d}d}	 |j                  j
                  }|j                  rB|j                  j                  d      }|j                  d      xs i }|j                  d      }|j                  j                  rC|j                  j                  }|j                  d      }	t        |	j                  d	      
      }|rt        |      nd}
|j!                  t#        |      |
|       d{   }t%        |t&        j(                  j*                        r|S |j-                  |      S # t        t        f$ r Y }w xY w7 P# t.        $ r<}t1        t&        j(                  j3                  ddt#        |                  |d}~wt4        $ r3}t1        t&        j(                  j3                  dd|             |d}~ww xY ww)a  Handle MCP 'readResource' requests.

        Extracts task metadata from MCP request context and passes it explicitly
        to read_resource(). The resource's _read() method handles the backgrounding
        decision, ensuring middleware runs before Docket.

        Args:
            uri: The resource URI

        Returns:
            ReadResourceResult or CreateTaskResult for background execution
        r   rN   z"] Handler called: read_resource %sNTr   r   r   r   r   r   r   izResource not found: r   )r
   rW   rX   rQ   r+   r   r   r   r   r   r   r   r   r   r   r   r9   r    r   r   r   r   r   r   r   r   r   )r@   rd   r]   r   r   r   r   fastmcp_metar   r   r   rH   r$   s                r%   r:   z%MCPOperationsMixin._read_resource_mcp   s     i&q%GH#N$	&*K)-I((8888 # 3 3 3 FI#,==#;#ArL"."2"29"=K##++$'$4$4$B$BM%2%=%=4%=%PN (^-?-?-F GI 6Ak[1dG!//C'Y 0  F &#))"<"<='',, #K0   			##+?C|)L $  	
  			##;OPQs9S#T	sp   1G;E9 B=E" 60E9 &E7')E9 G;E9 !G;"E41E9 3E44E9 9	G87F99G8.G33G88G;c                .  K   t        d|       }t        j                  d|j                   d||       	 d}d}	 |j                  j
                  }|j                  r=|j                  j                  d      }|j                  di       j                  d      }|j                  j                  rC|j                  j                  }|j                  d      }	t        |	j                  d	      
      }|rt        |      nd}
|j!                  |||
|       d{   }t#        |t$        j&                  j(                        r|S |j+                         S # t        t        f$ r Y tw xY w7 O# t,        $ r}t/        d|      |d}~wt.        $ r  w xY ww)a  Handle MCP 'getPrompt' requests.

        Extracts task metadata from MCP request context and passes it explicitly
        to render_prompt(). The prompt's _render() method handles the backgrounding
        decision, ensuring middleware runs before Docket.

        Args:
            name: The prompt name
            arguments: Prompt arguments

        Returns:
            GetPromptResult or CreateTaskResult for background execution
        r   rN   z'] Handler called: get_prompt %s with %sNTr   r   r   r   r   r   r   zUnknown prompt: )r
   rW   rX   rQ   r+   r   r   r   r   r   r   r   r   r   r   r   render_promptr   r   r   r   to_mcp_prompt_resultr   r   )r@   rQ   r   r]   r   r   r   r   r   r   r   rH   r$   s                r%   r<   z"MCPOperationsMixin._get_prompt_mcp0  s      i&}CDdI	
	 '+K)-I((8888 # 3 3 3 FI"+--	2">"B"B9"MK##++$'$4$4$B$BM%2%=%=4%=%PN (^-?-?-F GI 6Ak[1dG!//iI 0  F &#))"<"<=..00 #K0   	D"24( ;<!C 		sd   2FE, B8E 2(E, E*)E, FE, FE'$E, &E''E, ,	F5FFFc                  K   ddl m} t        d|       }t        j	                  d|j
                   d|       	 |j                  j                  }|j                  }t        ||      r||_
        yy# t        $ r Y yw xY ww)zHandle MCP 'logging/setLevel' requests.

        Stores the requested minimum log level on the session so that
        subsequent log messages below this level are suppressed.
        r   )MiddlewareServerSessionr   rN   z&] Handler called: set_logging_level %sN)fastmcp.server.low_levelr   r
   rW   rX   rQ   r+   r   sessionr   _minimum_logging_levelr   )r@   levelr   r]   r   r   s         r%   r>   z)MCPOperationsMixin._set_logging_level_mcpe  s|      	Ei&q%KLeT	$$44CkkG'#:;16. < 		s(   7B 5A1 /B 1	A=:B <A==B N)r@   r   rK   None)r@   r   rI   zCallable[..., Awaitable[Any]]rK   z0Callable[..., Awaitable[mcp.types.ServerResult]])rG   zmcp.types.ListToolsRequestrK   zmcp.types.ListToolsResult)rG   zmcp.types.ListResourcesRequestrK   zmcp.types.ListResourcesResult)rG   z&mcp.types.ListResourceTemplatesRequestrK   z%mcp.types.ListResourceTemplatesResult)rG   zmcp.types.ListPromptsRequestrK   zmcp.types.ListPromptsResult)r   r    r   zdict[str, Any]rK   zvlist[ContentBlock] | tuple[list[ContentBlock], dict[str, Any]] | mcp.types.CallToolResult | mcp.types.CreateTaskResult)rd   zAnyUrl | strrK   z9mcp.types.ReadResourceResult | mcp.types.CreateTaskResult)rQ   r    r   zdict[str, Any] | NonerK   z6mcp.types.GetPromptResult | mcp.types.CreateTaskResult)r   zmcp.types.LoggingLevelrK   r   )__name__
__module____qualname____doc__rA   r2   r-   r/   r3   r1   r8   r:   r<   r>   rL   rB   r%   r(   r(   ,   s    -B		 =		9	M1M	"M,U5U	&U.
=
	.
2Q3Q	$Q(:A:A#1:A	%:Ax66	B6p33$93	?3jrB   r(   )r!   zSequence[PaginateT]r"   z
str | Noner#   z
int | NonerK   z"tuple[list[PaginateT], str | None])&r   
__future__r   collections.abcr   r   r   typingr   r   r	   r
   	mcp.typesr   mcp.shared.exceptionsr   r   pydanticr   fastmcp.exceptionsr   r   fastmcp.server.tasks.configr   fastmcp.utilities.loggingr   fastmcp.utilities.paginationr   fastmcp.utilities.versionsr   r   fastmcp.server.serverr   r   rW   r   r&   r(   rL   rB   r%   <module>r      s    M " 9 9 4 4  * "  ; 0 0 : H-	H	K 	PPP P (	P"I IrB   